-
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.
Add rudimentary notificationprofile support
Ugly but functional. --------- Co-authored-by: podliashanyk <[email protected]>
- Loading branch information
1 parent
c259f98
commit 58f1535
Showing
10 changed files
with
286 additions
and
17 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog.d/+add-rudimentary-notificationprofiles-page.added.md
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 notification profile 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
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>NOTIFICATION PROFILES PLACEHOLDER</h1> | ||
{% endblock main %} | ||
""" | ||
) | ||
context = RequestContext(request) | ||
return HttpResponse(template.render(context)) | ||
from . import views | ||
|
||
|
||
app_name = "htmx" | ||
urlpatterns = [ | ||
path("", placeholder, name="notificationprofile-placeholder"), | ||
path("", views.NotificationProfileListView.as_view(), name="notificationprofile-list"), | ||
path("create/", views.NotificationProfileCreateView.as_view(), name="notificationprofile-create"), | ||
path("<pk>/", views.NotificationProfileDetailView.as_view(), name="notificationprofile-detail"), | ||
path("<pk>/update/", views.NotificationProfileUpdateView.as_view(), name="notificationprofile-update"), | ||
path("<pk>/delete/", views.NotificationProfileDeleteView.as_view(), name="notificationprofile-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,92 @@ | ||
""" | ||
Everything needed python-wise to CRUD notificationprofiles | ||
See https://ccbv.co.uk/ to grok class-based views. | ||
""" | ||
|
||
from django import forms | ||
from django.urls import reverse | ||
from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView | ||
|
||
from argus.notificationprofile.models import NotificationProfile, Timeslot, Filter, DestinationConfig | ||
|
||
|
||
class NotificationProfileForm(forms.ModelForm): | ||
class Meta: | ||
model = NotificationProfile | ||
fields = ["name", "timeslot", "filters", "active", "destinations"] | ||
|
||
def __init__(self, *args, **kwargs): | ||
user = kwargs.pop("user") | ||
super().__init__(*args, **kwargs) | ||
self.fields["timeslot"].queryset = Timeslot.objects.filter(user=user) | ||
self.fields["filters"].queryset = Filter.objects.filter(user=user) | ||
self.fields["destinations"].queryset = DestinationConfig.objects.filter(user=user) | ||
|
||
|
||
class NotificationProfileMixin: | ||
"Common functionality for all views" | ||
|
||
model = NotificationProfile | ||
|
||
def get_queryset(self): | ||
qs = ( | ||
super() | ||
.get_queryset() | ||
.select_related("timeslot") | ||
.prefetch_related( | ||
"filters", | ||
"destinations", | ||
) | ||
) | ||
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/notificationprofile" | ||
self.model._meta.model_name = "notificationprofile" | ||
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:notificationprofile-list") | ||
|
||
|
||
class ChangeMixin: | ||
"Common functionality for create and update views" | ||
|
||
form_class = NotificationProfileForm | ||
|
||
def get_form_kwargs(self): | ||
kwargs = super().get_form_kwargs() | ||
kwargs["user"] = self.request.user | ||
return kwargs | ||
|
||
def form_valid(self, form): | ||
self.object = form.save(commit=False) | ||
self.object.user = self.request.user | ||
self.object.save() | ||
return super().form_valid(form) | ||
|
||
|
||
class NotificationProfileListView(NotificationProfileMixin, ListView): | ||
pass | ||
|
||
|
||
class NotificationProfileDetailView(NotificationProfileMixin, DetailView): | ||
pass | ||
|
||
|
||
class NotificationProfileCreateView(ChangeMixin, NotificationProfileMixin, CreateView): | ||
pass | ||
|
||
|
||
class NotificationProfileUpdateView(ChangeMixin, NotificationProfileMixin, UpdateView): | ||
pass | ||
|
||
|
||
class NotificationProfileDeleteView(NotificationProfileMixin, DeleteView): | ||
pass |
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
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
25 changes: 25 additions & 0 deletions
25
src/argus/htmx/templates/htmx/notificationprofile/_notificationprofile_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,25 @@ | ||
<section> | ||
<h2>{{ object.name|default:object.timeslot.name }}</h2> | ||
<div> | ||
<p>Active? {{ object.active }}</p> | ||
<p>Timeslot: {{ object.timeslot }}</p> | ||
<p> | ||
Filters: | ||
{% for filter_obj in object.filters.all %}{{ filter_obj }}{% endfor %} | ||
</p> | ||
<p> | ||
Destinations: | ||
{% for destination in object.destinations.all %}{{ destination }}{% endfor %} | ||
</p> | ||
{% if show_buttons %} | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-update" pk=object.pk %}">Update</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-delete" pk=object.pk %}">Delete</a> | ||
</p> | ||
{% endif %} | ||
</div> | ||
</section> |
9 changes: 9 additions & 0 deletions
9
src/argus/htmx/templates/htmx/notificationprofile/notificationprofile_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 %} |
6 changes: 6 additions & 0 deletions
6
src/argus/htmx/templates/htmx/notificationprofile/notificationprofile_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,6 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
{% with show_buttons=True %} | ||
{% include "./_notificationprofile_detail.html" %} | ||
{% endwith %} | ||
{% endblock main %} |
8 changes: 8 additions & 0 deletions
8
src/argus/htmx/templates/htmx/notificationprofile/notificationprofile_form.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,8 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form.as_div }} | ||
<input class="btn btn-primary" type="submit" value="Save"> | ||
</form> | ||
{% endblock main %} |
24 changes: 24 additions & 0 deletions
24
src/argus/htmx/templates/htmx/notificationprofile/notificationprofile_list.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,24 @@ | ||
{% extends "htmx/base.html" %} | ||
{% block main %} | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-create" %}">Create</a> | ||
</p> | ||
{% for object in object_list %} | ||
<div> | ||
{% include "./_notificationprofile_detail.html" %} | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-detail" pk=object.pk %}">View</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-update" pk=object.pk %}">Update</a> | ||
</p> | ||
<p> | ||
<a class="btn btn-primary" | ||
href="{% url "htmx:notificationprofile-delete" pk=object.pk %}">Delete</a> | ||
</p> | ||
</div> | ||
{% endfor %} | ||
{% endblock main %} |