Skip to content

Commit

Permalink
Merge branch '419-v2-casting-options-field-support' of github.com:ope…
Browse files Browse the repository at this point in the history
…n5e/open5e-api into 419-v2-casting-options-field-support
  • Loading branch information
augustjohnson committed Oct 22, 2024
2 parents 6d0f135 + dcb46da commit 005de61
Show file tree
Hide file tree
Showing 26 changed files with 44,306 additions and 5,801 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rdme-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Check out repo 📚
uses: actions/checkout@v3

- name: Run `openapi` command 🚀
- name: Run `openapi` command for v1🚀
uses: readmeio/rdme@v8
with:
rdme: openapi openapi-schema.yml --key=${{ secrets.README_API_KEY }} --id=641f6d9e0ffbcd06c0e7343c
rdme: openapi openapi-schema.yml --key=${{ secrets.README_API_KEY }} --id=641f6d9e0ffbcd06c0e7343c
4 changes: 3 additions & 1 deletion api/schema_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rest_framework.schemas.openapi import SchemaGenerator
'''from rest_framework.schemas.openapi import SchemaGenerator
from rest_framework.schemas.openapi import AutoSchema
# Adds additional metadata onto an OAS operation. Attach this to your view and provide the necessary constructor args.
Expand All @@ -21,6 +21,7 @@ def get_operation(self, path, method):
oldOperation = super().get_operation(path, method)
# I can't find a version of DRF that support summaries
if path.startswith('/v1/'): return oldOperation
oldOperation['summary'] = self.extra_info['summary'][path]
# Future versions of DRF support tags
Expand Down Expand Up @@ -61,3 +62,4 @@ def get_schema(self, *args, **kwargs):
# This isn't a real endpoint, so we remove it from the schema
schema['paths'].pop('/search/{id}/')
return schema
'''
17 changes: 9 additions & 8 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ def __init__(self, *args, **kwargs):

# The request doesn't exist when generating an OAS file, so we have to check that first
if 'request' in self.context:
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
if self.context['request'] is not None:
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)

