def create_notification(kind_name, target, metadata={}, lookup_path=None): kind_ref = NotificationKind.get(name=kind_name) notification = Notification.create(kind=kind_ref, target=target, metadata_json=json.dumps(metadata), lookup_path=lookup_path) return notification
def delete_matching_notifications(target, kind_name, **kwargs): kind_ref = NotificationKind.get(name=kind_name) # Load all notifications for the user with the given kind. notifications = Notification.select().where( Notification.target == target, Notification.kind == kind_ref ) # For each, match the metadata to the specified values. for notification in notifications: matches = True try: metadata = json.loads(notification.metadata_json) except: continue for (key, value) in kwargs.items(): if not key in metadata or metadata[key] != value: matches = False break if not matches: continue notification.delete_instance()
def list_notifications(user, kind_name=None, id_filter=None, include_dismissed=False, page=None, limit=None): base_query = Notification.select( Notification.id, Notification.uuid, Notification.kind, Notification.metadata_json, Notification.dismissed, Notification.lookup_path, Notification.created, Notification.created.alias("cd"), Notification.target, ).join(NotificationKind) if kind_name is not None: base_query = base_query.where(NotificationKind.name == kind_name) if id_filter is not None: base_query = base_query.where(Notification.uuid == id_filter) if not include_dismissed: base_query = base_query.where(Notification.dismissed == False) # Lookup directly for the user. user_direct = base_query.clone().where(Notification.target == user) # Lookup via organizations admined by the user. Org = User.alias() AdminTeam = Team.alias() AdminTeamMember = TeamMember.alias() AdminUser = User.alias() via_orgs = (base_query.clone().join( Org, on=(Org.id == Notification.target)).join( AdminTeam, on=(Org.id == AdminTeam.organization)).join( TeamRole, on=(AdminTeam.role == TeamRole.id)).switch(AdminTeam).join( AdminTeamMember, on=(AdminTeam.id == AdminTeamMember.team)).join( AdminUser, on=(AdminTeamMember.user == AdminUser.id )).where((AdminUser.id == user) & (TeamRole.name == "admin"))) query = user_direct | via_orgs if page: query = query.paginate(page, limit) elif limit: query = query.limit(limit) return query.order_by(SQL("cd desc"))
def process_trigger(self, trigger: Trigger) -> (Notification, bool): # Lookup the relevant change in the change map. change = self.post_count_change_map[trigger.time_window][trigger.follow.type + ":" + trigger.follow.target] # If the change is above the set threshold... if change >= trigger.threshold: # Check if there are unread notifications that are associated with this trigger. has_unread = any(n.read == 0 for n in trigger.notifications) # Check if the e-mail notifications for this trigger are turned on. notify_email = trigger.follow.notify_email # Decide whether to send or not send and email. should_send_email = notify_email and not has_unread # Create notification. message = "%s saw a %s%% increase in the last %s" \ % (trigger.follow.target, "{:.2f}".format(change), trigger.time_window) notification = Notification(user_id=trigger.follow.user_id, trigger_id=trigger.id, content=message, time=int(time.time()), read=False) print("Sending a notification to user", trigger.follow.user_id) return notification, should_send_email else: return None, False
def admin_ticket_comment(ticket): ticket = TroubleTicket.get(TroubleTicket.id == ticket) if request.form["comment"]: TicketComment.create(ticket=ticket, comment_by=session["admin"], comment=request.form["comment"], time=datetime.now()) Notification.create( team=ticket.team, notification="A response has been added for {}.".format( make_link("ticket #{}".format(ticket.id), url_for("tickets.detail", ticket_id=ticket.id)))) flash("Comment added.") if ticket.active and "resolved" in request.form: ticket.active = False ticket.save() Notification.create(team=ticket.team, notification="{} has been marked resolved.".format( make_link( "Ticket #{}".format(ticket.id), url_for("tickets.detail", ticket_id=ticket.id)))) flash("Ticket closed.") elif not ticket.active and "resolved" not in request.form: ticket.active = True ticket.save() Notification.create(team=ticket.team, notification="{} has been reopened.".format( make_link( "Ticket #{}".format(ticket.id), url_for("tickets.detail", ticket_id=ticket.id)))) flash("Ticket reopened.") return redirect(url_for(".admin_ticket_detail", ticket=ticket.id))
def lookup_notifications_by_path_prefix(prefix): return list( (Notification.select().where(Notification.lookup_path % prefix)))
def delete_notifications_by_kind(target, kind_name): kind_ref = NotificationKind.get(name=kind_name) Notification.delete().where(Notification.target == target, Notification.kind == kind_ref).execute()
def delete_all_notifications_by_kind(kind_name): kind_ref = NotificationKind.get(name=kind_name) (Notification.delete().where(Notification.kind == kind_ref).execute())
def delete_all_notifications_by_path_prefix(prefix): (Notification.delete().where(Notification.lookup_path**(prefix + "%")).execute())