diff --git a/RIGS/management/commands/generateSampleData.py b/RIGS/management/commands/generateSampleData.py index 237c0cb0..465a027c 100644 --- a/RIGS/management/commands/generateSampleData.py +++ b/RIGS/management/commands/generateSampleData.py @@ -12,4 +12,5 @@ class Command(BaseCommand): call_command('generateSampleUserData') call_command('generateSampleRIGSData') call_command('generateSampleAssetsData') - call_command('generateSampleTrainingData') + call_command('import_old_db') + call_command('generate_sample_training_users') diff --git a/RIGS/models.py b/RIGS/models.py index 3bca29bd..2f5e37d1 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -56,6 +56,11 @@ class Profile(AbstractUser): def latest_events(self): return self.event_mic.order_by('-start_date').select_related('person', 'organisation', 'venue', 'mic', 'riskassessment', 'invoice').prefetch_related('checklists') + @cached_property + def as_trainee(self): + from training.models import Trainee + return Trainee.objects.get(pk=self.pk) + @classmethod def admins(cls): return Profile.objects.filter(email__in=[y for x in settings.ADMINS for y in x]) diff --git a/training/decorators.py b/training/decorators.py new file mode 100644 index 00000000..50a19602 --- /dev/null +++ b/training/decorators.py @@ -0,0 +1,4 @@ +from PyRIGS.decorators import user_passes_test_with_403 + +def has_perm_or_supervisor(perm, login_url=None, oembed_view=None): + return user_passes_test_with_403(lambda u: (hasattr(u, 'as_trainee') and u.as_trainee.is_supervisor) or u.has_perm(perm), login_url=login_url, oembed_view=oembed_view) diff --git a/training/management/commands/generateSampleTrainingData.py b/training/management/commands/generateSampleTrainingData.py index d994670d..5a69491c 100644 --- a/training/management/commands/generateSampleTrainingData.py +++ b/training/management/commands/generateSampleTrainingData.py @@ -2,6 +2,7 @@ import datetime import random from django.contrib.auth.models import Group, Permission +from django.core.management import call_command from django.core.management.base import BaseCommand, CommandError from django.db import transaction from django.utils import timezone @@ -31,7 +32,7 @@ class Command(BaseCommand): self.setup_categories() self.setup_items() self.setup_levels() - self.setup_supervisor() + call_command('generate_sample_training_users') print("Done generating training data") def setup_categories(self): @@ -192,20 +193,3 @@ class Command(BaseCommand): models.TrainingLevelRequirement.objects.create(level=supervisor, item=item, depth=random.choice(models.TrainingItemQualification.CHOICES)[0]) self.levels.append(technician) self.levels.append(supervisor) - - def setup_supervisor(self): - supervisor = models.Profile.objects.create(username="supervisor", first_name="Super", last_name="Visor", - initials="SV", - email="supervisor@example.com", is_active=True, - is_staff=True, is_approved=True) - supervisor.set_password('supervisor') - supervisor.groups.add(Group.objects.get(name="Keyholders")) - supervisor.save() - models.TrainingLevelQualification.objects.create( - trainee=supervisor, - level=models.TrainingLevel.objects.filter( - level__gte=models.TrainingLevel.SUPERVISOR).exclude( - department=models.TrainingLevel.HAULAGE).exclude( - department__isnull=True).first(), - confirmed_on=timezone.now(), - confirmed_by=models.Trainee.objects.first()) diff --git a/training/management/commands/generate_sample_training_users.py b/training/management/commands/generate_sample_training_users.py new file mode 100644 index 00000000..dad563f1 --- /dev/null +++ b/training/management/commands/generate_sample_training_users.py @@ -0,0 +1,69 @@ +import datetime +import random + +from django.contrib.auth.models import Group, Permission +from django.core.management.base import BaseCommand, CommandError +from django.db import transaction +from django.utils import timezone +from reversion import revisions as reversion + +from training import models +from RIGS.models import Profile + + +class Command(BaseCommand): + help = 'Adds training users' + can_import_settings = True + + profiles = [] + committee_group = None + + def handle(self, *args, **options): + print("Generating useful training users") + from django.conf import settings + + if not (settings.DEBUG or settings.STAGING): + raise CommandError('You cannot run this command in production') + + random.seed('otherwise it is done by time, which could lead to inconsistent tests') + + with transaction.atomic(): + self.setup_groups() + self.setup_useful_profiles() + print("Done generating useful training users") + + def setup_groups(self): + self.committee_group = Group.objects.create(name='Committee') + + perms = ["add_trainingitemqualification", "change_trainingitemqualification", "delete_trainingitemqualification", "add_traininglevelqualification", "change_traininglevelqualification", "delete_traininglevelqualification", "add_traininglevelrequirement", "change_traininglevelrequirement", "delete_traininglevelrequirement"] + + for permId in perms: + self.committee_group.permissions.add(Permission.objects.get(codename=permId)) + + self.committee_group.save() + + + def setup_useful_profiles(self): + supervisor = Profile.objects.create(username="supervisor", first_name="Super", last_name="Visor", + initials="SV", + email="supervisor@example.com", is_active=True, + is_staff=True, is_approved=True) + supervisor.set_password('supervisor') + supervisor.groups.add(Group.objects.get(name="Keyholders")) + supervisor.save() + models.TrainingLevelQualification.objects.create( + trainee=supervisor, + level=models.TrainingLevel.objects.filter( + level__gte=models.TrainingLevel.SUPERVISOR).exclude( + department=models.TrainingLevel.HAULAGE).exclude( + department__isnull=True).first(), + confirmed_on=timezone.now(), + confirmed_by=models.Trainee.objects.first()) + + committee_user = Profile.objects.create(username="committee", first_name="Committee", last_name="Member", + initials="CM", + email="committee@example.com", is_active=True, is_approved=True) + committee_user.groups.add(self.committee_group) + supervisor.groups.add(Group.objects.get(name="Keyholders")) + committee_user.set_password('committee') + committee_user.save() diff --git a/training/templates/level_detail.html b/training/templates/level_detail.html index 1e72ed7e..48f3b617 100644 --- a/training/templates/level_detail.html +++ b/training/templates/level_detail.html @@ -44,7 +44,7 @@ {% endblock %} {% block content %} -{% if request.user.is_supervisor or perms.training.change_traininglevel %} +{% if request.user.as_trainee.is_supervisor or perms.training.add_traininglevelrequirement %}
{% for level in object_list %} {% endfor %} diff --git a/training/templates/trainee_detail.html b/training/templates/trainee_detail.html index af481b4f..2544bb53 100644 --- a/training/templates/trainee_detail.html +++ b/training/templates/trainee_detail.html @@ -43,9 +43,11 @@ {% block content %}