diff --git a/api/optimeet/groups/migrations/0003_alter_recommendations_group_id_and_more.py b/api/optimeet/groups/migrations/0003_alter_recommendations_group_id_and_more.py new file mode 100644 index 0000000..9870dc0 --- /dev/null +++ b/api/optimeet/groups/migrations/0003_alter_recommendations_group_id_and_more.py @@ -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')}, + }, + ), + ] diff --git a/api/optimeet/groups/models.py b/api/optimeet/groups/models.py index 84e2748..cce8d35 100644 --- a/api/optimeet/groups/models.py +++ b/api/optimeet/groups/models.py @@ -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) @@ -41,4 +41,12 @@ class Preferences(models.Model): time = models.JSONField() loc_lat = models.FloatField() - loc_long = models.FloatField() \ No newline at end of file + 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"]] diff --git a/api/optimeet/groups/serializers.py b/api/optimeet/groups/serializers.py index 50f453b..0943b8d 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) @@ -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) \ No newline at end of file diff --git a/api/optimeet/groups/urls.py b/api/optimeet/groups/urls.py index e608109..ce4c321 100644 --- a/api/optimeet/groups/urls.py +++ b/api/optimeet/groups/urls.py @@ -11,6 +11,6 @@ path('/users//prefs/', views.add_preference_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') ] \ No newline at end of file diff --git a/api/optimeet/groups/views.py b/api/optimeet/groups/views.py index 6a040eb..b77fe83 100644 --- a/api/optimeet/groups/views.py +++ b/api/optimeet/groups/views.py @@ -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) @@ -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):