ISSUE/Django
[ISSUE]decorator๋ฅผ ์ด์ฉํ ์ค๋ณต์์ ์ ์ฒ๋ฆฌ
- -
[22.11.26]
๐ ์ค๋ณต๋๋ฉฐ ๊ฐ ํจ์์ ์ฌ์ ์ ์ฒ๋ฆฌ๋๋ ์์ decorator
๐ SiteBulkAPIView - ๋ฒํฌ ์ฆ๊ฒจ์ฐพ๊ธฐ, ์ญ์
class SiteBulkAPIView(APIView):
"""
๋ฒํฌ ํญ๋ชฉ ์ฆ๊ฒจ์ฐพ๊ธฐ, ์ญ์ api
"""
def get_list(self):
pk_ids: list = self.request.data.get('pk_ids')
return get_list_or_404(Site, id__in=pk_ids)
def validate_ids(self):
pk_ids: list = self.request.data.get('pk_ids')
for id in pk_ids:
get_object_or_404(Site,id=id)
return self.get_list()
def put(self, request):
"""
Site ๋ฒํฌ ์ฆ๊ฒจ์ฐพ๊ธฐ ์ถ๊ฐ
"""
sites = self.validate_ids()
try:
with transaction.atomic():
'''ํธ๋์ ์
์์'''
for site in sites:
site.favorite = self.request.data.get('favorite')
site.save()
except:
raise Response({'msg':'Updated failed'}, status=status.HTTP_400_BAD_REQUEST)
return Response({'msg':'Updated successfully'}, status=status.HTTP_200_OK)
def delete(self, request):
"""
Site ๋ฒํฌ ์ญ์
"""
sites = self.get_list()
for site in sites:
site.delete()
return Response({'msg': 'Deleted successfully'}, status=status.HTTP_200_OK)
๐ SiteTagsAPIView - ๋ฒํฌ ํ๊ทธ
class SiteTagsAPIView(APIView):
"""
๋ฒํฌ ํญ๋ชฉ ํ๊ทธ api
"""
def get_list(self):
pk_ids: list = self.request.data.get('pk_ids')
return get_list_or_404(Site, id__in=pk_ids)
def validate_ids(self):
pk_ids: list = self.request.data.get('pk_ids')
for id in pk_ids:
get_object_or_404(Site,id=id)
return self.get_list()
def post(self, request, **kwards):
"""
Site ํ๊ทธ ์ถ๊ฐ
"""
sites = validate_ids()
tags = self.request.data.get('tags')
with transaction.atomic():
'''ํธ๋์ ์
์์'''
created_tags = [Tag.objects.create(name=tag) for tag in tags]
[tag.site.add(site.id) for tag in created_tags for site in sites]
return Response({'msg':'Updated successfully'}, status=status.HTTP_200_OK)
๐ ์ฝ๋ ์์ฑ ์ ๋ฌธ์ ์
โ๏ธ ๋ฒํฌ ์ฆ๊ฒจ์ฐพ๊ธฐ, ์ญ์ Class๋ฅผ ์ฝ๋๋ฅผ ์์ฑํ ํ, ๋ฒํฌ ํ๊ทธ Class ์ฝ๋๋ฅผ ์์ฑํ๋๋ฐ bulk์์ ์ด๋ค ๋ณด๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ๊ฒ์ฆํ๋ ์์ ์ด ๋ Class์์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ์ด ๋๋ค.
def get_list(self):
pk_ids: list = self.request.data.get('pk_ids')
return get_list_or_404(Site, id__in=pk_ids)
def validate_ids(self):
pk_ids: list = self.request.data.get('pk_ids')
for id in pk_ids:
get_object_or_404(Site,id=id)
return self.get_list()
๐ ํด๊ฒฐ๋ฐฉ๋ฒ
โ๏ธ ๋ฐ๋ณต๋๋ ๋ฐ์ดํฐ ์กฐํ์ ๊ฒ์ฆํ๋ ์์ ์ decorator๋ก ๋ถ๋ฆฌ๋ฅผ ์ํค๊ณ ๊ฐ class์ apiํจ์์์๋ ๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ ์ค์ด๋ ์์ ์ ์งํํด๋ณด์
๐ decorator ์ฌ์ฉ
def bulk_decorator(func):
@wraps(func)
def exec_func(self, request) -> func:
pk_ids: list = self.request.data.get('pk_ids')
sites = validate_ids(pk_ids)
return func(self, request, sites=sites)
def get_list(pk_ids: list) -> Site:
return get_list_or_404(Site, id__in=pk_ids)
def validate_ids(pk_ids: list) -> Site:
for id in pk_ids:
get_object_or_404(Site,id=id)
return get_list(pk_ids)
return exec_func
โ๏ธ bulk_decoratorํจ์
- ๋ฐ์ฝ๋ ์ดํฐ ์คํ ํจ์ exec_func๋ฅผ ์คํํ ์ ์๊ฒ return์ผ๋ก exec_func๋ฅผ ๋ฐํ
- ๊ฐ Class์์ ์์ฑํ๋ ์ค๋ณต๋๋ ํจ์ get_list์ validate_idsํจ์๋ฅผ bulk_decorator๋ด๋ถ ํจ์๋ก ์์ฑ
- exec_func๋ด๋ถ์ pk_ids๋ก ์ ๋ฌ๋ฐ์ pk(id) ๋ฐฐ์ด ๊ฐ์ validate_ids()ํจ์์ ๋งค๊ฐ๋ณ์๋ก ํธ์ถํ์ฌ sites ๋ณ์์ ๋ด์ ์คํํจ์ return ๊ฐ์ผ๋ก ์ ๋ฌ๋ฐ์ ํจ์์ ํค์๋ ๊ฐ๋ณ์ธ์ ๊ฐ์ผ๋ก ์ ๋ฌํด ์ฃผ๊ฒ ๋ณ๊ฒฝ
โ๏ธ @bulk_decorator ์ฌ์ฉ
class SiteTagsAPIView(APIView):
"""
๋ฒํฌ ํญ๋ชฉ ํ๊ทธ api
"""
**@bulk_decorator**
def post(self, request, **kwards):
"""
Site ํ๊ทธ ์ถ๊ฐ
"""
sites = **kwards['sites']**
tags = self.request.data.get('tags')
with transaction.atomic():
'''ํธ๋์ ์
์์'''
created_tags = [Tag.objects.get_or_create(name=tag)[0] for tag in tags]
[tag.site.add(site.id) for tag in created_tags for site in sites]
return Response({'msg':'Add tag successfully'}, status=status.HTTP_200_OK)
class SiteTagsAPIView(APIView):
"""
๋ฒํฌ ํญ๋ชฉ ํ๊ทธ api
"""
@bulk_decorator
def post(self, request, **kwards):
"""
Site ํ๊ทธ ์ถ๊ฐ
"""
sites = kwards['sites']
tags = self.request.data.get('tags')
with transaction.atomic():
'''ํธ๋์ ์
์์'''
created_tags = [Tag.objects.get_or_create(name=tag)[0] for tag in tags]
[tag.site.add(site.id) for tag in created_tags for site in sites]
return Response({'msg':'Add tag successfully'}, status=status.HTTP_200_OK)
'ISSUE > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ISSUE] Tag ํฌํจ๋ Site ๋ชจ๋ธ์ serialize ์ฒ๋ฆฌ (0) | 2023.05.06 |
---|---|
[ISSUE] ManytoMany ๊ด๊ณ๋ฅผ ๊ฐ์ง ๋ ๋ชจ๋ธ Bulk Create ์์ (1) | 2023.05.06 |
[ISSUE] signup AbstractBaseUser class (0) | 2023.05.05 |
Contents
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค