Skip to content

Commit

Permalink
toggling favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
ZendaInnocent committed Nov 4, 2023
1 parent 3414a78 commit 3872b6f
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 216 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ django-allauth = "*"
django-rosetta = "*"
pillow = "*"
django-phonenumber-field = {version = "*", extras = ["phonenumbers"]}
fontawesomefree = "*"

[dev-packages]
django-debug-toolbar = "*"
Expand Down
439 changes: 223 additions & 216 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'allauth.account',
'allauth.socialaccount',
'phonenumber_field',
'fontawesomefree',
]

MIDDLEWARE = [
Expand Down
4 changes: 4 additions & 0 deletions pim/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name) + '-' + get_random_string(8)
return super().save(*args, **kwargs)

def toggle_favorite(self):
self.is_favorite = not self.is_favorite
self.save()
3 changes: 3 additions & 0 deletions pim/contacts/templates/contacts/contact_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<div class="col-8">
<h2>
{{ contact.name | capfirst }}
<span hx-post="{% url 'contacts:contact-toggle-favorite' contact.slug %}">
{% include 'contacts/favorite.html' %}
</span>
</h2>
{% if contact.title %}
<h4>{{ contact.title }}</h4>
Expand Down
5 changes: 5 additions & 0 deletions pim/contacts/templates/contacts/favorite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if contact.is_favorite %}
<i class="fas fa-star" style="color: #efc701;"></i>
{% else %}
<i class="far fa-star"></i>
{% endif %}
5 changes: 5 additions & 0 deletions pim/contacts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
path('<slug:slug>/', views.contact_detail, name='contact-detail'),
path('<slug:slug>/edit/', views.contact_edit, name='contact-edit'),
path('<slug:slug>/delete/', views.contact_delete, name='contact-delete'),
path(
'<slug:slug>/toggle-favorite/',
views.toggle_favorite,
name='contact-toggle-favorite',
),
]
7 changes: 7 additions & 0 deletions pim/contacts/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpRequest
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
from django.urls import reverse_lazy
from django.views import generic
Expand Down Expand Up @@ -69,3 +70,9 @@ def contact_search(request: HttpRequest) -> TemplateResponse:
return TemplateResponse(
request, 'contacts/contact_list.html', {'contacts': contacts}
)


def toggle_favorite(request: HttpRequest, slug: str) -> TemplateResponse:
contact: Contact = get_object_or_404(Contact, slug=slug)
contact.toggle_favorite()
return TemplateResponse(request, 'contacts/favorite.html', {'contact': contact})
2 changes: 2 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
{% endblock title %}
</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link href="{% static 'fontawesomefree/css/fontawesome.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'fontawesomefree/css/all.css' %}" rel="stylesheet" type="text/css">
{% block extra_head %} {% endblock extra_head %}
</head>

Expand Down

0 comments on commit 3872b6f

Please sign in to comment.