Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
marissa-anj committed Nov 17, 2023
1 parent f480a3f commit 51cd7fc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
19 changes: 19 additions & 0 deletions api/optimeet/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

12 changes: 11 additions & 1 deletion api/optimeet/groups/serializers.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion api/optimeet/groups/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
path('<str:group_id>/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/<str:user_id>/', views.UserGroupListAPIView.as_view(), name='usergroup-byuser'),#get usergroup by user_id
path("<int:group_id>/recs", views.get_recommendation)
path("<int:group_id>/recs", views.get_recommendation),
path("<int:group_id>/votes", views.votes, name='votes'),
path("<int:group_id>/votescreate", views.create_vote, name='create_vote'),

]
28 changes: 28 additions & 0 deletions api/optimeet/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 51cd7fc

Please sign in to comment.