Compare commits

...

3 commits

Author SHA1 Message Date
Casper V. Kristensen b3ba6caa07
Fix not able to create users in admin by moving main-creation logic from manager's _create_user() to User's save(). 2019-10-25 13:28:33 +02:00
Casper V. Kristensen 8da844d2e4
Add further help text. 2019-10-25 13:27:20 +02:00
Casper V. Kristensen 660c1e75e6
Rename "signup deadline" -> "response deadline." 2019-10-25 13:26:42 +02:00
10 changed files with 44 additions and 37 deletions

View file

@ -18,7 +18,7 @@ class RaidCommentInline(admin.TabularInline):
@admin.register(Raid)
class RaidAdmin(admin.ModelAdmin):
list_display = ["title", "date", "signup_deadline"]
list_display = ["title", "date", "response_deadline"]
search_fields = ["title"]
inlines = [RaidResponseInline, RaidCommentInline]

View file

@ -63,7 +63,7 @@ class RaidResponseForm(ModelForm):
class RaidForm(ModelForm):
class Meta:
model = Raid
fields = ["title", "description", "date", "signup_deadline"]
fields = ["title", "description", "date", "response_deadline"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -78,7 +78,7 @@ class RaidForm(ModelForm):
Field("description"),
Row(
Column("date", css_class="form-group col-md-6"),
Column("signup_deadline", css_class="form-group col-md-6"),
Column("response_deadline", css_class="form-group col-md-6"),
),
submit_button
)

View file

@ -1,4 +1,4 @@
# Generated by Django 2.2.6 on 2019-10-25 01:58
# Generated by Django 2.2.6 on 2019-10-25 13:23
from django.db import migrations, models
@ -17,7 +17,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True)),
('date', models.DateField(help_text='Some date where the instance was reset in the past.')),
('time', models.TimeField()),
('time', models.TimeField(help_text='The time of day where the instance resets.')),
('duration', models.DurationField(help_text='Resets are calculated from the given date in intervals of this duration.', verbose_name='lockout duration')),
],
options={
@ -31,7 +31,7 @@ class Migration(migrations.Migration):
('title', models.CharField(max_length=40)),
('description', models.TextField(blank=True, null=True)),
('date', models.DateTimeField()),
('signup_deadline', models.DateTimeField(blank=True, help_text='Defaults to date and time of raid if not set.')),
('response_deadline', models.DateTimeField(blank=True, help_text='Defaults to 24 hours before date and time of raid if not set.')),
],
options={
'ordering': ['-date'],

View file

@ -1,4 +1,4 @@
# Generated by Django 2.2.6 on 2019-10-25 01:58
# Generated by Django 2.2.6 on 2019-10-25 13:23
from django.conf import settings
from django.db import migrations, models

View file

@ -1,3 +1,5 @@
from datetime import timedelta
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
@ -16,18 +18,19 @@ class Raid(models.Model):
)
date = models.DateTimeField()
signup_deadline = models.DateTimeField(
response_deadline = models.DateTimeField(
blank=True,
help_text="Defaults to date and time of raid if not set."
help_text="Defaults to 24 hours before date and time of raid if not set."
)
class Meta:
ordering = ["-date"]
def clean(self):
# Set the signup deadline to the date/time of the raid if it hasn't been set already
if self.signup_deadline is None:
self.signup_deadline = self.date
def save(self, *args, **kwargs):
if self.response_deadline is None:
self.response_deadline = self.date - timedelta(hours=24)
return super().save(*args, **kwargs)
def __str__(self):
return f"{self.title} on {self.date}"
@ -141,7 +144,9 @@ class InstanceReset(models.Model):
date = models.DateField(
help_text="Some date where the instance was reset in the past."
)
time = models.TimeField()
time = models.TimeField(
help_text="The time of day where the instance resets."
)
duration = models.DurationField(
"lockout duration",
help_text="Resets are calculated from the given date in intervals of this duration."

View file

@ -15,7 +15,7 @@
</h4>
<div class="card-body">
<p class="card-text">{{ raid.description | linebreaksbr | default:"<em>No description</em>" }}</p>
<p class="card-text"><small class="text-muted">Sign up deadline: {{ raid.signup_deadline }}</small></p>
<p class="card-text"><small class="text-muted">Response deadline: {{ raid.response_deadline }}</small></p>
</div>
</div>
{% if response_form %}

View file

@ -83,7 +83,7 @@ class RaidDetailView(SingleObjectMixin, MultiModelFormView):
classes = {
"comment_form": RaidCommentForm
}
if self.object.signup_deadline > timezone.now():
if self.object.response_deadline > timezone.now():
classes["response_form"] = RaidResponseForm
return classes

View file

@ -8,5 +8,3 @@ class CharacterForm(ModelForm):
model = Character
fields = ["name", "klass", "role"]
def clean_name(self):
return self.cleaned_data["name"].capitalize()

View file

@ -1,4 +1,4 @@
# Generated by Django 2.2.6 on 2019-10-25 01:58
# Generated by Django 2.2.6 on 2019-10-25 13:23
from django.conf import settings
import django.contrib.auth.validators
@ -55,7 +55,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='user',
name='main',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='users.Character'),
field=models.OneToOneField(blank=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='users.Character'),
),
migrations.AddField(
model_name='user',

View file

@ -12,22 +12,6 @@ class UserManager(DjangoUserManager):
avg_attendance=Avg("characters__raid_responses__attendance")
)
@transaction.atomic
def _create_user(self, username, email, password, **extra_fields):
if "main" not in extra_fields:
main = Character.objects.create(
user=None,
name=username,
klass=Character.WARRIOR,
role=Character.DAMAGE
)
main.save()
extra_fields["main"] = main
user = super()._create_user(username, email, password, **extra_fields)
extra_fields["main"].user = user
extra_fields["main"].save()
return user
class User(AbstractUser):
objects = UserManager()
@ -38,7 +22,8 @@ class User(AbstractUser):
main = models.OneToOneField(
"Character",
related_name="+",
on_delete=models.PROTECT
on_delete=models.PROTECT,
blank=True
)
class Meta:
@ -48,6 +33,22 @@ class User(AbstractUser):
if hasattr(self, "main") and self.main.user != self:
raise ValidationError({"main": "Main character must be owned by user."})
@transaction.atomic
def save(self, *args, **kwargs):
if not hasattr(self, "main"):
self.main = Character.objects.create(
user=None,
name=self.username,
klass=Character.WARRIOR,
role=Character.DAMAGE
)
self.main.save()
user = super().save(*args, **kwargs)
self.main.user = self
self.main.save()
return user
def avg_attendance(self):
return self.avg_attendance
@ -107,5 +108,8 @@ class Character(models.Model):
class Meta:
ordering = ["name"]
def clean(self):
self.name = self.name.capitalize()
def __str__(self):
return self.name