Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove unused rest apis #1529

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
69 changes: 0 additions & 69 deletions apps/analysis_framework/tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,75 +222,6 @@ def test_create_analysis_framework(self):
role=project.analysis_framework.get_or_create_owner_role(),
).first() is not None, "Membership Should be created"

def test_clone_analysis_framework_without_name(self):
analysis_framework = self.create(AnalysisFramework)
project = self.create(
Project, analysis_framework=analysis_framework,
role=self.admin_role
)

url = '/api/v1/clone-analysis-framework/{}/'.format(
analysis_framework.id
)
data = {
'project': project.id,
}

self.authenticate()
response = self.client.post(url, data)
self.assert_400(response)
assert 'title' in response.data['errors']

def test_clone_analysis_framework(self):
"""This is relevant only to public frameworks"""
analysis_framework = self.create(AnalysisFramework, is_private=False)
project = self.create(
Project, analysis_framework=analysis_framework,
role=self.admin_role
)
# Add self.user as member to analysis framework, to check if owner membership created or not
default_membership, _ = analysis_framework.add_member(self.user)
# Add owner user, but this should not be in the cloned framework
user = self.create(User)
owner_membership, _ = analysis_framework.add_member(user, analysis_framework.get_or_create_owner_role())

url = '/api/v1/clone-analysis-framework/{}/'.format(
analysis_framework.id
)
cloned_title = 'Cloned AF'
data = {
'project': project.id,
'title': cloned_title,
'description': 'New Description',
}

self.authenticate()
response = self.client.post(url, data)
self.assert_201(response)

self.assertNotEqual(response.data['id'], analysis_framework.id)
self.assertEqual(
response.data['title'],
cloned_title)

project = Project.objects.get(id=project.id)
new_af = project.analysis_framework

self.assertNotEqual(new_af.id, analysis_framework.id)
self.assertEqual(project.analysis_framework.id, response.data['id'])

# Check if description updated
assert new_af.description == data['description'], "Description should be updated"
assert new_af.title == data['title'], "Title should be updated"

# Test permissions cloned
# Only the requester should be the owner of the new framework
assert new_af.members.all().count() == 1, "The cloned framework should have only one owner"
assert AnalysisFrameworkMembership.objects.filter(
framework=new_af, role=owner_membership.role,
member=self.user,
).exists()

def test_create_private_framework_unauthorized(self):
project = self.create(Project, role=self.admin_role)

Expand Down
107 changes: 0 additions & 107 deletions apps/analysis_framework/tests/test_roles_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,66 +41,6 @@ def setUp(self):
key='text-widget-002',
)

def test_get_private_roles(self):
url = '/api/v1/private-framework-roles/'
self.authenticate()
response = self.client.get(url)
self.assert_200(response)

data = response.data
for role in data['results']:
assert role['is_private_role'] is True, "Must be a private role"

def test_get_public_roles_all(self):
url = '/api/v1/public-framework-roles/'
self.authenticate()
response = self.client.get(url)
self.assert_200(response)

data = response.data
for role in data['results']:
assert role['is_private_role'] is not True, "Must be a public role"

assert any(x['is_default_role'] for x in data['results']), "A default role should be present"

def test_get_public_roles_no_default(self):
url = '/api/v1/public-framework-roles/?is_default_role=false'
self.authenticate()
response = self.client.get(url)
self.assert_200(response)

data = response.data
for role in data['results']:
assert role['is_private_role'] is not True, "Must be a public role"

print([x['is_default_role'] for x in data['results']])
assert not any(x['is_default_role'] for x in data['results']), "No default role should be present"

def test_owner_role(self):
self.private_framework.add_member(
self.user,
self.private_framework.get_or_create_owner_role()
)
self.public_framework.add_member(
self.user,
self.public_framework.get_or_create_owner_role()
)
# CLONING THE FRAMEWORK
response = self._clone_framework_test(self.private_framework)
self.assert_403(response)

response = self._clone_framework_test(self.public_framework)
self.assert_201(response)

# EDITING THE FRAMEWORK OWNED
self._edit_framework_test(self.private_framework, self.user, 200)
self._edit_framework_test(self.public_framework, self.user, 200)

# Can use the framework in other projects

self._add_user_test(self.public_framework, self.user, 201)
self._add_user_test(self.private_framework, self.user, 201)

