initial commit
This commit is contained in:
commit
7409caf072
189 changed files with 38056 additions and 0 deletions
0
users/__init__.py
Normal file
0
users/__init__.py
Normal file
3
users/admin.py
Normal file
3
users/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
users/apps.py
Normal file
6
users/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UserProfilesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'users'
|
0
users/migrations/__init__.py
Normal file
0
users/migrations/__init__.py
Normal file
3
users/models.py
Normal file
3
users/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
39
users/serializers.py
Normal file
39
users/serializers.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from django.contrib.auth import authenticate
|
||||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User, Group
|
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["url", "username", "email", "password", "groups"]
|
||||
|
||||
|
||||
class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ["url", "name"]
|
||||
|
||||
|
||||
class AuthSerializer(serializers.Serializer):
|
||||
username = serializers.CharField()
|
||||
password = serializers.CharField(
|
||||
style={"input_type": "password"}, trim_whitespace=False
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
username = attrs.get("username")
|
||||
password = attrs.get("password")
|
||||
|
||||
user = authenticate(
|
||||
request=self.context.get("request"),
|
||||
username=username,
|
||||
password=password,
|
||||
)
|
||||
|
||||
if not user:
|
||||
msg = f"Invalid username or password."
|
||||
raise serializers.ValidationError(msg, code="authentication")
|
||||
|
||||
attrs["user"] = user
|
||||
return
|
3
users/tests.py
Normal file
3
users/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
5
users/urls.py
Normal file
5
users/urls.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = []
|
50
users/views.py
Normal file
50
users/views.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
from django.contrib.auth import login
|
||||
from django.contrib.auth.models import Group, User, AnonymousUser
|
||||
from rest_framework import permissions, viewsets, status
|
||||
from knox.auth import TokenAuthentication
|
||||
from knox.views import LoginView as KnoxLoginView
|
||||
from rest_framework.authentication import BasicAuthentication
|
||||
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import api_view
|
||||
|
||||
from users.serializers import GroupSerializer, UserSerializer
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
authentication_classes = [TokenAuthentication]
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
queryset = User.objects.all().order_by("-date_joined")
|
||||
serializer_class = UserSerializer
|
||||
|
||||
|
||||
class GroupViewSet(viewsets.ModelViewSet):
|
||||
authentication_classes = [TokenAuthentication]
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
queryset = Group.objects.all().order_by("name")
|
||||
serializer_class = GroupSerializer
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
def register(request):
|
||||
user_data = UserSerializer(data=request.data)
|
||||
|
||||
if user_data.is_valid():
|
||||
User.objects.create_user(**user_data.validated_data)
|
||||
return Response(request.data, status=status.HTTP_201_CREATED)
|
||||
else:
|
||||
return Response([], status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class LoginView(KnoxLoginView):
|
||||
serializer_class = UserSerializer
|
||||
permission_classes = [permissions.AllowAny]
|
||||
|
||||
def post(self, request, format=None):
|
||||
serializer = AuthTokenSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
user = serializer.validated_data["user"]
|
||||
login(request, user)
|
||||
return super(LoginView, self).post(request, format=None)
|
Loading…
Add table
Add a link
Reference in a new issue