Split out registrations form, show own state

This commit is contained in:
Midgard 2020-07-22 16:22:29 +02:00
parent 1b2a9a3e0b
commit f5aebd99b2
Signed by: midgard
GPG key ID: 511C112F1331BBB4
4 changed files with 36 additions and 16 deletions

View file

@ -35,12 +35,20 @@ class Event(models.Model):
return self.count_with_status(EventRegistration.ADMITTED)
def registration_of(self, user):
registrations = self.eventregistration_set.filter(user=user).all()
if not registrations:
return None
assert len(registrations) == 1, "Registrations should be unique per user and event"
return registrations[0]
class EventRegistration(models.Model):
INTERESTED = "I"
ADMITTED = "A"
REGISTRATION_STATE = {
INTERESTED: "Interested",
ADMITTED: "Admitted",
INTERESTED: "Op wachtlijst",
ADMITTED: "Bevestigd",
}
event = models.ForeignKey(Event, on_delete=models.CASCADE)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)

View file

@ -2,16 +2,9 @@
{% block content %}
<ul>
{% for event in events %}
<li id="{{event.id}}">{{ event.date }} {{ event.time_str }}
<form action="{% url 'events:register' event.id %}" method="post">
<ul>
{% csrf_token %}
<li>{{ event.count_admitted }}/{{ event.capacity }} bevestigd</li>
<li>{{ event.count_interested }} geïnteresseerd</li>
<li><input type="submit" value="Ik wil komen"></li>
</ul>
</form>
{% for x in events %}
<li id="{{x.event.id}}">{{ x.event.date }} {{ x.event.time_str }}
{% include "events/registrations.html" with event=x.event my_registration=x.my_registration %}
</li>
{% endfor %}
</ul>

View file

@ -0,0 +1,14 @@
<form action="{% url 'events:register' event.id %}" method="post">
{% csrf_token %}
<p>{{ event.count_admitted }}/{{ event.capacity }} bevestigd</p>
{% if event.count_interested %}
<p>{{ event.count_interested }} in wachtlijst</p>
{% endif %}
{% if not my_registration %}
<p><input type="submit" value="Ik wil komen"></p>
{% else %}
<p>Mijn status: {{ my_registration.state_str }} <input type="button" disabled="disabled" value="Uitschrijven (todo)"></input></p>
{% endif %}
</form>

View file

@ -12,7 +12,12 @@ from events.tasks import assign_reservations
def index(request):
events = Event.objects.filter(date__gte=timezone.now().date()).order_by("date")[:20]
return render(request, "events/index.html", {"events": events})
events_data = [
{ "event": event, "my_registration": event.registration_of(request.user) }
for event in events
]
return render(request, "events/index.html", {"events": events_data})
def view_score_stuff(request):
@ -26,8 +31,8 @@ def register(request, event_id):
event = get_object_or_404(Event, id=event_id)
event.eventregistration_set.create(
state=EventRegistration.INTERESTED,
event=event,
user=CustomUser.objects.get(),
state=EventRegistration.INTERESTED,
event=event,
user=request.user,
)
return HttpResponseRedirect(reverse("events:index") + f"#{event.id}")