class DynamicFieldsHyperlinkedModelSerializer(
DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer
Expand Down
28 changes: 28 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from rest_framework import routers
from django.conf.urls import include
from django.urls import path

from api import views

router = routers.DefaultRouter()
router.register(r'manifest', views.ManifestViewSet)
router.register(r'spells', views.SpellViewSet)
router.register(r'spelllist',views.SpellListViewSet)
router.register(r'monsters', views.MonsterViewSet)
router.register(r'documents', views.DocumentViewSet)
router.register(r'backgrounds', views.BackgroundViewSet)
router.register(r'planes', views.PlaneViewSet)
router.register(r'sections', views.SectionViewSet)
router.register(r'feats', views.FeatViewSet)
router.register(r'conditions', views.ConditionViewSet)
router.register(r'races',views.RaceViewSet)
router.register(r'classes',views.CharClassViewSet)
router.register(r'magicitems',views.MagicItemViewSet)
router.register(r'weapons',views.WeaponViewSet)
router.register(r'armor',views.ArmorViewSet)

urlpatterns = [
path('', include(router.urls)), #Consider removing this after a while.
path('version/', views.get_version, name="version"),
path('v1/', include(router.urls))
]
143 changes: 18 additions & 125 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.schemas.openapi import AutoSchema

from api import models
from api import serializers
from api import filters
from api.schema_generator import CustomSchema



class ManifestViewSet(viewsets.ReadOnlyModelViewSet):
Expand All @@ -28,13 +29,6 @@ class ManifestViewSet(viewsets.ReadOnlyModelViewSet):
automatically downloads data from Open5e, you can periodically check
the manifests to determine whether your data is out of date.
"""
schema = CustomSchema(
summary={
'/manifest/': 'List Manifests',
'/manifest/{id}/': 'Retrieve Manifest',
},
tags=['Manifests'],
)
queryset = models.Manifest.objects.all().order_by("pk")
serializer_class = serializers.ManifestSerializer

Expand Down Expand Up @@ -102,18 +96,7 @@ class DocumentViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of documents.
retrieve: API endpoint for returning a particular document.
"""
schema = CustomSchema(
summary={
'/documents/': 'List Documents',
'/documents/{id}/': 'Retrieve Document',
},
tags=['Documents'],
query={
'slug': 'A short, human readable string uniquely identifying this document',
'title': 'A short descriptive title of this document',
'organization': 'The organization that published the document',
'license': 'The license under which the document is published',
})
schema = AutoSchema(operation_id_base='V1Document')
queryset = models.Document.objects.all().order_by("pk")
serializer_class = serializers.DocumentSerializer
search_fields = ['title', 'desc']
Expand All @@ -130,13 +113,7 @@ class SpellViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of spells.
retrieve: API endpoint for returning a particular spell.
"""
schema = CustomSchema(
summary={
'/spells/': 'List Spells',
'/spells/{slug}/': 'Retrieve Spell',
},
tags=['Spells']
)
schema = AutoSchema(operation_id_base='V1Spell')
queryset = models.Spell.objects.all().order_by("pk")
filterset_class=filters.SpellFilter
serializer_class = serializers.SpellSerializer
Expand All @@ -163,13 +140,7 @@ class SpellListViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of spell lists.
retrieve: API endpoint for returning a particular spell list.
"""
schema = CustomSchema(
summary={
'/spelllist/': 'List Spell Lists',
'/spelllist/{slug}/': 'Retrieve Spell List',
},
tags=['SpellList']
)
schema = AutoSchema(operation_id_base='V1SpellList')
queryset = models.SpellList.objects.all().order_by("pk")
serializer_class = serializers.SpellListSerializer
filterset_class = filters.SpellListFilter
Expand All @@ -181,13 +152,7 @@ class MonsterViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of monsters.
retrieve: API endpoint for returning a particular monster.
"""
schema = CustomSchema(
summary={
'/monsters/': 'List Monsters',
'/monsters/{slug}/': 'Retrieve Monster',
},
tags=['Monsters']
)
schema = AutoSchema(operation_id_base='V1Monster')
queryset = models.Monster.objects.all().order_by("pk")
filterset_class = filters.MonsterFilter

Expand All @@ -199,13 +164,7 @@ class BackgroundViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of backgrounds.
retrieve: API endpoint for returning a particular background.
"""
schema = CustomSchema(
summary={
'/backgrounds/': 'List Backgrounds',
'/backgrounds/{slug}/': 'Retrieve Background',
},
tags=['Backgrounds']
)
schema = AutoSchema(operation_id_base='V1Background')
queryset = models.Background.objects.all().order_by("pk")
serializer_class = serializers.BackgroundSerializer
ordering_fields = '__all__'
Expand All @@ -219,13 +178,7 @@ class PlaneViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of planes.
retrieve: API endpoint for returning a particular plane.
"""
schema = CustomSchema(
summary={
'/planes/': 'List Planes',
'/planes/{slug}/': 'Retrieve Plane',
},
tags=['Planes']
)
schema = AutoSchema(operation_id_base='V1Plane')
queryset = models.Plane.objects.all().order_by("pk")
serializer_class = serializers.PlaneSerializer
filterset_class = filters.PlaneFilter
Expand All @@ -237,13 +190,7 @@ class SectionViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of sections.
retrieve: API endpoint for returning a particular section.
"""
schema = CustomSchema(
summary={
'/sections/': 'List Sections',
'/sections/{slug}/': 'Retrieve Section',
},
tags=['Sections']
)
schema = AutoSchema(operation_id_base='V1Section')
queryset = models.Section.objects.all().order_by("pk")
serializer_class = serializers.SectionSerializer
ordering_fields = '__all__'
Expand All @@ -257,13 +204,7 @@ class FeatViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of feats.
retrieve: API endpoint for returning a particular feat.
"""
schema = CustomSchema(
summary={
'/feats/': 'List Feats',
'/feats/{slug}/': 'Retrieve Feat',
},
tags=['Feats']
)
schema = AutoSchema(operation_id_base='V1Feat')
queryset = models.Feat.objects.all().order_by("pk")
serializer_class = serializers.FeatSerializer
filterset_class = filters.FeatFilter
Expand All @@ -275,13 +216,7 @@ class ConditionViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of conditions.
retrieve: API endpoint for returning a particular condition.
"""
schema = CustomSchema(
summary={
'/conditions/': 'List Conditions',
'/conditions/{slug}/': 'Retrieve Condition',
},
tags=['Conditions']
)
schema = AutoSchema(operation_id_base='V1Condition')
queryset = models.Condition.objects.all().order_by("pk")
serializer_class = serializers.ConditionSerializer
search_fields = ['name', 'desc']
Expand All @@ -293,13 +228,7 @@ class RaceViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of races.
retrieve: API endpoint for returning a particular race.
"""
schema = CustomSchema(
summary={
'/races/': 'List Races',
'/races/{slug}/': 'Retrieve Race',
},
tags=['Races']
)
schema = AutoSchema(operation_id_base='V1Race')
queryset = models.Race.objects.all().order_by("pk")
serializer_class = serializers.RaceSerializer
filterset_class = filters.RaceFilter
Expand All @@ -312,13 +241,7 @@ class SubraceViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint that allows viewing of Subraces.
retrieve: API endpoint for returning a particular subrace.
"""
schema = CustomSchema(
summary={
'/subraces/': 'List Subraces',
'/subraces/{slug}/': 'Retrieve Subrace',
},
tags=['Subraces']
)
schema = AutoSchema(operation_id_base='V1Subrace')
queryset = models.Subrace.objects.all().order_by("pk")
serializer_class = serializers.SubraceSerializer
search_fields = ['name', 'desc']
Expand All @@ -333,13 +256,7 @@ class CharClassViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of classes and archetypes.
retrieve: API endpoint for returning a particular class or archetype.
"""
schema = CustomSchema(
summary={
'/classes/': 'List Classes',
'/classes/{slug}/': 'Retrieve Class',
},
tags=['Classes']
)
schema = AutoSchema(operation_id_base='V1Class')
queryset = models.CharClass.objects.all().order_by("pk")
serializer_class = serializers.CharClassSerializer
filterset_class = filters.CharClassFilter
Expand All @@ -352,13 +269,7 @@ class ArchetypeViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint that allows viewing of Archetypes.
retrieve: API endpoint for returning a particular archetype.
"""
schema = CustomSchema(
summary={
'/archetypes/': 'List Archetypes',
'/archetypes/{slug}/': 'Retrieve Archetype',
},
tags=['Archetypes']
)
schema = AutoSchema(operation_id_base='V1Archetype')
queryset = models.Archetype.objects.all().order_by("pk")
serializer_class = serializers.ArchetypeSerializer
search_fields = ['name', 'desc']
Expand All @@ -373,13 +284,7 @@ class MagicItemViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of magic items.
retrieve: API endpoint for returning a particular magic item.
"""
schema = CustomSchema(
summary={
'/magicitems/': 'List Magic Items',
'/magicitems/{slug}/': 'Retrieve Magic Item',
},
tags=['Magic Items']
)
schema = AutoSchema(operation_id_base='V1MagicItem')
queryset = models.MagicItem.objects.all().order_by("pk")
serializer_class = serializers.MagicItemSerializer
filterset_class = filters.MagicItemFilter
Expand All @@ -391,13 +296,7 @@ class WeaponViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of weapons.
retrieve: API endpoint for returning a particular weapon.
"""
schema = CustomSchema(
summary={
'/weapons/': 'List Weapons',
'/weapons/{slug}/': 'Retrieve Weapon',
},
tags=['Weapons']
)
schema = AutoSchema(operation_id_base='V1Weapon')
queryset = models.Weapon.objects.all().order_by("pk")
serializer_class = serializers.WeaponSerializer
filterset_class = filters.WeaponFilter
Expand All @@ -409,13 +308,7 @@ class ArmorViewSet(viewsets.ReadOnlyModelViewSet):
list: API endpoint for returning a list of armor.
retrieve: API endpoint for returning a particular armor.
"""
schema = CustomSchema(
summary={
'/armor/': 'List Armor',
'/armor/{slug}/': 'Retrieve Armor',
},
tags=['Armor']
)
schema = AutoSchema(operation_id_base='V1Armor')
queryset = models.Armor.objects.all().order_by("pk")
serializer_class = serializers.ArmorSerializer
filterset_class = filters.ArmorFilter
Expand Down
14 changes: 14 additions & 0 deletions api_v2/migrations/0009_merge_20241010_0919.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 5.1.1 on 2024-10-10 09:19

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0004_alter_rule_index_alter_rule_initialheaderlevel_and_more'),
('api_v2', '0008_weapon_distance_unit'),
]

operations = [
]
14 changes: 14 additions & 0 deletions api_v2/migrations/0010_merge_20241016_1952.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 5.1.1 on 2024-10-16 19:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api_v2', '0009_merge_20241010_0144'),
('api_v2', '0009_merge_20241010_0919'),
]

operations = [
]
Loading

0 comments on commit 005de61

Please sign in to comment.