From 625bd0f072d1a3576f0bdca3c4ddc9450e177006 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Thu, 26 Jan 2017 20:36:55 +0100 Subject: [PATCH] Contest Entry class management foo --- TODO | 2 +- contest/admin.py | 3 +- contest/forms.py | 15 ++++++++- contest/migrations/0012_auto_20170126_1852.py | 29 ++++++++++++++++ contest/migrations/0013_auto_20170126_1909.py | 20 +++++++++++ contest/models.py | 10 +++++- contest/views.py | 33 ++++++++++++++----- templates/contest/index.html | 17 ++++++++-- templates/registration/profile.html | 12 +++++-- 9 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 contest/migrations/0012_auto_20170126_1852.py create mode 100644 contest/migrations/0013_auto_20170126_1909.py diff --git a/TODO b/TODO index 90c5fee..96ccd05 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ - register users still has "reference" labels, make form better - contest log add deadline - cbr parser zum hochladen - - contest teilnahme klassen - "Please register with your (uppercase) Callsign as Usernames. For DN-Calls, /[A-Z] is allowed." gibt es wohl noch wo (Partially) Done @@ -24,6 +23,7 @@ - beim qso log kann noch vorne die uhrzeit dran - bastla will anderen regex - align log button with log + - contest teilnahme klassen Glaube nich, dass ich das mache - call dupe validation könnte ins model wandern diff --git a/contest/admin.py b/contest/admin.py index 77465c2..94059ac 100644 --- a/contest/admin.py +++ b/contest/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Frequency, Band, Reference, QSO, User, Contest, ShadowCall +from .models import Frequency, Band, Reference, QSO, User, Contest, ShadowCall, EntryCategory admin.site.register(User) admin.site.register(QSO) @@ -8,3 +8,4 @@ admin.site.register(Frequency) admin.site.register(Reference) admin.site.register(Contest) admin.site.register(ShadowCall) +admin.site.register(EntryCategory) diff --git a/contest/forms.py b/contest/forms.py index eeb45a7..9c0ab5f 100644 --- a/contest/forms.py +++ b/contest/forms.py @@ -5,7 +5,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout from django.urls import reverse -from .models import User, Reference, QSO, ShadowCall +from .models import User, Reference, QSO, ShadowCall, EntryCategory from .validators import CallUsernameValidator, CallLogValidator class CustomUserCreationForm(UserCreationForm): @@ -37,6 +37,19 @@ class UpdateRefForm(forms.Form): if not existingRef and not newRefName: raise forms.ValidationError("Select either an existing ref or create a new one!") +class UpdateCategoryForm(forms.Form): + entry = forms.ModelChoiceField(label="Entry category", queryset=EntryCategory.objects.all()) + def __init__(self, *args, **kwargs): + super(UpdateCategoryForm, self).__init__(*args, **kwargs) + + self.helper = FormHelper() + self.helper.form_class = "form-inline " + self.helper.form_style = 'inline' + self.helper.field_template = "bootstrap3/layout/inline_field.html" + self.helper.action = reverse("profile") + self.helper.add_input(Submit('submit', 'Set category')) + self.helper.layout = Layout('entry') + class QSOForm(forms.ModelForm): class Meta: model = QSO diff --git a/contest/migrations/0012_auto_20170126_1852.py b/contest/migrations/0012_auto_20170126_1852.py new file mode 100644 index 0000000..c8bec9f --- /dev/null +++ b/contest/migrations/0012_auto_20170126_1852.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.3 on 2017-01-26 18:52 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0011_auto_20170125_0126'), + ] + + operations = [ + migrations.CreateModel( + name='EntryCategory', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=40, unique=True)), + ('description', models.TextField(blank=True)), + ], + ), + migrations.AddField( + model_name='user', + name='cat', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='contest.EntryCategory'), + ), + ] diff --git a/contest/migrations/0013_auto_20170126_1909.py b/contest/migrations/0013_auto_20170126_1909.py new file mode 100644 index 0000000..8144ed4 --- /dev/null +++ b/contest/migrations/0013_auto_20170126_1909.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.3 on 2017-01-26 19:09 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0012_auto_20170126_1852'), + ] + + operations = [ + migrations.AlterField( + model_name='entrycategory', + name='name', + field=models.CharField(max_length=64, unique=True), + ), + ] diff --git a/contest/models.py b/contest/models.py index da663f7..476a7e9 100644 --- a/contest/models.py +++ b/contest/models.py @@ -24,6 +24,13 @@ class Reference(models.Model): def __str__(self): return self.name +class EntryCategory(models.Model): + name = models.CharField(max_length=64, unique=True) + description = models.TextField(blank=True) + + def __str__(self): + return self.name + class ShadowCall(models.Model): username = models.CharField(max_length=20, unique=True, db_index=True, validators=[CallUsernameValidator()]) ref = models.ForeignKey(Reference, models.SET_NULL,null=True, blank=True) @@ -32,7 +39,8 @@ class ShadowCall(models.Model): return self.username class User(AbstractUser): - ref = models.ForeignKey(Reference, models.SET_NULL,null=True, blank=True) + ref = models.ForeignKey(Reference, models.SET_NULL, null=True, blank=True) + cat = models.ForeignKey(EntryCategory, models.SET_NULL, null=True, blank=True) def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) diff --git a/contest/views.py b/contest/views.py index b4483ef..246ab26 100644 --- a/contest/views.py +++ b/contest/views.py @@ -14,7 +14,7 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger import datetime from .models import User, Contest, Frequency, Reference, QSO, ShadowCall -from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm, ShadowCallAddForm +from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm, ShadowCallAddForm, UpdateCategoryForm def index(request): if request.user.is_authenticated(): @@ -208,13 +208,30 @@ def register(request): @login_required def profile(request): pwForm = None + catForm = None + if request.method == 'POST': - pwForm = PasswordChangeForm(user=request.user, data=request.POST) - if pwForm.is_valid(): - pwForm.save() - auth_login(request, pwForm.user) - messages.success(request, "Password changed") - else: + if request.POST.get("submit", None) == "pwchange": + pwForm = PasswordChangeForm(user=request.user, data=request.POST) + if pwForm.is_valid(): + pwForm.save() + auth_login(request, pwForm.user) + messages.success(request, "Password changed") + + return HttpResponseRedirect(reverse("profile")) + else: + catForm = UpdateCategoryForm(data=request.POST) + if catForm.is_valid(): + request.user.cat = catForm.cleaned_data["entry"] + request.user.save() + messages.success(request, "Entry category set") + + return HttpResponseRedirect(reverse("profile")) + + if not pwForm: pwForm = PasswordChangeForm(user=request.user) - return render(request, 'registration/profile.html', {"pwForm": pwForm}) + if not catForm: + catForm = UpdateCategoryForm(initial={'entry': request.user.cat}) + + return render(request, 'registration/profile.html', {"pwForm": pwForm, "catForm": catForm}) diff --git a/templates/contest/index.html b/templates/contest/index.html index a1443b6..01bfb55 100644 --- a/templates/contest/index.html +++ b/templates/contest/index.html @@ -40,15 +40,28 @@ {% endif %} +{% if not user.cat %} +
+
+
+
You don't have a category!
+
+

+ You don't have an entry category! Please set one in your profile. +

+
+
+
+
+{% endif %} + {% if user.is_staff %}
You are staff!
-

Hey, you are staff. Do you want to start registering people?

-

diff --git a/templates/registration/profile.html b/templates/registration/profile.html index addd94a..94ba01c 100644 --- a/templates/registration/profile.html +++ b/templates/registration/profile.html @@ -9,13 +9,19 @@
User Info
- +

{{ user }}

- +

{{ user.ref|default:"Not registered" }}

+
+ +

+ {% crispy catForm %} +

+
@@ -26,7 +32,7 @@
{% csrf_token %} {{ pwForm|crispy }} - +