src/cm/models.py
changeset 504 b2e0186daa5b
parent 478 b9772b94b624
child 511 54d72b4da009
equal deleted inserted replaced
502:8ec189cc214d 504:b2e0186daa5b
    24 from tagging.fields import TagField
    24 from tagging.fields import TagField
    25 import pickle
    25 import pickle
    26 from django.db import connection
    26 from django.db import connection
    27 from datetime import datetime
    27 from datetime import datetime
    28 
    28 
       
    29 # default conf values
       
    30 DEFAULT_CONF = {
       
    31                 'workspace_name' : 'Workspace',
       
    32                 'site_url' : settings.SITE_URL,
       
    33                 'email_from' : settings.DEFAULT_FROM_EMAIL,
       
    34                 }
       
    35 
       
    36 from cm.role_models import change_role_model
       
    37 
       
    38 class ConfigurationManager(models.Manager):
       
    39     def set_workspace_name(self, workspace_name):
       
    40         if workspace_name:
       
    41             self.set_key('workspace_name', workspace_name)
       
    42 
       
    43     def get_key(self, key, default_value=None):
       
    44         try:
       
    45             return self.get(key=key).value
       
    46         except Configuration.DoesNotExist:
       
    47             return DEFAULT_CONF.get(key, default_value)
       
    48 
       
    49     def del_key(self, key):
       
    50         try:
       
    51             self.get(key=key).delete()
       
    52         except Configuration.DoesNotExist:
       
    53             return None
       
    54         
       
    55     def set_key(self, key, value):
       
    56         conf, created = self.get_or_create(key=key)
       
    57         if created or conf.value != value:
       
    58             conf.value = value
       
    59             conf.save()
       
    60             if key == 'workspace_role_model':
       
    61                 change_role_model(value)
       
    62 
       
    63     def __getitem__(self, key):
       
    64         if not key.startswith('f_'):
       
    65             return self.get_key(key, None)
       
    66         else:
       
    67             return getattr(self,key)()
       
    68     
       
    69     def f_get_logo_url(self):
       
    70         key = self.get_key('workspace_logo_file_key', None)
       
    71         if key:
       
    72             attach = Attachment.objects.get(key=key)
       
    73             return attach.data.url
       
    74         else:
       
    75             return None 
       
    76     
       
    77 import base64
       
    78 
       
    79 class Configuration(models.Model):
       
    80     key = models.TextField(blank=False) # , unique=True cannot be added: creates error on mysql (?)
       
    81     raw_value = models.TextField(blank=False)
       
    82     
       
    83     def get_value(self):
       
    84         return pickle.loads(base64.b64decode(self.raw_value.encode('utf8')))
       
    85         
       
    86     def set_value(self, value):        
       
    87         self.raw_value = base64.b64encode(pickle.dumps(value, 0)).encode('utf8')
       
    88                 
       
    89     value = property(get_value, set_value)
       
    90                 
       
    91     objects = ConfigurationManager()
       
    92     
       
    93     def __unicode__(self):
       
    94         return '%s: %s' % (self.key, self.value)    
       
    95     
       
    96 ApplicationConfiguration = Configuration.objects     
       
    97 
    29 class TextManager(Manager):
    98 class TextManager(Manager):
    30     def create_text(self, title, format, content, note, name, email, tags, user=None, state='approved', **kwargs):
    99     def create_text(self, title, format, content, note, name, email, tags, user=None, state='approved', **kwargs):
    31         content = on_content_receive(content, format)
   100         content = on_content_receive(content, format)
    32         text = self.create(name=name, email=email, user=user, state=state)
   101         text = self.create(name=name, email=email, user=user, state=state)
    33         text_version = TextVersion.objects.create(title=title, format=format, content=content, text=text, note=note, name=name, email=email, tags=tags, user=user)
   102         text_version = TextVersion.objects.create(title=title, format=format, content=content, text=text, note=note, name=name, email=email, tags=tags, user=user)
   189     tags = TagField(ugettext_lazy("Tags"), max_length=1000)
   258     tags = TagField(ugettext_lazy("Tags"), max_length=1000)
   190 
   259 
   191     note = models.CharField(ugettext_lazy("Note"), max_length=100, null=True, blank=True)
   260     note = models.CharField(ugettext_lazy("Note"), max_length=100, null=True, blank=True)
   192 
   261 
   193     mod_posteriori = models.BooleanField(ugettext_lazy('Moderation a posteriori?'), default=True)
   262     mod_posteriori = models.BooleanField(ugettext_lazy('Moderation a posteriori?'), default=True)
       
   263 
       
   264     from django.utils.safestring import mark_safe
       
   265 
       
   266     category_1 = models.CharField(ugettext_lazy("Label for the first category of comments"), help_text=mark_safe(_("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #1523f4">&nbsp;</span><br />' + _("Leave blank to use the value configured for the workspace.") + '<br />' + _("To disable this category for this text whatever the configuration for the workspace, enter: ") + '<em>none</em>'), max_length=20, null=True, blank=True, default=ApplicationConfiguration['workspace_category_1'])
       
   267     category_2 = models.CharField(ugettext_lazy("Label for the second category of comments"), help_text=mark_safe(_("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #f4154f">&nbsp;</span><br />' + _("Leave blank to use the value configured for the workspace. ") + '<br />' + _("To disable this category for this text whatever the configuration for the workspace, enter: ") + '<em>none</em>'), max_length=20, null=True, blank=True, default=ApplicationConfiguration['workspace_category_2'])
       
   268     category_3 = models.CharField(ugettext_lazy("Label for the third category of comments"), help_text=mark_safe(_("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #09ff09">&nbsp;</span><br />' + _("Leave blank to use the value configured for the workspace. ") + '<br />' + _("To disable this category for this text whatever the configuration for the workspace, enter: ") + '<em>none</em>'), max_length=20, null=True, blank=True, default=ApplicationConfiguration['workspace_category_3'])
       
   269     category_4 = models.CharField(ugettext_lazy("Label for the fourth category of comments"), help_text=mark_safe(_("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #bc39cf">&nbsp;</span><br />' + _("Leave blank to use the value configured for the workspace. ") + '<br />' + _("To disable this category for this text whatever the configuration for the workspace, enter: ") + '<em>none</em>'), max_length=20, null=True, blank=True, default=ApplicationConfiguration['workspace_category_4'])
       
   270     category_5 = models.CharField(ugettext_lazy("Label for the fifth category of comments"), help_text=mark_safe(_("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #ffbd08">&nbsp;</span><br />' + _("Leave blank to use the value configured for the workspace. ") + '<br />' + _("To disable this category for this text whatever the configuration for the workspace, enter: ") + '<em>none</em>'), max_length=20, null=True, blank=True, default=ApplicationConfiguration['workspace_category_5'])
   194 
   271 
   195     text = models.ForeignKey("Text")
   272     text = models.ForeignKey("Text")
   196 
   273 
   197     objects = TextVersionManager()
   274     objects = TextVersionManager()
   198     
   275     
   277     content_html = models.TextField()
   354     content_html = models.TextField()
   278     
   355     
   279     format = models.CharField(_("Format:"), max_length=20, blank=False, default=DEFAULT_INPUT_FORMAT, choices=CHOICES_INPUT_FORMATS)
   356     format = models.CharField(_("Format:"), max_length=20, blank=False, default=DEFAULT_INPUT_FORMAT, choices=CHOICES_INPUT_FORMATS)
   280 
   357 
   281     tags = TagField()
   358     tags = TagField()
       
   359 
       
   360     category = models.PositiveSmallIntegerField(default=0)
   282         
   361         
   283     start_wrapper = models.IntegerField(null=True, blank=True)
   362     start_wrapper = models.IntegerField(null=True, blank=True)
   284     end_wrapper = models.IntegerField(null=True, blank=True)
   363     end_wrapper = models.IntegerField(null=True, blank=True)
   285     start_offset = models.IntegerField(null=True, blank=True)
   364     start_offset = models.IntegerField(null=True, blank=True)
   286     end_offset = models.IntegerField(null=True, blank=True)
   365     end_offset = models.IntegerField(null=True, blank=True)
   338         self.start_wrapper = self.end_wrapper = self.start_offset = self.end_offset = -1
   417         self.start_wrapper = self.end_wrapper = self.start_offset = self.end_offset = -1
   339         self.save()
   418         self.save()
   340         
   419         
   341 # http://docs.djangoproject.com/en/dev/topics/files/#topics-files
   420 # http://docs.djangoproject.com/en/dev/topics/files/#topics-files
   342 
   421 
   343 # default conf values
       
   344 DEFAULT_CONF = {
       
   345                 'workspace_name' : 'Workspace',
       
   346                 'site_url' : settings.SITE_URL,
       
   347                 'email_from' : settings.DEFAULT_FROM_EMAIL,
       
   348                 }
       
   349 
       
   350 from cm.role_models import change_role_model
       
   351 
       
   352 class ConfigurationManager(models.Manager):
       
   353     def set_workspace_name(self, workspace_name):
       
   354         if workspace_name:
       
   355             self.set_key('workspace_name', workspace_name)
       
   356 
       
   357     def get_key(self, key, default_value=None):
       
   358         try:
       
   359             return self.get(key=key).value
       
   360         except Configuration.DoesNotExist:
       
   361             return DEFAULT_CONF.get(key, default_value)
       
   362 
       
   363     def del_key(self, key):
       
   364         try:
       
   365             self.get(key=key).delete()
       
   366         except Configuration.DoesNotExist:
       
   367             return None
       
   368         
       
   369     def set_key(self, key, value):
       
   370         conf, created = self.get_or_create(key=key)
       
   371         if created or conf.value != value:
       
   372             conf.value = value
       
   373             conf.save()
       
   374             if key == 'workspace_role_model':
       
   375                 change_role_model(value)
       
   376 
       
   377     def __getitem__(self, key):
       
   378         if not key.startswith('f_'):
       
   379             return self.get_key(key, None)
       
   380         else:
       
   381             return getattr(self,key)()
       
   382     
       
   383     def f_get_logo_url(self):
       
   384         key = self.get_key('workspace_logo_file_key', None)
       
   385         if key:
       
   386             attach = Attachment.objects.get(key=key)
       
   387             return attach.data.url
       
   388         else:
       
   389             return None 
       
   390     
       
   391 import base64
       
   392 
       
   393 class Configuration(models.Model):
       
   394     key = models.TextField(blank=False) # , unique=True cannot be added: creates error on mysql (?)
       
   395     raw_value = models.TextField(blank=False)
       
   396     
       
   397     def get_value(self):
       
   398         return pickle.loads(base64.b64decode(self.raw_value.encode('utf8')))
       
   399         
       
   400     def set_value(self, value):        
       
   401         self.raw_value = base64.b64encode(pickle.dumps(value, 0)).encode('utf8')
       
   402                 
       
   403     value = property(get_value, set_value)
       
   404                 
       
   405     objects = ConfigurationManager()
       
   406     
       
   407     def __unicode__(self):
       
   408         return '%s: %s' % (self.key, self.value)    
       
   409     
       
   410 ApplicationConfiguration = Configuration.objects     
       
   411 
       
   412 class AttachmentManager(KeyManager):
   422 class AttachmentManager(KeyManager):
   413     def create_attachment(self, text_version, filename, data):
   423     def create_attachment(self, text_version, filename, data):
   414         attach = self.create(text_version=text_version)
   424         attach = self.create(text_version=text_version)
   415         ff = ContentFile(data)
   425         ff = ContentFile(data)
   416         attach.data.save(filename, ff)
   426         attach.data.save(filename, ff)
   417         return attach
   427         return attach
   418     
   428     
   419 class Attachment(KeyModel):
   429 class Attachment(KeyModel):
   420     data = models.FileField(upload_to="attachments/%Y/%m/%d/", max_length=1000)
   430     data = models.FileField(upload_to="attachments/%Y/%m/%d/", max_length=1000)
   421     text_version = models.ForeignKey(TextVersion, null=True)
   431     text_version = models.ForeignKey(TextVersion, null=True, blank=True, default=None)
   422 
   432 
   423     objects = AttachmentManager()
   433     objects = AttachmentManager()
   424     
   434     
   425 class NotificationManager(KeyManager):
   435 class NotificationManager(KeyManager):
   426     def create_notification(self, text, type, active, email_or_user):
   436     def create_notification(self, text, type, active, email_or_user):