Skip to content

Instantly share code, notes, and snippets.

@tingletech
Last active October 21, 2020 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tingletech/65cbce01207ba5b92ee5ffcd255ed913 to your computer and use it in GitHub Desktop.
Save tingletech/65cbce01207ba5b92ee5ffcd255ed913 to your computer and use it in GitHub Desktop.
move_preprints manage command for janeway
usage: manage.py move_preprints [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] active_user proxy_user

move preprints from a proxy account to a new account

positional arguments:
  active_user           `email` of new active account
  proxy_user            `email` of old proxy account

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
from django.core.management.base import BaseCommand, CommandError
from django.db import connection
from core.models import Account
class Command(BaseCommand):
help = "move preprints from a proxy account to a new account"
def add_arguments(self, parser):
parser.add_argument(
"active_user", help="`email` of new active account", type=str
)
parser.add_argument("proxy_user", help="`email` of old proxy account", type=str)
def handle(self, *args, **options):
active_user = Account.objects.get(email=options["active_user"])
proxy_user = Account.objects.get(email=options["proxy_user"])
if proxy_user == active_user:
raise CommandError(
"active_user and proxy_user have the same id, nothing to do"
)
if active_user.is_active is False:
raise CommandError(
"active_user {} must have an active account".format(
options["active_user"]
)
)
if proxy_user.is_active is True:
raise CommandError(
"proxy_user {} must not have an active account".format(
options["proxy_user"]
)
)
query1 = (
"update janeway.repository_preprint set owner_id={} where owner_id={};".format(
active_user.id, proxy_user.id
)
)
query2 = "delete from core_account where id={};".format(proxy_user.id)
self.stdout.write(self.style.SUCCESS(query1))
self.stdout.write(self.style.SUCCESS(query2))
with connection.cursor() as cursor:
cursor.execute(query1)
cursor.execute(query2)
cursor.fetchall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment