def newPost(self, user, message, clean_team, notification=True): self.user = user self.clean_team = clean_team self.message = message self.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="message_posted") # The names that will go in the notification message template name_strings = [self.clean_team.name] link_strings = [str(self.clean_team.id)] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self.clean_team, status="approved") members_list = list(clean_team_members) if role == "agent": clean_champions = CleanChampion.objects.filter(clean_team=self.clean_team, status="approved") members_list = list(chain(clean_team_members, clean_champions)) for member in members_list: user_notification = UserNotification() user_notification.create_notification("message_posted", member.user, name_strings, link_strings) except Exception, e: print e
def add_team_clean_creds(self, amount, notification=True): self.clean_creds += amount self.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="clean_team_add_clean_creds") # The names that will go in the notification message template name_strings = [self.name, amount] link_strings = [str(self.id)] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("clean_team_add_clean_creds", member.user, name_strings, link_strings) except Exception, e: print e
def newPost(self, user, message, community, notification=True): self.user = user self.community = community self.message = message self.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="message_posted") # The names that will go in the notification message template name_strings = [self.community.name] link_strings = [str(self.community.id)] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: community_members = UserCommunityMembership.objects.filter(community=self.community) members_list = list(community_members) for member in members_list: user_notification = UserNotification() user_notification.create_notification("message_posted", member.user, name_strings, link_strings) except Exception, e: print e
def becomeCleanChampionNew(self, user, userid, selected_team): self.user_id = userid self.clean_team_id = selected_team self.status = "approved" self.save() # Send notifications notification = Notification.objects.get(notification_type="cc_joined") # The names that will go in the notification message template full_name = u'%s %s' %(self.user.first_name, self.user.last_name) name_strings = [full_name, self.clean_team.name] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: # print self.clean_team_id clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self.clean_team, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("cc_joined", member.user, name_strings) self.clean_team.add_team_clean_creds(5) self.user.profile.add_clean_creds(20)
def requestBecomeCleanAmbassador(self, user, selected_team, notification=True): self.user = user self.clean_team = selected_team self.status = "pending" self.role = "leader" self.save() self.user.profile.clean_team_member = CleanTeamMember.objects.latest('id') self.user.profile.save() try: if notification: # Send notifications # notification = Notification.objects.get(notification_type="ca_request") # The names that will go in the notification message template full_name = u'%s %s' %(self.user.first_name, self.user.last_name) name_strings = [full_name, self.clean_team.name] # users_to_notify_str = notification.users_to_notify users_to_notify_str = "leader" users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self.clean_team, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("ca_request", member.user, name_strings) except Exception, e: print e
def becomeCleanChampion(self, user, selected_team, notification=True): self.user = user self.clean_team = selected_team self.status = "approved" self.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="cc_joined") # The names that will go in the notification message template full_name = u'%s %s' %(self.user.first_name, self.user.last_name) name_strings = [full_name, self.clean_team.name] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self.clean_team, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("cc_joined", member.user, name_strings) except Exception, e: print e
def read_notification(request, methods=['GET']): notification = get_object_or_404(Notification, pk=request.GET['pk']) read_user = request.user read_notification = UserNotification.objects.filter(user=read_user).filter(notification=notification) if not read_notification: user_notification = UserNotification(user=read_user, notification=notification) user_notification.save() return JsonResponse({'notification': notification.as_json(), 'result': 'success'})
def archive_notification(request, methods=['GET']): notification = get_object_or_404(Notification, pk=request.GET['pk']) user_notification = UserNotification.objects.filter(user=request.user).filter(notification=notification).first() if not user_notification: user_notification = UserNotification(user=request.user, notification=notification, archived=True) user_notification.archived = True user_notification.save() return JsonResponse({'notification': notification.as_json(), 'result': 'success'})
def innerpub_msg_added_cb(sender, instance, **kwargs): from_email = instance.from_email users = get_emailusers(-1, -1) for u in users: if u.email == from_email: continue try: UserNotification.objects.get(to_user=u.email, msg_type='innerpub_msg') except UserNotification.DoesNotExist: n = UserNotification(to_user=u.email, msg_type='innerpub_msg', detail='') n.save()
def grpmsg_added_cb(sender, **kwargs): group_id = kwargs['group_id'] from_email = kwargs['from_email'] group_members = ccnet_threaded_rpc.get_group_members(int(group_id)) for m in group_members: if from_email == m.user_name: continue try: UserNotification.objects.get(to_user=m.user_name, msg_type='group_msg', detail=group_id) except UserNotification.DoesNotExist: n = UserNotification(to_user=m.user_name, msg_type='group_msg', detail=group_id) n.save()
def add_clean_creds(self, amount, notification=True): self.clean_creds += amount self.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="user_add_clean_creds") # The names that will go in the notification message template name_strings = [amount] link_strings = [str(self.user.id)] user_notification = UserNotification() user_notification.create_notification("user_add_clean_creds", self.user, name_strings, link_strings) except Exception, e: print e
def grpmsg_reply_added_cb(sender, **kwargs): msg_id = kwargs['msg_id'] reply_from_email = kwargs['from_email'] # this value may be used in future try: group_msg = GroupMessage.objects.get(id=msg_id) except GroupMessage.DoesNotExist: pass try: UserNotification.objects.get(to_user=group_msg.from_email, msg_type='grpmsg_reply', detail=msg_id) except UserNotification.DoesNotExist: n = UserNotification(to_user=group_msg.from_email, msg_type='grpmsg_reply', detail=msg_id) n.save()
def grpmsg_added_cb(sender, **kwargs): group_id = kwargs['group_id'] from_email = kwargs['from_email'] group_members = get_group_members(int(group_id)) if len(group_members) > 15: # No need to send notification when group is return # too large for m in group_members: if from_email == m.user_name: continue try: UserNotification.objects.get(to_user=m.user_name, msg_type='group_msg', detail=group_id) except UserNotification.DoesNotExist: n = UserNotification(to_user=m.user_name, msg_type='group_msg', detail=group_id) n.save()
def level_up(self, notification=True): # If they don't have a badge, ie. new team, make them a Seedling if self.level is None: level = CleanTeamLevel.objects.get(name="Seedling") else: level = CleanTeamLevel.objects.get(name="Seedling") self.level = level # Add rewards for certain badges self.save() level_tasks = CleanTeamLevelTask.objects.filter(clean_team_level=self.level) # Create the new tasks the Change Team must complete for task in level_tasks: level_progress = CleanTeamLevelProgress(clean_team=self, level_task=task) level_progress.save() if notification: try: # Send notifications notification = Notification.objects.get(notification_type="level_up") # The names that will go in the notification message template name_strings = [self.name, self.level.name] link_strings = [str(self.id)] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("level_up", member.user, name_strings, link_strings) except Exception, e: print e
def acceptInvite(self, user, notification=True): if self.role == "agent": clean_champion = CleanChampion() clean_champion.becomeCleanChampion(user, self.clean_team) elif self.role == "leader": ctm = CleanTeamMember() ctm.becomeCleanAmbassador(user, self.clean_team) emails = User.objects.filter(email=self.email).count() if emails > 0: self.status = "accepted" self.save() self.user.profile.add_clean_creds(10) if notification: if self.role: try: # Send notifications notification = Notification.objects.get(notification_type="ca_joined") # The names that will go in the notification message template full_name = u'%s %s' %(self.user.first_name, self.user.last_name) name_strings = [full_name, self.clean_team.name] users_to_notify_str = notification.users_to_notify users_to_notify = users_to_notify_str.split(', ') # Notify all of the Users that have the roles within users_to_notify for role in users_to_notify: clean_team_members = CleanTeamMember.objects.filter(role=role, clean_team=self.clean_team, status="approved") for member in clean_team_members: user_notification = UserNotification() user_notification.create_notification("ca_joined", member.user, name_strings) # self.clean_team.add_team_clean_creds(5) except Exception, e: print e return True
def msgreply_save_handler(sender, instance, **kwargs): """ Handle sending notification to '@<user>' when reply messages. """ from_email = instance.from_email reply_msg = instance.message group_msg = instance.reply_to to_user = '' from base.templatetags.seahub_tags import at_pattern m = re.match(at_pattern, reply_msg) if m: nickname_or_emailprefix = m.group()[1:] for member in get_group_members(group_msg.group_id): # For every group member, get his username and nickname if # it exists, check whether match. username = member.user_name if username == from_email: continue p = get_first_object_or_none( Profile.objects.filter(user=username)) nickname = p.nickname if p else '' if nickname == nickname_or_emailprefix or \ username.split('@')[0] == nickname_or_emailprefix: to_user = username break if to_user: # Send notification to the user if he replies someone else' # message. try: UserNotification.objects.get(to_user=to_user, msg_type='grpmsg_reply', detail=group_msg.id) except UserNotification.DoesNotExist: n = UserNotification(to_user=to_user, msg_type='grpmsg_reply', detail=group_msg.id) n.save()
def innerpubmsg_reply_added_cb(sender, instance, **kwargs): innerpub_msg = instance.reply_to from_email = instance.from_email msg_id = innerpub_msg.id if from_email == innerpub_msg.from_email: # No need to send notification when reply own message. return try: innerpub_msg = InnerPubMsg.objects.get(id=msg_id) except InnerPubMsg.DoesNotExist: pass try: UserNotification.objects.get(to_user=innerpub_msg.from_email, msg_type='innerpubmsg_reply', detail=msg_id) except UserNotification.DoesNotExist: n = UserNotification(to_user=innerpub_msg.from_email, msg_type='innerpubmsg_reply', detail=msg_id) n.save()
def becomeCleanAmbassador(self, user, selected_team, notification=True): CleanChampion.objects.filter(user=user, clean_team=selected_team).delete() self.user = user self.clean_team = selected_team self.role = "leader" self.status = "approved" self.save() self.user.profile.clean_team_member = CleanTeamMember.objects.latest('id') self.user.profile.save() if notification: try: # Send notifications # notification = Notification.objects.get(notification_type="ca_approved") # The names that will go in the notification message template name_strings = [self.clean_team.name] link_strings = [str(self.clean_team.id)] user_notification = UserNotification() user_notification.create_notification("ca_approved", self.user, name_strings, link_strings) except Exception, e: print e
self.status = "approved" self.save() try: CleanChampion.objects.filter(user=self.user, clean_team=self.clean_team).delete() except Exception, e: print e if notification: try: # Send notifications # notification = Notification.objects.get(notification_type="ca_approved") # The names that will go in the notification message template name_strings = [self.clean_team.name] link_strings = [str(self.clean_team.id)] user_notification = UserNotification() user_notification.create_notification("ca_approved", self.user, name_strings, link_strings) except Exception, e: print e self.user.profile.add_clean_creds(50) def removedCleanAmbassador(self): self.user.profile.clean_team_member = None self.user.profile.save() self.delete() def requestBecomeCleanAmbassador(self, user, selected_team, notification=True): self.user = user self.clean_team = selected_team self.status = "pending"
def unread_notifications(request, methods=['GET']): unread_notifications = UserNotification.unread_notifications(request.user) return JsonResponse(unread_notifications)