bug 572596, bug 562943 adds memoized site notice

This commit is contained in:
Dave Dash 2010-06-18 10:13:35 -07:00
Родитель 8676941e0d
Коммит 8f3c496470
5 изменённых файлов: 65 добавлений и 1 удалений

Просмотреть файл

@ -16,6 +16,18 @@ import tower
import amo
from . import urlresolvers
from .helpers import urlparams
from zadmin.models import get_config
class AdminMessageMiddleware(object):
"""
1. Check db for an admin message.
2. Store it in request.
"""
def process_request(self, request):
# Get the value from Config if it exists
request.ADMIN_MESSAGE = get_config('site_notice')
class LocaleAndAppURLMiddleware(object):

Просмотреть файл

@ -1,6 +1,11 @@
from django import test
from test_utils import TestCase
from nose.tools import eq_
from pyquery import PyQuery as pq
from amo.urlresolvers import reverse
from zadmin.models import Config, _config_cache
def test_no_vary_cookie():
@ -14,7 +19,10 @@ def test_no_vary_cookie():
def test_redirect_with_unicode_get():
response = test.Client().get('/da/firefox/addon/5457?from=/da/firefox/addon/5457%3Fadvancedsearch%3D1&lang=ja&utm_source=Google+%E3%83%90%E3%82%BA&utm_medium=twitter&utm_term=Google+%E3%83%90%E3%82%BA')
response = test.Client().get('/da/firefox/addon/5457?from=/da/firefox/'
'addon/5457%3Fadvancedsearch%3D1&lang=ja&utm_source=Google+%E3'
'%83%90%E3%82%BA&utm_medium=twitter&utm_term=Google+%E3%83%90%'
'E3%82%BA')
eq_(response.status_code, 301)
@ -22,3 +30,26 @@ def test_trailing_slash_middleware():
response = test.Client().get(u'/en-US/firefox/about/?xxx=\xc3')
eq_(response.status_code, 301)
assert response['Location'].endswith('/en-US/firefox/about?xxx=%C3%83')
class AdminMessageMiddlewareTest(TestCase):
def test_message(self):
c = Config()
c.key = 'site_notice'
c.value = 'ET Sighted.'
c.save()
if ('site_notice',) in _config_cache:
del _config_cache[('site_notice',)]
r = self.client.get(reverse('home'), follow=True)
doc = pq(r.content)
eq_(doc('#site-notice').text(), 'ET Sighted.')
c.delete()
del _config_cache[('site_notice',)]
r = self.client.get(reverse('home'), follow=True)
doc = pq(r.content)
eq_(len(doc('#site-notice')), 0)

Просмотреть файл

@ -1,6 +1,9 @@
import json
from django.db import models
from django.utils.functional import memoize
_config_cache = {}
class Config(models.Model):
@ -14,3 +17,13 @@ class Config(models.Model):
@property
def json(self):
return json.loads(self.value)
def get_config(conf):
try:
c = Config.objects.get(key=conf)
return c.value
except Config.DoesNotExist:
return
get_config = memoize(get_config, _config_cache, 1)

Просмотреть файл

@ -197,6 +197,8 @@ MIDDLEWARE_CLASSES = (
'cake.middleware.CakeCookieMiddleware',
'cake.middleware.CookieCleaningMiddleware',
'amo.middleware.AdminMessageMiddleware',
# This should come after authentication middleware
'access.middleware.ACLMiddleware',
)

Просмотреть файл

@ -29,6 +29,12 @@
data-appid="{{ request.APP.id }}"
data-anonymous="{{ request.user.is_authenticated() and 'false' or 'true' }}"
data-media-url="{{ MEDIA_URL }}">
{% if request.ADMIN_MESSAGE %}
<div id="site-notice">
<p>{{ request.ADMIN_MESSAGE }}</p>
</div>
{% endif %}
<div class="section">
{% block site_header %}