Skip to content

Commit

Permalink
#100 feat : return like user set & user_likes (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
0321minji authored Aug 31, 2024
1 parent c5ffe5b commit 7b02821
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
25 changes: 25 additions & 0 deletions lands/migrations/0016_land_like_cnt_land_likeuser_set.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
15 changes: 14 additions & 1 deletion lands/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
15 changes: 7 additions & 8 deletions lands/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,)
Expand Down

0 comments on commit 7b02821

Please sign in to comment.