Replace "_CHOICES" with Django 3.0 IntegerChoices Enum class.
This commit is contained in:
parent
f6c4c732d6
commit
006f1c6c45
|
@ -13,50 +13,33 @@ class Item(models.Model):
|
||||||
max_length=50
|
max_length=50
|
||||||
)
|
)
|
||||||
|
|
||||||
QUALITY_POOR = 0
|
class Quality(models.IntegerChoices):
|
||||||
QUALITY_COMMON = 1
|
POOR = 0
|
||||||
QUALITY_UNCOMMON = 2
|
COMMON = 1
|
||||||
QUALITY_RARE = 3
|
UNCOMMON = 2
|
||||||
QUALITY_EPIC = 4
|
RARE = 3
|
||||||
QUALITY_LEGENDARY = 5
|
EPIC = 4
|
||||||
QUALITY_ARTIFACT = 6
|
LEGENDARY = 5
|
||||||
QUALITY_CHOICES = [
|
ARTIFACT = 6
|
||||||
(QUALITY_POOR, "Poor"),
|
|
||||||
(QUALITY_COMMON, "Common"),
|
|
||||||
(QUALITY_UNCOMMON, "Uncommon"),
|
|
||||||
(QUALITY_RARE, "Rare"),
|
|
||||||
(QUALITY_EPIC, "Epic"),
|
|
||||||
(QUALITY_LEGENDARY, "Legendary"),
|
|
||||||
(QUALITY_ARTIFACT, "Artifact"),
|
|
||||||
]
|
|
||||||
quality = models.PositiveSmallIntegerField(
|
quality = models.PositiveSmallIntegerField(
|
||||||
choices=QUALITY_CHOICES
|
choices=Quality.choices
|
||||||
)
|
)
|
||||||
|
|
||||||
KLASS_CONSUMABLES = 0
|
class Klass(models.IntegerChoices):
|
||||||
KLASS_CONTAINERS = 1
|
CONSUMABLES = 0
|
||||||
KLASS_WEAPONS = 2
|
CONTAINERS = 1
|
||||||
KLASS_ARMOR = 4
|
WEAPONS = 2
|
||||||
KLASS_PROJECTILES = 6
|
ARMOR = 4
|
||||||
KLASS_TRADE_GOODS = 7
|
PROJECTILES = 6
|
||||||
KLASS_RECIPES = 9
|
TRADE_GOODS = 7
|
||||||
KLASS_QUIVER = 11
|
RECIPES = 9
|
||||||
KLASS_QUEST_ITEMS = 12
|
QUIVER = 11
|
||||||
KLASS_MISCELLANEOUS = 15
|
QUEST_ITEMS = 12
|
||||||
KLASS_CHOICES = [
|
MISCELLANEOUS = 15
|
||||||
(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"),
|
|
||||||
]
|
|
||||||
klass = models.PositiveSmallIntegerField(
|
klass = models.PositiveSmallIntegerField(
|
||||||
choices=KLASS_CHOICES
|
choices=Klass.choices
|
||||||
)
|
)
|
||||||
|
|
||||||
sub_klass = models.SmallIntegerField(
|
sub_klass = models.SmallIntegerField(
|
||||||
|
|
|
@ -160,11 +160,11 @@ CRISPY_TEMPLATE_PACK = "bootstrap4"
|
||||||
|
|
||||||
# Drakul
|
# Drakul
|
||||||
DEFAULT_ATTENDANCE = {
|
DEFAULT_ATTENDANCE = {
|
||||||
"No Response": 0.0,
|
"NO_RESPONSE": 0.0,
|
||||||
"Signed Off": 0.0,
|
"SIGNED_OFF": 0.0,
|
||||||
"Signed Up": 1.0,
|
"SIGNED_UP": 1.0,
|
||||||
"Stand By": 1.0,
|
"STANDBY": 1.0,
|
||||||
"Confirmed": 1.0,
|
"CONFIRMED": 1.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
ATTENDANCE_COLORS = [
|
ATTENDANCE_COLORS = [
|
||||||
|
|
|
@ -16,16 +16,16 @@ class RaidResponseForm(ModelForm):
|
||||||
self.fields["character"].queryset = user.characters
|
self.fields["character"].queryset = user.characters
|
||||||
self.fields["character"].initial = user.main
|
self.fields["character"].initial = user.main
|
||||||
self.fields["role"].initial = user.main.role
|
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()
|
self.helper = FormHelper()
|
||||||
|
|
||||||
if self.instance.status <= RaidResponse.SIGNED_OFF:
|
if self.instance.status <= RaidResponse.Status.SIGNED_OFF:
|
||||||
signup_button = StrictButton(
|
signup_button = StrictButton(
|
||||||
"Sign Up",
|
"Sign Up",
|
||||||
type="submit",
|
type="submit",
|
||||||
name="status",
|
name="status",
|
||||||
value=RaidResponse.SIGNED_UP,
|
value=RaidResponse.Status.SIGNED_UP,
|
||||||
css_class="btn-success btn-block"
|
css_class="btn-success btn-block"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -33,7 +33,7 @@ class RaidResponseForm(ModelForm):
|
||||||
"Change",
|
"Change",
|
||||||
type="submit",
|
type="submit",
|
||||||
name="status",
|
name="status",
|
||||||
value=RaidResponse.SIGNED_UP,
|
value=RaidResponse.Status.SIGNED_UP,
|
||||||
css_class="btn-primary btn-block"
|
css_class="btn-primary btn-block"
|
||||||
)
|
)
|
||||||
self.helper.layout = Layout(
|
self.helper.layout = Layout(
|
||||||
|
@ -46,9 +46,9 @@ class RaidResponseForm(ModelForm):
|
||||||
"Sign Off",
|
"Sign Off",
|
||||||
type="submit",
|
type="submit",
|
||||||
name="status",
|
name="status",
|
||||||
value=RaidResponse.SIGNED_OFF,
|
value=RaidResponse.Status.SIGNED_OFF,
|
||||||
css_class="btn-danger btn-block",
|
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"
|
css_class="col-md-3"
|
||||||
),
|
),
|
||||||
|
|
18
drakul/raids/migrations/0007_auto_20200421_0256.py
Normal file
18
drakul/raids/migrations/0007_auto_20200421_0256.py
Normal 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')]),
|
||||||
|
),
|
||||||
|
]
|
|
@ -57,38 +57,29 @@ class RaidResponse(models.Model):
|
||||||
)
|
)
|
||||||
|
|
||||||
role = models.PositiveSmallIntegerField(
|
role = models.PositiveSmallIntegerField(
|
||||||
choices=Character.ROLE_CHOICES,
|
choices=Character.Roles.choices,
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
|
|
||||||
NO_RESPONSE = 0
|
class Status(models.IntegerChoices):
|
||||||
SIGNED_OFF = 1
|
NO_RESPONSE = 0
|
||||||
SIGNED_UP = 2
|
SIGNED_OFF = 1
|
||||||
STANDBY = 3
|
SIGNED_UP = 2
|
||||||
CONFIRMED = 4
|
STANDBY = 3
|
||||||
STATUS_CHOICES = [
|
CONFIRMED = 4
|
||||||
(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 = {
|
@property
|
||||||
NO_RESPONSE: settings.DEFAULT_ATTENDANCE["No Response"],
|
def default_attendance(self):
|
||||||
SIGNED_OFF: settings.DEFAULT_ATTENDANCE["Signed Off"],
|
return settings.DEFAULT_ATTENDANCE[self.name]
|
||||||
SIGNED_UP: settings.DEFAULT_ATTENDANCE["Signed Up"],
|
|
||||||
STANDBY: settings.DEFAULT_ATTENDANCE["Stand By"],
|
class UserStatus(models.IntegerChoices):
|
||||||
CONFIRMED: settings.DEFAULT_ATTENDANCE["Confirmed"],
|
SIGNED_OFF = 1
|
||||||
}
|
SIGNED_UP = 2
|
||||||
|
|
||||||
|
status = models.PositiveSmallIntegerField(
|
||||||
|
choices=Status.choices
|
||||||
|
)
|
||||||
|
|
||||||
attendance = models.DecimalField(
|
attendance = models.DecimalField(
|
||||||
max_digits=3,
|
max_digits=3,
|
||||||
|
@ -113,14 +104,14 @@ class RaidResponse(models.Model):
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
# Make sure no-responses and sign-offs are role-agnostic, but all other responses are not
|
# 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
|
self.role = None
|
||||||
elif self.role is None:
|
elif self.role is None:
|
||||||
raise ValidationError({"role": "This field is required."})
|
raise ValidationError({"role": "This field is required."})
|
||||||
|
|
||||||
# Set attendance to one of the default values if status was changed
|
# Set attendance to one of the default values if status was changed
|
||||||
if self.status != self._original_status:
|
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):
|
def save(self, *args, **kwargs):
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
|
@ -11,7 +11,7 @@ User = get_user_model()
|
||||||
@receiver(post_save, sender=Raid)
|
@receiver(post_save, sender=Raid)
|
||||||
def create_raid_no_responses(instance: Raid, **kwargs):
|
def create_raid_no_responses(instance: Raid, **kwargs):
|
||||||
# Delete all pre-existing no-responses, in case the deadline was changed
|
# 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)
|
# Then create them (again)
|
||||||
users = User.objects \
|
users = User.objects \
|
||||||
.exclude(Q(date_joined__gt=instance.date) | Q(characters__raid_responses__raid=instance) | Q(is_active=False)) \
|
.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(
|
RaidResponse(
|
||||||
raid=instance,
|
raid=instance,
|
||||||
character=user.main,
|
character=user.main,
|
||||||
status=RaidResponse.NO_RESPONSE,
|
status=RaidResponse.Status.NO_RESPONSE,
|
||||||
attendance=RaidResponse.STATUS_DEFAULT_ATTENDANCE[RaidResponse.NO_RESPONSE]
|
attendance=RaidResponse.Status.NO_RESPONSE.default_attendance
|
||||||
)
|
)
|
||||||
for user in users
|
for user in users
|
||||||
)
|
)
|
||||||
|
@ -30,7 +30,7 @@ def create_raid_no_responses(instance: Raid, **kwargs):
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=User)
|
||||||
def create_user_no_responses(instance: User, **kwargs):
|
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
|
# 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)
|
# Then create them (again)
|
||||||
if not instance.is_active:
|
if not instance.is_active:
|
||||||
return
|
return
|
||||||
|
@ -38,8 +38,8 @@ def create_user_no_responses(instance: User, **kwargs):
|
||||||
RaidResponse(
|
RaidResponse(
|
||||||
raid=raid,
|
raid=raid,
|
||||||
character=instance.main,
|
character=instance.main,
|
||||||
status=RaidResponse.NO_RESPONSE,
|
status=RaidResponse.Status.NO_RESPONSE,
|
||||||
attendance=RaidResponse.STATUS_DEFAULT_ATTENDANCE[RaidResponse.NO_RESPONSE]
|
attendance=RaidResponse.Status.NO_RESPONSE.default_attendance
|
||||||
)
|
)
|
||||||
for raid in Raid.objects.exclude(
|
for raid in Raid.objects.exclude(
|
||||||
Q(date__lt=instance.date_joined) | Q(responses__character__user=instance)
|
Q(date__lt=instance.date_joined) | Q(responses__character__user=instance)
|
||||||
|
|
|
@ -93,41 +93,29 @@ class Character(models.Model):
|
||||||
unique=True
|
unique=True
|
||||||
)
|
)
|
||||||
|
|
||||||
DRUID = 1
|
class Klasses(models.IntegerChoices):
|
||||||
HUNTER = 2
|
DRUID = 1
|
||||||
MAGE = 3
|
HUNTER = 2
|
||||||
PALADIN = 4
|
MAGE = 3
|
||||||
PRIEST = 5
|
PALADIN = 4
|
||||||
ROGUE = 6
|
PRIEST = 5
|
||||||
SHAMAN = 7
|
ROGUE = 6
|
||||||
WARLOCK = 8
|
SHAMAN = 7
|
||||||
WARRIOR = 9
|
WARLOCK = 8
|
||||||
CLASS_CHOICES = [
|
WARRIOR = 9
|
||||||
(DRUID, "Druid"),
|
|
||||||
(HUNTER, "Hunter"),
|
|
||||||
(MAGE, "Mage"),
|
|
||||||
(PALADIN, "Paladin"),
|
|
||||||
(PRIEST, "Priest"),
|
|
||||||
(ROGUE, "Rogue"),
|
|
||||||
(SHAMAN, "Shaman"),
|
|
||||||
(WARLOCK, "Warlock"),
|
|
||||||
(WARRIOR, "Warrior"),
|
|
||||||
]
|
|
||||||
klass = models.PositiveSmallIntegerField(
|
klass = models.PositiveSmallIntegerField(
|
||||||
"class",
|
"class",
|
||||||
choices=CLASS_CHOICES
|
choices=Klasses.choices
|
||||||
)
|
)
|
||||||
|
|
||||||
TANK = 1
|
class Roles(models.IntegerChoices):
|
||||||
HEALER = 2
|
TANK = 1
|
||||||
DAMAGE = 3
|
HEALER = 2
|
||||||
ROLE_CHOICES = [
|
DAMAGE = 3
|
||||||
(TANK, "Tank"),
|
|
||||||
(HEALER, "Healer"),
|
|
||||||
(DAMAGE, "Damage"),
|
|
||||||
]
|
|
||||||
role = models.PositiveSmallIntegerField(
|
role = models.PositiveSmallIntegerField(
|
||||||
choices=ROLE_CHOICES
|
choices=Roles.choices
|
||||||
)
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -14,7 +14,7 @@ def attendance_cell(response: RaidResponse):
|
||||||
if response is None:
|
if response is None:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
if response.attendance == RaidResponse.STATUS_DEFAULT_ATTENDANCE[response.status]:
|
if response.attendance == RaidResponse.Status(response.status).default_attendance:
|
||||||
cell = ""
|
cell = ""
|
||||||
else:
|
else:
|
||||||
cell = response.attendance
|
cell = response.attendance
|
||||||
|
|
Loading…
Reference in a new issue