diff --git a/apps/MySU/serializers/association_membership.py b/apps/MySU/serializers/association_membership.py index 9fb45aaf85a639b2a71868a5f26169717007255c..0cecef47de0c10ef0c7639fe9c61d2d14e623f89 100644 --- a/apps/MySU/serializers/association_membership.py +++ b/apps/MySU/serializers/association_membership.py @@ -27,6 +27,7 @@ class AssociationMembershipSerializerList(BaseSerializer): 'association': {'lookup_field': 'slug'} } + class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers.HyperlinkedModelSerializer): specific_data = AssociationSpecificDataSerializerSimple(many=True) debt_collection_mandate_id = serializers.ReadOnlyField(source="sepa_mandate.mandate_id") @@ -36,7 +37,8 @@ class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers class Meta: model = AssociationMembership fields = ('url', 'slug', 'profile', 'association', 'date_joined', 'date_left', 'current', 'new_type', 'pay_by', - 'debt_collection_mandate_id', 'debt_collection_mandate_given', 'debt_collection_mandate_signature_date', + 'debt_collection_mandate_id', 'debt_collection_mandate_given', + 'debt_collection_mandate_signature_date', 'status', 'type', 'visible_after_date_left', 'specific_data', 'financial_obligations_satisfied', 'data_fields_last_modified', 'last_modified') extra_kwargs = { @@ -90,9 +92,11 @@ class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers errors['date_left'] = "End date must be after start date." if pay_by == AssociationMembership.PayBy.DEBT_COLLECTION.value: - if (self.instance and self.instance.pay_by != AssociationMembership.PayBy.DEBT_COLLECTION.value) or (self.instance is None): + if (self.instance and self.instance.pay_by != AssociationMembership.PayBy.DEBT_COLLECTION.value) or ( + self.instance is None): if not debt_collection_mandate_given: - errors["debt_collection_mandate_given"] = "Mandate for debt collection has to be given when paying by debt collection." + errors[ + "debt_collection_mandate_given"] = "Mandate for debt collection has to be given when paying by debt collection." if type_.association != association: errors["membertype E_1"] = "Association of membertype does not belong to this association" @@ -102,7 +106,8 @@ class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers if old_type is not None and old_type != type_: errors["membertype E_3"] = "Only the board can change current membertype" if change_type_on_period_end and new_type is None: - errors["change_type_on_period_end"] = "You cannot say you change your type, but not have a type to change to" + errors[ + "change_type_on_period_end"] = "You cannot say you change your type, but not have a type to change to" if type_.disabled: errors["membertype E_4"] = "This is not a valid type, it has been disabled." if new_type and new_type.disabled: @@ -116,9 +121,9 @@ class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers # \\\\\\\\\\\\\\\\\\\\\\\\\\ if self.instance is None: - not_supplied_all_mandatory_data_fields = AssociationSpecificDataFields.objects\ - .filter(association=association, mandatory=True)\ - .exclude(slug__in=[data['data_field'].slug for data in specific_data])\ + not_supplied_all_mandatory_data_fields = AssociationSpecificDataFields.objects \ + .filter(association=association, mandatory=True) \ + .exclude(slug__in=[data['data_field'].slug for data in specific_data]) \ .exists() if not_supplied_all_mandatory_data_fields: errors["mandatory"] = "Not all mandatory data fields have been supplied." @@ -126,7 +131,8 @@ class AssociationMembershipSerializerBase(FlexFieldsModelSerializer, serializers if specific_data: for data in specific_data: if association != data["data_field"].association: - errors[data["data_field"].name] = f"{association.slug}, doesn't match the association, {data['data_field'].association.slug}, of the data_field {data['data_field'].slug}" + errors[data[ + "data_field"].name] = f"{association.slug}, doesn't match the association, {data['data_field'].association.slug}, of the data_field {data['data_field'].slug}" if data["data_field"].type == AssociationSpecificDataFields.Type.BOOLEAN.value: if data["value"] not in ("True", "False"): @@ -173,7 +179,8 @@ class AssociationMembershipSerializerUpdate(AssociationMembershipSerializerBase) # Update sepa mandate # \\\\\\\\\\\\\\\\\\\\\\\\\\ if validated_data.get("pay_by", ""): - if instance.pay_by != AssociationMembership.PayBy.DEBT_COLLECTION and validated_data["pay_by"] == AssociationMembership.PayBy.DEBT_COLLECTION: + if instance.pay_by != AssociationMembership.PayBy.DEBT_COLLECTION and validated_data[ + "pay_by"] == AssociationMembership.PayBy.DEBT_COLLECTION: sepa_mandate = SepaMandate.objects.create( mandate_id=uuid.uuid4(), signature_date=datetime.date.today(), @@ -181,7 +188,8 @@ class AssociationMembershipSerializerUpdate(AssociationMembershipSerializerBase) profile=instance.profile, ) instance.sepa_mandate = sepa_mandate - elif instance.pay_by == AssociationMembership.PayBy.DEBT_COLLECTION and validated_data["pay_by"] != AssociationMembership.PayBy.DEBT_COLLECTION: + elif instance.pay_by == AssociationMembership.PayBy.DEBT_COLLECTION and validated_data[ + "pay_by"] != AssociationMembership.PayBy.DEBT_COLLECTION: sepa_mandate = instance.sepa_mandate sepa_mandate.withdrawal_date = datetime.date.today() sepa_mandate.save() diff --git a/apps/MySU/viewsets/user.py b/apps/MySU/viewsets/user.py index fff161f7f1478c838b5c1c8fcd3f5356950595a1..1b02277939c0759a3f368ec5bf7af311a2f99eec 100644 --- a/apps/MySU/viewsets/user.py +++ b/apps/MySU/viewsets/user.py @@ -1,7 +1,8 @@ from django.db.models import Prefetch -from rest_framework import viewsets +from rest_framework import viewsets, status from rest_framework.permissions import BasePermission from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response from url_filter import filtersets from apps.MySU.models import AssociationMembership @@ -60,6 +61,8 @@ class UserViewSet(viewsets.ModelViewSet): def get_queryset(self): queryset = User.objects.filter(username=self.request.user.username) + token = super().get_authenticators() + print(token) membership_subquery = AssociationMembership.objects.filter(user=self.request.user) membership_subquery = membership_subquery\ .select_related('association', 'new_type', 'type', 'profile', 'profile__study')\ diff --git a/apps/calendar/serializers/event.py b/apps/calendar/serializers/event.py index ee44c09516b10a9893d6701a0c89a3bb14072463..535a50d406229c36388d441415069d59bd3707b4 100644 --- a/apps/calendar/serializers/event.py +++ b/apps/calendar/serializers/event.py @@ -19,6 +19,7 @@ class EventSerializer(FlexFieldsModelSerializer, BaseSerializer): fields = ('start_date', 'end_date', 'public', 'enrollable', 'enrollable_from', 'enrollable_until', 'unenrollable', 'unenrollable_until', 'max_number_of_enrollments', 'association', 'type', 'type_name', 'hidden_for_members', 'slug', 'url', 'name', 'description', 'location', 'debt_collection') + lookup_field = 'slug' extra_kwargs = { 'url': {'lookup_field': 'slug'}, 'association': {'lookup_field': 'slug'}, diff --git a/apps/calendar/urls.py b/apps/calendar/urls.py index 82bf40cc04b6f4a26b9b869764ae617f1331530b..0924634ed043abcae93637bd1feb1dd8c4b8b671 100644 --- a/apps/calendar/urls.py +++ b/apps/calendar/urls.py @@ -1,4 +1,5 @@ from django.conf.urls import url +from django.urls import path, include from rest_framework.routers import DefaultRouter from apps.calendar.views.enrollments_emails import EmailEnrollmentsView @@ -8,7 +9,7 @@ from apps.calendar.viewsets.event import EventViewSet from apps.calendar.viewsets.eventtype import EventTypeViewSet router = DefaultRouter() -router.register(r'events', EventViewSet) +router.register(r'events', EventViewSet, basename='event') router.register(r'event_types', EventTypeViewSet) router.register(r'enrollments', EnrollmentViewSet) router.register(r'enrollment_options', EnrollmentOptionViewSet) @@ -16,3 +17,4 @@ router.register(r'enrollment_options', EnrollmentOptionViewSet) urlpatterns = [ url(r'^enrollments/email', EmailEnrollmentsView.as_view(), name='enrollements-email'), ] + diff --git a/apps/calendar/viewsets/event.py b/apps/calendar/viewsets/event.py index da73293daf860b781c1b864d3c51d92267583437..642eb51e16dfae869a15c8819a26ba85dacc1837 100644 --- a/apps/calendar/viewsets/event.py +++ b/apps/calendar/viewsets/event.py @@ -1,3 +1,5 @@ +from django.contrib.auth.models import Permission +from django.shortcuts import get_object_or_404 from django.utils import timezone from guardian.shortcuts import get_objects_for_user from guardian.shortcuts import Q @@ -34,13 +36,17 @@ class EventHttpPermissions(BasePermission): return request.user.has_perm('board', obj.association) return False - def has_permission(self, request, view): + def has_permission(self, request, view): # this is subject to change ! if request.method == "GET": # HANDLES THE LIST return True if request.method == "POST": # HANDLES THE CREATION/INSERTION return get_objects_for_user(request.user, 'MySU.board').count() > 0 + if request.method == "PUT" or request.method == "PATCH": + return True return True +# and self.user.has_perm('board') + class EventFilterSet(filtersets.ModelFilterSet): class Meta: @@ -71,6 +77,17 @@ class EventViewSet(viewsets.ModelViewSet): queryset = queryset.select_related(field) return queryset.distinct() + def patch(self, request, *args, **kwargs): + instance = self.get_object() + if not instance: + return Response(status=status.HTTP_404_NOT_FOUND) + serializer = self.get_serializer(instance, data=request.data, partial=True) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + else: + return Response(status=status.HTTP_400_BAD_REQUEST) + def destroy(self, request: 'Request', *args: 'Any', **kwargs: 'Any') -> 'Response': event = self.get_object() if event.attendants.exists():