-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
175 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Replaced the placeholder time-slots page with a very ugly but functional one. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/argus/htmx/templates/htmx/timeslot/_timeslot_detail.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<section> | ||
<h2>{{ object.name }}</h2> | ||
<div> | ||
{% for time_recurrence in object.time_recurrences.all %}<p>{{ time_recurrence }}</p>{% endfor %} | ||
{% if show_buttons %} | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:timeslot-update" pk=object.pk %}">Update</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:timeslot-delete" pk=object.pk %}">Delete</a> | ||
</p> | ||
{% endif %} | ||
</div> | ||
</section> |
9 changes: 9 additions & 0 deletions
9
src/argus/htmx/templates/htmx/timeslot/timeslot_confirm_delete.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<form method="post"> | ||
{% csrf_token %} | ||
<p>Are you sure you want to delete "{{ object }}"?</p> | ||
{{ form }} | ||
<input class="btn btn-primary" type="submit" value="Confirm"> | ||
</form> | ||
{% endblock main %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
{% with show_buttons=True %} | ||
{% include "./_timeslot_detail.html" %} | ||
{% endwith %} | ||
{% endblock main %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form.as_div }} | ||
{{ formset.as_div }} | ||
<input class="btn btn-primary" type="submit" value="Save"> | ||
</form> | ||
{% endblock main %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<p> | ||
<a class="btn btn-primary" href="{% url "htmx:timeslot-create" %}">Create</a> | ||
</p> | ||
{% for object in object_list %} | ||
<div> | ||
{% include "./_timeslot_detail.html" %} | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:timeslot-detail" pk=object.pk %}">View</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:timeslot-update" pk=object.pk %}">Update</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:timeslot-delete" pk=object.pk %}">Delete</a> | ||
</p> | ||
</div> | ||
{% endfor %} | ||
{% endblock main %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,13 @@ | ||
from django.http import HttpResponse | ||
from django.template import Template, RequestContext | ||
from django.urls import path | ||
from django.views.decorators.http import require_GET | ||
|
||
|
||
@require_GET | ||
def placeholder(request): | ||
template = Template( | ||
"""{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<h1>TIMESLOT PLACEHOLDER</h1> | ||
{% endblock main %} | ||
""" | ||
) | ||
context = RequestContext(request) | ||
return HttpResponse(template.render(context)) | ||
from . import views | ||
|
||
|
||
app_name = "htmx" | ||
urlpatterns = [ | ||
path("", placeholder, name="timeslot-placeholder"), | ||
path("", views.TimeslotListView.as_view(), name="timeslot-list"), | ||
path("create/", views.TimeslotCreateView.as_view(), name="timeslot-create"), | ||
path("<pk>/", views.TimeslotDetailView.as_view(), name="timeslot-detail"), | ||
path("<pk>/update/", views.TimeslotUpdateView.as_view(), name="timeslot-update"), | ||
path("<pk>/delete/", views.TimeslotDeleteView.as_view(), name="timeslot-delete"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from typing import Optional | ||
|
||
from django import forms | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView | ||
|
||
from argus.notificationprofile.models import Timeslot, TimeRecurrence | ||
|
||
|
||
class TimeRecurrenceForm(forms.ModelForm): | ||
class Meta: | ||
model = TimeRecurrence | ||
exclude = ["timeslot"] | ||
|
||
|
||
def make_timerecurrence_formset(data: Optional[dict] = None, timeslot: Optional[Timeslot] = None): | ||
extra = 0 if not timeslot else 1 | ||
TimeRecurrenceFormSet = forms.inlineformset_factory( | ||
Timeslot, TimeRecurrence, fields="__all__", extra=extra, can_delete=False, min_num=1 | ||
) | ||
return TimeRecurrenceFormSet(data=data, instance=timeslot) | ||
|
||
|
||
class TimeslotMixin: | ||
model = Timeslot | ||
|
||
def get_queryset(self): | ||
qs = super().get_queryset().prefetch_related("time_recurrences") | ||
return qs.filter(user_id=self.request.user.id) | ||
|
||
def get_template_names(self): | ||
orig_app_label = self.model._meta.app_label | ||
orig_model_name = self.model._meta.model_name | ||
self.model._meta.app_label = "htmx/timeslot" | ||
self.model._meta.model_name = "timeslot" | ||
templates = super().get_template_names() | ||
self.model._meta.app_label = orig_app_label | ||
self.model._meta.model_name = orig_model_name | ||
return templates | ||
|
||
def get_success_url(self): | ||
return reverse("htmx:timeslot-list") | ||
|
||
|
||
class FormsetMixin: | ||
def post(self, request, *args, **kwargs): | ||
form = self.get_form() | ||
formset = make_timerecurrence_formset(data=request.POST, timeslot=self.object) | ||
if form.is_valid() and formset.is_valid(): | ||
return self.form_valid(form, formset) | ||
else: | ||
return self.form_invalid(form, formset) | ||
|
||
def form_invalid(self, form, formset): | ||
return self.render_to_response(self.get_context_data(form=form, formset=formset)) | ||
|
||
def form_valid(self, form, formset): | ||
self.object = form.save(commit=False) | ||
self.object.user = self.request.user | ||
self.object.save() | ||
trs = formset.save(commit=False) | ||
for tr in trs: | ||
tr.timeslot = self.object | ||
tr.save() | ||
return HttpResponseRedirect(self.get_success_url()) | ||
|
||
|
||
class TimeslotListView(TimeslotMixin, ListView): | ||
pass | ||
|
||
|
||
class TimeslotDetailView(TimeslotMixin, DetailView): | ||
pass | ||
|
||
|
||
class TimeslotCreateView(FormsetMixin, TimeslotMixin, CreateView): | ||
fields = ["name"] | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
context["formset"] = make_timerecurrence_formset() | ||
return context | ||
|
||
def post(self, request, *args, **kwargs): | ||
self.object = None | ||
return super().post(request, *args, **kwargs) | ||
|
||
|
||
class TimeslotUpdateView(FormsetMixin, TimeslotMixin, UpdateView): | ||
fields = ["name"] | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
context["formset"] = make_timerecurrence_formset(timeslot=self.object) | ||
return context | ||
|
||
def post(self, request, *args, **kwargs): | ||
self.object = self.get_object() | ||
return super().post(request, *args, **kwargs) | ||
|
||
|
||
class TimeslotDeleteView(TimeslotMixin, DeleteView): | ||
pass |