2nd [Hines] ๐Ÿ’ซ8Day

์ด์œ ์ง„ยท2021๋…„ 12์›” 20์ผ
0

2nd_project(Hines)

๋ชฉ๋ก ๋ณด๊ธฐ
5/6
post-thumbnail

1. 5์ผ์ฐจ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ ์ˆ˜์ •

  • 5์ผ์ฐจ๋•Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์—
    decorator ์ถ”๊ฐ€ + ์œ ๋‹›ํ…Œ์ŠคํŠธ ์œ„ํ•ด ์ž„์˜๋กœ ์œ ์ € ์ •๋ณด๋Š” kakao_id๋กœ ๋ฐ›์€๊ฑธ request.usr๋กœ ์ฝ”๋“œ์ˆ˜์ • + ์œ ์ € ํ•„ํ„ฐ ์กฐ๊ฑด ๋ณ€๊ฒฝ(if)

class ReviewView(View):

    # 8000/products/1/review
    @login_required
    def post(self, request):
        try:
            data       = json.loads(request.body)

            user       = request.user    # --> decorator์—์„œ ๋ฐ›์•„์˜ฌ ์œ ์ € ์ •๋ณด
            # kakao_id   = data['kakao_id']  # --> ์œ„์˜ user์— ๋Œ€์‹  unit test์šฉ ์ž„์‹œ๋กœ ๋ถˆ๋Ÿฌ์˜จ user ์ •๋ณด
            product_id = data['product_id']
            content    = data['content']
            image      = data.get('image_url', None)
            
            # if not User.objects.filter(id=kakao_id).exists():              
            if not User.objects.filter(id=user.id).exists():
                return JsonResponse({'message':'UNAUTHORIZED_USER'}, status=401)

            if not Product.objects.get(id=product_id):
                return JsonResponse({'message':'NO_PRODUCT'}, status=400)
            
            Review.objects.create(
                # user_id    = kakao_id,
                user_id    = user.id, 
                product_id = product_id,
                content    = content,
                image_url  = image
            )
            return JsonResponse({'message':'SUCCESS'}, status=200)

        except KeyError:
            return JsonResponse({'message':'KEY_ERROR'}, status=400)

        except Product.DoesNotExist:
            return JsonResponse({'message':'NO_PRODUCT'}, status=400)



2. 8์ผ์ฐจ์— ์ฝ”๋“œ ์ˆ˜์ • ์‚ฌํ•ญ

:: path parmeter๋กœ product_id๋ฅผ ๋ฐ›๊ณ 
data['product_id'] --> product_id = product_id ์ˆ˜์ • ๊ฐ€๋Šฅ

class ReviewView(View):
    # 8000/products/1/review
    @login_required
    def post(self, request, product_id):
        try:
            data       = json.loads(request.body)

            # user       = request.user    # --> decorator์—์„œ ๋ฐ›์•„์˜ฌ ์œ ์ € ์ •๋ณด
            # kakao_id   = data['kakao_id']  # --> ์œ„์˜ user์— ๋Œ€์‹  unit test์šฉ ์ž„์‹œ๋กœ ๋ถˆ๋Ÿฌ์˜จ user ์ •๋ณด
            product_id = product_id
            content    = data['content']
            image      = data.get('image_url', None)
            
            # if not User.objects.filter(id=kakao_id).exists():              
            if not User.objects.filter(id=request.user.id).exists():
                return JsonResponse({'message':'UNAUTHORIZED_USER'}, status=401)

            if not Product.objects.get(id=product_id):
                return JsonResponse({'message':'NO_PRODUCT'}, status=400)
            
            Review.objects.create(
                # user_id    = kakao_id,
                user_id    = request.user.id, 
                product_id = product_id,
                content    = content,
                image_url  = image
            )
            return JsonResponse({'message':'SUCCESS'}, status=200)

        except KeyError:
            return JsonResponse({'message':'KEY_ERROR'}, status=400)

        except Product.DoesNotExist:
            return JsonResponse({'message':'NO_PRODUCT'}, status=400)




5์ผ์ฐจ์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ๋น„๊ตํ•˜๋ฉด,
get ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ๋งŒ ํ—ค๋”์— product_id๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์„๊ฑฐ๋ผ๊ณ  ๋ฏฟ์—ˆ๋‹ค.

์˜ค๋Š˜ ์˜ค์ „ ์ˆ˜์—…์ด ๋๋‚˜๊ฐ€๋Š” ๊ณผ์ • ์ค‘์—,
ํŒ€์›๋“ค์˜ ๋กœ๊ทธ์ธ๊ณผ ๋กœ๊ทธ์ธ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ๋“ฑ์ด ๋จธ์ง€๋˜์„œ
main์„ ์ตœ์‹ ํ™”๋ฅผ ์‹œํ‚จํ›„
login decorator๋ฅผ ReviewView์˜ post ๋ฉ”์†Œ๋“œ์— ๋‹ค์‹œ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

๊ทผ๋ฐ, ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๊ฒƒ์ด๋‹ค!!
1์ฐจ ํ”„๋กœ์ ํŠธ์™€ ๋‹ค๋ฅด๊ฒŒ 2์ฐจ ํ”„๋กœ์ ํŠธ๋•Œ๋Š” ์†Œ์…œ ๋กœ๊ทธ์ธ API๋ฅผ ์ ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

