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

Small changes

parent 7e98b964
No related branches found
No related tags found
1 merge request!3Resolve "New input format"
......@@ -21,6 +21,8 @@ build/
static/
tests/report/
*.sqlite3
env/
static/
# project specific
env-grady/
......
......@@ -4,6 +4,7 @@ import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grady.settings')
import secrets
import sys
import json
import django
django.setup()
......@@ -25,6 +26,20 @@ def parseme():
'newstudentpasswordlist',
help='all student passwords will be changed and a list of these password will be printed'
)
newstudentpasswordlist.add_argument(
'instance',
help='name of the instance that generated the passwords'
)
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',
type=argparse.FileType('r')
)
enableusers = subparsers.add_parser(
'enableusers',
......@@ -36,12 +51,18 @@ def parseme():
default='on',
help='enable all users (on) or disable all (off)'
)
enableusers.add_argument(
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.add_argument(
'-o', '--output',
......@@ -53,12 +74,12 @@ def parseme():
return parser.parse_args()
def handle_newstudentpasswordlist(output=sys.stdout):
def handle_newstudentpasswordlist(output=sys.stdout, instance=""):
with open('/usr/share/dict/words') as words:
choose_from = list({word.strip().lower() for word in words if 5 < len(word) < 8})
print(len(choose_from))
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))
......@@ -67,21 +88,37 @@ def handle_newstudentpasswordlist(output=sys.stdout):
student.user.save()
writer.writerow([student.name, student.matrikel_no,
student.user.username, password])
student.user.username, password, instance])
def handle_enableusers(switch, exclude, include):
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_enableusers(switch, exclude):
for user in User.objects.exclude(username__in=exclude):
user.is_active = switch == 'off'
user.save()
def handle_replace_usernames(matno2username):
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 main():
args = parseme()
if args.command == 'newstudentpasswordlist':
handle_newstudentpasswordlist(args.output)
elif args.command == 'enableusers':
handle_enableusers(args.switch, args.exclude)
handle_newstudentpasswordlist(args.output, args.instance)
if args.command == 'enableusers':
handle_enableusers(args.switch, args.exclude, args.include)
if args.command == 'replaceusernames':
handle_replace_usernames(json.JSONDecoder().decode(args.matno2username_dict.read()))
if __name__ == '__main__':
main()
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