From b6a3c2718021fc5cb30ce541f1c5a2ddd7ec381e Mon Sep 17 00:00:00 2001
From: janmax <mail-github@jmx.io>
Date: Tue, 6 Jun 2017 15:57:19 +0200
Subject: [PATCH] Small changes

---
 .gitignore |  2 ++
 grrr.py    | 59 ++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index fee44660..13baa08b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,6 +21,8 @@ build/
 static/
 tests/report/
 *.sqlite3
+env/
+static/
 
 # project specific
 env-grady/
diff --git a/grrr.py b/grrr.py
index b0dd5780..b73afb33 100644
--- a/grrr.py
+++ b/grrr.py
@@ -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()
-- 
GitLab