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 all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.6 on 2023-11-22 02:07

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('groups', '0002_rename_groups_usergroup_group_id_and_more'),
]

operations = [
migrations.AlterField(
model_name='recommendations',
name='group_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='groups.group'),
),
migrations.AlterField(
model_name='usergroup',
name='group_id',
field=models.ForeignKey(default='', on_delete=django.db.models.deletion.DO_NOTHING, to='groups.group'),
),
migrations.CreateModel(
name='Votes',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user_id', models.CharField(max_length=64)),
('group_id', models.ForeignKey(default='', on_delete=django.db.models.deletion.DO_NOTHING, to='groups.group')),
('rec_id', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='groups.recommendations')),
],
options={
'unique_together': {('rec_id', 'user_id')},
},
),
]
14 changes: 11 additions & 3 deletions api/optimeet/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Group(models.Model):

class UserGroup(models.Model):
user_id = models.CharField(max_length=64)
group_id = models.ForeignKey(Group, on_delete=models.CASCADE, default="")
group_id = models.ForeignKey(Group, on_delete=models.DO_NOTHING, default="")
class Meta:
unique_together = [["group_id","user_id"]]

class Recommendations(models.Model):
group_id = models.ForeignKey(Group, models.CASCADE)
group_id = models.ForeignKey(Group, models.DO_NOTHING)
activity_id = models.CharField(max_length=50)
place_name = models.CharField(max_length=50)
place_url = models.CharField(max_length=100)
Expand All @@ -41,4 +41,12 @@ class Preferences(models.Model):
time = models.JSONField()

loc_lat = models.FloatField()
loc_long = models.FloatField()
loc_long = models.FloatField()

class Votes(models.Model):
rec_id = models.ForeignKey(Recommendations, on_delete=models.DO_NOTHING)
group_id = models.ForeignKey(Group, on_delete=models.DO_NOTHING, default="")
user_id = models.CharField(max_length=64)

class Meta:
unique_together = [["rec_id", "user_id"]]
10 changes: 9 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 @@ -41,3 +41,11 @@ def create(self, validated_data):

return super(PreferencesSerializer, self).create(validated_data)

class VotesSerializer(serializers.ModelSerializer):
class Meta:
model = models.Votes
fields = '__all__'

def create(self, validated_data):
validated_data['group_id'] = self.context['group_id']
return super(VotesSerializer, self).create(validated_data)
4 changes: 2 additions & 2 deletions api/optimeet/groups/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
path('<str:group_id>/users/<str:user_id>/prefs/', views.add_preference_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("<str:group_id>/recs", views.get_recommendation)

path("<str:group_id>/recs/", views.get_recommendation),
path("<str:group_id>/votes/", views.votes, name='votes')
]
23 changes: 22 additions & 1 deletion api/optimeet/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def add_users_to_group(request, group_id):
@csrf_exempt
@api_view(['POST'])
def add_preference_to_group(request, group_id, user_id, format=None):
# Check if the user group exists
# Check if the user group exists
try:
group = models.Group.objects.get(group_id=group_id)

Expand All @@ -88,6 +88,27 @@ def add_preference_to_group(request, group_id, user_id, format=None):

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@csrf_exempt
@api_view(['GET', 'POST'])
def votes(request, group_id):
if request.method == 'GET':
votes = models.Votes.objects.filter(group_id=group_id)
serializer = serializers.VotesSerializer(votes, many=True)
return Response(serializer.data)
elif request.method == 'POST':
try:
group = models.Group.objects.get(pk=group_id)
except models.Group.DoesNotExist:
raise Http404

serializer = serializers.VotesSerializer(data = request.data, context={'group_id': group})

if serializer.is_valid():
serializer.save()
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