-
Notifications
You must be signed in to change notification settings - Fork 0
Version 2 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Version 2 #7
Changes from all commits
6fc132d
add1208
0f682a7
6ef1fdb
bb3b2f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| import jwt | ||
| from django.conf import settings | ||
| from django.contrib.auth import user_logged_in | ||
| from django.shortcuts import render | ||
| from rest_framework.generics import ListAPIView | ||
| from rest_framework.generics import ListAPIView, RetrieveUpdateAPIView | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
| from rest_framework import status | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.permissions import IsAuthenticated, AllowAny | ||
| from .models import MacModelUser | ||
| from api.serializers import ( | ||
| API_Serializer, | ||
| VisitDataSerializer, | ||
| MACAddressSerializer | ||
| MACAddressSerializer, | ||
| ) | ||
| from visits.models import Visit, UserAccount | ||
| from django.utils import timezone | ||
| from rest_framework.pagination import LimitOffsetPagination | ||
| from django.core.exceptions import ValidationError | ||
| from django.core.exceptions import ValidationError, ObjectDoesNotExist | ||
| import datetime | ||
| from rest_framework.filters import SearchFilter | ||
| # import logging | ||
|
|
@@ -23,6 +26,60 @@ | |
| # logging.basicConfig(level=logging.DEBUG, ) | ||
| # {"mac_address": "00:26:57:00:1f:02"} | ||
|
|
||
| class AuthentificationTokenView(APIView): | ||
| permission_classes = [AllowAny, ] | ||
|
|
||
| def get(self, request): | ||
| res = {'status': 'Authentification Token View'} | ||
| return Response(res, status=status.HTTP_200_OK) | ||
|
|
||
| def post(self, request): | ||
| try: | ||
| email = request.data['email'] | ||
| password = request.data['password'] | ||
|
|
||
| user = UserAccount.objects.get(email=email) | ||
| if user: | ||
| try: | ||
| payload = { | ||
| 'email': user.email, | ||
| 'name': user.get_short_name(), | ||
| # 'photo': user.profile.photo, | ||
| } | ||
| token = jwt.encode(payload, settings.SECRET_KEY) | ||
|
|
||
| user_details = { | ||
| 'name': user.get_full_name(), | ||
| 'token': token, | ||
| } | ||
| return Response(user_details, status=status.HTTP_200_OK) | ||
| except Exception as err: | ||
| raise err | ||
| else: | ||
| res = {'error': 'can not authenticate with the given credentials or the account has been deactivated'} | ||
| return Response(res, status=status.HTTP_403_FORBIDDEN) | ||
| except ObjectDoesNotExist: | ||
| res = {'error': 'please provide a email and a password', | ||
| 'email': email, | ||
| 'password': password} | ||
| return Response(res, status=status.HTTP_204_NO_CONTENT) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь нужен другой код. 2xx - успешные ответы. В частности 204 - успешный ответ без тела (например при удалении объекта). Здесь явно невалидная ситуация - нужно возвращать 4xx код |
||
|
|
||
|
|
||
| class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView): | ||
| permission_classes = (IsAuthenticated,) | ||
| serializer_class = VisitDataSerializer | ||
|
|
||
| def get(self, request): | ||
| serializer = self.serializer_class(request.user) | ||
| return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
|
||
| def put(self, request, *args, **kwargs): | ||
| serializer_data = request.data.get('user', {}) | ||
|
|
||
| serializer = VisitDataSerializer(request.user, data=serializer_data, partial=True) | ||
| serializer.is_valid(raise_exception=True) | ||
| return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
|
||
|
|
||
| class ApiCreateView(APIView): | ||
| permission_classes = (IsAuthenticated,) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| from django.urls import re_path, include, path | ||
| from rest_framework.routers import DefaultRouter | ||
| from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token | ||
|
|
||
|
|
||
| from api.urls import visits_api_urls | ||
| from api.views import ApiCreateView | ||
| from api.views import ApiCreateView, AuthentificationTokenView, UserRetrieveUpdateAPIView | ||
| from knowledges.api.viewsets import ArticleViewSet | ||
|
|
||
|
|
||
| api_router = DefaultRouter() | ||
|
|
||
| api_router.register('posts', ArticleViewSet) | ||
|
|
@@ -14,4 +18,13 @@ | |
| re_path(r'^api-auth/', include('rest_framework.urls')), | ||
| path('visits/', include((visits_api_urls, 'visits'))), | ||
| path('mac_addr/create', ApiCreateView.as_view()), | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А зачем оба варианта авторизации сразу? |
||
| # Auth with handler generation | ||
| path(r'authorisation/', AuthentificationTokenView.as_view()), | ||
| path('update/', UserRetrieveUpdateAPIView.as_view()), | ||
|
|
||
| # Auth with rest_framework_jwt | ||
| re_path(r'api-token-auth/', obtain_jwt_token), | ||
| re_path(r'^api-token-refresh/', refresh_jwt_token), | ||
| re_path(r'^api-token-verify/', verify_jwt_token), | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А где проверка пароля то? сейчас авторизация только по username по сути