Skip to content

Commit

Permalink
add visibility conditions to the announcement messages and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kimakan committed Sep 20, 2023
1 parent 40de99b commit b4fd9d3
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
11 changes: 10 additions & 1 deletion daiquiri/contact/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from django import forms
from django.contrib import admin

from .models import ContactMessage, AnnouncementMessage
from .models import ContactMessage, AnnouncementMessage, MessageFilter


class AnnouncementMessageAdminForm(forms.ModelForm):
visibility_filter = forms.ChoiceField(choices=MessageFilter().CHOICES)


@admin.action(description="Make selected messages visible")
def make_visible(modeladmin, request, queryset):
queryset.update(visible=True)


@admin.action(description="Make selected messages invisible")
def make_invisible(modeladmin, request, queryset):
queryset.update(visible=False)
Expand All @@ -16,7 +22,10 @@ class ContactMessageAdmin(admin.ModelAdmin):
search_fields = ("subject", "email", "author", "status", "user__username")
list_display = ("subject", "email", "author", "status")


class AnnouncementMessageAdmin(admin.ModelAdmin):
form = AnnouncementMessageAdminForm

search_fields = ("title", "announcement")
list_display = ("title", "visible", "announcement", "announcement_type", "updated")
actions = [make_visible, make_invisible]
Expand Down
33 changes: 31 additions & 2 deletions daiquiri/contact/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from rest_framework import filters

from .models import ContactMessage


class SpamBackend(filters.BaseFilterBackend):


def filter_queryset(self, request, queryset, view):

from daiquiri.contact.models import ContactMessage

spam = request.GET.get('spam')

if spam is not None:
Expand All @@ -15,3 +17,30 @@ def filter_queryset(self, request, queryset, view):
queryset = queryset.exclude(status=ContactMessage.STATUS_SPAM)

return queryset



class DefaultMessageFilter(object):

CHOICES = (
("no_filter", "Show to all users"),
("logged_in_users", "Show to the logged in users only"),
("user_has_not_consented", "Show to user who has not consented yet"),
)

def no_filter(request):
return True

def logged_in_users(request):
if request.user.is_authenticated:
return True
return False

def user_has_not_consented(request):
if request.user.is_authenticated:
if not request.user.profile.consent:
return True
return False



13 changes: 7 additions & 6 deletions daiquiri/contact/migrations/0007_announcementmessage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.4 on 2023-08-08 09:57
# Generated by Django 4.2.5 on 2023-09-20 07:37

from django.db import migrations, models

Expand All @@ -14,11 +14,12 @@ class Migration(migrations.Migration):
name='AnnouncementMessage',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(blank=True, max_length=100, null=True)),
('announcement', models.TextField()),
('announcement_type', models.CharField(choices=[('info', 'info'), ('warning', 'warning'), ('danger', 'urgent')], default='info', max_length=8)),
('updated', models.DateTimeField(auto_now=True)),
('visible', models.BooleanField(default=False)),
('title', models.CharField(blank=True, max_length=100, null=True, verbose_name='Title')),
('announcement', models.TextField(verbose_name='Announcement')),
('announcement_type', models.CharField(choices=[('info', 'info'), ('warning', 'warning'), ('danger', 'urgent')], default='info', max_length=8, verbose_name='Announcement type')),
('updated', models.DateTimeField(auto_now=True, verbose_name='Updated')),
('visible', models.BooleanField(default=False, verbose_name='Visible')),
('visibility_filter', models.CharField(default='no_filter', max_length=50, verbose_name='Visibility filter')),
],
options={
'verbose_name': 'Announcement message',
Expand Down
45 changes: 38 additions & 7 deletions daiquiri/contact/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _

from daiquiri.core.utils import import_class


class ContactMessage(models.Model):


STATUS_ACTIVE = 'ACTIVE'
STATUS_CLOSED = 'CLOSED'
STATUS_SPAM = 'SPAM'
Expand Down Expand Up @@ -46,24 +50,48 @@ def set_status_spam(self):
self.save()



def MessageFilter():
return import_class(settings.ANNOUNCEMENT_MESSAGE_FILTER)

class AnnouncementMessage(models.Model):

# alert types must correspond to the bootstrap alert types
ALERT_TYPE_INFO = "info"
ALERT_TYPE_WARNING = "warning"
ALERT_TYPE_ERROR = "danger"
ALERT_TYPE_URGENT = "danger"

ANNOUNCEMENT_TYPE_CHOICES = (
(ALERT_TYPE_INFO, "info"),
(ALERT_TYPE_WARNING, "warning"),
(ALERT_TYPE_ERROR, "urgent"),
(ALERT_TYPE_URGENT, "urgent"),
)

title = models.CharField(max_length=100, blank=True, null=True)
announcement = models.TextField()
announcement_type = models.CharField(max_length=8, choices=ANNOUNCEMENT_TYPE_CHOICES, default=ALERT_TYPE_INFO)
updated = models.DateTimeField(auto_now=True)
visible = models.BooleanField(default=False)
title = models.CharField(
max_length=100, blank=True, null=True,
verbose_name=_("Title")
)
announcement = models.TextField(
verbose_name=_("Announcement")
)
announcement_type = models.CharField(
max_length=8,
choices=ANNOUNCEMENT_TYPE_CHOICES,
default=ALERT_TYPE_INFO,
verbose_name=_("Announcement type")
)
updated = models.DateTimeField(
auto_now=True,
verbose_name=_("Updated")
)
visible = models.BooleanField(
default=False,
verbose_name=_("Visible")
)
visibility_filter = models.CharField(
max_length=50, default="no_filter",
verbose_name="Visibility filter"
)

class Meta:
ordering = ('-updated', 'title')
Expand All @@ -74,3 +102,6 @@ class Meta:
def __str__(self):
return f"{self.title}: '{self.announcement}'"

def get_filter(self):
return getattr(MessageFilter(), str(self.visibility_filter))

1 change: 1 addition & 0 deletions daiquiri/contact/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANNOUNCEMENT_MESSAGE_FILTER = 'daiquiri.contact.filters.DefaultMessageFilter'
2 changes: 1 addition & 1 deletion daiquiri/contact/templates/contact/announcements.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% for msg in announcements %}
<div class="alert alert-{{ msg.announcement_type }}"
role="alert">
{{ msg.announcement }}
{{ msg.announcement | safe }}
</div>
{% endfor %}
{% endif %}
6 changes: 4 additions & 2 deletions daiquiri/contact/templatetags/announcement_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
register = template.Library()


@register.inclusion_tag("contact/announcements.html")
def show_announcements():
@register.inclusion_tag("contact/announcements.html", takes_context=True)
def show_announcements(context):
request = context["request"]
announcements = AnnouncementMessage.objects.filter(visible=True)
announcements = [msg for msg in announcements if msg.get_filter()(request) is True]
return {
"announcements": announcements,
}

0 comments on commit b4fd9d3

Please sign in to comment.