Skip to content

Commit

Permalink
Merge pull request #372 from open5e/v2_item_category
Browse files Browse the repository at this point in the history
V2 item category
  • Loading branch information
augustjohnson authored Nov 5, 2023
2 parents 157ad5e + 0a74d27 commit 1cee789
Showing 16 changed files with 340 additions and 31 deletions.
3 changes: 2 additions & 1 deletion api_v2/admin.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ class FromDocumentModelAdmin(admin.ModelAdmin):


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


class TraitInline(admin.TabularInline):
@@ -77,6 +77,7 @@ class LanguageAdmin(admin.ModelAdmin):
admin.site.register(Weapon, admin_class=FromDocumentModelAdmin)
admin.site.register(Armor, admin_class=FromDocumentModelAdmin)

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

18 changes: 18 additions & 0 deletions api_v2/migrations/0023_rename_category_item_category_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2023-11-04 23:57

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0022_condition'),
]

operations = [
migrations.RenameField(
model_name='item',
old_name='category',
new_name='category_text',
),
]
23 changes: 23 additions & 0 deletions api_v2/migrations/0024_itemcategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.20 on 2023-11-05 00:06

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0023_rename_category_item_category_text'),
]

operations = [
migrations.CreateModel(
name='ItemCategory',
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)),
],
options={
'abstract': False,
},
),
]
23 changes: 23 additions & 0 deletions api_v2/migrations/0025_auto_20231105_0009.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.20 on 2023-11-05 00:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0024_itemcategory'),
]

operations = [
migrations.RemoveField(
model_name='itemcategory',
name='id',
),
migrations.AddField(
model_name='itemcategory',
name='key',
field=models.CharField(default='key', help_text='Unique key for the ItemCategory.', max_length=100, primary_key=True, serialize=False),
preserve_default=False,
),
]
25 changes: 25 additions & 0 deletions api_v2/migrations/0026_auto_20231105_0015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.20 on 2023-11-05 00:15

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


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0025_auto_20231105_0009'),
]

operations = [
migrations.AddField(
model_name='itemcategory',
name='document',
field=models.ForeignKey(default='srd', on_delete=django.db.models.deletion.CASCADE, to='api_v2.document'),
preserve_default=False,
),
migrations.AlterField(
model_name='itemcategory',
name='key',
field=models.CharField(help_text='Unique key for the Item.', max_length=100, primary_key=True, serialize=False),
),
]
19 changes: 19 additions & 0 deletions api_v2/migrations/0027_item_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.20 on 2023-11-05 00:24

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


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0026_auto_20231105_0015'),
]

operations = [
migrations.AddField(
model_name='item',
name='category',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='api_v2.itemcategory'),
),
]
18 changes: 18 additions & 0 deletions api_v2/migrations/0028_alter_item_category_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2023-11-05 00:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0027_item_category'),
]

operations = [
migrations.AlterField(
model_name='item',
name='category_text',
field=models.CharField(choices=[('staff', 'Staff'), ('rod', 'Rod'), ('scroll', 'Scroll'), ('potion', 'Potion'), ('wand', 'Wand'), ('wondrous-item', 'Wondrous item'), ('ring', 'Ring'), ('ammunition', 'Ammunition'), ('weapon', 'Weapon'), ('armor', 'Armor'), ('gem', 'Gem'), ('jewelry', 'Jewelry'), ('art', 'Art'), ('trade-good', 'Trade Good'), ('shield', 'Shield'), ('poison', 'Poison'), ('adventuring-gear', 'Adventuring gear'), ('tools', 'Tools')], help_text='The category of the magic item.', max_length=100, null=True),
),
]
24 changes: 24 additions & 0 deletions api_v2/migrations/0029_auto_20231105_0034.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.2.20 on 2023-11-05 00:34

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


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0028_alter_item_category_text'),
]

