Skip to content

Commit 2e22bf9

Browse files
564 standardize approach to weight across all of v2 similar to distance (open5e#720)
* Adding weight. * Adding weight fields. * Adding weight unit to documents. * Specifying lb * Adding to item. * Resolving tests.
1 parent 921a6cc commit 2e22bf9

34 files changed

+109
-14
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1.2 on 2025-04-29 23:41
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api_v2', '0034_rename_racetrait_speciestrait_and_more'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='document',
15+
name='weight_unit',
16+
field=models.CharField(blank=True, choices=[('feet', 'feet'), ('miles', 'miles')], help_text='What distance unit the relevant field uses.', max_length=20, null=True),
17+
),
18+
]

api_v2/models/abstracts.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.core.validators import MaxValueValidator, MinValueValidator
66

77
from .enums import MODIFICATION_TYPES, DIE_TYPES
8-
from .enums import DISTANCE_UNIT_TYPES
8+
from .enums import DISTANCE_UNIT_TYPES, WEIGHT_UNIT_TYPES
99
from .enums import ABILITY_SCORE_MAXIMUM
1010
from .enums import SAVING_THROW_MAXIMUM, SAVING_THROW_MINIMUM
1111
from .enums import SKILL_BONUS_MINIMUM, SKILL_BONUS_MAXIMUM
@@ -66,6 +66,15 @@ def distance_unit_field():
6666
help_text='What distance unit the relevant field uses.'
6767
)
6868

69+
def weight_unit_field():
70+
return models.CharField(
71+
null=True,
72+
blank=True,
73+
max_length=20,
74+
choices=WEIGHT_UNIT_TYPES,
75+
help_text='What weight unit the relevant field uses.'
76+
)
77+
6978
# Define a field representing an ability score
7079
def ability_score_field(help_text):
7180
return models.SmallIntegerField(

api_v2/models/document.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Document(HasName, HasDescription):
3939
)
4040

4141
distance_unit = distance_unit_field()
42+
weight_unit = distance_unit_field()
4243

4344
@property
4445
def stats(self):

api_v2/models/enums.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111

1212
DISTANCE_UNIT_TYPES = [
1313
("feet","feet"),
14-
("miles","miles")
14+
("miles","miles"),
15+
]
16+
17+
WEIGHT_UNIT_TYPES = [
18+
("lb","lb"),
19+
("kg","kg")
1520
]
1621

1722
ABILITY_SCORE_MAXIMUM = 50

api_v2/models/object.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Object(HasName):
1313
"""
1414
This is the definition of the Object abstract base class.
1515
16-
The Object class will be inherited from by Item, Weapon, Character, etc.
16+
The Object class will be inherited from by Item, and Creature.
1717
Basically it describes any sort of matter in the 5e world.
1818
"""
1919

@@ -80,6 +80,11 @@ class Object(HasName):
8080
default=False,
8181
help_text='If the {} is immune to nonmagical attacks.'.format(__name__)
8282
)
83+
84+
def get_weight_unit(self):
85+
'''Returns document level weight unit.'''
86+
return self.document.weight_unit
87+
8388
class Meta:
8489
abstract = True
8590
ordering = ['pk']

api_v2/serializers/item.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,18 @@ class ItemSerializer(GameContentSerializer):
8686
rarity = ItemRaritySerializer()
8787
damage_immunities = DamageTypeSummarySerializer(many=True)
8888
size = SizeSummarySerializer()
89-
89+
weight_unit = serializers.SerializerMethodField()
90+
91+
@extend_schema_field(OpenApiTypes.STR)
92+
def get_weight_unit(self, item):
93+
return item.get_weight_unit()
94+
9095
class Meta:
9196
model = models.Item
9297
fields = '__all__'
9398

9499

100+
95101
class ItemSetSerializer(GameContentSerializer):
96102
key = serializers.ReadOnlyField()
97103
items = ItemSerializer(many=True, read_only=True, context={'request':{}})

api_v2/tests/responses/TestObjects.test_document_example.approved.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
"name": "Wizards of the Coast",
2929
"url": "http://localhost:8000/v2/publishers/wizards-of-the-coast/"
3030
},
31-
"url": "http://localhost:8000/v2/documents/srd/"
31+
"url": "http://localhost:8000/v2/documents/srd/",
32+
"weight_unit": "lb"
3233
}

api_v2/tests/responses/TestObjects.test_item_armor_example.approved.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@
6565
},
6666
"url": "http://localhost:8000/v2/items/srd_splint-armor/",
6767
"weapon": null,
68-
"weight": "60.000"
68+
"weight": "60.000",
69+
"weight_unit": "lb"
6970
}

api_v2/tests/responses/TestObjects.test_item_example.approved.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@
5959
},
6060
"url": "http://localhost:8000/v2/items/srd_apparatus-of-the-crab/",
6161
"weapon": null,
62-
"weight": "0.000"
62+
"weight": "0.000",
63+
"weight_unit": "lb"
6364
}

api_v2/tests/responses/TestObjects.test_item_melee_weapon_example.approved.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
},
5555
"url": "http://localhost:8000/v2/items/srd_shortsword/",
5656
"weapon": "http://localhost:8000/v2/weapons/srd_shortsword/",
57-
"weight": "2.000"
57+
"weight": "2.000",
58+
"weight_unit": "lb"
5859
}

0 commit comments

Comments
 (0)