From 7b02821ce9b86bdaf04c021f3e0b5545616ff84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=8D?= <79096808+0321minji@users.noreply.github.com> Date: Sat, 31 Aug 2024 17:42:09 +0900 Subject: [PATCH] #100 feat : return like user set & user_likes (#101) --- .../0016_land_like_cnt_land_likeuser_set.py | 25 +++++++++++++++++++ lands/models.py | 15 ++++++++++- lands/views.py | 15 ++++++----- 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 lands/migrations/0016_land_like_cnt_land_likeuser_set.py diff --git a/lands/migrations/0016_land_like_cnt_land_likeuser_set.py b/lands/migrations/0016_land_like_cnt_land_likeuser_set.py new file mode 100644 index 0000000..40bf0e2 --- /dev/null +++ b/lands/migrations/0016_land_like_cnt_land_likeuser_set.py @@ -0,0 +1,25 @@ +# Generated by Django 4.0 on 2024-08-31 08:26 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('lands', '0015_location_unique_land_item'), + ] + + operations = [ + migrations.AddField( + model_name='land', + name='like_cnt', + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name='land', + name='likeuser_set', + field=models.ManyToManyField(blank=True, related_name='liked_lands', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/lands/models.py b/lands/models.py index 470c967..bf01f1a 100644 --- a/lands/models.py +++ b/lands/models.py @@ -17,7 +17,10 @@ class Meta: models.UniqueConstraint(fields=['land', 'item'], name='unique_land_item')] def __str__(self): - return str(self.land.user)+'의 '+str(self.item) + if self.land: + return str(self.land.user)+'의 '+str(self.item) + else: + return 'null 의 '+str(self.item) #일단 lands 앱 안에 item 모델도 생성하긴 했는데 따로 앱 만드는게 좋을지 아니면 그냥 list api정도만 만들어도 괜찮을지 class ItemImage(models.Model): @@ -34,5 +37,15 @@ class Land(models.Model): #섬 타입(배경), 아이템(27가지:선택된 이미지에 대한 url과 (x,y,z)), 섬 주인(유저) background=models.CharField(max_length=10,default=1) user=models.OneToOneField('users.User',related_name='lands',on_delete=models.SET_NULL, null=True, blank=False) + #해당 섬 좋아요 누른 사람 / 해당 섬 좋아요 수 + likeuser_set = models.ManyToManyField("users.User", related_name='liked_lands', blank=True) + like_cnt = models.PositiveIntegerField(default=0) + + def like(self): + self.like_cnt += 1 + + def dislike(self): + self.like_cnt -= 1 + def __str__(self): return str(self.user) \ No newline at end of file diff --git a/lands/views.py b/lands/views.py index 0c03537..da2c4f5 100644 --- a/lands/views.py +++ b/lands/views.py @@ -67,12 +67,13 @@ class LandItemOutputSerializer(serializers.Serializer): items = serializers.SerializerMethodField() def get_land(self, obj): - print(obj) - print(f"Object type: {type(obj)}") s3_base_url = "https://s3.ap-northeast-2.amazonaws.com/cognisle.shop/media/lands/background/" land_img = f'{s3_base_url}land{obj.background}.png' bg_img = f'{s3_base_url}bg{obj.background}.png' - return {'state': int(obj.background), 'land_img': land_img, 'bg_img': bg_img} + request = self.context.get('request') + if request and hasattr(request, 'user'): + user_likes = request.user in obj.likeuser_set.all() + return {'state': int(obj.background), 'land_img': land_img, 'bg_img': bg_img, 'like_cnt':int(obj.like_cnt), 'user_likes':user_likes} def get_items(self, obj): request = self.context.get('request') @@ -195,16 +196,14 @@ def get(self, request): items=LandSelector.get_user_items(user_email=user_email) logger.info(f"Lands and items retrieved: {land}") output_serializer = self.LandItemOutputSerializer(land, context={'user_email':user_email,'request': request,'items':items}) - # if request.user.id == user_id: - # output_serializer = self.LandItemOutputSerializer(lands_items, many=True) - # else: - # output_serializer = self.PublicLandItemOutputSerializer(lands_items, many=True) + return Response( {'status':'sucess', 'data':{'owner':{'email':user.email, 'name':user.name}, 'land':output_serializer.data.get('land'), - 'items':output_serializer.data.get('items')}}, status=status.HTTP_200_OK) + 'items':output_serializer.data.get('items'), + }}, status=status.HTTP_200_OK) class ItemImageCreateApi(APIView): permission_classes=(IsAuthenticated,)