Skip to content

Commit

Permalink
Added Likes Count for Paintings
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaBansal2310 committed Mar 16, 2024
1 parent 0a33dec commit ca3a9bd
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 21 deletions.
Binary file modified Painting/__pycache__/urls.cpython-311.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion Painting/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin
from django.urls import path , include
from PaintingApp.views import PaintingApiView
from PaintingApp.views import LikePaintingAPIView, PaintingApiView
from django.conf import settings
from django.conf.urls.static import static

Expand All @@ -10,4 +10,5 @@
path('painting/', PaintingApiView.as_view(), name='painting-list'),
path('painting/<int:painting_ID>/', PaintingApiView.as_view(), name='paining-detail'),
path('api/', include('accounts.urls')),
path('painting/<int:painting_ID>/like/', LikePaintingAPIView.as_view(), name='like-painting'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file modified PaintingApp/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified PaintingApp/__pycache__/views.cpython-311.pyc
Binary file not shown.
28 changes: 28 additions & 0 deletions PaintingApp/migrations/0005_like.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.2 on 2024-03-16 04:35

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


class Migration(migrations.Migration):

dependencies = [
('PaintingApp', '0004_alter_painting_id'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Like',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('painting', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='PaintingApp.painting')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'unique_together': {('user', 'painting')},
},
),
]
Binary file not shown.
10 changes: 10 additions & 0 deletions PaintingApp/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from django.db import models


class Like(models.Model):
user = models.ForeignKey('accounts.CustomUser', on_delete=models.CASCADE)
painting = models.ForeignKey('PaintingApp.Painting', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
unique_together = ('user', 'painting')


class Painting(models.Model):
ID = models.IntegerField(primary_key=True)
Title = models.CharField(max_length=200)
Expand Down
60 changes: 40 additions & 20 deletions PaintingApp/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Painting
from .models import Like, Painting
from .serializers import PaintingSerializer
import ipdb;

from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import DjangoModelPermissions
from rest_framework import generics, permissions

class LikePaintingAPIView(generics.CreateAPIView):
permission_classes = [permissions.IsAuthenticated]

def post(self, request, painting_ID):
try:
painting = Painting.objects.get(ID=painting_ID)
except Painting.DoesNotExist:
return Response({'error': 'Painting not found'}, status=status.HTTP_404_NOT_FOUND)

like, created = Like.objects.get_or_create(user=request.user, painting=painting)
if created:
return Response({'message': 'Painting liked successfully'}, status=status.HTTP_201_CREATED)
else:
return Response({'message': 'Painting already liked'}, status=status.HTTP_200_OK)

class PaintingApiView(APIView):
permission_classes = (IsAuthenticated,)
Expand All @@ -16,15 +28,28 @@ class PaintingApiView(APIView):

def get(self, request, painting_ID=None):
if painting_ID:

painting = Painting.objects.get(ID=painting_ID)
serializer = PaintingSerializer(painting)
return Response(serializer.data)
try:
painting = Painting.objects.get(ID=painting_ID)
serializer = PaintingSerializer(painting)
likes_count = Like.objects.filter(painting=painting).count()
user_has_liked = Like.objects.filter(user=request.user, painting=painting).exists()
painting_data = serializer.data
painting_data['likes_count'] = likes_count
painting_data['user_has_liked'] = user_has_liked
return Response(painting_data)
except Painting.DoesNotExist:
return Response({'error': 'Painting not found'}, status=status.HTTP_404_NOT_FOUND)
else:

paintings = Painting.objects.all()
serializer = PaintingSerializer(paintings, many=True)
return Response(serializer.data)
painting_data = serializer.data
for painting in painting_data:
painting_obj = Painting.objects.get(ID=painting['ID'])
likes_count = Like.objects.filter(painting=painting_obj).count()
user_has_liked = Like.objects.filter(user=request.user, painting=painting_obj).exists()
painting['likes_count'] = likes_count
painting['user_has_liked'] = user_has_liked
return Response(painting_data)

def post(self, request):
serializer = PaintingSerializer(data=request.data)
Expand All @@ -35,17 +60,13 @@ def post(self, request):

def delete(self, request, painting_ID):
try:
print("Testing")
painting = Painting.objects.get(ID=painting_ID)
print(painting_ID)
print(painting_ID)
except Painting.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

print(painting.ID)
return Response({'error': 'Painting not found'}, status=status.HTTP_404_NOT_FOUND)

painting.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

def put(self, request, painting_ID):
try:
painting = Painting.objects.get(ID=painting_ID)
Expand All @@ -57,15 +78,14 @@ def put(self, request, painting_ID):
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)



def patch(self, request, painting_ID):
try:
painting = Painting.objects.get(ID=painting_ID)
except Painting.DoesNotExist:
return Response({'error': 'Painting not found'}, status=status.HTTP_404_NOT_FOUND)

serializer = PaintingSerializer(painting, data=request.data, partial=True) # Set partial=True for partial updates
serializer = PaintingSerializer(painting, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
Expand Down
Binary file modified db.sqlite3
Binary file not shown.

0 comments on commit ca3a9bd

Please sign in to comment.