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
|
2017-07-08 00:45:51 +02:00
|
|
|
from django.core.management.base import CommandError
|
2013-05-01 19:50:52 +02:00
|
|
|
|
2013-08-08 20:38:24 +02:00
|
|
|
from zerver.lib.actions import do_update_message_flags
|
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
|
|
|
|
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')
|
2017-07-08 00:45:51 +02:00
|
|
|
self.add_realm_args(parser, True)
|
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
|
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
|
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,))
|