Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Bug 1538723, add banners django model and register with django admin (#…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
35 changes: 35 additions & 0 deletions kuma/banners/admin.py
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'
29 changes: 29 additions & 0 deletions kuma/banners/migrations/0001_initial.py
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.
30 changes: 30 additions & 0 deletions kuma/banners/models.py
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 added kuma/banners/tests/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions kuma/banners/tests/test_models.py
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

0 comments on commit bc11177

Please sign in to comment.