[๐ŸคœMiniproject]Westagram-๋กœ๊ทธ์ธ/ํšŒ์›๊ฐ€์ž…

์—ฌ์ฃผ๋งยท2020๋…„ 12์›” 17์ผ
0

Project

๋ชฉ๋ก ๋ณด๊ธฐ
2/6

์œ„์Šคํƒ€๊ทธ๋žจ์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์…‹ํŒ…์ด ๋ชจ๋‘ ๋๋‚ฌ๋‹ค! ์ด์ œ ์‹ค์ „์œผ๋กœ ๋„˜์–ด๊ฐ€ ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋ณด์ž

๋ฏธ์…˜(ํšŒ์›๊ฐ€์ž…)

  • email validation์ ์šฉ(ํšŒ์›๊ฐ€์ž…์‹œ ์ด๋ฉ”์ผ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ด๋ฉ”์ผ์—๋Š” @์™€ .์ด ํ•„์ˆ˜๋กœ ํฌํ•จ๋˜์–ด์•ผ)
  • password validation(ํšŒ์›๊ฐ€์ž…์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 8์ž๋ฆฌ ์ด์ƒ.)
  • ํšŒ์›๊ฐ€์ž…์‹œ ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ ์ž๋ฃŒ์™€ ์ค‘๋ณต๋˜์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค-> ์ ์ ˆํ•œ ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜.
  • [์ถ”๊ฐ€ ๊ตฌํ˜„ ์‚ฌํ•ญ] -> email validation ๋˜๋Š” password validation ๊ณผ์ •์—์„œ ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๋ฏธ์…˜(๋กœ๊ทธ์ธ)

  • ์ธ์Šคํƒ€๊ทธ๋žจ์— ๋กœ๊ทธ์ธ ์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ•„์ˆ˜๋กœ ํ•„์š”

1. models.py

from django.db import models

 class Account(models.Model):
     email      = models.EmailField(max_length=200, unique=True)
     password   = models.CharField(max_length=200)
     updated_at = models.DateTimeField(auto_now=True)
     created_at = models.DateTimeField(auto_now_add=True)
     class Meta:
         db_table = 'Accounts'
     def __str__(self):
         return self.name

1)์ด๋ฉ”์ผ๊ณผ ํŒจ์Šค์›Œ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ–ˆ๋‹ค.
Email์˜ ๊ฒฝ์šฐ ๊ณ ์œ ๊ฐ’์„ ์ค˜์•ผํ•˜๋ฏ€๋กœ unique=True๋ฅผ ๋„ฃ๊ณ , ์ค‘๋ณต๋˜๋Š” ๊ฐ’์ด ์˜ฌ๊ฒฝ์šฐ error๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ํ–ˆ๋‹ค.

2)์—ฌ๊ธฐ์„œ ์ถ”๊ฐ€๋กœ ๊ผญ ๋„ฃ์œผ๋ฉด ์ข‹์€๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์–ธ์ œ ์ด ๊ณ„์ •์ด ์ถ”๊ฐ€๋˜๊ณ , ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” updated_at,created_at๋ฅผ ์ ์šฉํ•˜์˜€๋‹ค.

2.views.py

๋ฉ˜๋ถ•์˜ ์‹œ์ž‘

ํšŒ์›๊ฐ€์ž… ๋ถ€๋ถ„

import json, re

from django.http import JsonResponse
from django.views import View

from user.models import Account
 class AccountView(View):
     def post(self, request):
         data = json.loads(request.body)
         REGAX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
         REGEX_PASSWORD = '^[A-Za-z0-9@#$%^&+=]{8,}$'

         try:
             if not re.match(email_validation, data['email']):
                 return JsonResponse(
                     {"message":"INVALID_MAIL"},status=401
                 )

             if not re.match(pw_validation, data['password']):
                 return JsonResponse(
                     {"message":"INVALID_PW"},status=401
                 )
             if Account.objects.filter(email=data['email']).exists():
                 return JsonResponse(
                     {"message":"USER_EXIST"},status=400
                 )

             else:
                 Account.objects.create(
                         email = data['email'],
                         password  = data['password']
                 )
                 return JsonResponse({"message":"SUCCESS"},status=200)

         except:
             return JsonResponse({"message":"KEY_ERROR"},status=401) 

