Skip to content

Commit c260072

Browse files
Merge pull request open5e#755 from calumbell/751/add-services
Add Services to API
2 parents 3f68413 + d19b311 commit c260072

File tree

13 files changed

+307
-14
lines changed

13 files changed

+307
-14
lines changed

api_v2/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ class LanguageAdmin(admin.ModelAdmin):
109109
admin.site.register(Rule)
110110
admin.site.register(RuleSet)
111111

112-
admin.site.register(Image)
112+
admin.site.register(Image)
113+
114+
admin.site.register(Service)

api_v2/migrations/0047_service.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 5.2.1 on 2025-06-04 09:56
2+
3+
import django.core.validators
4+
import django.db.models.deletion
5+
from decimal import Decimal
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('api_v2', '0046_characterclass_primary_abilities_and_more'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Service',
18+
fields=[
19+
('desc', models.TextField(help_text='Description of the game content item. Markdown.')),
20+
('cost', models.DecimalField(decimal_places=2, default=None, help_text='Number representing the cost of the object.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
21+
('key', models.CharField(help_text='Unique key for the Item.', max_length=100, primary_key=True, serialize=False)),
22+
('detail', models.TextField(blank=True, help_text="Additional contextual infomation about the service. ie. For 'Squalid Living' the detail would be '1 day'", null=True)),
23+
('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api_v2.document')),
24+
],
25+
options={
26+
'abstract': False,
27+
},
28+
),
29+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 5.2.1 on 2025-06-04 10:53
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api_v2', '0047_service'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='service',
15+
name='name',
16+
field=models.CharField(default='default', help_text='Name of the item.', max_length=100),
17+
preserve_default=False,
18+
),
19+
]

api_v2/models/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from .item import Item
55
from .item import ItemSet
66
from .item import ItemRarity
7-
87
from .abilities import Ability
98
from .abilities import Skill
109

@@ -61,3 +60,5 @@
6160
from .rule import Rule, RuleSet
6261

6362
from .image import Image
63+
64+
from .service import Service

api_v2/models/abstracts.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,23 @@ class HasSenses(models.Model):
464464
class Meta:
465465
abstract = True
466466

467+
468+
class HasPrice(models.Model):
469+
"""
470+
The HasPrice abstract class is inherited by other models that have a price;
471+
items, services, etc.
472+
473+
When inherited, it add the 'cost' field to the derived model
474+
"""
475+
476+
cost = models.DecimalField(
477+
null=True, # Allow an unspecified cost.
478+
default=None,
479+
max_digits=10,
480+
decimal_places=2, # Only track down to 2 decimal places.
481+
validators=[MinValueValidator(decimal.Decimal(0.0))],
482+
help_text='Number representing the cost of the object.'
483+
)
484+
485+
class Meta:
486+
abstract = True

api_v2/models/item.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from .weapon import Weapon
77
from .armor import Armor
8-
from .abstracts import HasName, HasDescription
8+
from .abstracts import HasName, HasDescription, HasPrice
99
from .object import Object
1010
from .damagetype import DamageType
1111
from .document import FromDocument
@@ -24,21 +24,13 @@ class ItemCategory(HasName, FromDocument):
2424
"""A class describing categories of items."""
2525

2626

27-
class Item(Object, HasDescription, FromDocument):
27+
class Item(Object, HasDescription, FromDocument, HasPrice):
2828
"""
2929
This is the model for an Item, which is an object that can be used.
3030
3131
This extends the object model, but adds cost, and is_magical.
3232
"""
3333

34-
cost = models.DecimalField(
35-
null=True, # Allow an unspecified cost.
36-
default=None,
37-
max_digits=10,
38-
decimal_places=2, # Only track down to 2 decimal places.
39-
validators=[MinValueValidator(decimal.Decimal(0.0))],
40-
help_text='Number representing the cost of the object.')
41-
4234
weapon = models.ForeignKey(
4335
Weapon,
4436
on_delete=models.CASCADE,

api_v2/models/service.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django.db import models
2+
from .abstracts import HasName, HasDescription, HasPrice
3+
from .document import FromDocument
4+
5+
6+
class Service(HasName, HasDescription, FromDocument, HasPrice):
7+
"""
8+
9+
This is the Model for a purchasable Service
10+
11+
This is a companion model to Item for those purchasable services that aren't
12+
actually Items; life style expenses, hireling, etc.
13+
14+
"""
15+
# 'name', 'document', 'cost' fields inherited from abstract classes
16+
17+
detail = models.TextField(
18+
null=True,
19+
blank=True,
20+
help_text="Additional contextual infomation about the service. ie. For 'Squalid Living' the detail would be '1 day'"
21+
)

api_v2/serializers/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@
5050

5151
from .rule import RuleSerializer, RuleSetSerializer
5252

53-
from .image import ImageSerializer
53+
from .image import ImageSerializer
54+
55+
from .service import ServiceSerializer

api_v2/serializers/service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework import serializers
2+
from api_v2 import models
3+
from .abstracts import GameContentSerializer
4+
from .document import DocumentSummarySerializer
5+
6+
class ServiceSerializer(GameContentSerializer):
7+
key = serializers.ReadOnlyField()
8+
document = DocumentSummarySerializer()
9+
10+
class Meta:
11+
model = models.Service
12+
fields = '__all__'

api_v2/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
router.register(r'rulesets', views.RuleSetViewSet)
3737
router.register(r'images', views.ImageViewSet)
3838
router.register(r'weaponproperties', views.WeaponPropertyViewSet)
39+
router.register(r'services', views.ServiceViewSet)
3940
urlpatterns = [
4041
path('v2/', include(router.urls)),
4142
path('v2/enums/', views.get_enums, name="enums")

0 commit comments

Comments
 (0)