Skip to content

Commit

Permalink
Fixed votes view issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloj2001 committed Nov 22, 2023
1 parent 7c7d35f commit 6bcf6a5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
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: 8 additions & 6 deletions api/optimeet/groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ 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)
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)

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"]]
6 changes: 5 additions & 1 deletion api/optimeet/groups/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ class Meta:

class VotesSerializer(serializers.ModelSerializer):
class Meta:
model = Votes
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)
6 changes: 3 additions & 3 deletions api/optimeet/groups/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +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>/votes", views.votes, name='votes'),
path("<int:group_id>/votes", views.create_vote, name='create_vote'),
path("<str:group_id>/recs/", views.get_recommendation),
path("<str:group_id>/votes/", views.votes, name='votes'),
path("<str:group_id>/votes/", views.create_vote, name='create_vote'),

]
16 changes: 8 additions & 8 deletions api/optimeet/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def add_users_to_group(request, group_id):
raise Http404


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


if serializer.is_valid():
Expand All @@ -73,19 +73,19 @@ def votes(request, group_id):
@csrf_exempt
@api_view(['POST'])
def create_vote(request, group_id):

try:
group = models.Group.objects.get(group_id=group_id)
group = models.Group.objects.get(pk=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})
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)
return Response(status=status.HTTP_400_BAD_REQUEST)

# #for testing
# class UserGroupListAPIView(APIView):
# def get(request, self):
Expand Down

0 comments on commit 6bcf6a5

Please sign in to comment.