์ฒ˜์Œ ์‹œ๋„๋Š” ๋ฉ˜ํ† ๋‹˜๊ณผ ์ƒ์˜ํ•˜์—ฌ ๋ฏธ์…˜์— ๋‚ด์šฉ์„ ๊ฑฐ์˜ ๋ฌด์‹œํ•˜๊ณ  ํŒจ์Šค์›Œ์Šค์™€ ์•„์ด๋””์— ๋ฌด์Šจ ๊ฐ’์„ ๋„ฃ๋˜ ํ†ต์‹ ๋งŒ ๋˜๋ฉด okํ•˜์ž!์˜€๋‹ค. ๊ทธ๊ฑธ ์™„์„ฑํ•˜๋ฉฐ ๊ธฐ๋ณธํ‹€์ด์žกํžˆ์ž ์ถ”๊ฐ€์ ์œผ๋กœ email์œ ํšจ์„ฑ๊ณผ, password์œ ํšจ์„ฑ ๊ทธ๋ฆฌ๊ณ  ์ •๊ทœํ™”์— ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ–ˆ๋Š”๋ฐ, ์ด๋ถ€๋ถ„์€ ์ฐจํ›„์— ๋‹ค์‹œ ์‹œ๊ฐ„์ด๋‚˜๋ฉด(๋‚ ์ง€๋ชจ๋ฅด๊ฒ ๋‹ค..)์ •๋ฆฌํ•˜๊ฒ ๋‹ค.

๋กœ๊ทธ์ธ ๋ถ€๋ถ„

ํšŒ์›๊ฐ€์ž… ํด๋ž˜์Šค ๋ฐ”๋กœ ๋ฐ‘์— ์ƒˆ๋กœ ํด๋ž˜์Šค๋ฅผ ๋„ฃ์—ˆ๋‹ค

class LoginView(View):
     def post(self, request):
         data = json.loads(request.body)
         
         try:
             if Account.objects.all().filter(email=data['email'],password=data['password']).exists():
             	return JsonResponse({"message":"SUCCESS", "TOKEN" : access_token},status=200)
            else:
                return JsonResponse({"message":"INVALID_PASSWORD"},status=402)
except KeyError:
        return JsonResponse({"message":"KEY_ERROR"},status=401)

except ValueError:
       return JsonResponse({"message":"INVALID_USER"},status=401)

3.urls.py

westagram(projectํŒŒ์ผ)/urls.py

 #from django.contrib import admin
 from django.urls import path,include

 urlpatterns = [
 #    path('admin/', admin.site.urls),
     path('user',include('user.urls')),
 ]

user/urls.py

from django.urls import path,include
 from .views import AccountView,LoginView


 urlpatterns = [
     path('/account', AccountView.as_view()),
     path('/login', LoginView.as_view())
 ]

urls.py๊ฐ€ ๋‘๊ฐœ์žˆ๊ณ  ์•ค๋“œํฌ์ธํŠธ์— ๊ด€ํ•ด ์ง€์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

๊ฒฐ๊ณผ๋ฌผ

์‹œ์ž‘์ „์— โญ๏ธโญ๏ธโญ๏ธโญ๏ธrunserverโญ๏ธโญ๏ธโญ๏ธโญ๏ธํ•˜์„ธ์š”~

200OK!!!!

์ง€๊ธˆ ์˜ฌ๋ฆฐ ์‚ฌ์ง„์€ ์•”ํ˜ธ,์ด๋ฉ”์ผ ์œ ํšจํ™”๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š์€ ์ •๋ง ์ฒ˜์Œ์˜ ๋‚ ๊ฒƒ(?)๊ทธ๋Œ€๋กœ ์‚ฌ์ง„์ด๋‹ค.. ๊ทผ๋ฐ ์ด๊ฑฐ ๋˜๋Š” ์ˆœ๊ฐ„ ์–ผ๋งˆ๋‚˜ ๊ธฐ๋ปค๋Š”์ง€!!!

์ด ๊ธฐ์จ์€ ์•”ํ˜ธํ™”์—์„œ ํฝใ…‹ํŒ”ใ…‹ํ•œ๋‹ค
To be continue๐ŸŒน

profile
๐ŸŒฑBackend Developer๐Ÿ‘ฉโ€๐Ÿ’ป

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