Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api v2 feat race #319

Closed
wants to merge 14 commits into from
Closed
24 changes: 15 additions & 9 deletions api_v2/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from django.contrib import admin

from api_v2.models import Weapon
from api_v2.models import Armor
from api_v2.models import Item, ItemSet

from api_v2.models import Document
from api_v2.models import License
from api_v2.models import Publisher
from api_v2.models import Ruleset
from api_v2.models import *


# Register your models here.
Expand All @@ -17,14 +10,27 @@ class FromDocumentModelAdmin(admin.ModelAdmin):


class ItemModelAdmin(admin.ModelAdmin):
list_display = ['key','category','name']
list_display = ['key', 'category', 'name']

class TraitInline(admin.TabularInline):
model = Trait

class RaceAdmin(admin.ModelAdmin):
inlines = [
TraitInline,
]

admin.site.register(Weapon, admin_class=FromDocumentModelAdmin)
admin.site.register(Armor, admin_class=FromDocumentModelAdmin)

admin.site.register(Item, admin_class=ItemModelAdmin)
admin.site.register(ItemSet, admin_class=FromDocumentModelAdmin)

admin.site.register(Race, admin_class=RaceAdmin)

#admin.site.register(Trait)
admin.site.register(Feat, admin_class=FromDocumentModelAdmin)

admin.site.register(Document)
admin.site.register(License)
admin.site.register(Publisher)
Expand Down
5 changes: 4 additions & 1 deletion api_v2/management/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ def handle(self, *args, **options) -> None:
for model in app_models:
SKIPPED_MODEL_NAMES = ['Document']
if model._meta.app_label == 'api_v2' and model.__name__ not in SKIPPED_MODEL_NAMES:
modelq = model.objects.filter(document=doc).order_by('pk')
if model.__name__ in ['Trait']:
modelq = model.objects.filter(race__document=doc).order_by('pk')
else:
modelq = model.objects.filter(document=doc).order_by('pk')
model_path = get_filepath_by_model(
model.__name__,
model._meta.app_label,
Expand Down
27 changes: 27 additions & 0 deletions api_v2/migrations/0002_feat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.20 on 2023-08-13 01:53

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Feat',
fields=[
('name', models.CharField(help_text='Name of the item.', max_length=100)),
('desc', models.TextField(help_text='Description of the game content item. Markdown.')),
('prerequisite_desc', models.CharField(help_text='Prerequisite for the game content item.', max_length=100)),
('key', models.CharField(help_text='Unique key for the Item.', max_length=100, primary_key=True, serialize=False)),
('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api_v2.document')),
],
options={
'verbose_name_plural': 'feats',
},
),
]
39 changes: 39 additions & 0 deletions api_v2/migrations/0003_race_trait.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 3.2.20 on 2023-08-13 20:39

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0002_feat'),
]

operations = [
migrations.CreateModel(
name='Race',
fields=[
('name', models.CharField(help_text='Name of the item.', max_length=100)),
('desc', models.TextField(help_text='Description of the game content item. Markdown.')),
('key', models.CharField(help_text='Unique key for the Item.', max_length=100, primary_key=True, serialize=False)),
('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api_v2.document')),
('subrace_of', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='api_v2.race')),
],
options={
'verbose_name_plural': 'races',
},
),
migrations.CreateModel(
name='Trait',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Name of the item.', max_length=100)),
('desc', models.TextField(help_text='Description of the game content item. Markdown.')),
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api_v2.race')),
],
options={
'abstract': False,
},
),
]
18 changes: 18 additions & 0 deletions api_v2/migrations/0004_alter_feat_prerequisite_desc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2023-08-15 13:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0003_race_trait'),
]

operations = [
migrations.AlterField(
model_name='feat',
name='prerequisite_desc',
field=models.CharField(blank=True, help_text='Prerequisite for the game content item.', max_length=200),
),
]
12 changes: 9 additions & 3 deletions api_v2/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
The initialization for models for open5e's api v2.
"""

from .abstracts import HasName
from .abstracts import HasDescription
from .abstracts import Object
#from .abstracts import HasName
#from .abstracts import HasDescription
#from .abstracts import Object


from .item import Item
from .item import ItemSet
Expand All @@ -13,6 +14,11 @@

from .weapon import Weapon

from .race import Trait
from .race import Race

from .feat import Feat

from .document import Document
from .document import License
from .document import Publisher
Expand Down
10 changes: 10 additions & 0 deletions api_v2/models/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Meta:
abstract = True


class HasPrerequisite(models.Model):
prerequisite_desc = models.CharField(
max_length=200,
blank=True,
help_text='Prerequisite for the game content item.')

class Meta:
abstract = True


class Object(HasName):
"""
This is the definition of the Object abstract base class.
Expand Down
9 changes: 6 additions & 3 deletions api_v2/models/armor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@


