데코레이터를 정말 장식품처럼 코드 위에 걸어놓기만 했다... 같이 천천히 적용해보자.
✅ 로그인 데코레이터는 따로 utils.py라는 파일을 만들고 거기에 담았다.
access_token
으로 저장한다.access_token
을 decode 해서 payload
변수에 넣는다.payload
에는 (토큰에 담은) 우리 정보(PK, id)가 들어있다! id
값을 추출해 user
변수에 담는다. user
변수로 바꾼다. Authorization : TOKEN
으로 사용자를 로그인 상태로 유지할 수 있다!Posting은 게시물 등록, 나열 기능을 담당하고 있다.
http -v GET 127.0.0.1:8000/posting/comment "authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6M30.TFktnKt-LfDLruDr2m9Qv3vt15Fb9XjgnoJLtT4U0KQ"
HTTP/1.1 201 Created
Content-Length: 187
Content-Type: application/json
Date: Thu, 11 Feb 2021 03:08:06 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.9.1
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"data": [
{
"comment_username": "highlighter",
"created_at": "2021-02-09T23:05:37.091Z",
"posting_photo": 4,
"root": 12,
"text": "Please work I do not want to get angry in the morning"
}
]
}
http -v POST 127.0.0.1:8000/posting image_url='https://image.com/0987' description='Apple Macbook Pro 16inch' "Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6M30.TFktnKt-LfDLruDr2m9Qv3vt15Fb9XjgnoJLtT4U0KQ"
POST /posting HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6M30.TFktnKt-LfDLruDr2m9Qv3vt15Fb9XjgnoJLtT4U0KQ
Connection: keep-alive
Content-Length: 82
Content-Type: application/json
Host: 127.0.0.1:8000
User-Agent: HTTPie/2.3.0
{
"description": "Apple Macbook Pro 16inch",
"image_url": "https://image.com/0987"
}
HTTP/1.1 200 OK
Content-Length: 22
Content-Type: application/json
Date: Thu, 11 Feb 2021 03:13:24 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.9.1
Vary: Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
{
"message": "SUCCESS"
}
mysql> select * from postings;
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+--------------------------+-------------+
| id | image_url | created_at | description | username_id |
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+--------------------------+-------------+
| 2 | https://imageurlwillchange.com | 2021-02-05 00:48:07.298620 | NULL | 1 |
| 3 | https://www.google.com/url\?sa=i\&url=https%3A%2F%2Fwww.amazon.com%2FCute-Cats-Blu-ray-Nada-Bascarevic%2Fdp%2FB07QK7SRKZ\&psig=AOvVaw1dLsxJzx8lZ6_7uynP-LjJ\&ust=1612572994804000\&source=images\&cd=vfe\&ved=0CAIQjRxqFwoTCPjv0NbE0e4CFQAAAAAdAAAAABAD | 2021-02-05 01:30:19.752123 | NULL | 1 |
| 4 | https://pleasework.com | 2021-02-09 11:44:06.806212 | Oneshot Onekill... | 3 |
| 5 | https://image.com/1234 | 2021-02-11 02:50:45.207880 | Heyyyyyy | 3 |
| 6 | https://image.com/0987 | 2021-02-11 03:13:24.457341 | Apple Macbook Pro 16inch | 3 |
+----+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+--------------------------+-------------+
5 rows in set (0.00 sec)
id #6으로 잘 들어갔다 :)
사용자의 id값을 이처럼 request.user로 바꾼다. 그리고 (로그인한 상태에서..!) 해당 기능을 실행할 수 있는지 확인한다. comment, like, follow도 수정하러 가야겠다🤯