스타일쉐어-클론-프로젝트-product

정현석·2020년 11월 25일
0
import json
import decimal

from django.views    import View
from django.http     import JsonResponse
from django.db.models import Q,F

from product.models  import(
    FirstCategory,
    SecondCategory,
    ThirdCategory,
    Brand,
    Color,
    ProductColor,
    Size,
    ProductSize,
    ProductImageUrl,
    Stock,
    Product,
    ProductOotd
    )

class ProductDetail(View):
    def get(self, request, product_id): # 상세페이지
        if not Product.objects.filter(id=product_id).exists():
        		# Product.objects.filter의 id 가 product_id 같은것이 존재하지 않을 경우
            return JsonResponse({'message':'PRODUCT_NOT_FOUND'}, status=404)

        product = Product.objects.select_related('brand').prefetch_related(
        		# select_related 'barnd'만 정참조,
            'brand__product_set',
            	# prfetch_related brand__product_set 브랜드를 참조하여 product뷰를 역참조한다.
            'ootd', # manytomany관계로 profetch사용
            'ootd__like_user', #ootd를 참조하여 like의 user 값 가져오기
            'ootd__comment_set', # ootd를 참조하여 comment_set  역참조
            'ootd__user', # ootd를 참조하여 user 값 가져오기
            'ootd__ootdimageurl_set', # ootd를 참조하여 ootdimageurl 역참조
            'color', # manytomany관계
            'size',  # manytomany관계
            'stock_set', #stock 역참조
            'stock_set__color', #stock을 역참조하여 stock의 color 값 가져오기 
            'stock_set__size', #stock을 역참조하여 stock의 size 값 가져오기 
            'productimageurl_set' # productimageirl 역참조
        ).get(id=product_id) # get(id) == product_id 값이 같은 값만 출력
        			
                    

        return JsonResponse({
            'product' : {
                'headerTopTitle'  : product.title,
                'headerMiddleImg' : product.main_image_url,
                'discount'        : product.discount_rate,
                'originPrice'     : product.price,
                'discountPrice'   : format(int(round(product.price - (product.price * product.discount_rate),-2)),'.2f'),
                'likeNumber'      : product.like,
                'reviewNumber'    : product.ootd.count(),
                'mile'            : int(product.price - (product.price * product.discount_rate) * decimal.Decimal(0.05)),
                'colors'          : [color.name for color in product.color.all()],
                'sizes'           : [size.name for size in product.size.all()],
                'stocks'          : [{
                    'color' : stock.color.name,
                    'size'  : stock.size.name,
                    'stock' : stock.stock_count
                } for stock in product.stock_set.all()],
                'headerBottomBrand'   : product.brand.name,
                'brand_product_count' : product.brand.product_set.count(),
                'headerBottomImg'     : product.brand.image_url,
                'ootd'                : [{
                    'author'            : ootd.user.nickname,
                    'authorImg'         : ootd.user.profile_image_url,
                    'description'       : ootd.description,
                    'ootd_image_url'    : [image.image_url for image in ootd.ootdimageurl_set.all()],
                    'like'              : ootd.like_user.count(),
                    'comment_count'     : ootd.comment_set.count()
                } for ootd in product.ootd.all()],
                'articleProductDetailImgTitle' : product.description,
                'articleProductDetailImg'      : [image.image_url for image in product.productimageurl_set.all()]
            }
        }, status=200)

class CategoryView(View):
    def get(self, request):
        try:
            first_categories = FirstCategory.objects.prefetch_related('secondcategory_set__thirdcategory_set')

            categories = [{
                'id'   : first_category.id,
                'name' : first_category.name,
                'second_category' : [{
                    'id'   : second_category.id,
                    'name' : second_category.name,
                    'third_category': [{
                        'id'   : third_category.id,
                        'name' : third_category.name,
                    }for third_category in second_category.thirdcategory_set.all()]
                }for second_category in first_category.secondcategory_set.all()]
            }for first_category in first_categories]

            return JsonResponse({ 'categories':categories}, status=200)
        except Category.DoesNotExist :
            return JsonResponse({'message:':'INVALID_CATEGORY'}, status=400)


class ProductView(View):
    def get(self, request):
        try:
            sort               = request.GET.get('sort', '0')
            first_category_id  = request.GET.get('first')
            second_category_id = request.GET.get('second')
            third_category_id  = request.GET.get('third')
            products           = Product.objects.select_related('third_category','second_category__first_category','brand').filter(
                Q(first_category_id=first_category_id)|
                Q(second_category_id=second_category_id)|
                Q(third_category_id=third_category_id)
            )

            if sort :

                sort_type = {
                    '0' : '-created_at',
                    '1' : '-sales_product',
                    '2' : '-discount_rate',
                    '3' : 'discount_price',
                    '4' : '-discount_price'
                }
                sorting =[
                {   'id' : 0,
                  'name' : '최신순'},
                {   'id' : 1,
                  'name' : '인기순'},
                {   'id' : 2,
                  'name' : '할인율순'},
                {   'id' : 3,
                  'name' : '가격 낮은순'},
                {   'id' : 4,
                  'name' : '가격 높은순'}
                ]

                category_list =[{
                    'brand'          : product.brand.name,
                    'title'          : product.title,
                    'price'          : product.price,
                    'discount_rate'  : product.discount_rate,
                    'main_image_url' : product.main_image_url,
                    'discount_price' : product.discount_price
                    }for product in products.annotate(discount_price=F('price') - F('price')*('discount_rate')).order_by(sort_type[sort])]

                return JsonResponse({'category_list':category_list, 'sorting':sorting}, status=200)

class MdChoiceView(View):
    def get (self,request, mdchoice):
        try :
            offset = [0,8,16]
            limit  = [8,16,24]

            products = Product.objects.select_related('third_category','second_category__first_category','brand')

            mdchoice_list = [{
            'id'             : product.id,
            'brand'          : product.brand.name,
            'title'          : product.title,
            'price'          : product.price,
            'discount_rate'  : product.discount_rate,
            'main_image_url' : product.main_image_url,
            'discount_price' : format(int(round(product.price - (product.price * product.discount_rate),-2)),'.2f')
            }for product in products[offset[mdchoice]:limit[mdchoice]]]

            return JsonResponse({'message:':'SUCCESS', 'mdchoice_list':mdchoice_list}, status=200)
        except Product.DoesNotExist:
            return JsonResponse({'message:':'PRODUCT_DOES_NOT_EXIST'}, status=400)

profile
기록하는 벨로그

0개의 댓글