Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mae/groupsvoteroute #40

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/optimeet/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ class Recommendations(models.Model):
place_url = models.CharField(max_length=100)
times = models.JSONField()

class Votes(models.Model):
group_id = models.ForeignKey(Group, related_name='group_votes', on_delete=models.CASCADE)
user_id = models.CharField(max_length=64, blank=True)
results_of_voted = models.CharField(max_length=64, blank=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would results_of_voted be used? Since this is a single vote that a particular user is making in a group we wouldn't keep track of multiple votes. Also remember what we're voting for: recommendations, so we should add the recommendation id this vote is for as a foreign key.


6 changes: 5 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,7 @@ class Meta:
model = models.Recommendations
fields = '__all__'

class VotesSerializer(serializers.ModelSerializer):
class Meta:
model = Votes
fields = '__all__'
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>/votes", views.create_vote, name='create_vote'),
marissa-anj marked this conversation as resolved.
Show resolved Hide resolved

]
23 changes: 23 additions & 0 deletions api/optimeet/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ 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

request.data['group_id'] = group_id
serializer = serializers.VotesSerializer(data=request.data, context={'group_id': group_id})
marissa-anj marked this conversation as resolved.
Show resolved Hide resolved
if serializer.is_valid():
serializer.save()
pabloj2001 marked this conversation as resolved.
Show resolved Hide resolved
return Response(status=status.HTTP_201_CREATED)

return Response(status=status.HTTP_400_BAD_REQUEST)
# #for testing
# class UserGroupListAPIView(APIView):
# def get(request, self):
Expand Down