Skip to content

Commit

Permalink
add remote site fields to project form (#817), refactor ui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jun 12, 2024
1 parent 78ee70c commit 274988b
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 135 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Added
- ``settings_link`` kwarg in ``send_generic_email()`` (#1418)
- ``addremotesite`` and ``syncgroups`` command tests (#352)
- ``RemoteSite.owner_modifiable`` field (#817)
- ``assert_displayed()`` UI test helper
- **Timeline**
- ``sodar_uuid`` field in ``TimelineEventObjectRef`` model (#1415)
- REST API views (#1350)
Expand Down Expand Up @@ -99,6 +100,7 @@ Fixed
- ``ProjectStarringAjaxView`` creating redundant database objects (#1416)
- ``addremotesite`` crash in ``TimelineAPI.add_event()`` (#1425)
- ``addremotesite`` allows creation of site with mode identical to host (#1426)
- Public guest access field not correctly hidden in project form (#1429)
- **Sodarcache**
- REST API set view ``app_name`` incorrectly set (#1405)

Expand Down
54 changes: 47 additions & 7 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
RoleAssignment,
ProjectInvite,
RemoteSite,
RemoteProject,
SODAR_CONSTANTS,
ROLE_RANKING,
APP_SETTING_VAL_MAXLENGTH,
Expand Down Expand Up @@ -50,6 +51,7 @@
SITE_MODE_SOURCE = SODAR_CONSTANTS['SITE_MODE_SOURCE']
SITE_MODE_TARGET = SODAR_CONSTANTS['SITE_MODE_TARGET']
APP_SETTING_SCOPE_PROJECT = SODAR_CONSTANTS['APP_SETTING_SCOPE_PROJECT']
REMOTE_LEVEL_READ_ROLES = SODAR_CONSTANTS['REMOTE_LEVEL_READ_ROLES']

# Local constants
APP_NAME = 'projectroles'
Expand Down Expand Up @@ -359,9 +361,38 @@ def _get_parent_choices(cls, instance, user):
ret += [(c.sodar_uuid, c.full_title) for c in categories]
return sorted(ret, key=lambda x: x[1])

def _set_app_setting_widget(self, plugin_name, s_field, s_key, s_val):
def _init_remote_sites(self, project):
"""
Internal helper for setting app setting widget and value.
Initialize remote site fields in the form.
:param project: Project object or None
"""
p_display = get_display_name(PROJECT_TYPE_PROJECT)
for site in RemoteSite.objects.filter(
mode=SITE_MODE_TARGET, user_display=True, owner_modifiable=True
).order_by('name'):
f_name = 'remote_site.{}'.format(site.sodar_uuid)
f_label = 'Enable {} on {}'.format(p_display, site.name)
f_help = 'Make {} available on remote site "{}" ({})'.format(
p_display, site.name, site.url
)
f_initial = False
if project:
rp = RemoteProject.objects.filter(
site=site, project=project
).first()
# NOTE: Only "read roles" is supported at the moment
f_initial = rp and rp.level == REMOTE_LEVEL_READ_ROLES
self.fields[f_name] = forms.BooleanField(
label=f_label,
help_text=f_help,
initial=f_initial,
required=False,
)

def _set_app_setting_field(self, plugin_name, s_field, s_key, s_val):
"""
Internal helper for setting app setting field, widget and value.
:param plugin_name: App plugin name
:param s_field: Form field name
Expand Down Expand Up @@ -500,6 +531,9 @@ def _set_app_setting_notes(self, s_field, s_val, plugin):
)

def _init_app_settings(self):
"""
Initialize app settings fields in the form.
"""
# Set up setting query kwargs
self.p_kwargs = {}
# Show unmodifiable settings to superusers
Expand All @@ -522,8 +556,8 @@ def _init_app_settings(self):
)
for s_key, s_val in p_settings.items():
s_field = 'settings.{}.{}'.format(plugin_name, s_key)
# Set widget and value
self._set_app_setting_widget(plugin_name, s_field, s_key, s_val)
# Set field, widget and value
self._set_app_setting_field(plugin_name, s_field, s_key, s_val)
# Set label notes
self._set_app_setting_notes(s_field, s_val, plugin)

Expand Down Expand Up @@ -601,12 +635,17 @@ def __init__(self, project=None, current_user=None, *args, **kwargs):
# Get current user for checking permissions for form items
if current_user:
self.current_user = current_user
# Add settings fields
self._init_app_settings()
# Access parent project if present
parent_project = None
if project:
parent_project = Project.objects.filter(sodar_uuid=project).first()
# Add remote site fields if on source site
if settings.PROJECTROLES_SITE_MODE == SITE_MODE_SOURCE and (
parent_project or project and project.type == PROJECT_TYPE_PROJECT
):
self._init_remote_sites(project)
# Add settings fields
self._init_app_settings()

# Update help texts to match DISPLAY_NAMES
self.fields['title'].help_text = 'Title'
Expand Down Expand Up @@ -777,7 +816,7 @@ def clean(self):
'Public guest access is not allowed for categories',
)

# Verify settings fields
# Verify remote site fields
cleaned_data, errors = self._validate_app_settings(
self.cleaned_data,
self.app_plugins,
Expand All @@ -789,6 +828,7 @@ def clean(self):
self.cleaned_data[key] = value
for field, error in errors:
self.add_error(field, error)
# TODO: Validate remote site fields
return self.cleaned_data


Expand Down
17 changes: 9 additions & 8 deletions projectroles/static/projectroles/js/project_form.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
$(document).ready(function() {
// Hide settings fields by default
$('div[id^="div_id_settings"]').hide();

// Temporary solution for hiding the public_guest_access field
// Hide public_guest_access field by default
$('#div_id_public_guest_access').hide();
// Hide remote sites by default
$('div[id^="div_id_remote_site"]').hide();

// Check if it's category/project update and show corresponding fields
if ($('#sodar-pr-project-form-title').attr('data-project-type') === 'PROJECT') {
Expand All @@ -21,9 +22,8 @@ $(document).ready(function() {
$parentDiv.hide();
}
});

// Temporary solution for hiding the public_guest_access field
$('#div_id_public_guest_access').show();
$('div[id^="div_id_remote_site"]').show();
}

if ($('#sodar-pr-project-form-title').attr('data-project-type') === 'CATEGORY') {
Expand All @@ -45,7 +45,7 @@ $(document).ready(function() {

// Show settings fields if selected type is project/category in update form
$('#div_id_type .form-control').change(function() {
if ($('#div_id_type .form-control').val() == 'PROJECT') {
if ($('#div_id_type .form-control').val() === 'PROJECT') {
$('div[id^="div_id_settings"]').each(function () {
var $parentDiv = $(this);
var $projectElements = $parentDiv.find('select[data-project-types="project"]')
Expand All @@ -59,11 +59,10 @@ $(document).ready(function() {
} else {
$parentDiv.hide();
}

// Temporary solution for hiding the public_guest_access field
$('#div_id_public_guest_access').show();
$('div[id^="div_id_remote_site"]').show();
});
} else if ($('#div_id_type .form-control').val() == 'CATEGORY') {
} else if ($('#div_id_type .form-control').val() === 'CATEGORY') {
$('div[id^="div_id_settings"]').each(function () {
var $parentDiv = $(this);
var $categoryElements = $parentDiv.find('select[data-project-types="category"]')
Expand All @@ -77,6 +76,8 @@ $(document).ready(function() {
} else {
$parentDiv.hide();
}
$('#div_id_public_guest_access').hide();
$('div[id^="div_id_remote_site"]').hide();
});
}
});
Expand Down
Loading

0 comments on commit 274988b

Please sign in to comment.