Skip to content

Commit

Permalink
feat(project, update): prevent views or tasks update when sync is ena…
Browse files Browse the repository at this point in the history
…bled

Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Jan 24, 2025
1 parent 4dc9f04 commit e19be64
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
4 changes: 3 additions & 1 deletion rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@
'PROJECT_IMPORTS_LIST',
'PROJECT_SEND_ISSUE',
'PROJECT_QUESTIONS_AUTOSAVE',
'NESTED_PROJECTS'
'NESTED_PROJECTS',
'PROJECT_VIEWS_SYNC',
'PROJECT_TASKS_SYNC'
]

SETTINGS_API = [
Expand Down
7 changes: 7 additions & 0 deletions rdmo/projects/serializers/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from rdmo.questions.models import Catalog
from rdmo.services.validators import ProviderValidator
Expand Down Expand Up @@ -93,6 +94,12 @@ class Meta:
ProjectParentValidator()
]

def validate_views(self, value):
"""Block updates to views if syncing is enabled."""
if settings.PROJECT_VIEWS_SYNC and value:
raise ValidationError(_('Updating views is not allowed.'))
return value


class ProjectCopySerializer(ProjectSerializer):

Expand Down
4 changes: 2 additions & 2 deletions rdmo/projects/templates/projects/project_detail_issues.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2>{% trans 'Tasks' %}</h2>
<th style="width: 15%">{% trans 'Time frame' %}</th>
<th style="width: 15%">{% trans 'Status' %}</th>
<th style="width: 10%" class="text-right">
{% if can_change_project %}
{% if can_change_project and not settings.PROJECT_TASKS_SYNC %}
<a href="{% url 'project_update_tasks' project.pk %}" title="{% trans 'Update project tasks.' %}">
<i class="fa fa-pencil"></i>
</a>
Expand Down Expand Up @@ -67,7 +67,7 @@ <h2>{% trans 'Tasks' %}</h2>

{% else %}

{% if can_change_project %}
{% if can_change_project and not settings.PROJECT_TASKS_SYNC %}
<p class="project-update">
<a href="{% url 'project_update_tasks' project.pk %}" title="{% trans 'Update project tasks.' %}">
<i class="fa fa-pencil"></i>
Expand Down
4 changes: 2 additions & 2 deletions rdmo/projects/templates/projects/project_detail_views.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>{% trans 'Views' %}</h2>
<th style="width: 20%">{% trans 'View' %}</th>
<th style="width: 60%">{% trans 'Description' %}</th>
<th style="width: 20%" class="text-right">
{% if can_change_project %}
{% if can_change_project and not settings.PROJECT_VIEWS_SYNC %}
<a href="{% url 'project_update_views' project.pk %}" title="{% trans 'Update project views' %}">
<i class="fa fa-pencil"></i>
</a>
Expand All @@ -45,7 +45,7 @@ <h2>{% trans 'Views' %}</h2>

{% else %}

{% if can_change_project %}
{% if can_change_project and not settings.PROJECT_VIEWS_SYNC %}
<p class="project-update">
<a href="{% url 'project_update_views' project.pk %}" title="{% trans 'Update project views' %}">
<i class="fa fa-pencil"></i>
Expand Down
14 changes: 14 additions & 0 deletions rdmo/projects/tests/test_viewset_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,20 @@ def test_update_parent(db, client, username, password, project_id):
assert Project.objects.get(pk=project_id).parent == project.parent


def test_update_project_views_not_allowed(db, client, settings):
assert settings.PROJECT_VIEWS_SYNC
client.login(username='owner', password='owner')

url = reverse(urlnames['detail'], args=[project_id])
data = {
'views': [1]
}
response = client.put(url, data, content_type='application/json')

assert response.status_code == 400
assert 'Updating views is not allowed' in ' '.join(response.json()['views'])


@pytest.mark.parametrize('username,password', users)
@pytest.mark.parametrize('project_id', projects)
def test_delete(db, client, username, password, project_id):
Expand Down
13 changes: 11 additions & 2 deletions rdmo/projects/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,20 @@ def get_context_data(self, **kwargs):
context['catalogs'] = Catalog.objects.filter_current_site() \
.filter_group(self.request.user) \
.filter_availability(self.request.user)
context['tasks_available'] = Task.objects.filter_current_site() \

if settings.PROJECT_TASKS_SYNC:
# tasks should be synced, the user can not change them
context['tasks_available'] = project.tasks.exists()
else:
context['tasks_available'] = Task.objects.filter_current_site() \
.filter_catalog(self.object.catalog) \
.filter_group(self.request.user) \
.filter_availability(self.request.user).exists()
context['views_available'] = View.objects.filter_current_site() \
if settings.PROJECT_VIEWS_SYNC:
# views should be synced, the user can not change them
context['views_available'] = project.views.exists()
else:
context['views_available'] = View.objects.filter_current_site() \
.filter_catalog(self.object.catalog) \
.filter_group(self.request.user) \
.filter_availability(self.request.user).exists()
Expand Down

0 comments on commit e19be64

Please sign in to comment.