|
| 1 | +from django.core.management.base import NoArgsCommand |
| 2 | +from django.template.loader import get_template |
| 3 | +from django.utils.translation import ugettext as _ |
| 4 | +from askbot.conf import settings as askbot_settings |
| 5 | +from askbot import const |
| 6 | +from askbot import mail |
| 7 | +from askbot.models import Activity |
| 8 | +from askbot.models import User |
| 9 | + |
| 10 | +def get_moderators(): |
| 11 | + return User.objects.filter(status__in=('d', 'm')) |
| 12 | + |
| 13 | +def get_last_mod_alert_activity(): |
| 14 | + atype = const.TYPE_ACTIVITY_MODERATION_ALERT_SENT |
| 15 | + acts = Activity.objects.filter(activity_type=atype).order_by('-id') |
| 16 | + count = len(acts) |
| 17 | + if count == 0: |
| 18 | + return None |
| 19 | + last_act = acts[0] |
| 20 | + |
| 21 | + if count > 1: |
| 22 | + #get last moderation activity and delete all others |
| 23 | + acts = acts.exclude(id=last_act.id) |
| 24 | + acts.delete() |
| 25 | + |
| 26 | + return last_act |
| 27 | + |
| 28 | + |
| 29 | +def get_last_notified_user(): |
| 30 | + last_act = get_last_mod_alert_activity() |
| 31 | + if last_act: |
| 32 | + return last_act.content_object |
| 33 | + return None |
| 34 | + |
| 35 | + |
| 36 | +def select_moderators_to_notify(candidates, num_needed): |
| 37 | + candidates_count = candidates.count() |
| 38 | + |
| 39 | + #special case - if we need to notify the same number of |
| 40 | + #moderators that are available, then we don't rotate them |
| 41 | + #and notify all, b/c otherwise we would stop notifications |
| 42 | + #because there are not enough moderators |
| 43 | + if candidates_count <= num_needed: |
| 44 | + return list(candidates) |
| 45 | + |
| 46 | + last_notified = get_last_notified_user() |
| 47 | + if last_notified is None: |
| 48 | + return candidates[:num_needed] |
| 49 | + |
| 50 | + mods = list(candidates.filter(id__gt=last_notified.id)) |
| 51 | + num_mods = len(mods) |
| 52 | + if num_mods >= num_needed: |
| 53 | + return mods[:num_needed] |
| 54 | + else: |
| 55 | + #wrap around the end to the beginning |
| 56 | + num_missing = num_needed - num_mods |
| 57 | + more_mods = get_moderators().order_by('id') |
| 58 | + more_mods = more_mods[:num_missing] |
| 59 | + mods.extend(list(more_mods)) |
| 60 | + return mods |
| 61 | + |
| 62 | + |
| 63 | +def select_last_moderator(mods): |
| 64 | + return max(mods, key=lambda item: item.id) |
| 65 | + |
| 66 | + |
| 67 | +def remember_last_moderator(user): |
| 68 | + act = get_last_mod_alert_activity() |
| 69 | + if act: |
| 70 | + act.content_object = user |
| 71 | + act.save() |
| 72 | + else: |
| 73 | + act = Activity( |
| 74 | + user=user, |
| 75 | + content_object=user, |
| 76 | + activity_type=const.TYPE_ACTIVITY_MODERATION_ALERT_SENT |
| 77 | + ) |
| 78 | + act.save() |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +def notify_moderator(user): |
| 83 | + template = get_template('email/notify_moderator.html') |
| 84 | + subject_line = _('%s moderation alert') % askbot_settings.APP_SHORT_NAME, |
| 85 | + mail.send_mail( |
| 86 | + subject_line=subject_line, |
| 87 | + body_text=template.render({'user': user}), |
| 88 | + recipient_list=[user,] |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +class Command(NoArgsCommand): |
| 93 | + def handle_noargs(self, *args, **kwargs): |
| 94 | + #get size of moderation queue |
| 95 | + queue = Activity.objects.filter(activity_type__in=const.MODERATED_ACTIVITY_TYPES) |
| 96 | + if queue.count() == 0: |
| 97 | + return |
| 98 | + |
| 99 | + #get moderators |
| 100 | + mods = get_moderators().order_by('id') |
| 101 | + if mods.count() == 0: |
| 102 | + return |
| 103 | + |
| 104 | + mods = select_moderators_to_notify(mods, 3) |
| 105 | + |
| 106 | + if len(mods) == 0: |
| 107 | + return |
| 108 | + |
| 109 | + for mod in mods: |
| 110 | + notify_moderator(mod) |
| 111 | + |
| 112 | + last_mod = select_last_moderator(mods) |
| 113 | + remember_last_moderator(last_mod) |
0 commit comments