Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 2.1.2 on 2020-03-10 15:09
# Generated by Django 2.1.2 on 2020-03-20 11:34

from django.db import migrations, models

Expand All @@ -10,11 +10,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.AddField(
model_name='profile',
name='gender',
field=models.CharField(blank=True, choices=[('m', 'Мужской'), ('w', 'Женский')], max_length=1, null=True, verbose_name='Пол'),
),
migrations.AddField(
model_name='profile',
name='photo',
Expand Down
2 changes: 1 addition & 1 deletion accounts/migrations/0013_auto_20200324_2012.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Migration(migrations.Migration):

dependencies = [
('accounts', '0012_auto_20200310_1809'),
('accounts', '0012_profile_photo'),
]

operations = [
Expand Down
65 changes: 61 additions & 4 deletions api/views.py
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
Expand All @@ -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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А где проверка пароля то? сейчас авторизация только по username по сути

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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,)
Expand Down
15 changes: 14 additions & 1 deletion api_core/urls.py
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)
Expand All @@ -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()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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),
]
21 changes: 21 additions & 0 deletions olimp_inside/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
import environ
import datetime

from django.urls.base import reverse_lazy

Expand Down Expand Up @@ -144,3 +145,23 @@

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
LOGIN_REDIRECT_URL = reverse_lazy('visits:people_inside')

# JWT AUTHENTICATION
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
}

JWT_AUTH = {
'JWT_VERIFY': True,
'JWT_VERIFY_EXPIRATION': True,
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3000),
'JWT_AUTH_HEADER_PREFIX': 'Bearer',
'JWT_ALLOW_REFRESH': True,
}
36 changes: 28 additions & 8 deletions portfolio/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,46 @@
# Register your models here.
@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
pass
list_display = ['name', 'is_visible', 'status_project', 'published']
list_display_links = ['name', 'is_visible', 'status_project', 'published']
search_fields = ['name', 'is_visible', 'status_project']
list_filter = ['is_visible', 'status_project']
list_per_page = 20


@admin.register(Assessment)
class AssessmentAdmin(admin.ModelAdmin):
list_display = ['id', 'profile_id', 'skill_id', 'rate']
list_display = ['profile_id', 'skill_id', 'rate']
search_fields = ['profile_id', 'skill_id', 'rate']
list_filter = ['skill_id']
list_per_page = 10
list_per_page = 20


@admin.register(Relationships, TypeRelationship)
@admin.register(Relationships)
class RelationshipsAdmin(admin.ModelAdmin):
pass
list_filter = ['type_relationship']
list_per_page = 20


@admin.register(Skills, TypeSkill)
@admin.register(TypeRelationship)
class RelationshipsTypeAdmin(admin.ModelAdmin):
search_fields = ['title']
list_per_page = 20


@admin.register(Skills)
class SkillsAdmin(admin.ModelAdmin):
# list_display = ['name']
list_per_page = 10
list_display = ['name', 'approved', 'type_skill']
list_display_links = ['name', 'approved', 'type_skill']
search_fields = ['name', 'type_skill']
list_filter = ['name', 'approved']
list_per_page = 20


@admin.register(TypeSkill)
class TypeSkillsAdmin(admin.ModelAdmin):
list_display = ['title']
list_per_page = 20


@admin.register(SkillProfile)
Expand Down
2 changes: 1 addition & 1 deletion portfolio/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('accounts', '0012_auto_20200310_1809'),
('accounts', '0012_profile_photo'),
]

operations = [
Expand Down
3 changes: 3 additions & 0 deletions portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class Relationships(models.Model):
on_delete=models.CASCADE,
verbose_name='Тип связи')

def __str__(self):
return self.type_relationship

class Meta:
verbose_name = 'Связь'
verbose_name_plural = 'Связи'
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ Django==2.1.2
django-environ==0.4.5
django-object-actions==1.1.0
djangorestframework==3.10.3
djangorestframework-jwt==1.11.0
Markdown==3.1.1
Pillow==7.0.0
pkg-resources==0.0.0
psycopg2==2.7.5
psycopg2-binary==2.7.5
PyJWT==1.7.1
pytz==2018.7
3 changes: 2 additions & 1 deletion visits/migrations/0004_merge_20191209_1423.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class Migration(migrations.Migration):

dependencies = [
('visits', '0003_auto_20191112_2022'),
# ('visits', '0003_auto_20191112_2022'),
# ('visits', '0003_auto_20191105_1931'),
('visits', '0003_auto_20191105_1931'),
]

Expand Down
3 changes: 2 additions & 1 deletion visits/migrations/0005_auto_20200319_2126.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
class Migration(migrations.Migration):

dependencies = [
('visits', '0003_auto_20191105_1931'),
# ('visits', '0003_auto_20191105_1931'),
('visits', '0004_merge_20191209_1423'),
]

operations = [
Expand Down