Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) 2018-2020 

2# MPIB <https://www.mpib-berlin.mpg.de/>, 

3# MPI-CBS <https://www.cbs.mpg.de/>, 

4# MPIP <http://www.psych.mpg.de/> 

5# 

6# This file is part of Castellum. 

7# 

8# Castellum is free software; you can redistribute it and/or modify it 

9# under the terms of the GNU Affero General Public License as published 

10# by the Free Software Foundation; either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# Castellum is distributed in the hope that it will be useful, but 

14# WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

16# Affero General Public License for more details. 

17# 

18# You should have received a copy of the GNU Affero General Public 

19# License along with Castellum. If not, see 

20# <http://www.gnu.org/licenses/>. 

21 

22import datetime 

23 

24from django.conf import settings 

25from django.contrib.auth.models import Group 

26from django.contrib.auth.models import Permission 

27from django.core.management.base import BaseCommand 

28from django.core.management.base import CommandError 

29from django.utils import timezone 

30 

31from castellum.castellum_auth.models import User 

32 

33USERS = { 

34 'Andreas': [], 

35 'Emma': ['Recruiter'], 

36 'Jonas': ['Subject manager', 'Data protection coordinator'], 

37 'Sabine': ['Receptionist'], 

38 

39 # first testing group 

40 'Vera': ['Study coordinator', 'Study conductor'], 

41 'Arne': ['Subject manager'], 

42 'Edwin': ['Subject manager', 'Recruiter'], 

43 

44 # second testing group 

45 'Anna': ['Study coordinator', 'Study conductor'], 

46 'Max': ['Subject manager'], 

47 'Sonja': ['Subject manager', 'Recruiter'], 

48 

49 # third testing group 

50 'Claus': ['Study coordinator', 'Study conductor'], 

51 'Flora': ['Subject manager'], 

52 'Oskar': ['Subject manager', 'Recruiter'], 

53 

54 # fourth testing group 

55 'Hans': ['Study coordinator', 'Study conductor'], 

56 'Nina': ['Subject manager'], 

57 'Pia': ['Subject manager', 'Recruiter'], 

58} 

59 

60 

61class Command(BaseCommand): 

62 help = 'Create a set of predefined users as examples' 

63 

64 def handle(self, *args, **options): 

65 if settings.PRODUCTION: 65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true

66 raise CommandError('Creating demo users in production environment is insecure.') 

67 

68 expiration_date = timezone.now() + datetime.timedelta(days=1000) 

69 

70 if not User.objects.filter(username='admin').exists(): 

71 User.objects.create_superuser( 

72 'admin', 'admin@example.com', User.DEMO_PASSWORD, expiration_date=expiration_date 

73 ) 

74 

75 for name, groups in USERS.items(): 

76 defaults = {'email': name + '@example.com', 'expiration_date': expiration_date} 

77 user, created = User.objects.get_or_create(username=name, defaults=defaults) 

78 if created: 

79 user.set_password(User.DEMO_PASSWORD) 

80 user.user_permissions.add(Permission.objects.get(codename='privacy_level_1')) 

81 user.groups.set(Group.objects.filter(name__in=groups)) 

82 user.save()