operations = [
migrations.RemoveField(
model_name='item',
name='category_text',
),
migrations.AlterField(
model_name='item',
name='category',
field=models.ForeignKey(default='pooop', on_delete=django.db.models.deletion.CASCADE, to='api_v2.itemcategory'),
preserve_default=False,
),
]
1 change: 1 addition & 0 deletions api_v2/models/__init__.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

from .abilities import Abilities

from .item import ItemCategory
from .item import Item
from .item import ItemSet

36 changes: 8 additions & 28 deletions api_v2/models/item.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@
from .abstracts import Object, HasName, HasDescription
from .document import FromDocument

class ItemCategory(HasName, FromDocument):
"""A class describing categories of items."""
pass

class Item(Object, HasDescription, FromDocument):
"""
@@ -40,34 +43,11 @@ class Item(Object, HasDescription, FromDocument):
blank=True,
null=True)

CATEGORY_CHOICES = [
('staff', 'Staff'),
('rod', 'Rod'),
('scroll', 'Scroll'),
('potion', 'Potion'),
('wand', 'Wand'),
('wondrous-item', 'Wondrous item'),
('ring', 'Ring'),
('ammunition', 'Ammunition'),
('weapon', 'Weapon'),
('armor', 'Armor'),
('gem', 'Gem'),
('jewelry', 'Jewelry'),
('art', 'Art'),
('trade-good', 'Trade Good'),
('shield', 'Shield'),
('poison', 'Poison'),
('adventuring-gear', 'Adventuring gear'),
('tools', 'Tools')
]

category = models.CharField(
null=False,
choices=CATEGORY_CHOICES,
max_length=100,
help_text='The category of the magic item.')
# Magic item types that should probably be filterable:
# Staff, Rod, Scroll, Ring, Potion, Ammunition, Wand = category
category = models.ForeignKey(
ItemCategory,
on_delete=models.CASCADE,
null=False
)

requires_attunement = models.BooleanField(
null=False,
1 change: 1 addition & 0 deletions api_v2/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
from .item import WeaponSerializer
from .item import ItemSerializer
from .item import ItemSetSerializer
from .item import ItemCategorySerializer

from .background import BackgroundBenefitSerializer
from .background import BackgroundSerializer
10 changes: 9 additions & 1 deletion api_v2/serializers/item.py
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@ class ItemSerializer(GameContentSerializer):
weapon = WeaponSerializer(read_only=True, context={'request': {}})
armor = ArmorSerializer(read_only=True, context={'request': {}})


class Meta:
model = models.Item
fields = '__all__'
@@ -49,3 +48,12 @@ class ItemSetSerializer(GameContentSerializer):
class Meta:
model = models.ItemSet
fields = '__all__'


class ItemCategorySerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
item_set = ItemSerializer(many=True, read_only=True, context={'request':{}})

class Meta:
model = models.ItemCategory
fields = "__all__"
1 change: 1 addition & 0 deletions api_v2/views/__init__.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

from .item import ItemFilterSet, ItemViewSet
from .item import ItemSetFilterSet, ItemSetViewSet
from .item import ItemCategoryViewSet
from .item import ArmorFilterSet, ArmorViewSet
from .item import WeaponFilterSet, WeaponViewSet

14 changes: 13 additions & 1 deletion api_v2/views/item.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class Meta:
'weight': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],
'rarity': ['exact', 'in', ],
'requires_attunement': ['exact'],
'category': ['in', 'iexact', 'exact'],
#'category': ['in', 'iexact', 'exact'],
'document__key': ['in','iexact','exact']
}

@@ -58,6 +58,18 @@ class ItemSetViewSet(viewsets.ReadOnlyModelViewSet):
filterset_class = ItemSetFilterSet


class ItemCategoryViewSet(viewsets.ReadOnlyModelViewSet):
""""
list: API Endpoint for returning a set of item categories.
retrieve: API endpoint for return a particular item categories.
"""
queryset = models.ItemCategory.objects.all().order_by('pk')
serializer_class = serializers.ItemCategorySerializer




class WeaponFilterSet(FilterSet):

class Meta:
Loading

0 comments on commit 1cee789

Please sign in to comment.