def test_patch_membership(self):
self.private_framework.add_member(
self.user,
Expand Down Expand Up @@ -136,47 +76,6 @@ def test_get_membership(self):

self.assert_200(resp)

def test_editor_role(self):
editor_user = self.create(User)
self.private_framework.add_member(
editor_user,
self.private_framework.get_or_create_editor_role()
)
self.public_framework.add_member(
editor_user,
self.public_framework.get_or_create_editor_role()
)

# CLONING FRAMEWORK
response = self._clone_framework_test(self.private_framework, editor_user)
self.assert_403(response)

# EDITING FRAMEWORK
self._edit_framework_test(self.private_framework, editor_user, status=200)
self._edit_framework_test(self.public_framework, editor_user, status=200)

# ADDING USER and ASSIGNING ROLES
self._add_user_test(self.public_framework, editor_user, 403)
self._add_user_test(self.private_framework, editor_user, 403)

def test_no_role(self):
normal_user = self.create(User)
# CLONING FRAMEWORK
# private framework
response = self._clone_framework_test(self.private_framework, normal_user)
self.assert_403(response)
# public framework
response = self._clone_framework_test(self.public_framework, normal_user)
self.assert_201(response)

# EDITING FRAMEWORK
self._edit_framework_test(self.public_framework, normal_user, status=403)
self._edit_framework_test(self.private_framework, normal_user, status=404)

# ADDING USER and ASSIGNING ROLES
self._add_user_test(self.public_framework, normal_user, 403)
self._add_user_test(self.private_framework, normal_user, 404)

def test_add_user_with_public_role_to_private_framework(self):
private_framework = self.create(AnalysisFramework, is_private=True)
public_framework = self.create(AnalysisFramework, is_private=False)
Expand Down Expand Up @@ -286,12 +185,6 @@ def _edit_framework_test(self, framework, user=None, status=200):
response = self.client.put(url, edit_data)
self.assertEqual(response.status_code, status)

def _clone_framework_test(self, framework, user=None):
clone_url = f'/api/v1/clone-analysis-framework/{framework.id}/'
self.authenticate(user)
data = {'title': 'Cloned'}
return self.client.post(clone_url, data=data)

def _add_user_test(self, framework, user, status=201, role=None):
add_user_url = '/api/v1/framework-memberships/'
role = (role and role.id) or framework.get_or_create_editor_role().id,
Expand Down
78 changes: 0 additions & 78 deletions apps/analysis_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@
import django_filters
from django.db import models
from rest_framework import (
exceptions,
permissions,
response,
status,
filters,
views,
viewsets,
)
from rest_framework.decorators import action
from deep.permissions import ModifyPermission
from deep.paginations import SmallSizeSetPagination

from project.models import Project
from entry.models import Entry
from .models import (
AnalysisFramework, Widget, Filter, Exportable,
AnalysisFrameworkMembership,
AnalysisFrameworkRole,
)
from .serializers import (
AnalysisFrameworkSerializer,
WidgetSerializer,
FilterSerializer, ExportableSerializer,
AnalysisFrameworkMembershipSerializer,
AnalysisFrameworkRoleSerializer,
)
from .filter_set import AnalysisFrameworkFilterSet
from .permissions import FrameworkMembershipModifyPermission
Expand Down Expand Up @@ -85,57 +80,6 @@ def get_memberships(self, request, pk=None, version=None):
return self.get_paginated_response(serializer.data)


class AnalysisFrameworkCloneView(views.APIView):
permission_classes = [permissions.IsAuthenticated]

def post(self, request, af_id, version=None):
if not AnalysisFramework.objects.filter(
id=af_id
).exists():
raise exceptions.NotFound()

analysis_framework = AnalysisFramework.objects.get(
id=af_id
)
if not analysis_framework.can_clone(request.user):
raise exceptions.PermissionDenied()

cloned_title = request.data.get('title')
if not cloned_title:
raise exceptions.ValidationError({
'title': 'Title should be present',
})

new_af = analysis_framework.clone(
request.user,
title=cloned_title,
description=request.data.get('description'),
)
# Set the requesting user as owner member, don't create other memberships of old framework
new_af.add_member(request.user, new_af.get_or_create_owner_role())

serializer = AnalysisFrameworkSerializer(
new_af,
context={'request': request},
)

project = request.data.get('project')
if project:
project = Project.objects.get(id=project)
if not project.can_modify(request.user):
raise exceptions.ValidationError({
'project': 'Invalid project',
})
project.analysis_framework = new_af
project.modified_by = request.user
project.save()

return response.Response(
serializer.data,
status=status.HTTP_201_CREATED,
)


class WidgetViewSet(viewsets.ModelViewSet):
serializer_class = WidgetSerializer
permission_classes = [permissions.IsAuthenticated,
Expand Down Expand Up @@ -185,25 +129,3 @@ def destroy(self, request, *args, **kwargs):

self.perform_destroy(instance)
return response.Response(status=status.HTTP_204_NO_CONTENT)


class PrivateAnalysisFrameworkRoleViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = AnalysisFrameworkRoleSerializer
permission_classes = [permissions.IsAuthenticated]

def get_queryset(self):
return AnalysisFrameworkRole.objects.filter(is_private_role=True)


class PublicAnalysisFrameworkRoleViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = AnalysisFrameworkRoleSerializer
permission_classes = [permissions.IsAuthenticated]

def get_queryset(self):
no_default_role = self.request.query_params.get('is_default_role', 'true') == 'false'
extra = {} if not no_default_role else {'is_default_role': False}

return AnalysisFrameworkRole.objects.filter(
is_private_role=False,
**extra,
)
4 changes: 2 additions & 2 deletions apps/ary/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from organization.models import Organization, OrganizationType
from organization.serializers import (
ArySourceOrganizationSerializer,
OrganizationTypeSerializer,
OrganizationGqSerializer,
)
from gallery.serializers import SimpleFileSerializer

Expand Down Expand Up @@ -322,7 +322,7 @@ def have_source(source_type):
context=self.context,
).data
if have_source(Field.ORGANIZATIONS or Field.DONORS) else [],
'organization_type': OrganizationTypeSerializer(
'organization_type': OrganizationGqSerializer(
OrganizationType.objects.all(),
many=True,
).data
Expand Down
Loading
Loading