diff --git a/api/optimeet/groups/models.py b/api/optimeet/groups/models.py index c4b8b96..1567f3c 100644 --- a/api/optimeet/groups/models.py +++ b/api/optimeet/groups/models.py @@ -32,3 +32,22 @@ class Recommendations(models.Model): place_url = models.CharField(max_length=100) times = models.JSONField() +class Votes(models.Model): + + PREFERENCES_ACTIVE = "Preferences Active" + PREFERENCES_COMPLETE = "Preferences Complete" + RECOMMENDATION_ACTIVE = "Recommendation Active" + RECOMMENDATION_COMPLETE = "Recommendation Complete" + + voting_status_choices = [ + (PREFERENCES_ACTIVE, "Preferences Active"), + (PREFERENCES_COMPLETE, "Preferences Complete"), + (RECOMMENDATION_ACTIVE, "Recommendation Active"), + (RECOMMENDATION_COMPLETE, "Recommendation Complete"), + ] + + group_id = models.ForeignKey(Group, related_name='group_votes', on_delete=models.CASCADE) + max_capacity = models.IntegerField(blank=True) + voting_status = models.CharField(max_length=50, choices=voting_status_choices, default=PREFERENCES_ACTIVE) + results_of_voted = models.JSONField(blank=True) + diff --git a/api/optimeet/groups/serializers.py b/api/optimeet/groups/serializers.py index c8ca206..4b9626f 100644 --- a/api/optimeet/groups/serializers.py +++ b/api/optimeet/groups/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers from . import models -from .models import UserGroup +from .models import UserGroup, Votes from random import randint def rnd_id(): return randint(1000000000,9999999999) @@ -30,3 +30,13 @@ class Meta: model = models.Recommendations fields = '__all__' +class VotesSerializer(serializers.ModelSerializer): + class Meta: + model = Votes + fields = '__all__' + def create(self, validated_data): + group_id = self.context['group_id'] + max_capacity = self.context['max_capacity'] + validated_data['group_id'] = group_id + validated_data['max_capacity'] = max_capacity + return super().create(validated_data) \ No newline at end of file diff --git a/api/optimeet/groups/urls.py b/api/optimeet/groups/urls.py index 9fb317f..afbf18b 100644 --- a/api/optimeet/groups/urls.py +++ b/api/optimeet/groups/urls.py @@ -10,6 +10,8 @@ path('/users/', views.add_users_to_group, name='add_users_to_group'), #path('usergroups/', views.UserGroupListAPIView.as_view(), name='usergroup-list'),#get all usergroup #path('usergroups//', views.UserGroupListAPIView.as_view(), name='usergroup-byuser'),#get usergroup by user_id - path("/recs", views.get_recommendation) + path("/recs", views.get_recommendation), + path("/votes", views.votes, name='votes'), + path("/votescreate", views.create_vote, name='create_vote'), ] \ No newline at end of file diff --git a/api/optimeet/groups/views.py b/api/optimeet/groups/views.py index 1fafc32..dbc321c 100644 --- a/api/optimeet/groups/views.py +++ b/api/optimeet/groups/views.py @@ -63,6 +63,34 @@ def add_users_to_group(request, group_id): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) +@csrf_exempt +@api_view(['GET']) +def votes(request, group_id): + votes = models.Votes.objects.filter(group_id=group_id) + serializer = serializers.VotesSerializer(votes, many=True) + return Response(serializer.data) + +@csrf_exempt +@api_view(['POST']) +def create_vote(request, group_id): + + try: + group = models.Group.objects.get(group_id=group_id) + except models.Group.DoesNotExist: + raise Http404 + + max_capacity = group.max_capacity + + request.data['max_capacity'] = max_capacity + request.data['group_id'] = group_id + + serializer = serializers.VotesSerializer(data=request.data, context={"group_id":group,"max_capacity":max_capacity}) + if serializer.is_valid(): + serializer.save() + return Response(status=status.HTTP_201_CREATED) + + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + # #for testing # class UserGroupListAPIView(APIView): # def get(request, self):