-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from dnd-al-krk/feature/13-models_for_characters
13 - added PlayerCharacter and related models This Pull Request introduces changes from #13 and also from #11 and #9 **Added:** + PlayerCharacter model (and related races, classes and factions) + DMNotes model + admin interface for all models + login with email instead of username + username is nickname for the system + API endpoints (CRUD) - also for Profile and User + permissions: PlayerCharacter can be changed only by owners, but visible to any user + permissions: DMNotes can be seen by any DM, but modified only by owner + migration for profiles has been changed so dbs should be reseted
- Loading branch information
Showing
14 changed files
with
343 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.conf.urls import url, include | ||
from django.urls import path | ||
from rest_framework.routers import DefaultRouter | ||
from profiles.api import views as profiles_views | ||
|
||
# Create a router and register our viewsets with it. | ||
router = DefaultRouter() | ||
router.register(r'characters', profiles_views.PlayerCharacterViewSet) | ||
router.register(r'notes', profiles_views.DMNoteViewSet) | ||
router.register(r'profiles', profiles_views.ProfileViewSet) | ||
|
||
# The API URLs are now determined automatically by the router. | ||
urlpatterns = [ | ||
url(r'^', include(router.urls)) | ||
] |
This file contains 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
This file contains 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
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.owner == request.user.profile | ||
|
||
|
||
class IsProfileOwnerOrReadOnly(permissions.BasePermission): | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.user == request.user | ||
|
||
|
||
class IsDMOwnerOrReadOnly(permissions.BasePermission): | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.dm == request.user.profile | ||
|
||
|
||
class OnlyDMCanRead(permissions.BasePermission): | ||
|
||
def has_permission(self, request, view): | ||
return request.user.profile.is_dm() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.contrib.auth.models import User | ||
from rest_framework import serializers | ||
from rest_framework.validators import UniqueValidator | ||
|
||
from ..models import PlayerCharacter, DMNote, Profile | ||
|
||
|
||
class PlayerCharacterSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = PlayerCharacter | ||
fields = ('id', 'name', 'pc_class', 'race', 'faction', 'level', ) | ||
|
||
|
||
class DMNoteSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = DMNote | ||
fields = ('id', 'player', 'note', ) | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
nickname = serializers.CharField(source='username', | ||
validators=[UniqueValidator(queryset=User.objects.all())]) | ||
|
||
class Meta: | ||
model = User | ||
fields = ('id', 'first_name', 'last_name', 'nickname', ) | ||
|
||
|
||
class ProfileSerializer(serializers.ModelSerializer): | ||
user = UserSerializer(required=True) | ||
|
||
class Meta: | ||
model = Profile | ||
fields = ('id', 'user', 'dci') | ||
|
||
def update(self, instance, validated_data): | ||
try: | ||
user_data = validated_data.pop('user') | ||
except KeyError: | ||
pass | ||
else: | ||
instance.dci = validated_data.get('dci', instance.dci) | ||
instance.save() | ||
|
||
UserSerializer().update(instance.user, user_data) | ||
|
||
return instance |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from rest_framework import viewsets, mixins | ||
from rest_framework.permissions import IsAuthenticated | ||
|
||
from .permissions import IsOwnerOrReadOnly, IsDMOwnerOrReadOnly, OnlyDMCanRead, IsProfileOwnerOrReadOnly | ||
from .serializers import PlayerCharacterSerializer, DMNoteSerializer, ProfileSerializer | ||
from ..models import PlayerCharacter, DMNote, Profile | ||
|
||
|
||
class PlayerCharacterViewSet(viewsets.ModelViewSet): | ||
serializer_class = PlayerCharacterSerializer | ||
queryset = PlayerCharacter.objects.all() | ||
permission_classes = [IsAuthenticated, | ||
IsOwnerOrReadOnly] | ||
|
||
def get_queryset(self): | ||
return PlayerCharacter.objects.filter(owner=self.request.user.profile) | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(owner=self.request.user.profile) | ||
|
||
|
||
class DMNoteViewSet(viewsets.ModelViewSet): | ||
serializer_class = DMNoteSerializer | ||
queryset = DMNote.objects.all() | ||
permission_classes = [IsAuthenticated, | ||
IsDMOwnerOrReadOnly, | ||
OnlyDMCanRead] | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(dm=self.request.user.profile) | ||
|
||
|
||
class ProfileViewSet(mixins.UpdateModelMixin, | ||
mixins.RetrieveModelMixin, | ||
mixins.ListModelMixin, | ||
viewsets.GenericViewSet): | ||
serializer_class = ProfileSerializer | ||
queryset = Profile.objects.all() | ||
permission_classes = [IsAuthenticated, | ||
IsProfileOwnerOrReadOnly] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
|
||
class EmailBackend(ModelBackend): | ||
def authenticate(self, username=None, password=None, **kwargs): | ||
UserModel = get_user_model() | ||
try: | ||
user = UserModel.objects.get(email=username) | ||
except UserModel.DoesNotExist: | ||
return None | ||
else: | ||
if user.check_password(password): | ||
return user | ||
return None |
This file contains 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
This file contains 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
Oops, something went wrong.