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/ ...@@ -21,6 +21,8 @@ build/
static/ static/
tests/report/ tests/report/
*.sqlite3 *.sqlite3
env/
static/
# project specific # project specific
env-grady/ env-grady/
......
...@@ -4,6 +4,7 @@ import os ...@@ -4,6 +4,7 @@ import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grady.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'grady.settings')
import secrets import secrets
import sys import sys
import json
import django import django
django.setup() django.setup()
...@@ -25,6 +26,20 @@ def parseme(): ...@@ -25,6 +26,20 @@ def parseme():
'newstudentpasswordlist', 'newstudentpasswordlist',
help='all student passwords will be changed and a list of these password will be printed' 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 = subparsers.add_parser(
'enableusers', 'enableusers',
...@@ -36,12 +51,18 @@ def parseme(): ...@@ -36,12 +51,18 @@ def parseme():
default='on', default='on',
help='enable all users (on) or disable all (off)' 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', '-e', '--exclude',
default=(), default=(),
nargs='+', nargs='+',
help='give exceptions by username in a comma separated list' 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( parser.add_argument(
'-o', '--output', '-o', '--output',
...@@ -53,12 +74,12 @@ def parseme(): ...@@ -53,12 +74,12 @@ def parseme():
return parser.parse_args() 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: with open('/usr/share/dict/words') as words:
choose_from = list({word.strip().lower() for word in words if 5 < len(word) < 8}) choose_from = list({word.strip().lower() for word in words if 5 < len(word) < 8})
print(len(choose_from))
writer = csv.writer(output) writer = csv.writer(output)
writer.writerow(['Name', 'Matrikel', 'Username', 'password', 'instance'])
for student in Student.objects.all(): for student in Student.objects.all():
password = ''.join(secrets.choice(choose_from) for _ in range(3)) password = ''.join(secrets.choice(choose_from) for _ in range(3))
...@@ -67,21 +88,37 @@ def handle_newstudentpasswordlist(output=sys.stdout): ...@@ -67,21 +88,37 @@ def handle_newstudentpasswordlist(output=sys.stdout):
student.user.save() student.user.save()
writer.writerow([student.name, student.matrikel_no, 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): def handle_replace_usernames(matno2username):
user.is_active = switch == 'off' for student in Student.objects.all():
user.save() if student.matrikel_no in matno2username:
new_name = matno2username[student.matrikel_no]
student.user.username = new_name
student.user.save()
def main(): def main():
args = parseme() args = parseme()
if args.command == 'newstudentpasswordlist': if args.command == 'newstudentpasswordlist':
handle_newstudentpasswordlist(args.output) handle_newstudentpasswordlist(args.output, args.instance)
elif args.command == 'enableusers': if args.command == 'enableusers':
handle_enableusers(args.switch, args.exclude) 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__': if __name__ == '__main__':
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