Skip to content

Commit

Permalink
finish migration command
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Jul 18, 2022
1 parent 4b076df commit 77c8aea
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 20 deletions.
77 changes: 77 additions & 0 deletions api/migrations/0025_accountmigration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Generated by Django 3.2.14 on 2022-07-18 00:07

import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("api", "0024_alter_transcriptioncheck_status"),
]

operations = [
migrations.CreateModel(
name="AccountMigration",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"create_time",
models.DateTimeField(default=django.utils.timezone.now),
),
(
"slack_channel_id",
models.CharField(
blank=True, default=None, max_length=50, null=True
),
),
(
"slack_message_ts",
models.CharField(
blank=True, default=None, max_length=50, null=True
),
),
("affected_submissions", models.ManyToManyField(to="api.Submission")),
(
"moderator",
models.ForeignKey(
default=None,
null=True,
on_delete=django.db.models.deletion.SET_DEFAULT,
to=settings.AUTH_USER_MODEL,
),
),
(
"new_user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="new_user",
to=settings.AUTH_USER_MODEL,
),
),
(
"old_user",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="old_user",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
6 changes: 2 additions & 4 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,10 @@ class AccountMigration(models.Model):
new_user = models.ForeignKey(
"authentication.BlossomUser",
on_delete=models.SET_NULL,
related_name="old_user",
related_name="new_user",
null=True,
blank=True,
)
completed = models.BooleanField(default=False)
# keep track of submissions that were modified in case we need to roll back
affected_submissions = models.ManyToManyField(Submission)
# who has approved this migration?
Expand All @@ -450,7 +449,7 @@ class AccountMigration(models.Model):
def perform_migration(self) -> None:
"""Move all submissions attributed to one account to another."""
existing_submissions = Submission.objects.filter(completed_by=self.old_user)
self.affected_submissions.add(existing_submissions)
self.affected_submissions.add(*existing_submissions)

existing_submissions.update(
claimed_by=self.new_user, completed_by=self.new_user
Expand All @@ -465,4 +464,3 @@ def revert(self) -> None:
self.affected_submissions.update(
claimed_by=self.old_user, completed_by=self.old_user
)
self.completed = False
2 changes: 2 additions & 0 deletions api/slack/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from api.slack.commands.dadjoke import dadjoke_cmd
from api.slack.commands.help import help_cmd
from api.slack.commands.info import info_cmd
from api.slack.commands.migrate_user import migrate_user_cmd
from api.slack.commands.ping import ping_cmd
from api.slack.commands.reset import reset_cmd
from api.slack.commands.unwatch import unwatch_cmd
Expand Down Expand Up @@ -58,6 +59,7 @@ def process_command(data: Dict) -> None:
"dadjoke": dadjoke_cmd,
"help": help_cmd,
"info": info_cmd,
"migrate": migrate_user_cmd,
"ping": ping_cmd,
"reset": reset_cmd,
"unwatch": unwatch_cmd,
Expand Down
33 changes: 17 additions & 16 deletions api/slack/commands/migrate_user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from copy import copy
from copy import deepcopy

from api.models import AccountMigration
from api.slack import client
Expand Down Expand Up @@ -82,37 +82,37 @@


def _create_blocks(
migration: AccountMigration, approve_cancel: bool = True, revert: bool = False
migration: AccountMigration, approve_cancel: bool = False, revert: bool = False
) -> dict:
blocks = copy(BASE)
header = copy(HEADER_BLOCK)
header["text"]["text"] = header["text"]["text"].format(
blocks = deepcopy(BASE)
header = deepcopy(HEADER_BLOCK)
header["text"]["text"] = HEADER_BLOCK["text"]["text"].format(
migration.old_user.username, migration.new_user.username
)
blocks["blocks"].append(header)

if migration.moderator and revert:
# show who approved it while when we show the button to revert it
mod_block = copy(MOD_BLOCK)
mod_block = deepcopy(MOD_BLOCK)
mod_block["text"]["text"] = MOD_BLOCK["text"]["text"].format(
migration.moderator.username
)
blocks["blocks"].append(mod_block)

blocks["blocks"].append(DIVIDER_BLOCK)

action_block = copy(ACTION_BLOCK)
action_block = deepcopy(ACTION_BLOCK)

if approve_cancel:
approve_button = copy(APPROVE_BUTTON)
approve_button = deepcopy(APPROVE_BUTTON)
approve_button["value"] = APPROVE_BUTTON["value"].format(migration.id)
cancel_button = copy(CANCEL_BUTTON)
cancel_button = deepcopy(CANCEL_BUTTON)
cancel_button["value"] = CANCEL_BUTTON["value"].format(migration.id)
action_block["elements"].append(approve_button)
action_block["elements"].append(cancel_button)

if revert:
revert_button = copy(REVERT_BUTTON)
revert_button = deepcopy(REVERT_BUTTON)
revert_button["value"] = revert_button["value"].format(migration.id)
action_block["elements"].append(revert_button)

Expand All @@ -127,9 +127,10 @@ def migrate_user_cmd(channel: str, message: str) -> None:
parsed_message = message.split()
blocks = None
msg = None
migration = None # appease linter
if len(parsed_message) < 3:
# Needs to have two usernames
msg = i18n["slack"]["errors"]["missing_username"]
msg = i18n["slack"]["errors"]["missing_multiple_usernames"]
elif len(parsed_message) == 3:
old_user, old_username = parse_user(parsed_message[1])
new_user, new_username = parse_user(parsed_message[2])
Expand All @@ -144,9 +145,9 @@ def migrate_user_cmd(channel: str, message: str) -> None:

if old_user and new_user:
migration = AccountMigration.objects.create(
delay_minutes=3, old_user=old_user, new_user=new_user
old_user=old_user, new_user=new_user
)
blocks = _create_blocks(migration)
blocks = _create_blocks(migration, approve_cancel=True)

else:
msg = i18n["slack"]["errors"]["too_many_params"]
Expand All @@ -160,8 +161,8 @@ def migrate_user_cmd(channel: str, message: str) -> None:
response = client.chat_postMessage(**args)

if blocks:
migration.report_slack_channel_id = response["channel"]
migration.report_slack_message_ts = response["message"]["ts"]
migration.slack_channel_id = response["channel"]
migration.slack_message_ts = response["message"]["ts"]
migration.save()


Expand Down Expand Up @@ -195,7 +196,7 @@ def process_migrate_user(data: dict) -> None:

if action == "approve":
migration.perform_migration()
blocks = _create_blocks(migration, approve_cancel=False, revert=True)
blocks = _create_blocks(migration, revert=True)
elif action == "revert":
migration.revert()
blocks = _create_blocks(migration) # Show no buttons here.
Expand Down
Loading

0 comments on commit 77c8aea

Please sign in to comment.