2013-05-01 19:50:52 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-05-01 19:50:52 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2013-08-08 20:38:24 +02:00
|
|
|
from zerver.lib.actions import do_update_message_flags
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import UserProfile, Message, get_user_profile_by_email
|
2013-05-01 19:50:52 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Bankrupt one or many users."""
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('emails', metavar='<email>', type=str, nargs='+',
|
|
|
|
help='email address to bankrupt')
|
2013-05-01 19:50:52 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2013-05-01 19:50:52 +02:00
|
|
|
try:
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("e-mail %s doesn't exist in the system, skipping" % (email,))
|
2013-05-01 19:50:52 +02:00
|
|
|
continue
|
|
|
|
|
2016-05-08 15:20:51 +02:00
|
|
|
do_update_message_flags(user_profile, "add", "read", None, True, None, None)
|
2013-05-01 19:50:52 +02:00
|
|
|
|
|
|
|
messages = Message.objects.filter(
|
|
|
|
usermessage__user_profile=user_profile).order_by('-id')[:1]
|
|
|
|
if messages:
|
|
|
|
old_pointer = user_profile.pointer
|
|
|
|
new_pointer = messages[0].id
|
|
|
|
user_profile.pointer = new_pointer
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["pointer"])
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%s: %d => %d" % (email, old_pointer, new_pointer))
|
2013-05-01 19:50:52 +02:00
|
|
|
else:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%s has no messages, can't bankrupt!" % (email,))
|