diff --git a/coldfront/core/allocation/forms_/secure_dir_forms.py b/coldfront/core/allocation/forms_/secure_dir_forms.py index 8f553fc0e..6ecea33bb 100644 --- a/coldfront/core/allocation/forms_/secure_dir_forms.py +++ b/coldfront/core/allocation/forms_/secure_dir_forms.py @@ -1,8 +1,11 @@ from django import forms from django.core.validators import MinLengthValidator -from coldfront.core.allocation.utils_.secure_dir_utils import is_secure_directory_name_suffix_available -from coldfront.core.allocation.utils_.secure_dir_utils import SECURE_DIRECTORY_NAME_PREFIX +from coldfront.core.allocation.utils_.secure_dir_utils.new_directory import is_secure_directory_name_suffix_available +from coldfront.core.allocation.utils_.secure_dir_utils.new_directory import SECURE_DIRECTORY_NAME_PREFIX +from coldfront.core.project.models import ProjectUser +from coldfront.core.project.models import ProjectUserRoleChoice +from coldfront.core.project.models import ProjectUserStatusChoice class SecureDirNameField(forms.CharField): @@ -72,6 +75,31 @@ class SecureDirManageUsersRequestCompletionForm(forms.Form): widget=forms.Select()) +class SecureDirPISelectionForm(forms.Form): + + pi = forms.ModelChoiceField( + label='Principal Investigator', + queryset=ProjectUser.objects.none(), + required=True, + widget=forms.Select()) + + def __init__(self, *args, **kwargs): + self._project_pk = kwargs.pop('project_pk', None) + super().__init__(*args, **kwargs) + + if not self._project_pk: + return + self._set_pi_queryset() + + def _set_pi_queryset(self): + """Set the 'pi' choices to active PIs on the project.""" + pi_role = ProjectUserRoleChoice.objects.get( + name='Principal Investigator') + active_status = ProjectUserStatusChoice.objects.get(name='Active') + self.fields['pi'].queryset = ProjectUser.objects.filter( + project__pk=self._project_pk, role=pi_role, status=active_status) + + class SecureDirDataDescriptionForm(forms.Form): department = forms.CharField( label=('Specify the full name of the department that this directory ' @@ -96,10 +124,6 @@ class SecureDirDataDescriptionForm(forms.Form): 'the Information Security and Policy team) about your data?', required=False) - def __init__(self, *args, **kwargs): - kwargs.pop('breadcrumb_project', None) - super().__init__(*args, **kwargs) - class SecureDirRDMConsultationForm(forms.Form): rdm_consultants = forms.CharField( @@ -110,10 +134,6 @@ class SecureDirRDMConsultationForm(forms.Form): required=True, widget=forms.Textarea(attrs={'rows': 3})) - def __init__(self, *args, **kwargs): - kwargs.pop('breadcrumb_project', None) - super().__init__(*args, **kwargs) - class SecureDirDirectoryNamesForm(forms.Form): @@ -125,11 +145,6 @@ class SecureDirDirectoryNamesForm(forms.Form): required=True, widget=forms.Textarea(attrs={'rows': 1})) - def __init__(self, *args, **kwargs): - kwargs.pop('breadcrumb_rdm_consultation', None) - kwargs.pop('breadcrumb_project', None) - super().__init__(*args, **kwargs) - class SecureDirSetupForm(forms.Form): @@ -217,6 +232,7 @@ class SecureDirRDMConsultationReviewForm(forms.Form): required=False, widget=forms.Textarea(attrs={'rows': 3})) + class SecureDirRequestEditDepartmentForm(forms.Form): department = forms.CharField( diff --git a/coldfront/core/allocation/migrations/0016_securedirrequest_pi.py b/coldfront/core/allocation/migrations/0016_securedirrequest_pi.py new file mode 100644 index 000000000..ae4aa1a27 --- /dev/null +++ b/coldfront/core/allocation/migrations/0016_securedirrequest_pi.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.5 on 2024-04-23 15:43 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('allocation', '0015_allocationrenewalrequest_renewal_survey_answers'), + ] + + operations = [ + migrations.AddField( + model_name='securedirrequest', + name='pi', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='secure_dir_request_pi', to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='securedirrequest', + name='requester', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='secure_dir_request_requester', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/coldfront/core/allocation/migrations/0017_add_pis_to_secure_dir_requests.py b/coldfront/core/allocation/migrations/0017_add_pis_to_secure_dir_requests.py new file mode 100644 index 000000000..f0835841a --- /dev/null +++ b/coldfront/core/allocation/migrations/0017_add_pis_to_secure_dir_requests.py @@ -0,0 +1,31 @@ +from django.db import migrations + + +def set_request_pis_to_requesters(apps, schema_editor): + """Prior to this migration, only PIs could request new secure + directories. Set the PI of each existing request to be equal to the + requester.""" + SecureDirRequest = apps.get_model('allocation', 'SecureDirRequest') + for secure_dir_request in SecureDirRequest.objects.all(): + secure_dir_request.pi = secure_dir_request.requester + secure_dir_request.save() + + +def unset_request_pis(apps, schema_editor): + """Unset the PI for all requests.""" + SecureDirRequest = apps.get_model('allocation', 'SecureDirRequest') + for secure_dir_request in SecureDirRequest.objects.all(): + secure_dir_request.pi = None + secure_dir_request.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('allocation', '0016_securedirrequest_pi'), + ] + + operations = [ + migrations.RunPython( + set_request_pis_to_requesters, unset_request_pis) + ] diff --git a/coldfront/core/allocation/models.py b/coldfront/core/allocation/models.py index a4c825483..704798bce 100644 --- a/coldfront/core/allocation/models.py +++ b/coldfront/core/allocation/models.py @@ -729,7 +729,12 @@ def secure_dir_request_state_schema(): class SecureDirRequest(TimeStampedModel): - requester = models.ForeignKey(User, on_delete=models.CASCADE) + requester = models.ForeignKey( + User, on_delete=models.CASCADE, + related_name='secure_dir_request_requester') + pi = models.ForeignKey( + User, null=True, on_delete=models.CASCADE, + related_name='secure_dir_request_pi') directory_name = models.TextField() department = models.TextField(null=True) data_description = models.TextField() diff --git a/coldfront/core/allocation/templates/allocation/allocation_detail.html b/coldfront/core/allocation/templates/allocation/allocation_detail.html index d068961e0..031a74987 100644 --- a/coldfront/core/allocation/templates/allocation/allocation_detail.html +++ b/coldfront/core/allocation/templates/allocation/allocation_detail.html @@ -203,15 +203,15 @@