This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
from django.db import models | |
from django.contrib.auth.models import PermissionsMixin | |
from django.contrib.auth.base_user import AbstractBaseUser | |
from django.utils import timezone | |
from .managers import CustomUserManager | |
# Create your models here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
from django.db import models | |
from django.contrib.auth.models import PermissionsMixin | |
from django.contrib.auth.base_user import AbstractBaseUser | |
from django.utils import timezone | |
from .managers import CustomUserManager | |
# Create your models here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.base_user import BaseUserManager | |
from django.utils.translation import ugettext_lazy as _ | |
class CustomUserManager(BaseUserManager): | |
""" | |
Custom user model where the email address is the unique identifier | |
and has an is_admin field to allow access to the admin app | |
""" | |
def create_user(self, email, password, **extra_fields): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Other settings not shown | |
# Set your auth user to the new user you have created | |
AUTH_USER_MODEL = 'api.User' | |
# We need to add our api app and the rest framework to INSTALLED_APPS | |
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .models import User | |
class UserRegistrationSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = User | |
fields = ( | |
'email', | |
'password' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from rest_framework_simplejwt.tokens import RefreshToken | |
class UserLoginSerializer(serializers.Serializer): | |
email = serializers.EmailField() | |
password = serializers.CharField(max_length=128, write_only=True) | |
access = serializers.CharField(read_only=True) | |
refresh = serializers.CharField(read_only=True) | |
role = serializers.CharField(read_only=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserListSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = AuthUser | |
fields = ( | |
'email', | |
'role' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import status | |
from rest_framework.views import APIView | |
from rest_framework.response import Response | |
from rest_framework.permissions import AllowAny, IsAuthenticated | |
from .serializers import ( | |
UserRegistrationSerializer, | |
UserLoginSerializer, | |
UserListSerializer | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthUserLoginView(APIView): | |
serializer_class = UserLoginSerializer | |
permission_classes = (AllowAny, ) | |
def post(self, request): | |
serializer = self.serializer_class(data=request.data) | |
valid = serializer.is_valid(raise_exception=True) | |
if valid: | |
status_code = status.HTTP_200_OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserListView(APIView): | |
serializer_class = UserListSerializer | |
permission_classes = (IsAuthenticated,) | |
def get(self, request): | |
user = request.user | |
if user.role != 1: | |
response = { | |
'success': False, | |
'status_code': status.HTTP_403_FORBIDDEN, |
OlderNewer