2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-08 00:45:51 +02:00
|
|
|
from django.core.management.base import CommandError
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2017-08-04 21:11:44 +02:00
|
|
|
from zerver.lib.actions import do_mark_all_as_read
|
2017-07-08 00:45:51 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import Message
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2017-07-08 00:45:51 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Bankrupt one or many users."""
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('emails', metavar='<email>', type=str, nargs='+',
|
|
|
|
help='email address to bankrupt')
|
2017-07-08 00:45:51 +02:00
|
|
|
self.add_realm_args(parser, True)
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2017-07-08 00:45:51 +02:00
|
|
|
realm = self.get_realm(options)
|
2015-08-21 02:10:41 +02:00
|
|
|
for email in options['emails']:
|
2013-05-01 19:50:52 +02:00
|
|
|
try:
|
2017-07-08 00:45:51 +02:00
|
|
|
user_profile = self.get_user(email, realm)
|
|
|
|
except CommandError:
|
|
|
|
print("e-mail %s doesn't exist in the realm %s, skipping" % (email, realm))
|
2013-05-01 19:50:52 +02:00
|
|
|
continue
|
2018-03-14 00:04:16 +01:00
|
|
|
do_mark_all_as_read(user_profile, self.get_client())
|
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,))
|