Commits

Adrien Lemaire committed 0134057

Break django-postman to allow users to write to another Model

  • Participants
  • Parent commits e05947f

Comments (0)

Files changed (6)

 glob:*.pyc
 glob:docs/_*
+glob:*swp
+django_postman.egg-info/

File postman/fields.py

         names = super(BasicCommaSeparatedUserField, self).clean(value)
         if not names:
             return []
-        users = list(User.objects.filter(is_active=True, username__in=names))
-        unknown_names = set(names) ^ set([u.username for u in users])
+        #users = list(User.objects.filter(is_active=True, username__in=names))
+        users = names
+        #unknown_names = set(names) ^ set([u.username for u in users])
         errors = []
-        if unknown_names:
-            errors.append(self.error_messages['unknown'].format(users=', '.join(unknown_names)))
+        #if unknown_names:
+            #errors.append(self.error_messages['unknown'].format(users=', '.join(unknown_names)))
         if self.user_filter:
             filtered_names = []
             for u in users[:]:

File postman/forms.py

         initial_moderation = self.instance.get_moderation()
         initial_dates = self.instance.get_dates()
         initial_status = self.instance.moderation_status
-        if recipient:
-            if isinstance(recipient, User) and recipient in recipients:
-                recipients.remove(recipient)
+        #if recipient:
+            #if isinstance(recipient, User) and recipient in recipients:
+        if recipient in recipients:
+            recipients.remove(recipient)
+        elif recipient:
             recipients.insert(0, recipient)
         is_successful = True
         for r in recipients:
-            if isinstance(r, User):
-                self.instance.recipient = r
-            else:
-                self.instance.recipient = None
-                self.instance.email = r
+            #if isinstance(r, User):
+                #self.instance.recipient = r
+            #else:
+                #self.instance.recipient = None
+                #self.instance.email = r
+            self.instance.recipient = r
             self.instance.pk = None # force_insert=True is not accessible from here
+            self.instance.moderation_status = 'a'
             self.instance.auto_moderate(auto_moderators)
             self.instance.clean_moderation(initial_status)
             self.instance.clean_for_visitor()
             self.instance.update_parent(initial_status)
             self.instance.notify_users(initial_status)
             # some resets for next reuse
-            if not isinstance(r, User):
-                self.instance.email = ''
+            #if not isinstance(r, User):
+                #self.instance.email = ''
             self.instance.set_moderation(*initial_moderation)
             self.instance.set_dates(*initial_dates)
         return is_successful

File postman/models.py

     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"))
+    #recipient = models.ForeignKey(User, related_name='received_messages', null=True, blank=True, verbose_name=_("recipient"))
+    recipient = models.EmailField(_("visitor"), blank=True) # instead of either sender or recipient, for an AnonymousUser
     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"))
         """Tell if the message is in the accepted state."""
         return self.moderation_status == STATUS_ACCEPTED
 
+
     @property
     def is_new(self):
         """Tell if the recipient has not yet read the message."""
             # no need to wait for a final moderation status to mark as deleted
             if not self.sender_deleted_at:
                 self.sender_deleted_at = now()
-        elif not self.recipient_id:
-            if self.is_accepted():
-                if not self.read_at:
-                    self.read_at = now()
-                if not self.recipient_deleted_at:
-                    self.recipient_deleted_at = now()
-            else:
-                # rollbacks
-                if self.read_at:
-                    self.read_at = None
-                # but stay deleted if rejected
-                if self.is_pending() and self.recipient_deleted_at:
-                    self.recipient_deleted_at = None
+        if self.is_accepted():
+            if not self.read_at:
+                self.read_at = now()
+            if not self.recipient_deleted_at:
+                self.recipient_deleted_at = now()
+        else:
+            # rollbacks
+            if self.read_at:
+                self.read_at = None
+            # but stay deleted if rejected
+            if self.is_pending() and self.recipient_deleted_at:
+                self.recipient_deleted_at = None
 
     def update_parent(self, initial_status):
         """Update the parent to actualize its response state."""
                 if not (self.sender_id and is_auto_moderated):
                     (notify_user if self.sender_id else email_visitor)(self, 'rejection')
             elif self.is_accepted():
-                (notify_user if self.recipient_id else email_visitor)(self, 'acceptance')
+                #(notify_user if self.recipient_id else email_visitor)(self, 'acceptance')
+                notify_user(self, 'acceptance')
 
     def get_dates(self):
         """Get some dates to restore later."""

File postman/utils.py

     if notification:
         notification.send(users=[user], label=label, extra_context={'message': object, 'action': action})
     else:
-        if not DISABLE_USER_EMAILING and user.email and user.is_active:
-            email('postman/email_user_subject.txt', 'postman/email_user.txt', [user.email], object, action)
+        #if not DISABLE_USER_EMAILING and user.email and user.is_active:
+        email('postman/email_user_subject.txt', 'postman/email_user.txt', [user], object, action)

File postman/views.py

         if recipients:
             # order_by() is not mandatory, but: a) it doesn't hurt; b) it eases the test suite
             # and anyway the original ordering cannot be respected.
-            usernames = list(User.objects.values_list('username', flat=True).filter(
-                is_active=True,
-                username__in=[r.strip() for r in recipients.split(':') if r and not r.isspace()],
-            ).order_by('username'))
-            if usernames:
-                initial.update(recipients=', '.join(usernames))
+#            usernames = list(User.objects.values_list('username', flat=True).filter(
+                #is_active=True,
+                #username__in=[r.strip() for r in recipients.split(':') if r and not r.isspace()],
+            #).order_by('username'))
+#            if usernames:
+             #initial.update(recipients=', '.join(usernames))
+            initial.update(recipients=', '.join(recipients.split(':')))
+                # code...
         form = form_class(initial=initial, channel=channel)
     return render_to_response(template_name, {
         'form': form,