bornhack-website/src/events/handler.py

82 lines
2.9 KiB
Python
Raw Normal View History

Merge teamcomms branch. Refactor team app and add events app. * Primary commit towards improved team communications. Add new events app to handle team notifications when various events happen, with a Type model which contain event types and a Routing model which controls routing of events to teams. Add shortslug for Camp and Team models. events.handler.py contains the code for sending irc and email notifications for teams. The first two eventtypes have been added in datamigrations, 'ticket_created' and 'public_credit_name_changed', and the tickets and profile apps have been adjusted accordingly. Team IRC channels can be marked as managed and if so the IRC bot will register the team channel with ChanServ if possible. Team IRC channels can be marked as private and the bot will set invite only and maintain an ACL with team members. Users can set their NickServ username in their profile to get on the ACL. Rework all team views and templates. Remove TeamArea model and make Team have an FK to Camp directly. Add docstrings a whole bunch of places. Move signal handlers to apps.py and signal_handlers.py in a few apps. Add basic team mailing list handling, more work to be done. Update bootstrap-devsite script to add more teammembers and add some team event routing for the two eventtypes we have. * default to the console backend for email unless we specifically ask for realworld email * fix signal for public_credit_name approval irc message * fix name display on /people/ page * fix the text on people pages when all non-responsible team members are anonymous * handle cases where we fallback to the area responsible properly * readd removed property, it is used in team_detail view * make it possible to filter profiles by public_credit_name_approved * add method for sending IRC messages in ircbot.utils.add_irc_message(), extend periodic bot method to do more than check for outgoing messages so rename it, refactor chanserv and nickserv handling code, create methods to check and join/part IRC channels as needed, maintain channel ACLs for private channels, do not autojoin any channels when instatiating the bot instead rely on the new check_irc_channels() method to join them, rename profile presave signal, add checking for changed nickserv usernames for acl handling, add teammember.irc_channel_acl_ok boolean to track ACL state, add missing help_text properties to TeamMember fields, rename teammember postsave signal, add teammember deleted signal, readd wrongly deleted EnsureTeamMemberResponsibleMixin * add a few missing early returns
2018-04-09 21:11:05 +00:00
from django.utils import timezone
from datetime import timedelta
from ircbot.utils import add_irc_message
import logging
logger = logging.getLogger("bornhack.%s" % __name__)
def handle_team_event(eventtype, irc_message=None, irc_timeout=60, email_template=None, email_formatdict=None):
"""
This method is our basic event handler.
The type of event determines which teams receive notifications.
TODO: Add some sort of priority to messages
"""
logger.info("Inside handle_team_event, eventtype %s" % eventtype)
# get event type from database
from .models import Type
try:
eventtype = Type.objects.get(name=eventtype)
except Type.DoesNotExist:
# unknown event type, do nothing
logger.error("Unknown eventtype %s" % eventtype)
return
if not eventtype.teams:
# no routes found for this eventtype, do nothing
logger.error("No routes round for eventtype %s" % eventtype)
return
# loop over routes (teams) for this eventtype
for team in eventtype.teams:
logger.info("Handling eventtype %s for team %s" % (eventtype, team))
team_irc_notification(team=team, eventtype=eventtype, irc_message=irc_message, irc_timeout=irc_timeout)
team_email_notification(team=team, eventtype=eventtype, email_template=None, email_formatdict=None)
# handle any future notification types here..
def team_irc_notification(team, eventtype, irc_message=None, irc_timeout=60):
"""
Sends IRC notifications for events to team IRC channels
"""
logger.info("Inside team_irc_notification, message %s" % irc_message)
if not irc_message:
logger.error("No IRC message found")
return
if not eventtype.irc_notification:
logger.error("IRC notifications not enabled for eventtype %s" % eventtype)
return
if not team.irc_channel or not team.irc_channel_name:
logger.error("team %s is not IRC enabled" % team)
return
# send an IRC message to the the channel for this team
add_irc_message(
target=team.irc_channel_name,
message=irc_message,
timeout=60
)
logger.info("Added new IRC message for channel %s" % team.irc_channel_name)
def team_email_notification(team, eventtype, email_template=None, email_formatdict=None):
"""
Sends email notifications for events to team mailinglists (if possible,
otherwise directly to the team responsibles)
"""
if not email_template or not email_formatdict or not eventtype.email_notification:
# no email message found, or email notifications are not enabled for this event type
return
if team.mailing_list:
# send notification to the team mailing list
recipient_list = [team.mailing_list]
else:
# no team mailinglist, send to the team responsibles instead
recipient_list = [resp.email for resp in team.responsible_members.all()]
# TODO: actually send the email here