from django.db import models
from django.utils import timezone
import os
from colock.key_generator import message_key_gen

upload_prefix='upload'


class Message(models.Model):
    @staticmethod
    def new_filename(self, filename):
        fn, ext = os.path.splitext(filename)
        newfn = message_key_gen(self.sender_uid,
                self.receiver_uid, str( self.send_time ) )
        self.filetype = ext
        return "%s/%s.%s" % ( upload_prefix, newfn, ext )

    sender_uid = models.IntegerField()
    receiver_uid = models.IntegerField()
    message_key = models.CharField(max_length=32)
    send_time = models.DateTimeField(default=timezone.now)
    exist = models.BooleanField(default=True)
    filetype = models.CharField(max_length=10, default="png")
    # no () means it's called every time instead of only when loading the model

    img = models.ImageField(upload_to=new_filename)

    class Meta:
        ordering = ('receiver_uid',)


@@ -426,21 +426,20 @@
 urn 
-%22%25s/%25s.%25s%22 %25 
+os.path.join
 ( up
@@ -451,16 +451,27 @@
 prefix, 
+%22%25s.%25s%22 %25 (
 newfn, e
@@ -472,16 +472,17 @@
 wfn, ext
+)
  )%0A%0A    

rename uploaded file