Add alerts model and UI (#252)

This commit is contained in:
Paul Hallett 2016-08-13 08:24:56 +01:00 committed by GitHub
parent a706bfbd9a
commit 85dd999396
9 changed files with 73 additions and 6 deletions

0
alerts/__init__.py Normal file
View file

5
alerts/admin.py Normal file
View file

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Alert
admin.site.register(Alert)

View file

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Alert',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('active', models.BooleanField()),
('message', models.CharField(max_length=255)),
],
options={
},
bases=(models.Model,),
),
]

View file

7
alerts/models.py Normal file
View file

@ -0,0 +1,7 @@
from django.db import models
class Alert(models.Model):
active = models.BooleanField()
message = models.CharField(max_length=255)

View file

@ -118,6 +118,7 @@ CUSTOM_APPS = (
'pokemon', 'pokemon',
'pokemon_v2', 'pokemon_v2',
'hits', 'hits',
'alerts',
) )
INSTALLED_APPS = ( INSTALLED_APPS = (

View file

@ -7,6 +7,7 @@ from django.shortcuts import render_to_response, redirect
from django.template import RequestContext from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from alerts.models import Alert
from hits.models import ResourceView from hits.models import ResourceView
import stripe import stripe
@ -24,11 +25,19 @@ def about(request):
average_day = int(round(total_views / ResourceView.objects.count())) average_day = int(round(total_views / ResourceView.objects.count()))
cache.set('average_day', average_day) cache.set('average_day', average_day)
alert = cache.get('alert')
if not alert:
active_alerts = Alert.objects.filter(active=True)
if active_alerts:
cache.set('alert', active_alerts.first())
alert = active_alerts.first()
return render_to_response( return render_to_response(
'pages/about.html', 'pages/about.html',
{ {
'total': total_views, 'total': total_views,
'average_day': average_day, 'average_day': average_day,
'alert': alert
}, },
context_instance=RequestContext(request) context_instance=RequestContext(request)
) )
@ -44,11 +53,19 @@ def home(request):
stripe_key = settings.STRIPE_KEYS['publishable'] stripe_key = settings.STRIPE_KEYS['publishable']
alert = cache.get('alert')
if not alert:
active_alerts = Alert.objects.filter(active=True)
if active_alerts:
cache.set('alert', active_alerts.first())
alert = active_alerts.first()
return render_to_response( return render_to_response(
'pages/home.html', 'pages/home.html',
{ {
'total_views': total_views, 'total_views': total_views,
'stripe_key': stripe_key 'stripe_key': stripe_key,
'alert': alert
}, },
context_instance=RequestContext(request) context_instance=RequestContext(request)
) )

View file

@ -33,6 +33,19 @@
<h1>The RESTful Pokémon Data API</h1> <h1>The RESTful Pokémon Data API</h1>
</div> </div>
{% if alert %}
<div class="container">
<div class="row" style="padding-top:20px;text-align:center;">
<div class="col-md-2">
</div>
<div class="col-md-8">
<div class="alert alert-danger" role="alert">{{ alert.message|safe }}</div>
</div>
<div class="col-md-2">
</div>
</div>
</div>
{% endif %}
<div class="container"> <div class="container">

View file

@ -33,19 +33,19 @@
<h4>Over <span class="count">{{ total_views|intcomma }}</span> API calls received!</h4> <h4>Over <span class="count">{{ total_views|intcomma }}</span> API calls received!</h4>
</div> </div>
{% if alert %}
<div class="container"> <div class="container">
{% if update %} <div class="row" style="padding-top:20px;text-align:center;">
<div class="row" style="padding-top:80px;text-align:center;">
<div class="col-md-2"> <div class="col-md-2">
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<div class="alert alert-success">{{update.content|safe}}</div> <div class="alert alert-danger" role="alert">{{ alert.message|safe }}</div>
</div> </div>
<div class="col-md-2"> <div class="col-md-2">
</div> </div>
</div><!-- /.header row --> </div>
{% endif %}
</div> </div>
{% endif %}
<div class="cta"> <div class="cta">
<p>Finally; all the Pokémon data you'll ever need, in one place,<br /> and easily accessible through a modern RESTful API.</p> <p>Finally; all the Pokémon data you'll ever need, in one place,<br /> and easily accessible through a modern RESTful API.</p>