예제 #1
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        if not options["flag"] or not options["op"] or not options["email"]:
            print("Please specify an operation, a flag and an email")
            exit(1)

        op = options['op']
        flag = getattr(UserMessage.flags, options['flag'])
        all_until = options['all_until']
        email = options['email']

        realm = self.get_realm(options)
        user_profile = self.get_user(email, realm)

        if all_until:
            filt = models.Q(id__lte=all_until)
        else:
            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')
        ]

        if options["for_real"]:
            sys.stdin.close()
            sys.stdout.close()
            sys.stderr.close()

        def do_update(batch):
            # type: (Iterable[int]) -> None
            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))

        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)
예제 #2
0
    def handle(self, *args: Any, **options: Any) -> None:
        if not options["flag"] or not options["op"] or not options["email"]:
            raise CommandError(
                "Please specify an operation, a flag and an email")

        op = options["op"]
        flag = getattr(UserMessage.flags, options["flag"])
        all_until = options["all_until"]
        email = options["email"]

        realm = self.get_realm(options)
        user_profile = self.get_user(email, realm)

        if all_until:
            filt = models.Q(id__lte=all_until)
        else:
            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")
        ]

        if options["for_real"]:
            sys.stdin.close()
            sys.stdout.close()
            sys.stderr.close()

        def do_update(batch: Collection[int]) -> None:
            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))

        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."
            )
            raise CommandError

        utils.run_in_batches(mids, 400, do_update, sleep_time=3)
        exit(0)
예제 #3
0
    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        if not options["flag"] or not options["op"] or not options["email"]:
            print("Please specify an operation, a flag and an email")
            exit(1)

        op = options['op']
        flag = getattr(UserMessage.flags, options['flag'])
        all_until = options['all_until']
        email = options['email']

        user_profile = get_user_profile_by_email(email)

        if all_until:
            filt = models.Q(id__lte=all_until)
        else:
            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')]

        if options["for_real"]:
            sys.stdin.close()
            sys.stdout.close()
            sys.stderr.close()

        def do_update(batch):
            # type: (Iterable[int]) -> None
            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))

        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)