Dmitry Mukhin avatar Dmitry Mukhin committed 5dbbd40

add POSTMAN_ON_USER_DELETE setting

Comments (0)

Files changed (1)

postman/models.py

 dbms = settings.DATABASES['default']['ENGINE'].rsplit('.',1)[-1]
 QUOTE_CHAR = '`' if dbms == 'mysql' else '"'
 
+_on_delete_name = getattr(settings, 'POSTMAN_ON_USER_DELETE', 'CASCADE')
+if _on_delete_name not in ['CASCADE', 'PROTECT', 'SET_NULL', 'SET_DEFAULT',
+                          'DO_NOTHING']:
+    _on_delete_name = 'CASCADE'
+
+ON_USER_DELETE = getattr(models, _on_delete_name)
+
+
 def get_order_by(query_dict):
     """
     Return a field name, optionally prefixed for descending order, or None if not found.
     ``query_dict``: a dictionary to look for a key dedicated to ordering purpose
 
     >>> get_order_by({})
-    
+
     >>> get_order_by({ORDER_BY_KEY: 'f'})
     'sender__username'
     >>> get_order_by({ORDER_BY_KEY: 'D'})
 
         """
         return self.inbox(user, related=False, option=OPTION_MESSAGES).filter(read_at__isnull=True).count()
-    
+
     def sent(self, user, **kwargs):
         """
         Return all messages sent by a user but not marked as archived or deleted.
 
     subject = models.CharField(_("subject"), max_length=SUBJECT_MAX_LENGTH)
     body = models.TextField(_("body"), blank=True)
-    sender = models.ForeignKey(User, related_name='sent_messages', null=True, blank=True, verbose_name=_("sender"))
-    recipient = models.ForeignKey(User, related_name='received_messages', null=True, blank=True, verbose_name=_("recipient"))
+    sender = models.ForeignKey(User, related_name='sent_messages',
+                               null=True, blank=True,
+                               verbose_name=_("sender"),
+                               on_delete=ON_USER_DELETE)
+    recipient = models.ForeignKey(User, related_name='received_messages',
+                                  null=True, blank=True,
+                                  verbose_name=_("recipient"),
+                                  on_delete=ON_USER_DELETE)
     email = models.EmailField(_("visitor"), blank=True) # instead of either sender or recipient, for an AnonymousUser
-    parent = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name=_("parent message"))
-    thread = models.ForeignKey('self', related_name='child_messages', null=True, blank=True, verbose_name=_("root message"))
+    parent = models.ForeignKey('self', related_name='next_messages', null=True,
+                               blank=True, verbose_name=_("parent message"))
+    thread = models.ForeignKey('self', related_name='child_messages',
+                               null=True, blank=True,
+                               verbose_name=_("root message"))
     sent_at = models.DateTimeField(_("sent at"), default=datetime.datetime.now)
     read_at = models.DateTimeField(_("read at"), null=True, blank=True)
     replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
     sender_archived = models.BooleanField(_("archived by sender"))
     recipient_archived = models.BooleanField(_("archived by recipient"))
-    sender_deleted_at = models.DateTimeField(_("deleted by sender at"), null=True, blank=True)
-    recipient_deleted_at = models.DateTimeField(_("deleted by recipient at"), null=True, blank=True)
+    sender_deleted_at = models.DateTimeField(_("deleted by sender at"),
+                                             null=True, blank=True)
+    recipient_deleted_at = models.DateTimeField(_("deleted by recipient at"),
+                                                null=True, blank=True)
     # moderation fields
-    moderation_status = models.CharField(_("status"), max_length=1, choices=STATUS_CHOICES, default=STATUS_PENDING)
+    moderation_status = models.CharField(_("status"), max_length=1,
+                                         choices=STATUS_CHOICES,
+                                         default=STATUS_PENDING)
     moderation_by = models.ForeignKey(User, related_name='moderated_messages',
         null=True, blank=True, verbose_name=_("moderator"))
-    moderation_date = models.DateTimeField(_("moderated at"), null=True, blank=True)
-    moderation_reason = models.CharField(_("rejection reason"), max_length=120, blank=True)
+    moderation_date = models.DateTimeField(_("moderated at"), null=True,
+                                           blank=True)
+    moderation_reason = models.CharField(_("rejection reason"), max_length=120,
+                                         blank=True)
 
     objects = MessageManager()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.