2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any, Iterable
|
|
|
|
|
2013-03-15 20:07:38 +01:00
|
|
|
from optparse import make_option
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib import utils
|
|
|
|
from zerver.models import UserMessage, get_user_profile_by_email
|
2013-11-04 17:22:58 +01:00
|
|
|
from django.db import models
|
2013-03-15 20:07:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Sets user message flags. Used internally by actions.py. Marks all
|
|
|
|
Expects a comma-delimited list of user message ids via stdin, and an EOF to terminate."""
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('-r', '--for-real',
|
|
|
|
dest='for_real',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help="Actually change message flags. Default is a dry run."),
|
|
|
|
make_option('-f', '--flag',
|
|
|
|
dest='flag',
|
|
|
|
type='string',
|
|
|
|
help="The flag to add of remove"),
|
|
|
|
make_option('-o', '--op',
|
|
|
|
dest='op',
|
|
|
|
type='string',
|
|
|
|
help="The operation to do: 'add' or 'remove'"),
|
|
|
|
make_option('-u', '--until',
|
|
|
|
dest='all_until',
|
|
|
|
type='string',
|
|
|
|
help="Mark all messages <= specific usermessage id"),
|
|
|
|
make_option('-m', '--email',
|
|
|
|
dest='email',
|
|
|
|
type='string',
|
|
|
|
help="Email to set messages for"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2013-03-15 20:07:38 +01:00
|
|
|
if not options["flag"] or not options["op"] or not options["email"]:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Please specify an operation, a flag and an email")
|
2013-03-15 20:07:38 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
op = options['op']
|
|
|
|
flag = getattr(UserMessage.flags, options['flag'])
|
|
|
|
all_until = options['all_until']
|
|
|
|
email = options['email']
|
|
|
|
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = get_user_profile_by_email(email)
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2013-03-21 22:32:13 +01:00
|
|
|
if all_until:
|
|
|
|
filt = models.Q(id__lte=all_until)
|
2013-03-15 20:07:38 +01:00
|
|
|
else:
|
2013-03-21 22:32:13 +01:00
|
|
|
filt = models.Q(message__id__in=[mid.strip() for mid in sys.stdin.read().split(',')])
|
|
|
|
mids = [m.id for m in
|
|
|
|
UserMessage.objects.filter(filt, user_profile=user_profile).order_by('-id')]
|
|
|
|
|
2013-03-15 20:07:38 +01:00
|
|
|
if options["for_real"]:
|
|
|
|
sys.stdin.close()
|
|
|
|
sys.stdout.close()
|
|
|
|
sys.stderr.close()
|
|
|
|
|
|
|
|
def do_update(batch):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (Iterable[int]) -> None
|
2013-11-01 19:06:45 +01:00
|
|
|
msgs = UserMessage.objects.filter(id__in=batch)
|
|
|
|
if op == 'add':
|
|
|
|
msgs.update(flags=models.F('flags').bitor(flag))
|
|
|
|
elif op == 'remove':
|
|
|
|
msgs.update(flags=models.F('flags').bitand(~flag))
|
2013-03-15 20:07:38 +01:00
|
|
|
|
|
|
|
if not options["for_real"]:
|
|
|
|
logging.info("Updating %s by %s %s" % (mids, op, flag))
|
|
|
|
logging.info("Dry run completed. Run with --for-real to change message flags.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
utils.run_in_batches(mids, 400, do_update, sleep_time=3)
|
|
|
|
exit(0)
|