์•„์ง ์†Œ์…œ๋กœ๊ทธ์ธ์˜ ํ”„๋ฐฑ ํ†ต์‹ ์ด ์™„๋ฃŒ๋˜์ง€ ์•Š์•„,
tokon์„ ๋‚ด๊ฐ€ ๋ฐ›์•„์˜ฌ์ˆ˜ ์—†๋Š” ์ƒํ™ฉ์— ์ฒ˜ํ•œ๊ฒƒ์ด๋‹ค.

token์„ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ž…๋ ฅ์„ ํ• ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€
postman์„ ์ด์šฉํ• ๋•Œ header์— ์ž„์˜๋กœ ์•„๋ฌด ํ† ํฐ์„ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ ์—ญ์‹œ๋‚˜...login decorator API์—์„œ ์„ค์ •ํ•œ invalide token message๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

๊ฒฐ๊ตญ,
์–ด๋–ป๊ฒŒ token์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋ฉ˜ํ† ๋‹˜๊ป˜ ๋„์›€์„ ์š”์ฒญํ•˜์˜€๋‹ค.

๋‚ด๊ฐ€ ์†Œ์…œ ๋กœ๊ทธ์ธ์œผ๋กœ ์ง์ ‘ token์„ ๋ฐ›์„์ˆ˜์žˆ์ง€๋งŒ
shell์—์„œ DB์— ์ž„์˜๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์œ ์ €์˜ id๊ฐ’์œผ๋กœ token์„ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

๋ฐ”๋กœ,
์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šด bcrypt๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง์ ‘ token์„ ๋งŒ๋“ค๋ฉด ๋˜๋Š”๊ฒƒ์ด๋‹ค!!

login view์—์„œ,
๋‚ด๊ฐ€ token์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ •๋ณด๋Š” ์•„๋ž˜(๐Ÿ‘‡)์™€ ๊ฐ™๋‹ค.

hines_token = jwt.encode({'id' : user.id}, SECRET_KEY, ALGORITHM)

๊ทธ๋Ÿผ,
์ง์ ‘ shell์— ์ž…๋ ฅํ•˜์—ฌ ํ† ํฐ์„ ๋ฐœํ–‰ํ•ด๋ณด์ž!

๊ทธ๋ฆฌ๊ณ ,
๋ฐœํ–‰ํ•œ token์„ postman์˜ ํ—ค๋”์— ๋‹ด์•„์„œ

ReviewView์— ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ
postman์œผ๋กœ ํ•„์š”ํ•œ ํ‚ค๊ฐ’๊ณผ ๋ฒจ๋ฅ˜๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ 
success ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ธฐ!


<์ตœ์ข… ๋ฆฌ๋ทฐ ๋ฐ›๊ธฐ ์ „ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ ๐Ÿ‘‡>


[ ์ถ”๊ฐ€ ]
์ด์ „์— ์ž‘์„ฑํ•œ

๋ฐฉ๋ฒ•1) ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋ฐฉ๋ฒ•

 #products views.py
def post (self, requset)
	try:
    	data = json.loads(requset.body)
        product_id = data['product_id']
        content = data['content']
        image = data.get('image_url', '')
 #products urls.py 
from django.urls import path, include
from .views      import ProductListView, ProductDetailView, CategoriesView, ReviewVie
urlpatterns=[
     path('/<int:product_id>', ProductDetailView.as_view()),
     ]

๋ฐฉ๋ฒ•1)์˜ veiws.py๋ฅผ ์ž‘์„ฑ ํ•  ๋‹น์‹œ์—, product_id๋ฅผ pathparameter๋กœ ๋ฐ›์•„์˜ฌ ์˜ˆ์ •์ด์˜€๊ณ , product_id๋ฅผ ๋‹ค์‹œ body์— ๋‹ด์•„์ ธ ์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑํ•˜์˜€๋‹ค.

๋ฐฉ๋ฒ•1)์ฒ˜๋Ÿผ body์— product_id๋ฅผ ๋ฐ›์•„๋„ ๋˜๊ณ ,

์•„๋ž˜์˜ ๋ฐฉ๋ฒ•2)์™€ ๊ฐ™์ด ์–ด์ฐจํ”ผ pathparmeter๋กœ ๋ฐ›์•„์˜ค๊ฒŒ ๋˜๋ฉด,
product_id๋ฅผ ๋ฐ”๋กœ Review๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค๊ณ  ํ•˜์…จ๋‹ค.

๋ฐฉ๋ฒ•2) ๋ฉ˜ํ† ๋‹˜์˜ ์•Œ๋ ค์ฃผ์‹  ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

def post (self, requset, product_id)
	try:
    	data = json.loads(requset.body)
        product_id = product_id
        content = data['content']
        image = data.get('image_url', '')

์ฆ‰, ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๋ฅผ ๋‹ค ์•Œ๋ฉด ์ข‹๊ณ ,
์ ์ ˆํ•˜๊ฒŒ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค!

profile
์ฐจ๊ทผ์ฐจ๊ทผ ๋ฐฐ์›Œ๋‚˜๊ฐ€๋Š” ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€