Add API routes to pause / unpause membership

This commit is contained in:
Tanner Collin 2020-01-20 01:57:05 +00:00
parent e92182724b
commit c9faca94ad
2 changed files with 31 additions and 0 deletions

View File

@ -90,6 +90,7 @@ def tally_membership_months(member, fake_date=None):
if not start_date: return False if not start_date: return False
txs = models.Transaction.objects.filter(member_id=member.id) txs = models.Transaction.objects.filter(member_id=member.id)
txs = txs.filter(date__gte=start_date)
total_months_agg = txs.aggregate(Sum('number_of_membership_months')) total_months_agg = txs.aggregate(Sum('number_of_membership_months'))
total_months = total_months_agg['number_of_membership_months__sum'] or 0 total_months = total_months_agg['number_of_membership_months__sum'] or 0

View File

@ -1,7 +1,9 @@
import datetime
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.db.models import Max from django.db.models import Max
from rest_framework import viewsets, views, mixins, generics, exceptions from rest_framework import viewsets, views, mixins, generics, exceptions
from rest_framework.decorators import action
from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS
from rest_framework.response import Response from rest_framework.response import Response
from rest_auth.views import PasswordChangeView from rest_auth.views import PasswordChangeView
@ -133,6 +135,34 @@ class MemberViewSet(Base, Retrieve, Update):
else: else:
return serializers.MemberSerializer return serializers.MemberSerializer
def perform_create(self, serializer):
member = serializer.save()
utils.tally_membership_months(member)
def perform_update(self, serializer):
member = serializer.save()
utils.tally_membership_months(member)
@action(detail=True)
def pause(self, request, pk=None):
if not is_admin_director(self.request.user):
raise exceptions.PermissionDenied()
member = get_object_or_404(self.queryset, pk=pk)
member.paused_date = datetime.date.today()
member.save()
return Response(200)
@action(detail=True)
def unpause(self, request, pk=None):
if not is_admin_director(self.request.user):
raise exceptions.PermissionDenied()
member = get_object_or_404(self.queryset, pk=pk)
member.current_start_date = datetime.date.today()
member.paused_date = None
member.save()
utils.tally_membership_months(member)
return Response(200)
class CardViewSet(Base, Create, Retrieve, Update, Destroy): class CardViewSet(Base, Create, Retrieve, Update, Destroy):
permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin] permission_classes = [AllowMetadata | IsAuthenticated, IsObjOwnerOrAdmin]