Skip to content

Commit d329cc6

Browse files
authored
Merge pull request #58 from Xpirix/archived_orgs_permissions
Disable features for archived organisations
2 parents 8006e9f + e3ddfe8 commit d329cc6

File tree

14 files changed

+1823
-1523
lines changed

14 files changed

+1823
-1523
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ max-line-length = 79
55
# E12x continuation line indentation
66
# E251 no spaces around keyword / parameter equals
77
# E303 too many blank lines (3)
8-
ignore = E125,E126,E251,E303,W504,W60,F405,E501,E128,E111,E114,E121
8+
ignore = E125,E126,E251,E303,W504,W60,F405,E501,E128,E111,E114,E121,W503

django_project/certification/api_views/course.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from rest_framework import status
77
from rest_framework.views import APIView, Response
88

9+
from ..mixins import ActiveCertifyingOrganisationRequiredMixin
910
from ..models.certifying_organisation import CertifyingOrganisation
1011
from ..models.course import Course
1112
from ..serializers.course_serializer import CourseSerializer
@@ -48,7 +49,7 @@ def get(self, request):
4849
)
4950

5051

51-
class GetUpcomingCourseOrganisation(APIView):
52+
class GetUpcomingCourseOrganisation(ActiveCertifyingOrganisationRequiredMixin, APIView):
5253
"""API returns GeoJSON location of upcoming courses within a certifying
5354
organisation. The location is the location of the training center where
5455
this course will be held.
@@ -107,7 +108,7 @@ def get(self, request):
107108
)
108109

109110

110-
class GetPastCourseOrganisation(APIView):
111+
class GetPastCourseOrganisation(ActiveCertifyingOrganisationRequiredMixin, APIView):
111112
"""API returns GeoJSON location of past courses within a certifying
112113
organisation. The location is the location of the training center where
113114
this course will be held.
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# coding=utf-8
22
import json
3+
4+
from base.models.project import Project
5+
from certification.mixins import ActiveCertifyingOrganisationRequiredMixin
36
from django.http import HttpResponse
4-
from rest_framework.views import APIView, Response
57
from rest_framework import status
6-
from base.models.project import Project
8+
from rest_framework.views import APIView, Response
9+
710
from ..models.certifying_organisation import CertifyingOrganisation
811
from ..models.training_center import TrainingCenter
912
from ..utilities import CustomSerializer
@@ -14,53 +17,48 @@ class GetTrainingCenterProjectLocation(APIView):
1417

1518
def get(self, request):
1619
try:
17-
project = Project.objects.get(slug='qgis')
20+
project = Project.objects.get(slug="qgis")
1821
training_centers = TrainingCenter.objects.filter(
1922
certifying_organisation__project=project
20-
).order_by('certifying_organisation__name')
23+
).order_by("certifying_organisation__name")
2124
serializers = CustomSerializer()
22-
data = (
23-
serializers.serialize(
24-
training_centers,
25-
geometry_field='location',
26-
fields=('name', 'certifying_organisation__name')
27-
)
25+
data = serializers.serialize(
26+
training_centers,
27+
geometry_field="location",
28+
fields=("name", "certifying_organisation__name"),
2829
)
2930
return Response(json.loads(data))
3031
except Project.DoesNotExist:
3132
return HttpResponse(
32-
'Project does not exist.',
33-
status=status.HTTP_400_BAD_REQUEST
33+
"Project does not exist.", status=status.HTTP_400_BAD_REQUEST
3434
)
3535

3636

37-
class GetTrainingCenterOrganisationLocation(APIView):
37+
class GetTrainingCenterOrganisationLocation(
38+
ActiveCertifyingOrganisationRequiredMixin, APIView
39+
):
3840
"""API returns GeoJSON location of training center within
3941
a certifying organisation.
4042
4143
"""
4244

4345
def get(self, request, organisation_slug):
4446
try:
45-
project = Project.objects.get(slug='qgis')
47+
project = Project.objects.get(slug="qgis")
4648
organisation = CertifyingOrganisation.objects.get(
47-
slug=organisation_slug,
48-
project=project
49+
slug=organisation_slug, project=project
4950
)
5051
training_centers = TrainingCenter.objects.filter(
5152
certifying_organisation=organisation
52-
).order_by('name')
53+
).order_by("name")
5354
serializers = CustomSerializer()
54-
data = (
55-
serializers.serialize(
56-
training_centers,
57-
geometry_field='location',
58-
fields=('name', 'certifying_organisation__name')
59-
)
55+
data = serializers.serialize(
56+
training_centers,
57+
geometry_field="location",
58+
fields=("name", "certifying_organisation__name"),
6059
)
6160
return Response(json.loads(data))
6261
except Project.DoesNotExist:
6362
return HttpResponse(
64-
'Project does not exist.',
65-
status=status.HTTP_400_BAD_REQUEST
63+
"Project does not exist.", status=status.HTTP_400_BAD_REQUEST
6664
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from certification.models import CertifyingOrganisation
2+
from django.http import Http404
3+
4+
5+
class ActiveCertifyingOrganisationRequiredMixin:
6+
"""Mixin to ensure that the certifyin organisation is not archived."""
7+
8+
def dispatch(self, request, *args, **kwargs):
9+
organisation_slug = kwargs.get("organisation_slug") or kwargs.get("slug")
10+
try:
11+
organisation = CertifyingOrganisation.objects.get(slug=organisation_slug)
12+
except CertifyingOrganisation.DoesNotExist:
13+
raise Http404("Organisation does not exist.")
14+
if organisation.is_archived:
15+
raise Http404("This organisation is archived.")
16+
return super().dispatch(request, *args, **kwargs)

0 commit comments

Comments
 (0)