Replace "_CHOICES" with Django 3.0 IntegerChoices Enum class.

This commit is contained in:
Casper V. Kristensen 2020-04-21 03:02:21 +02:00
parent f6c4c732d6
commit 006f1c6c45
Signed by: caspervk
GPG key ID: 289CA03790535054
8 changed files with 97 additions and 117 deletions

View file

@ -13,50 +13,33 @@ class Item(models.Model):
max_length=50
)
QUALITY_POOR = 0
QUALITY_COMMON = 1
QUALITY_UNCOMMON = 2
QUALITY_RARE = 3
QUALITY_EPIC = 4
QUALITY_LEGENDARY = 5
QUALITY_ARTIFACT = 6
QUALITY_CHOICES = [
(QUALITY_POOR, "Poor"),
(QUALITY_COMMON, "Common"),
(QUALITY_UNCOMMON, "Uncommon"),
(QUALITY_RARE, "Rare"),
(QUALITY_EPIC, "Epic"),
(QUALITY_LEGENDARY, "Legendary"),
(QUALITY_ARTIFACT, "Artifact"),
]
class Quality(models.IntegerChoices):
POOR = 0
COMMON = 1
UNCOMMON = 2
RARE = 3
EPIC = 4
LEGENDARY = 5
ARTIFACT = 6
quality = models.PositiveSmallIntegerField(
choices=QUALITY_CHOICES
choices=Quality.choices
)
KLASS_CONSUMABLES = 0
KLASS_CONTAINERS = 1
KLASS_WEAPONS = 2
KLASS_ARMOR = 4
KLASS_PROJECTILES = 6
KLASS_TRADE_GOODS = 7
KLASS_RECIPES = 9
KLASS_QUIVER = 11
KLASS_QUEST_ITEMS = 12
KLASS_MISCELLANEOUS = 15
KLASS_CHOICES = [
(KLASS_CONSUMABLES, "Consumables"),
(KLASS_CONTAINERS, "Containers"),
(KLASS_WEAPONS, "Weapons"),
(KLASS_ARMOR, "Armor"),
(KLASS_PROJECTILES, "Projectiles"),
(KLASS_TRADE_GOODS, "Trade Goods"),
(KLASS_RECIPES, "Recipes"),
(KLASS_QUIVER, "Quiver"),
(KLASS_QUEST_ITEMS, "Quest Items"),
(KLASS_MISCELLANEOUS, "Miscellaneous"),
]
class Klass(models.IntegerChoices):
CONSUMABLES = 0
CONTAINERS = 1
WEAPONS = 2
ARMOR = 4
PROJECTILES = 6
TRADE_GOODS = 7
RECIPES = 9
QUIVER = 11
QUEST_ITEMS = 12
MISCELLANEOUS = 15
klass = models.PositiveSmallIntegerField(
choices=KLASS_CHOICES
choices=Klass.choices
)
sub_klass = models.SmallIntegerField(

View file

@ -160,11 +160,11 @@ CRISPY_TEMPLATE_PACK = "bootstrap4"
# Drakul
DEFAULT_ATTENDANCE = {
"No Response": 0.0,
"Signed Off": 0.0,
"Signed Up": 1.0,
"Stand By": 1.0,
"Confirmed": 1.0,
"NO_RESPONSE": 0.0,
"SIGNED_OFF": 0.0,
"SIGNED_UP": 1.0,
"STANDBY": 1.0,
"CONFIRMED": 1.0,
}
ATTENDANCE_COLORS = [

View file

@ -16,16 +16,16 @@ class RaidResponseForm(ModelForm):
self.fields["character"].queryset = user.characters
self.fields["character"].initial = user.main
self.fields["role"].initial = user.main.role
self.fields["status"].choices = RaidResponse.USER_STATUS_CHOICES
self.fields["status"].choices = RaidResponse.UserStatus.choices
self.helper = FormHelper()
if self.instance.status <= RaidResponse.SIGNED_OFF:
if self.instance.status <= RaidResponse.Status.SIGNED_OFF:
signup_button = StrictButton(
"Sign Up",
type="submit",
name="status",
value=RaidResponse.SIGNED_UP,
value=RaidResponse.Status.SIGNED_UP,
css_class="btn-success btn-block"
)
else:
@ -33,7 +33,7 @@ class RaidResponseForm(ModelForm):
"Change",
type="submit",
name="status",
value=RaidResponse.SIGNED_UP,
value=RaidResponse.Status.SIGNED_UP,
css_class="btn-primary btn-block"
)
self.helper.layout = Layout(
@ -46,9 +46,9 @@ class RaidResponseForm(ModelForm):
"Sign Off",
type="submit",
name="status",
value=RaidResponse.SIGNED_OFF,
value=RaidResponse.Status.SIGNED_OFF,
css_class="btn-danger btn-block",
disabled=self.instance.pk and self.instance.status == RaidResponse.SIGNED_OFF
disabled=self.instance.pk and self.instance.status == RaidResponse.Status.SIGNED_OFF
),
css_class="col-md-3"
),

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.2 on 2020-04-21 02:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('raids', '0006_auto_20200417_0854'),
]
operations = [
migrations.AlterField(
model_name='raidresponse',
name='status',
field=models.PositiveSmallIntegerField(choices=[(0, 'No Response'), (1, 'Signed Off'), (2, 'Signed Up'), (3, 'Standby'), (4, 'Confirmed')]),
),
]

