parent
5dc2d69142
commit
731eb50559
@ -1,6 +1,29 @@ |
|||||||
from django.contrib import admin |
from django.contrib import admin |
||||||
|
from .models import Club, Tournament |
||||||
|
from django.contrib.auth.admin import UserAdmin |
||||||
|
from django.contrib.auth.forms import UserCreationForm, UserChangeForm |
||||||
|
|
||||||
# Register your models here. |
from .forms import CustomUserCreationForm, CustomUserChangeForm |
||||||
from .models import Club |
from .models import CustomUser |
||||||
|
|
||||||
|
class CustomUserAdmin(UserAdmin): |
||||||
|
form = CustomUserChangeForm |
||||||
|
add_form = CustomUserCreationForm |
||||||
|
model = CustomUser |
||||||
|
list_display = ["email", "username", "umpire_code"] |
||||||
|
fieldsets = [ |
||||||
|
(None, {"fields": ["username", "email", "password", "club", "umpire_code"]}), |
||||||
|
] |
||||||
|
add_fieldsets = [ |
||||||
|
( |
||||||
|
None, |
||||||
|
{ |
||||||
|
"classes": ["wide"], |
||||||
|
"fields": ["username", "email", "password1", "password2", "club", "umpire_code"], |
||||||
|
}, |
||||||
|
), |
||||||
|
] |
||||||
|
|
||||||
|
admin.site.register(CustomUser, CustomUserAdmin) |
||||||
admin.site.register(Club) |
admin.site.register(Club) |
||||||
|
admin.site.register(Tournament) |
||||||
|
|||||||
@ -0,0 +1,14 @@ |
|||||||
|
from django.contrib.auth.forms import UserCreationForm, UserChangeForm |
||||||
|
from .models import CustomUser |
||||||
|
|
||||||
|
class CustomUserCreationForm(UserCreationForm): |
||||||
|
|
||||||
|
class Meta: |
||||||
|
model = CustomUser |
||||||
|
fields = UserCreationForm.Meta.fields + ("umpire_code", ) |
||||||
|
|
||||||
|
class CustomUserChangeForm(UserChangeForm): |
||||||
|
|
||||||
|
class Meta: |
||||||
|
model = CustomUser |
||||||
|
fields = UserCreationForm.Meta.fields + ("umpire_code", ) |
||||||
@ -1,13 +1,57 @@ |
|||||||
from django.contrib.auth.models import User |
|
||||||
from rest_framework import serializers |
from rest_framework import serializers |
||||||
from .models import Club |
from .models import Club, Tournament, CustomUser |
||||||
|
from django.contrib.auth import password_validation |
||||||
|
from django.utils.translation import gettext_lazy as _ |
||||||
|
|
||||||
class UserSerializer(serializers.HyperlinkedModelSerializer): |
class UserSerializer(serializers.HyperlinkedModelSerializer): |
||||||
|
|
||||||
|
password = serializers.CharField(write_only=True) |
||||||
|
|
||||||
|
def create(self, validated_data): |
||||||
|
user = CustomUser.objects.create_user( |
||||||
|
username=validated_data['username'], |
||||||
|
password=validated_data['password'], |
||||||
|
) |
||||||
|
return user |
||||||
|
|
||||||
class Meta: |
class Meta: |
||||||
model = User |
club_id = serializers.PrimaryKeyRelatedField(queryset=Club.objects.all()) |
||||||
fields = ['url', 'username', 'email'] |
model = CustomUser |
||||||
|
fields = ['id', 'username', 'password', 'club_id', 'umpire_code'] |
||||||
|
|
||||||
class ClubSerializer(serializers.HyperlinkedModelSerializer): |
class ClubSerializer(serializers.HyperlinkedModelSerializer): |
||||||
class Meta: |
class Meta: |
||||||
model = Club |
model = Club |
||||||
fields = ['id', 'name'] |
fields = ['id', 'name', 'address'] |
||||||
|
|
||||||
|
class TournamentSerializer(serializers.HyperlinkedModelSerializer): |
||||||
|
class Meta: |
||||||
|
club_id = serializers.PrimaryKeyRelatedField(queryset=Club.objects.all()) |
||||||
|
model = Tournament |
||||||
|
fields = ['id', 'name', 'club_id'] |
||||||
|
|
||||||
|
class ChangePasswordSerializer(serializers.Serializer): |
||||||
|
old_password = serializers.CharField(max_length=128, write_only=True, required=True) |
||||||
|
new_password1 = serializers.CharField(max_length=128, write_only=True, required=True) |
||||||
|
new_password2 = serializers.CharField(max_length=128, write_only=True, required=True) |
||||||
|
|
||||||
|
def validate_old_password(self, value): |
||||||
|
user = self.context['request'].user |
||||||
|
if not user.check_password(value): |
||||||
|
raise serializers.ValidationError( |
||||||
|
_('Your old password was entered incorrectly. Please enter it again.') |
||||||
|
) |
||||||
|
return value |
||||||
|
|
||||||
|
def validate(self, data): |
||||||
|
if data['new_password1'] != data['new_password2']: |
||||||
|
raise serializers.ValidationError({'new_password2': _("The two password fields didn't match.")}) |
||||||
|
password_validation.validate_password(data['new_password1'], self.context['request'].user) |
||||||
|
return data |
||||||
|
|
||||||
|
def save(self, **kwargs): |
||||||
|
password = self.validated_data['new_password1'] |
||||||
|
user = self.context['request'].user |
||||||
|
user.set_password(password) |
||||||
|
user.save() |
||||||
|
return user |
||||||
|
|||||||
@ -1,26 +1,48 @@ |
|||||||
from django.shortcuts import render |
from django.shortcuts import render |
||||||
from django.http import HttpResponse |
from django.http import HttpResponse |
||||||
|
from .serializers import ClubSerializer, TournamentSerializer, UserSerializer, ChangePasswordSerializer |
||||||
|
from .models import Club, Tournament, CustomUser |
||||||
from rest_framework import viewsets, permissions |
from rest_framework import viewsets, permissions |
||||||
from django.contrib.auth.models import User |
from rest_framework.authtoken.models import Token |
||||||
from .serializers import UserSerializer, ClubSerializer |
from rest_framework.response import Response |
||||||
from .models import Club |
from rest_framework.decorators import api_view |
||||||
|
from rest_framework import status |
||||||
|
from rest_framework.generics import UpdateAPIView |
||||||
|
|
||||||
def index(request): |
def index(request): |
||||||
return HttpResponse("Hello, world. You're at the tournaments index.") |
return HttpResponse("Hello, you're at the top of the world.") |
||||||
|
|
||||||
|
@api_view(['GET']) |
||||||
|
def user_by_token(request): |
||||||
|
# return Response({"message": "Hello for today! See you tomorrow!"}) |
||||||
|
# key = request.data['token'] |
||||||
|
# token = Token.objects.get(key=key) |
||||||
|
# user = CustomUser.objects.get(username=token.user) |
||||||
|
serializer = UserSerializer(request.user) |
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK) |
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet): |
class UserViewSet(viewsets.ModelViewSet): |
||||||
""" |
queryset = CustomUser.objects.all() |
||||||
API endpoint that allows users to be viewed or edited. |
|
||||||
""" |
|
||||||
queryset = User.objects.all().order_by('-date_joined') |
|
||||||
serializer_class = UserSerializer |
serializer_class = UserSerializer |
||||||
permission_classes = [permissions.IsAuthenticated] |
|
||||||
|
|
||||||
class ClubViewSet(viewsets.ModelViewSet): |
class ClubViewSet(viewsets.ModelViewSet): |
||||||
""" |
queryset = Club.objects.all() |
||||||
API endpoint that allows clubs to be viewed or edited. |
|
||||||
""" |
|
||||||
queryset = Club.objects.all().order_by('id') |
|
||||||
serializer_class = ClubSerializer |
serializer_class = ClubSerializer |
||||||
permission_classes = [permissions.IsAuthenticated] |
|
||||||
|
class TournamentViewSet(viewsets.ModelViewSet): |
||||||
|
queryset = Tournament.objects.all() |
||||||
|
serializer_class = TournamentSerializer |
||||||
|
|
||||||
|
class ChangePasswordView(UpdateAPIView): |
||||||
|
serializer_class = ChangePasswordSerializer |
||||||
|
|
||||||
|
def update(self, request, *args, **kwargs): |
||||||
|
serializer = self.get_serializer(data=request.data) |
||||||
|
serializer.is_valid(raise_exception=True) |
||||||
|
user = serializer.save() |
||||||
|
# if using drf authtoken, create a new token |
||||||
|
if hasattr(user, 'auth_token'): |
||||||
|
user.auth_token.delete() |
||||||
|
token, created = Token.objects.get_or_create(user=user) |
||||||
|
# return new token |
||||||
|
return Response({'token': token.key}, status=status.HTTP_200_OK) |
||||||
|
|||||||
Loading…
Reference in new issue