Skip to content
Snippets Groups Projects
Verified Commit 12e9f05a authored by Jan Maximilian Michal's avatar Jan Maximilian Michal
Browse files

Migrated the commands in delbert.py into Django commands

parent 07a12540
No related branches found
No related tags found
1 merge request!33Move the delbert script into Commands for manage.py
Pipeline #
from django.core.management.base import BaseCommand
from core import models
class Command(BaseCommand):
help = 'Extract all submissions from this instance'
def handle(self, *args, **kwargs):
for submission in models.Submission.objects.filter(
feedback__isnull=False).order_by('type'):
print(submission.feedback.score, repr(submission.text),
file=open(str(submission.type).replace(' ', '_'), 'a'))
from django.core.management.base import BaseCommand
import util.importer
class Command(BaseCommand):
help = 'Start the Grady command line importer'
def handle(self, *args, **kwargs):
util.importer.start()
from django.core.management.base import BaseCommand
from util.factories import init_test_instance
class Command(BaseCommand):
help = 'Creates some initial test data for the application'
def handle(self, *args, **options):
init_test_instance()
import argparse
import json
import sys
from django.core.management.base import BaseCommand
from core.models import Student
class Command(BaseCommand):
help = ('replaces all usernames based on a '
'matrikel_no -> new_name dict (input should be JSON)')
def add_arguments(self, parser):
parser.add_argument(
'matno2username_dict',
help='the mapping as a JSON file',
default=sys.stdin,
type=argparse.FileType('r')
)
def _handle(self, matno2username_dict, **kwargs):
matno2username = json.JSONDecoder().decode(matno2username_dict.read())
for student in Student.objects.all():
if student.matrikel_no in matno2username:
new_name = matno2username[student.matrikel_no]
student.user.username = new_name
student.user.save()
def handle(self, *args, **options):
self._handle(*args, **options)
import csv
import secrets
import sys
from django.core.management.base import BaseCommand
from core.models import Student
class Command(BaseCommand):
help = ('All student passwords will be changed'
'and a list of these password will be printed')
def add_arguments(self, parser):
parser.add_argument(
'instance',
help="Name of the instance that generated the passwords"
)
def _handle(self, *args, output=sys.stdout, instance="", **kwargs):
with open('/usr/share/dict/words') as words:
choose_from = list({word.strip().lower()
for word in words if 5 < len(word) < 8})
writer = csv.writer(output)
writer.writerow(
['Name', 'Matrikel', 'Username', 'password', 'instance'])
for student in Student.objects.all():
password = ''.join(secrets.choice(choose_from) for _ in range(3))
student.user.set_password(password)
student.user.save()
if not student.user.fullname:
student.user.fullname = '__no_name__'
writer.writerow([student.user.fullname, student.matrikel_no,
student.user.username, password, instance])
def handle(self, *args, **options):
self._handle(*args, **options)
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'All user accounts will be disabled'
def add_arguments(self, parser):
parser.add_argument(
'switch',
choices=('enable', 'disable'),
default='enable',
help='enable all users (enable) or disable all (disable)'
)
filter_group = parser.add_mutually_exclusive_group()
filter_group.add_argument(
'--exclude',
default=(),
nargs='+',
help='Provide all users you want to exclude from the operation'
)
filter_group.add_argument(
'--include',
help=('Provide users you want to operate on'
'Everything else is untouched'),
nargs='+',
default=())
def handle(self, switch, exclude=None, include=None, *args, **kwargs):
if include:
for user in get_user_model().objects.filter(username__in=include):
user.is_active = switch == 'enable'
user.save()
else: # this includes nothing set
for user in get_user_model().objects.exclude(username__in=exclude):
user.is_active = switch == 'enable'
user.save()
from django.core.management import call_command
from django.contrib.auth import get_user_model
from django.test import TestCase
from util.factories import GradyUserFactory
from core.models import Student
import tempfile
import json
class CommandsTestCase(TestCase):
factory = GradyUserFactory()
def test_usermod(self):
self.factory.make_tutor(username='otto')
args = ['disable']
opts = {'include': ('otto',)}
call_command('usermod', *args, **opts)
someone = get_user_model().objects.get(username='otto')
self.assertFalse(someone.is_active)
def test_replaceusernames(self):
self.factory.make_student(matrikel_no=88884444, username='before')
with tempfile.NamedTemporaryFile() as matno2username:
matno2username.write(json.dumps({'88884444': 'after'}).encode())
matno2username.flush()
args = [matno2username.name]
call_command('replaceusernames', *args, **{})
student = Student.objects.get(matrikel_no=88884444)
self.assertEqual('after', student.user.username)
import argparse
import csv
import json
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grady.settings')
import secrets
import sys
import django
django.setup()
from django.contrib.auth.models import User
import util.importer
from core.models import Student, Submission
unused_variable = []
def parseme():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
parser.add_argument(
'-o', '--output',
help='Where the output goes (not info messages)',
default=sys.stdout,
type=argparse.FileType(mode='r'),
)
### parser for printing out new passwordlists ###
passwordlist = subparsers.add_parser(
'passwordlist',
help='all student passwords will be changed and a list of these password will be printed'
)
passwordlist.add_argument(
'instance',
default='',
help='name of the instance that generated the passwords'
)
### parser for replacing usernames ###
replaceusernames = subparsers.add_parser(
'replaceusernames',
help='replaces all usernames based on a matrikel_no -> new_name dict (input should be JSON)'
)
replaceusernames.add_argument(
'matno2username_dict',
help='the mapping as a JSON file',
default=sys.stdin,
type=argparse.FileType('r')
)
### parser for enabling or disabling users ###
enableusers = subparsers.add_parser(
'enableusers',
help='All user accounts will be disabled'
)
enableusers.add_argument(
'switch',
choices=('on', 'off'),
default='on',
help='enable all users (on) or disable all (off)'
)
filter_group = enableusers.add_mutually_exclusive_group()
filter_group.add_argument(
'-e', '--exclude',
default=(),
nargs='+',
help='give exceptions by username in a comma separated list'
)
filter_group.add_argument(
'-i', '--include',
help='only apply to these users',
nargs='+',
default=())
### parser for extracting submissions ###
subparsers.add_parser('extractsubmissions')
### parser for extracting submissions ###
subparsers.add_parser('importer')
return parser.parse_args()
def handle_passwordlist(output=sys.stdout, instance="", **kwargs):
with open('/usr/share/dict/words') as words:
choose_from = list({word.strip().lower()
for word in words if 5 < len(word) < 8})
writer = csv.writer(output)
writer.writerow(['Name', 'Matrikel', 'Username', 'password', 'instance'])
for student in Student.objects.all():
password = ''.join(secrets.choice(choose_from) for _ in range(3))
student.user.set_password(password)
student.user.save()
writer.writerow([student.name, student.matrikel_no,
student.user.username, password, instance])
def handle_enableusers(switch, exclude, include, **kwargs):
if include:
for user in User.objects.filter(username__in=include):
user.is_active = switch == 'on'
user.save()
else: # this includes nothing set
for user in User.objects.exclude(username__in=exclude):
user.is_active = switch == 'on'
user.save()
def handle_replaceusernames(matno2username_dict, **kwargs):
matno2username = json.JSONDecoder().decode(matno2username_dict.read())
for student in Student.objects.all():
if student.matrikel_no in matno2username:
new_name = matno2username[student.matrikel_no]
student.user.username = new_name
student.user.save()
def handle_extractsubmissions(output, **kwargs):
for submission in Submission.objects.filter(feedback__isnull=False).order_by('type'):
print(submission.feedback.score, repr(submission.text),
file=open(str(submission.type).replace(' ', '_'), 'a'))
def handle_importer(**kwargs):
util.importer.start()
def main():
args = parseme()
if args.command:
globals()['handle_' + args.command](**vars(args))
if __name__ == '__main__':
main()
......@@ -45,12 +45,13 @@ class GradyUserFactory:
def _get_random_name(prefix='', suffix='', k=1):
return ''.join((prefix, get_random_password(k), suffix))
def _make_base_user(self, username, groupname, password=None, store_pw=False, **kwargs):
def _make_base_user(self, username, groupname, password=None,
store_pw=False, **kwargs):
""" This is a specific wrapper for the django update_or_create method of
objects.
* A new user is created and password and group are set accordingly
* If the user was there before password is NOT change but group is. A
user must only have one group.
* If the user was there before password is NOT change but group is.
* A user must only have one group.
Returns:
(User object, str): The user object that was added to the group and
......@@ -96,7 +97,9 @@ class GradyUserFactory:
return generic_user
def make_student(self, username=None, matrikel_no=None, exam=None, **kwargs):
def make_student(self, username=None,
matrikel_no=None,
exam=None, **kwargs):
""" Creates a student. Defaults can be passed via kwargs like in
relation managers objects.update method. """
user = self._make_user_generic(username, STUDENTS, **kwargs)
......@@ -125,7 +128,7 @@ def make_exams(exams=[], **kwargs):
def make_submission_types(submission_types=[], **kwargs):
return [SubmissionType.objects.get_or_create(
name=submission_type['name'], defaults=submission_type)[0]
for submission_type in submission_types]
for submission_type in submission_types]
def make_students(students=[], **kwargs):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment