def create_leadership(sender, instance, created, **kwargs): if created: notification_type = NotificationType.objects.get(name="new_leadership") notification = Notification(notification_type=notification_type) notification.save() from petri.account.models import UserProfile for leader_profile in UserProfile.objects.filter(chapter=instance.chapter, is_leader=True): if "?n=" + leader_profile.user.username.lower() not in instance.content: notification.users.add(leader_profile.user) notification.add_dictionary({"chapter": instance.chapter.slug, "bulletin": instance.pk, "title": instance.title, "content": instance.content}) notification.save() notification.dispatch()
def approve(self, user): self.response = Leadership.APPROVED self.processed_by = user self.save() # Send out notifications if self.request == self.JOIN: notification_type = NotificationType.objects.get(name="membership_approved") else: notification_type = NotificationType.objects.get(name="leadership_approved") notification = Notification(notification_type=notification_type) notification.save() notification.users.add(self.owner) notification.add_dictionary({"chapter": self.chapter.slug, "processor": self.processed_by.get_profile().display_name, "request_title": self.title, "request_pk": self.pk}) notification.save() notification.dispatch()
def _create_comment(sender, instance, created, **kwargs): if created: if instance.can_notify(): notification_type = NotificationType.objects.get(name="new_comment") notification = Notification(notification_type=notification_type) notification.save() if instance.bulletin.owner.pk != instance.owner.pk: notification.users.add(instance.bulletin.owner) for comment in Comment.objects.filter(bulletin=instance.bulletin): if comment.bulletin.owner.pk != comment.owner.pk and comment.owner.pk != instance.owner.pk: notification.users.add(comment.owner) for follower in instance.bulletin.followed_by.all(): if follower.pk != comment.owner.pk and follower.pk != instance.owner.pk: notification.users.add(follower) notification.add_dictionary({"chapter": instance.bulletin.chapter.slug, "bulletin": instance.bulletin.pk, "comment": instance.pk}) notification.save() notification.dispatch()
def create_mention_notifications(self): add_users = [] for user in self.users_mentioned.all(): if user not in self.users_notified.all(): add_users.append(user) if len(add_users) == 0: return notification_type = NotificationType.objects.get(name="new_mention") notification = Notification(notification_type=notification_type) notification.save() for user in add_users: notification.users.add(user) self.users_notified.add(user) notification.add_dictionary({"chapter": self.bulletin.chapter.slug, "bulletin": self.bulletin.pk, "comment": self.pk}) notification.save() notification.dispatch()
def message(request): if request.method == 'POST': data = request.POST valid = True recipients = [] for recipient in data.getlist("to"): try: user = User.objects.get(username=recipient) except User.DoesNotExist: continue if not request.user.is_authenticated() or not request.user.is_active: if not user.get_profile().is_leader: valid = False if not valid: return json.errors({"Message": "You are not Allowed to do that!"}) recipients.append(user) notification_type = NotificationType.objects.get(name="message") if request.user.is_authenticated() and request.user.is_active: notification = Notification(notification_type=notification_type, sender=request.user.get_profile().get_email()) else: notification = Notification(notification_type=notification_type, sender=data.get("from")) notification.save() for recipient in recipients: notification.users.add(recipient) if request.user.is_authenticated() and request.user.is_active: notification.add_dictionary({"message": data['message'], "sender_display_name": request.user.get_profile().display_name, "sender_username": request.user.username}) else: notification.add_dictionary({"message": data['message'], "sender_display_name": data.get('from'), "sender_username": request.user.username}) notification.save() notification.dispatch() return json.success() return HttpResponseNotAllowed(['POST'])