from django.db import models
from .abstracts import HasName, HasDescription
from .abstracts import HasName
from .document import FromDocument


class Armor(HasName, FromDocument):
"""
This is the model for an armor.

This does not represent the armor set itself, because that would be an
item. Only the unique attributes of a type of armor are here. An item
that is armor would link to this model instance.
Expand Down Expand Up @@ -41,6 +41,7 @@ class Armor(HasName, FromDocument):

@property
def ac_display(self):
"""Display text for armor class."""
ac_string = str(self.ac_base)

if self.ac_add_dexmod:
Expand All @@ -52,4 +53,6 @@ def ac_display(self):
return ac_string

class Meta:
verbose_name_plural = "armor"
"""To assist with the UI layer."""

verbose_name_plural = "armor"
20 changes: 20 additions & 0 deletions api_v2/models/feat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""The model for a feat."""
from django.db import models
from .abstracts import HasName, HasDescription, HasPrerequisite
from .document import FromDocument


class Feat(HasName, HasDescription, HasPrerequisite, FromDocument):
"""
This is the model for a feat.

A feat represents a talent or an area of expertise that
gives a character special capabilities. It embodies
training, experience, and abilities beyond what a
class provides.
"""

class Meta:
"""To assist with the UI layer."""

verbose_name_plural = "feats"
45 changes: 45 additions & 0 deletions api_v2/models/race.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""The model for a race, subrace, and it's traits."""

from django.db import models
from .abstracts import HasName, HasDescription, HasPrerequisite
from .document import FromDocument


class Trait(HasName, HasDescription):
"""
This is the model for a racial trait.

Each trait ties to an individual race or subrace.
"""

race = models.ForeignKey('Race', on_delete=models.CASCADE)


class Race(HasName, HasDescription, FromDocument):
"""
This is the model for a race or subrace.

This model can be used to represent races and subraces. Subraces are
represented by using a self-relation to the parent race.
"""

subrace_of = models.ForeignKey('self',
default=None,
blank=True,
null=True,
on_delete=models.CASCADE)

@property
def is_subrace(self):
"""Returns whether the object is a subrace."""
return self.subrace_of is not None

@property
def traits(self):
"""Returns the set of traits that are related to this race."""
return self.trait_set

class Meta:
"""To assist with the UI layer."""

verbose_name_plural = "races"
27 changes: 27 additions & 0 deletions api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,30 @@ class ItemSetSerializer(GameContentSerializer):
class Meta:
model = models.ItemSet
fields = '__all__'


class FeatSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()

class Meta:
model = models.Feat
fields = '__all__'


class TraitSerializer(serializers.ModelSerializer):

class Meta:
model = models.Trait
fields = ['name', 'desc']


class RaceSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_subrace = serializers.ReadOnlyField()
traits = TraitSerializer(
many=True)


class Meta:
model = models.Race
fields = '__all__'
40 changes: 40 additions & 0 deletions api_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,43 @@ class ArmorViewSet(viewsets.ReadOnlyModelViewSet):
queryset = models.Armor.objects.all().order_by('pk')
serializer_class = serializers.ArmorSerializer
filterset_class = ArmorFilterSet


class FeatFilterSet(FilterSet):
class Meta:
model = models.Feat
fields = {
'key': ['in', 'iexact', 'exact' ],
'name': ['iexact', 'exact'],
'document__key': ['in','iexact','exact'],
}


class FeatViewSet(viewsets.ReadOnlyModelViewSet):
"""
list: API endpoint for returning a list of feats.
retrieve: API endpoint for returning a particular feat.
"""
queryset = models.Feat.objects.all().order_by('pk')
serializer_class = serializers.FeatSerializer
filterset_class = FeatFilterSet


class RaceFilterSet(FilterSet):
class Meta:
model = models.Race
fields = {
'key': ['in', 'iexact', 'exact' ],
'name': ['iexact', 'exact'],
'document__key': ['in','iexact','exact'],
}


class RaceViewSet(viewsets.ReadOnlyModelViewSet):
"""
list: API endpoint for returning a list of races.
retrieve: API endpoint for returning a particular race.
"""
queryset = models.Race.objects.all().order_by('pk')
serializer_class = serializers.RaceSerializer
filterset_class = RaceFilterSet
9 changes: 9 additions & 0 deletions data/v2/Ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@
"desc": "",
"content_prefix": ""
}
},
{
"model": "api_v2.ruleset",
"pk": "a5e",
"fields": {
"name": "Advanced 5th Edition",
"desc": "",
"content_prefix": ""
}
}
]
9 changes: 9 additions & 0 deletions data/v2/en-publishing/Publisher.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"model": "api_v2.publisher",
"pk": "en-publishing",
"fields": {
"name": "EN Publishing"
}
}
]
Loading