Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions physionet-django/project/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
)
from user.models import User, TrainingType
from user.validators import validate_affiliation
from django.forms import ModelMultipleChoiceField

INVITATION_CHOICES = (
(1, 'Accept'),
Expand Down Expand Up @@ -923,13 +924,23 @@ def __init__(self, *args, **kwargs):
project_types=self.instance.resource_type,
access_policy=self.access_policy
)

if self.access_policy not in {AccessPolicy.CREDENTIALED, AccessPolicy.CONTRIBUTOR_REVIEW}:
# Open and restricted projects do not require training
if self.access_policy in {AccessPolicy.OPEN, AccessPolicy.RESTRICTED}:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here could be clearer. It's unclear how the current set of statements are organised. My preference would be a set of if else statements, working through each access policy, e.g.

if open: do x
elif restricted: do y
etc

self.fields['required_trainings'].disabled = True
self.fields['required_trainings'].required = False
self.fields['required_trainings'].widget = forms.HiddenInput()
self.initial['required_trainings'] = ''

# Credentialed and Contributor Review projects may or may not require training
if self.access_policy in {AccessPolicy.CREDENTIALED, AccessPolicy.CONTRIBUTOR_REVIEW}:
original_field = self.fields['required_trainings']
custom_field = CustomModelMultipleChoiceField(
queryset=original_field.queryset.order_by('name'),
required=True,
widget=original_field.widget
)
self.fields['required_trainings'] = custom_field

if self.access_policy == AccessPolicy.OPEN:
self.fields['dua'].disabled = True
self.fields['dua'].required = False
Expand All @@ -941,6 +952,34 @@ def __init__(self, *args, **kwargs):
field.disabled = True


class CustomModelMultipleChoiceField(ModelMultipleChoiceField):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add empty option to the widget
self.widget.choices = list(self.widget.choices)
if ('', 'No training required') not in self.widget.choices:
self.widget.choices.append(('', 'No training required'))

def clean(self, value):
# If 'No training required' is selected, return an empty list
if value == ['']:
return []

# If no selection is made or value is None, return an empty list
# (equivalent to 'No training required')
if not value:
return []

return super().clean(value)

def prepare_value(self, value):
# If the value is None or an empty list, return ['']
# to preselect 'No training required'
if value is None or value == []:
return ['']
return super().prepare_value(value)


class AuthorCommentsForm(forms.Form):
author_comments = forms.CharField(max_length=12000, required=False,
label='Comments for editor (optional)',
Expand Down
12 changes: 8 additions & 4 deletions physionet-django/project/templates/project/project_preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,14 @@ <h5 class="card-header">Access</h5>
{% if project.access_policy == AccessPolicy.CREDENTIALED or project.access_policy == AccessPolicy.CONTRIBUTOR_REVIEW %}
<p>
<strong>Required training:</strong>
{% for training in project.required_trainings.all %}
<br>
<a href="{% url 'project_required_trainings_preview' project.slug %}#{{ training.id }}">{{ training }}</a>
{% endfor%}
{% if project.required_trainings.all|length > 0 %}
{% for training in project.required_trainings.all %}
<br>
<a href="{% url 'project_required_trainings_preview' project.slug %}#{{ training.id }}">{{ training }}</a>
{% endfor %}
{% else %}
<br>No training required
{% endif %}
</p>
{% endif %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,14 @@ <h5 class="card-header">Access</h5>
{% if project.access_policy == AccessPolicy.CREDENTIALED or project.access_policy == AccessPolicy.CONTRIBUTOR_REVIEW %}
<p>
<strong>Required training:</strong>
{% for training in project.required_trainings.all %}
<br>
<a href="{% url 'published_project_required_training' project.slug project.version %}#{{ training.id }}"{{ training }}>{{ training }}</a>
{% endfor %}
{% if project.required_trainings.all|length > 0 %}
{% for training in project.required_trainings.all %}
<br>
<a href="{% url 'project_required_trainings_preview' project.slug %}#{{ training.id }}">{{ training }}</a>
{% endfor %}
{% else %}
<br>No training required
{% endif %}
</p>
{% endif %}
</div>
Expand Down
Loading