Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rudimentary notificationprofile support #1095

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
22 changes: 6 additions & 16 deletions src/argus/htmx/notificationprofile/urls.py
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"),
]
82 changes: 82 additions & 0 deletions src/argus/htmx/notificationprofile/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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:
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:
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
2 changes: 1 addition & 1 deletion src/argus/htmx/templates/htmx/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<a href="{% url 'htmx:timeslot-list' %}">Timeslots</a>
</li>
<li>
<a href="{% url 'htmx:notificationprofile-placeholder' %}">Profiles</a>
<a href="{% url 'htmx:notificationprofile-list' %}">Profiles</a>
</li>
{% endblock navlinks %}
</ul>
Expand Down
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: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>
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 %}
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 %}
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 %}
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 %}
Loading