-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added for contents: serializers/deserializers for create/update, factories, api rest and tests * Improved game views and test for added contents to Game
- Loading branch information
Showing
26 changed files
with
648 additions
and
185 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
from factory.django import DjangoModelFactory | ||
from factory import LazyAttribute, SelfAttribute, SubFactory | ||
|
||
from .models import Content | ||
from character.factories import NPCFactory, PlayerFactory | ||
from things.factories import ItemFactory, KnowledgeFactory, RolFactory | ||
|
||
|
||
class ContentFactory(DjangoModelFactory): | ||
content_id = SelfAttribute('content.pk') | ||
content_type = LazyAttribute( | ||
lambda o: ContentType.objects.get_for_model(o.content)) | ||
|
||
class Meta: | ||
exclude = ['content'] | ||
abstract = True | ||
|
||
|
||
class ContentPlayerFactory(ContentFactory): | ||
content = SubFactory(PlayerFactory) | ||
|
||
class Meta: | ||
model = Content | ||
|
||
|
||
class ContentNPCFactory(ContentFactory): | ||
content = SubFactory(NPCFactory) | ||
|
||
class Meta: | ||
model = Content | ||
|
||
|
||
class ContentItemFactory(ContentFactory): | ||
content = SubFactory(ItemFactory) | ||
|
||
class Meta: | ||
model = Content | ||
|
||
|
||
class ContentKnowledgeFactory(ContentFactory): | ||
content = SubFactory(KnowledgeFactory) | ||
|
||
class Meta: | ||
model = Content | ||
|
||
|
||
class ContentRolFactory(ContentFactory): | ||
content = SubFactory(RolFactory) | ||
|
||
class Meta: | ||
model = Content |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from drf_extra_fields.geo_fields import PointField | ||
from rest_framework import serializers | ||
|
||
from .models import Content | ||
from character.models import NPC, Player | ||
from character.serializers import NPCSerializer, PlayerSerializer | ||
from things.models import Item, Knowledge, Rol | ||
from things.serializers import ItemSerializer, KnowledgeSerializer, RolSerializer | ||
|
||
|
||
SERIALIZERS = [NPCSerializer, PlayerSerializer, ItemSerializer, | ||
KnowledgeSerializer, RolSerializer] | ||
|
||
def get_serializer(model): | ||
for ser in SERIALIZERS: | ||
if ser.Meta.model == model: | ||
return ser | ||
return None | ||
|
||
|
||
class ContentRelatedField(serializers.RelatedField): | ||
queryset = Content.objects.all() | ||
|
||
def to_representation(self, value): | ||
if isinstance(value, Player): | ||
return PlayerSerializer(value).data | ||
elif isinstance(value, NPC): | ||
return NPCSerializer(value).data | ||
elif isinstance(value, Item): | ||
return ItemSerializer(value).data | ||
elif isinstance(value, Knowledge): | ||
return KnowledgeSerializer(value).data | ||
elif isinstance(value, Rol): | ||
return RolSerializer(value).data | ||
else: | ||
return None | ||
|
||
def to_internal_value(self, value): | ||
if isinstance(value, dict): | ||
return value | ||
else: | ||
return None | ||
|
||
|
||
class ContentSerializer(serializers.ModelSerializer): | ||
content = ContentRelatedField(required=False) | ||
position = PointField(required=False) | ||
|
||
def create(self, data, *args, **kwargs): | ||
content_type = data.get('content_type') | ||
model = content_type.model_class() | ||
content = data.get('content') | ||
if content and data.get('content_id') <= 0: | ||
ser = get_serializer(model)(data=content) | ||
ser.is_valid(raise_exception=True) | ||
content_obj = ser.save() | ||
data['content'] = content_obj | ||
data['content_id'] = content_obj.pk | ||
else: | ||
content_id = data.get('content_id') | ||
content_obj = model.objects.get(pk=content_id) | ||
data['content'] = content_obj | ||
return super().create(data, *args, **kwargs) | ||
|
||
def update(self, instance, data, *args, **kwargs): | ||
content_type = data.get('content_type') | ||
model = content_type.model_class() | ||
content = data.get('content') | ||
if content and data.get('content_id') <= 0: | ||
ser = get_serializer(model)(instance.content, data=content) | ||
ser.is_valid(raise_exception=True) | ||
content_obj = ser.save() | ||
data['content'] = content_obj | ||
data['content_id'] = content_obj.pk | ||
else: | ||
content_id = data.get('content_id') | ||
content_obj = model.objects.get(pk=content_id) | ||
data['content'] = content_obj | ||
return super().create(data, *args, **kwargs) | ||
|
||
class Meta: | ||
model = Content | ||
fields = '__all__' |
Oops, something went wrong.