From f1ca816d6a3c478d7d0f8018a57aa6b8a0762201 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 14 Apr 2022 14:17:25 +0200 Subject: [PATCH] Banners only for authenticated users. --- src/annoy/admin.py | 2 +- .../0012_banner_only_authenticated.py | 18 ++++++++++++++++++ src/annoy/models.py | 10 +++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/annoy/migrations/0012_banner_only_authenticated.py diff --git a/src/annoy/admin.py b/src/annoy/admin.py index 8255e96a5..8470a1203 100644 --- a/src/annoy/admin.py +++ b/src/annoy/admin.py @@ -9,7 +9,7 @@ class BannerAdmin(TranslationAdmin): 'place', 'text', 'text_color', 'background_color', 'priority', 'since', 'until', - 'show_members', 'staff_preview'] + 'show_members', 'staff_preview', 'only_authenticated'] admin.site.register(models.Banner, BannerAdmin) diff --git a/src/annoy/migrations/0012_banner_only_authenticated.py b/src/annoy/migrations/0012_banner_only_authenticated.py new file mode 100644 index 000000000..53584bdf0 --- /dev/null +++ b/src/annoy/migrations/0012_banner_only_authenticated.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.27 on 2022-04-14 12:10 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('annoy', '0011_auto_20210610_1437'), + ] + + operations = [ + migrations.AddField( + model_name='banner', + name='only_authenticated', + field=models.BooleanField(default=False, verbose_name='only for authenticated users'), + ), + ] diff --git a/src/annoy/models.py b/src/annoy/models.py index 55bb2ac38..5a613f1e0 100644 --- a/src/annoy/models.py +++ b/src/annoy/models.py @@ -34,6 +34,7 @@ class Banner(models.Model): until = models.DateTimeField(_('until'), null=True, blank=True) show_members = models.BooleanField(_('show members'), default=False) staff_preview = models.BooleanField(_('staff preview'), default=False) + only_authenticated = models.BooleanField(_('only for authenticated users'), default=False) class Meta: verbose_name = _('banner') @@ -65,12 +66,15 @@ class Banner(models.Model): until__lt=n ).order_by('-priority', '?') + if not request.user.is_authenticated: + banners = banners.filter(only_authenticated=False) + if not request.user.is_staff: banners = banners.filter(staff_preview=False) - if request: - if Membership.is_active_for(request.user): - banners = banners.filter(show_members=True) + if Membership.is_active_for(request.user): + banners = banners.filter(show_members=True) + return banners -- 2.20.1