start of 404ing some addons urls and flipping home page (bug 698494)

This commit is contained in:
Andy McKay 2011-11-02 09:21:31 -07:00
Родитель d890596208
Коммит d3e88af395
7 изменённых файлов: 78 добавлений и 10 удалений

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

@ -49,8 +49,6 @@ detail_patterns = patterns('',
urlpatterns = patterns('',
# The homepage.
url('^$', views.home, name='home'),
# Promo modules for the homepage
url('^i/promos$', views.homepage_promos, name='addons.homepage_promos'),

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

@ -9,7 +9,7 @@ import urllib
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from django.http import HttpResponsePermanentRedirect
from django.http import Http404, HttpResponsePermanentRedirect
from django.middleware import common
from django.shortcuts import redirect
from django.utils.cache import patch_vary_headers, patch_cache_control
@ -227,20 +227,36 @@ class LazyPjaxMiddleware(object):
return response
class LoginRequiredMiddleware(object):
class ViewMiddleware(object):
def get_name(self, view_func):
return '%s.%s' % (view_func.__module__, view_func.__name__)
class LoginRequiredMiddleware(ViewMiddleware):
"""
If enabled, will force a login on all requests. Unless the view
is decorated with the no_login_required decorator, or placed
in the NO_LOGIN_REQUIRED_MODULES tuple.
"""
def get_name(self, view_func):
return '%s.%s' % (view_func.__module__, view_func.__name__)
def process_view(self, request, view_func, view_args, view_kwargs):
name = self.get_name(view_func)
if (request.user.is_authenticated() or
getattr(view_func, '_no_login_required', False) or
self.get_name(view_func) in settings.NO_LOGIN_REQUIRED_MODULES):
name.startswith(settings.NO_LOGIN_REQUIRED_MODULES)):
return
return redirect(settings.LOGIN_URL)
class NoAddonsMiddleware(ViewMiddleware):
"""
If enabled will try and stop any requests to addons by 404'ing them.
Here there be dragons. Fortunately this is temporary right?
"""
def process_view(self, request, view_func, view_args, view_kwargs):
name = self.get_name(view_func)
if name.startswith(settings.NO_ADDONS_MODULES):
raise Http404

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

@ -13,7 +13,8 @@ from test_utils import RequestFactory
import amo.tests
from amo.decorators import no_login_required
from amo.middleware import LazyPjaxMiddleware, LoginRequiredMiddleware
from amo.middleware import (LazyPjaxMiddleware, LoginRequiredMiddleware,
NoAddonsMiddleware)
from amo.urlresolvers import reverse
from zadmin.models import Config, _config_cache
@ -203,3 +204,20 @@ class TestLoginRequiredMiddleware(amo.tests.TestCase):
def test_modules(self):
assert not self.process(False)
assert not self.process(True)
class TestNoAddonsMiddleware(amo.tests.TestCase):
@patch('amo.middleware.ViewMiddleware.get_name')
def process(self, name, get_name):
get_name.return_value = name
request = RequestFactory().get('/')
view = Mock()
return NoAddonsMiddleware().process_view(request, view, [], {})
@patch.object(settings, 'NO_ADDONS_MODULES',
('some.addons',))
def test_middleware(self):
self.assertRaises(http.Http404, self.process, 'some.addons')
self.assertRaises(http.Http404, self.process, 'some.addons.thingy')
assert not self.process('something.else')

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

@ -2,6 +2,7 @@ from django import test, shortcuts
from django.conf import settings
from django.core.urlresolvers import set_script_prefix
from mock import patch
from nose.tools import eq_, assert_not_equal
import test_utils
@ -159,6 +160,19 @@ class TestPrefixer:
set_script_prefix('/oremj')
eq_(urlresolvers.reverse('home'), '/oremj/en-US/firefox/')
@patch.object(settings, 'APP_PREVIEW', True)
def test_app_preview(self):
rf = test_utils.RequestFactory()
request = rf.get('/')
prefixer = urlresolvers.Prefixer(request)
eq_(prefixer.fix(prefixer.shortened_path), '/en-US/apps/')
@patch.object(settings, 'APP_PREVIEW', True)
def test_home(self):
client = test.Client()
client.get('/')
eq_(urlresolvers.reverse('home'), '/en-US/apps/')
def test_redirect():
"""Make sure django.shortcuts.redirect uses our reverse."""

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

@ -133,7 +133,11 @@ class Prefixer(object):
locale = self.locale if self.locale else self.get_language()
url_parts.append(locale)
if path.partition('/')[0] not in settings.SUPPORTED_NONAPPS:
# Temporarily force home page to /locale/apps/
if settings.APP_PREVIEW and not path.partition('/')[0]:
url_parts.append('apps')
elif path.partition('/')[0] not in settings.SUPPORTED_NONAPPS:
app = self.app if self.app else self.get_app()
url_parts.append(app)

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

@ -1295,6 +1295,16 @@ NO_LOGIN_REQUIRED_MODULES = (
# number of users meets or exceeds this, they can't register.
REGISTER_USER_LIMIT = 0
NO_ADDONS_MODULES = (
'addons.views',
'devhub.views.dashboard', # The apps dashboard is a different view.
'devhub.views.submit', # Addon submit not ok, app submit a-ok.
'browse.views.personas',
'browse.views.extensions',
'browse.views.language_tools',
'browse.views.themes',
)
# Cron jobs that aren't critical will check this flag and not run if this
# is True.
IGNORE_NON_CRITICAL_CRONS = False

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

@ -16,6 +16,12 @@ admin.autodiscover()
handler404 = 'amo.views.handler404'
handler500 = 'amo.views.handler500'
# Get the correct home for APP_PREVIEW, temporary.
home = ('webapps.views.app_home' if settings.APP_PREVIEW
else 'addons.views.home')
urlpatterns = patterns('',
# Discovery pane is first for undetectable efficiency wins.
('^discovery/', include('discovery.urls')),
@ -26,6 +32,8 @@ urlpatterns = patterns('',
blocklist.views.blocklist, name='blocklist'),
('^blocked/', include('blocklist.urls')),
url('$^', home, name='home'),
# Add-ons.
('', include('addons.urls')),