This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1538723, add banners django model and register with django admin (#…
…5314) * Bug 1538723, add banners django model and register with django admin * Changes based on review feedback
- Loading branch information
Schalk Neethling
authored
Mar 27, 2019
1 parent
dc7097a
commit bc11177
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.contrib import admin | ||
|
||
from .models import Banner | ||
|
||
|
||
@admin.register(Banner) | ||
class BannerAdmin(admin.ModelAdmin): | ||
actions = ['activate_all', 'deactivate_all'] | ||
list_display = ('name', 'active', 'priority') | ||
fields = ('name', 'title', 'main_copy', 'button_copy', | ||
'theme', 'active', 'priority') | ||
search_fields = ('name', 'active') | ||
ordering = ('priority',) | ||
|
||
def activate_all(self, request, queryset): | ||
rows_updated = queryset.update(active=True) | ||
if rows_updated == 1: | ||
message_bit = '1 banner was' | ||
else: | ||
message_bit = '%s banners were ' % rows_updated | ||
self.message_user(request, '%s successfully marked as active.' % message_bit) | ||
|
||
def deactivate_all(self, request, queryset): | ||
rows_updated = queryset.update(active=False) | ||
if rows_updated == 1: | ||
message_bit = '1 banner was' | ||
else: | ||
message_bit = '%s banners were ' % rows_updated | ||
self.message_user(request, '%s successfully deactived.' % message_bit) | ||
|
||
activate_all.short_description = 'Activate all selected banners' | ||
deactivate_all.short_description = 'Deactivate all selected banners' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.18 on 2019-03-26 00:57 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Banner', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=50, verbose_name='Banner Name')), | ||
('title', models.CharField(max_length=100, verbose_name='Banner Title')), | ||
('main_copy', models.TextField(max_length=200, verbose_name='Main Copy')), | ||
('button_copy', models.CharField(max_length=50, verbose_name='Button Copy')), | ||
('theme', models.CharField(choices=[(b'default', b'Default'), (b'gradient', b'Gradient'), (b'dinohead', b'Dinohead')], default=b'default', max_length=20, verbose_name='Theme')), | ||
('active', models.BooleanField(verbose_name='Activate')), | ||
('priority', models.PositiveSmallIntegerField(default=100, verbose_name='Priority (1-100)')), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models | ||
|
||
|
||
class Banner(models.Model): | ||
""" | ||
Defines the model for a single call to action banner | ||
""" | ||
THEME_DEFAULT = 'default' | ||
THEME_GRADIENT = 'gradient' | ||
THEME_DINOHEAD = 'dinohead' | ||
THEMES = ( | ||
(THEME_DEFAULT, 'Default'), | ||
(THEME_GRADIENT, 'Gradient'), | ||
(THEME_DINOHEAD, 'Dinohead') | ||
) | ||
name = models.CharField('Banner Name', max_length=50) | ||
title = models.CharField('Banner Title', max_length=100) | ||
main_copy = models.TextField('Main Copy', max_length=200) | ||
button_copy = models.CharField('Button Copy', max_length=50) | ||
theme = models.CharField('Theme', max_length=20, | ||
choices=THEMES, default=THEME_DEFAULT) | ||
active = models.BooleanField('Activate') | ||
priority = models.PositiveSmallIntegerField('Priority (1-100)', | ||
default=100) | ||
|
||
def __str__(self): | ||
return self.name |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
import pytest | ||
|
||
from ..models import Banner | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_add_new_banner(): | ||
"""Test that banner creation succeeds""" | ||
sample_banner = { | ||
"name": "GreatSuccess", | ||
"title": "Active Banner", | ||
"main_copy": "Some sample main copy", | ||
"button_copy": "Click Me!", | ||
"theme": "default", | ||
"active": True, | ||
"priority": "2" | ||
} | ||
banner = Banner.objects.create(**sample_banner) | ||
|
||
assert banner.name == "GreatSuccess" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_default_theme_set(): | ||
"""Test that theme is set to default if empty""" | ||
sample_banner = { | ||
"name": "notheme", | ||
"title": "Inactive Banner", | ||
"main_copy": "Some sample main copy", | ||
"button_copy": "Click Me!", | ||
"active": False, | ||
"priority": "1" | ||
} | ||
banner = Banner.objects.create(**sample_banner) | ||
|
||
assert banner.theme == "default" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_default_priority_set(): | ||
"""Test that priority is set to 100 if empty""" | ||
sample_banner = { | ||
"name": "nopriority", | ||
"title": "Inactive Banner", | ||
"main_copy": "Some sample main copy", | ||
"button_copy": "Click Me!", | ||
"theme": "default", | ||
"active": False | ||
} | ||
banner = Banner.objects.create(**sample_banner) | ||
|
||
assert banner.priority == 100 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_activate_banner(): | ||
"""Test changing banner state from inactive to active""" | ||
sample_banner = { | ||
"name": "inactive", | ||
"title": "Inactive Banner", | ||
"main_copy": "Some sample main copy", | ||
"button_copy": "Click Me!", | ||
"theme": "default", | ||
"active": False, | ||
"priority": "1" | ||
} | ||
banner = Banner.objects.create(**sample_banner) | ||
assert banner.active is False | ||
|
||
banner.active = True | ||
banner.save() | ||
|
||
banner2 = Banner.objects.get(pk=banner.pk) | ||
assert banner2.active |