오늘은 사이트 이용자가 사이트에 로그인 한 후, 출석체크 버튼을 누르면 1포인트를 지급하는 시스템을 만들어 보았다.
#views.py
class GetPointView(APIView): # 출석 포인트 View (하루에 한번 가능)
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [JWTAuthentication]
def post(self, request, user_id):
now = datetime.today().strftime("%Y-%m-%d")
user= get_object_or_404(User, id=user_id)
if user == request.user:
if user.click_time == now:
return Response({"message":"이미 출석을 하셨습니다."}, status=status.HTTP_400_BAD_REQUEST)
else:
user.click_time = now
user.point += 1
user.save()
return Response({"message":"출석점수 1점을 획득하셨습니다."}, status=status.HTTP_200_OK)
return Response({"message":"권한이 없습니다."}, status=status.HTTP_400_BAD_REQUEST)
click_time은 하루에 한번만 클릭할 수 있게 체크하도록 user 모델에 만들어 둔 필드이다.
point는 IntegerField를 사용하여 만들 필드이다.
'TTL > 13주차 ~ 16주차 TIL (11.21 ~ 12.16)' 카테고리의 다른 글
| 2022-12-07 TIL (django 가상환경 관리하기) (0) | 2022.12.08 |
|---|---|
| 2022-12-06 TIL (테스트코드) (0) | 2022.12.06 |
| 2022-12-02 TIL (drf serializer extra_kwargs) (0) | 2022.12.04 |
| 2022-12-01 TIL (일정기간 지나면 DB 삭제) (0) | 2022.12.04 |
| 2022-11-30 TIL (웹소켓이란?) (0) | 2022.11.30 |