Skip to content

Commit

Permalink
load contacts when contact is created or updated
Browse files Browse the repository at this point in the history
closes #23
  • Loading branch information
ZendaInnocent committed Nov 6, 2023
1 parent e858c93 commit f2298a6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
3 changes: 3 additions & 0 deletions pim/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Contact(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)

class Meta:
ordering = ('name',)

def __str__(self):
return self.name

Expand Down
13 changes: 6 additions & 7 deletions pim/contacts/templates/contacts/contact_form.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{% load crispy_forms_tags %}
{% load i18n %}

<h2>
{% blocktrans %}
{{ title }} Contact
{% endblocktrans %}
</h2>

{% if object %}
<form method="post" enctype="multipart/form-data" hx-post="{% url 'contacts:contact-edit' object.slug %}"
hx-target="#detail">
{% else %}
<form method="post" enctype="multipart/form-data" hx-post="{% url 'contacts:contact-add' %}" hx-target="body">
<form method="post" enctype="multipart/form-data" hx-post="{% url 'contacts:contact-add' %}" target="#detail">
{% endif %}
<h2>
{% blocktrans %}
{{ title }} Contact
{% endblocktrans %}
</h2>
{% csrf_token %}
{{ form | crispy }}
<button class="btn btn-primary" type="submit">
Expand Down
4 changes: 3 additions & 1 deletion pim/contacts/templates/contacts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ <h3>{% trans 'Contacts' %}</h3>
<div class="spinner-border text-primary htmx-indicator" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<ul class="list-group" id="contacts" hx-get="{% url 'contacts:contact-list' %}" hx-trigger="load">
<ul class="list-group" id="contacts" hx-get="{% url 'contacts:contact-list' %}"
hx-trigger="load, loadcontacts from:body">
</ul>
</div>

<div class="col-md-8 px-4" id="detail">
<h3 class="my-3">No contact selected</h3>
<div class="row mt-4 mt-md-0">
<div class="col-md-4">
<div class="bg-secondary h-100 w-100 rounded"></div>
Expand Down
18 changes: 16 additions & 2 deletions pim/contacts/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Any

from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.db.models import Q
from django.http import HttpRequest
from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
from django.urls import reverse_lazy
Expand Down Expand Up @@ -29,12 +32,16 @@ def get_queryset(self):
class ContactCreateView(LoginRequiredMixin, generic.CreateView):
model = Contact
form_class = ContactForm
success_url = reverse_lazy('contacts:index')
extra_context = {'title': 'Add'}

def get_initial(self):
return {'user': self.request.user}

def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
response = super().dispatch(request, *args, **kwargs)
response['HX-Trigger'] = 'loadcontacts'
return response


contact_add = ContactCreateView.as_view()

Expand All @@ -46,6 +53,11 @@ class ContactDetailView(LoginRequiredMixin, UserPassesTestMixin, generic.DetailV
def test_func(self) -> bool | None:
return self.get_object().user == self.request.user

def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
response = super().dispatch(request, *args, **kwargs)
response['HX-Trigger'] = 'loadcontacts'
return response


contact_detail = ContactDetailView.as_view()

Expand Down Expand Up @@ -92,4 +104,6 @@ def toggle_favorite(request: HttpRequest, slug: str) -> TemplateResponse:
contact: Contact = get_object_or_404(Contact, slug=slug, user=request.user)
contact.toggle_favorite()
context = {'contact': contact}
return TemplateResponse(request, 'contacts/favorite.html', context)
response = TemplateResponse(request, 'contacts/favorite.html', context)
response['HX-Trigger'] = 'loadcontacts'
return response
18 changes: 9 additions & 9 deletions static/js/django-debug-toolbar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
if (typeof window.htmx !== "undefined") {
htmx.on("htmx:afterSettle", function (detail) {
if (
typeof window.djdt !== "undefined"
&& detail.target instanceof HTMLBodyElement
) {
djdt.show_toolbar();
}
});
if (typeof window.htmx !== 'undefined') {
htmx.on('htmx:afterSettle', function (detail) {
if (
typeof window.djdt !== 'undefined' &&
detail.target instanceof HTMLBodyElement
) {
djdt.show_toolbar();
}
});
}

0 comments on commit f2298a6

Please sign in to comment.