View file

@ -57,38 +57,29 @@ class RaidResponse(models.Model):
)
role = models.PositiveSmallIntegerField(
choices=Character.ROLE_CHOICES,
choices=Character.Roles.choices,
blank=True,
null=True
)
class Status(models.IntegerChoices):
NO_RESPONSE = 0
SIGNED_OFF = 1
SIGNED_UP = 2
STANDBY = 3
CONFIRMED = 4
STATUS_CHOICES = [
(NO_RESPONSE, "No Response"),
(SIGNED_OFF, "Signed Off"),
(SIGNED_UP, "Signed Up"),
(STANDBY, "Stand By"),
(CONFIRMED, "Confirmed"),
]
USER_STATUS_CHOICES = [
(SIGNED_OFF, "Signed Off"),
(SIGNED_UP, "Signed Up"),
]
status = models.PositiveSmallIntegerField(
choices=STATUS_CHOICES
)
STATUS_DEFAULT_ATTENDANCE = {
NO_RESPONSE: settings.DEFAULT_ATTENDANCE["No Response"],
SIGNED_OFF: settings.DEFAULT_ATTENDANCE["Signed Off"],
SIGNED_UP: settings.DEFAULT_ATTENDANCE["Signed Up"],
STANDBY: settings.DEFAULT_ATTENDANCE["Stand By"],
CONFIRMED: settings.DEFAULT_ATTENDANCE["Confirmed"],
}
@property
def default_attendance(self):
return settings.DEFAULT_ATTENDANCE[self.name]
class UserStatus(models.IntegerChoices):
SIGNED_OFF = 1
SIGNED_UP = 2
status = models.PositiveSmallIntegerField(
choices=Status.choices
)
attendance = models.DecimalField(
max_digits=3,
@ -113,14 +104,14 @@ class RaidResponse(models.Model):
def clean(self):
# Make sure no-responses and sign-offs are role-agnostic, but all other responses are not
if self.status <= RaidResponse.SIGNED_OFF:
if self.status <= RaidResponse.Status.SIGNED_OFF:
self.role = None
elif self.role is None:
raise ValidationError({"role": "This field is required."})
# Set attendance to one of the default values if status was changed
if self.status != self._original_status:
self.attendance = RaidResponse.STATUS_DEFAULT_ATTENDANCE[self.status]
self.attendance = RaidResponse.Status(self.status).default_attendance
def save(self, *args, **kwargs):
super().save(*args, **kwargs)

View file

@ -11,7 +11,7 @@ User = get_user_model()
@receiver(post_save, sender=Raid)
def create_raid_no_responses(instance: Raid, **kwargs):
# Delete all pre-existing no-responses, in case the deadline was changed
instance.responses.filter(status=RaidResponse.NO_RESPONSE).delete()
instance.responses.filter(status=RaidResponse.Status.NO_RESPONSE).delete()
# Then create them (again)
users = User.objects \
.exclude(Q(date_joined__gt=instance.date) | Q(characters__raid_responses__raid=instance) | Q(is_active=False)) \
@ -20,8 +20,8 @@ def create_raid_no_responses(instance: Raid, **kwargs):
RaidResponse(
raid=instance,
character=user.main,
status=RaidResponse.NO_RESPONSE,
attendance=RaidResponse.STATUS_DEFAULT_ATTENDANCE[RaidResponse.NO_RESPONSE]
status=RaidResponse.Status.NO_RESPONSE,
attendance=RaidResponse.Status.NO_RESPONSE.default_attendance
)
for user in users
)
@ -30,7 +30,7 @@ def create_raid_no_responses(instance: Raid, **kwargs):
@receiver(post_save, sender=User)
def create_user_no_responses(instance: User, **kwargs):
# Delete all pre-existing no-responses for this user, in case date_joined or main was changed
RaidResponse.objects.filter(character__user=instance, status=RaidResponse.NO_RESPONSE).delete()
RaidResponse.objects.filter(character__user=instance, status=RaidResponse.Status.NO_RESPONSE).delete()
# Then create them (again)
if not instance.is_active:
return
@ -38,8 +38,8 @@ def create_user_no_responses(instance: User, **kwargs):
RaidResponse(
raid=raid,
character=instance.main,
status=RaidResponse.NO_RESPONSE,
attendance=RaidResponse.STATUS_DEFAULT_ATTENDANCE[RaidResponse.NO_RESPONSE]
status=RaidResponse.Status.NO_RESPONSE,
attendance=RaidResponse.Status.NO_RESPONSE.default_attendance
)
for raid in Raid.objects.exclude(
Q(date__lt=instance.date_joined) | Q(responses__character__user=instance)

View file

@ -93,6 +93,7 @@ class Character(models.Model):
unique=True
)
class Klasses(models.IntegerChoices):
DRUID = 1
HUNTER = 2
MAGE = 3
@ -102,32 +103,19 @@ class Character(models.Model):
SHAMAN = 7
WARLOCK = 8
WARRIOR = 9
CLASS_CHOICES = [
(DRUID, "Druid"),
(HUNTER, "Hunter"),
(MAGE, "Mage"),
(PALADIN, "Paladin"),
(PRIEST, "Priest"),
(ROGUE, "Rogue"),
(SHAMAN, "Shaman"),
(WARLOCK, "Warlock"),
(WARRIOR, "Warrior"),
]
klass = models.PositiveSmallIntegerField(
"class",
choices=CLASS_CHOICES
choices=Klasses.choices
)
class Roles(models.IntegerChoices):
TANK = 1
HEALER = 2
DAMAGE = 3
ROLE_CHOICES = [
(TANK, "Tank"),
(HEALER, "Healer"),
(DAMAGE, "Damage"),
]
role = models.PositiveSmallIntegerField(
choices=ROLE_CHOICES
choices=Roles.choices
)
class Meta:

View file

@ -14,7 +14,7 @@ def attendance_cell(response: RaidResponse):
if response is None:
return ""
if response.attendance == RaidResponse.STATUS_DEFAULT_ATTENDANCE[response.status]:
if response.attendance == RaidResponse.Status(response.status).default_attendance:
cell = ""
else:
cell = response.attendance