Permalink
Browse files

Merge branch 'feature/replace_frontendadmin' of github.com:servee/ser…

…vee into feature/replace_frontendadmin
  • Loading branch information...
2 parents 32de103 + eaaadc0 commit 5467fceaa6c85e9b630cc18ae736e256329b320b @kkellydesign kkellydesign committed Mar 15, 2011
@@ -24,7 +24,7 @@ class AddObject(Tag):
Argument('label', required=False, resolve=False)
)
- def render_tag(self, context, model_instance, label=None):
+ def render_tag(self, context, queryset_instance, label=None):
if not isinstance(queryset_instance, QuerySet):
raise template.TemplateSyntaxError, "'%s' argument must be a queryset" % queryset_instance
@@ -39,12 +39,8 @@ def render_tag(self, context, model_instance, label=None):
label = _("Add")
return '<a class="frontendadmin frontendadmin_add" href="%s">%s</a>' % (
- reverse("servee:%s_%s_add" % (
- app_label,
- model_name,
- ), args=[model_instance.pk,]
- ),
- label
+ reverse("servee:%s_%s_add" % (app_label, model_name,)),
+ unicode(label)
)
class ChangeObject(Tag):
@@ -75,7 +71,7 @@ def render_tag(self, context, model_instance, label=None):
model_name,
), args=[model_instance.pk,]
),
- label
+ unicode(label)
)
class DeleteObject(Tag):
@@ -106,7 +102,7 @@ def render_tag(self, context, model_instance, label=None):
model_name,
), args=[model_instance.pk,]
),
- label
+ unicode(label)
)
register.tag(AddObject)
@@ -0,0 +1 @@
+from tests import *
@@ -0,0 +1,14 @@
+from django.contrib.flatpages.models import FlatPage
+from django.contrib.flatpages.admin import FlatPageAdmin
+
+import frontendadmin
+
+class FlatPageFrontendadminAdmin(FlatPageAdmin, frontendadmin.ServeeModelAdmin):
+ """
+ This class extends from the normal FlatPageAdmin, as well as frontendadmin.ServeeModelAdmin
+ The later simply adds to the templates that should be processed when looking
+ for the templates to render this page.
+ """
+ pass
+
+frontendadmin.site.register(FlatPage, FlatPageFrontendadminAdmin)
@@ -0,0 +1,50 @@
+from django.test import Client
+from django.core.handlers.wsgi import WSGIRequest
+from django.core.handlers.base import BaseHandler
+
+# From: http://djangosnippets.org/snippets/2231/
+# Adapted from Simon Willison's snippet: http://djangosnippets.org/snippets/963/
+class RequestFactory(Client):
+ """
+ Class that lets you create mock Request objects for use in testing.
+
+ Usage:
+
+ rf = RequestFactory()
+ get_request = rf.get('/hello/')
+ post_request = rf.post('/submit/', {'foo': 'bar'})
+
+ This class re-uses the django.test.client.Client interface, docs here:
+ http://www.djangoproject.com/documentation/testing/#the-test-client
+
+ Once you have a request object you can pass it to any view function,
+ just as if that view had been hooked up using a URLconf.
+
+ """
+ def request(self, **request):
+ """
+ Similar to parent class, but returns the request object as soon as it
+ has created it.
+ """
+ environ = {
+ 'HTTP_COOKIE': self.cookies,
+ 'PATH_INFO': '/',
+ 'QUERY_STRING': '',
+ 'REQUEST_METHOD': 'GET',
+ 'SCRIPT_NAME': '',
+ 'SERVER_NAME': 'testserver',
+ 'SERVER_PORT': 80,
+ 'SERVER_PROTOCOL': 'HTTP/1.1',
+ }
+ environ.update(self.defaults)
+ environ.update(request)
+ request = WSGIRequest(environ)
+
+ handler = BaseHandler()
+ handler.load_middleware()
+ for middleware_method in handler._request_middleware:
+ if middleware_method(request):
+ raise Exception("Couldn't create request object - "
+ "request middleware returned a response")
+
+ return request
@@ -0,0 +1 @@
+# Django models go here
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+
+import os, sys
+
+os.environ["DJANGO_SETTINGS_MODULE"] = "test_settings"
+
+parent = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
+root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "..")
+sys.path.insert(0, parent)
+sys.path.insert(0, root)
+
+from django.test.simple import DjangoTestSuiteRunner
+from django.conf import settings
+
+def runtests():
+ runner = DjangoTestSuiteRunner()
+ failures = runner.run_tests(["frontendadmin"], verbosity=1, interactive=True)
+ sys.exit(failures)
+
+if __name__ == "__main__":
+ runtests()
+
@@ -0,0 +1,39 @@
+import os
+
+BASE_DIR = os.path.dirname(__file__)
+
+INSTALLED_APPS = (
+ "django.contrib.admin",
+ "django.contrib.auth",
+ "django.contrib.flatpages",
+ "django.contrib.sessions",
+ "django.contrib.contenttypes",
+ "frontendadmin",
+ "servee",
+)
+
+DATABASES = {
+ "default": {
+ "ENGINE": "django.db.backends.sqlite3",
+ }
+}
+
+MIDDLEWARE_CLASSES = (
+ "django.middleware.common.CommonMiddleware",
+ "django.middleware.csrf.CsrfViewMiddleware",
+ "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware",
+ "django.contrib.sessions.middleware.SessionMiddleware",
+ "django.contrib.auth.middleware.AuthenticationMiddleware",
+)
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+ "django.contrib.auth.context_processors.auth",
+ "django.core.context_processors.debug",
+ "django.core.context_processors.i18n",
+ "django.core.context_processors.media",
+ 'django.core.context_processors.request',
+)
+
+ROOT_URLCONF = "frontendadmin.tests.urls"
+
+SITE_ID = 1
@@ -0,0 +1,100 @@
+from django.contrib.auth import authenticate, login
+from django.contrib.auth.models import User
+from django.contrib.flatpages.models import FlatPage
+from django.template import RequestContext, Template
+from django.test import Client, TestCase
+
+# Django 1.3's RequestFactory doesn't run requests through MiddleWare
+from helpers import RequestFactory
+
+
+class TestTemplateTags(TestCase):
+ def setUp(self):
+ self.user = User.objects.create(username="test")
+ self.user.set_password("secret")
+ self.user.is_staff = True
+ self.user.is_superuser = True
+ self.user.save()
+
+ self.factory = RequestFactory()
+
+ FlatPage.objects.create(
+ url="/",
+ title="Test",
+ content="This is a test.",
+ )
+
+ def tearDown(self):
+ pass
+
+ def get_request_for_url(self, url):
+ request = self.factory.get(url)
+ user = authenticate(username=self.user.username, password="secret")
+ login(request, user)
+ return request
+
+ def test_frontendadmin_add(self):
+ frontendadmin_add_html = """<a class="frontendadmin frontendadmin_add" href="/servee/flatpages/flatpage/add/">Add a Flatpage</a>"""
+ frontendadmin_add_plain_html = """<a class="frontendadmin frontendadmin_add" href="/servee/flatpages/flatpage/add/">Add</a>"""
+ template = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_add flatpages "Add a Flatpage" %}\
+""" )
+ template_plain = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_add flatpages %}\
+""" )
+ request = self.get_request_for_url("/frontendadmin_add/")
+
+ flatpages = FlatPage.objects.all()
+ context = RequestContext(request, {"flatpages": flatpages})
+ # Test with custom label
+ content = template.render(context)
+ self.assertEqual(content, frontendadmin_add_html)
+ # Test without custom label
+ content = template_plain.render(context)
+ self.assertEqual(content, frontendadmin_add_plain_html)
+
+ def test_frontendadmin_change(self):
+ frontendadmin_change_html = """<a class="frontendadmin frontendadmin_edit" href="/servee/flatpages/flatpage/1/">Edit This Flatpage</a>"""
+ frontendadmin_change_plain_html = """<a class="frontendadmin frontendadmin_edit" href="/servee/flatpages/flatpage/1/">Change</a>"""
+ template = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_change flatpage "Edit This Flatpage" %}\
+""" )
+ template_plain = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_change flatpage %}\
+""" )
+ request = self.get_request_for_url("/frontendadmin_change/")
+
+ flatpage = FlatPage.objects.get(pk=1)
+ context = RequestContext(request, {"flatpage": flatpage})
+ # Test with custom label
+ content = template.render(context)
+ self.assertEqual(content, frontendadmin_change_html)
+ # Test without custom label
+ content = template_plain.render(context)
+ self.assertEqual(content, frontendadmin_change_plain_html)
+
+ def test_frontendadmin_delete(self):
+ frontendadmin_delete_html = """<a class="frontendadmin frontendadmin_delete" href="/servee/flatpages/flatpage/1/delete/">Delete This Flatpage</a>"""
+ frontendadmin_delete_plain_html = """<a class="frontendadmin frontendadmin_delete" href="/servee/flatpages/flatpage/1/delete/">Delete</a>"""
+ template = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_delete flatpage "Delete This Flatpage" %}\
+""" )
+ template_plain = Template("""\
+{% load frontendadmin_tags %}\
+{% frontendadmin_delete flatpage %}\
+""" )
+ request = self.get_request_for_url("/frontendadmin_delete/")
+
+ flatpage = FlatPage.objects.get(pk=1)
+ context = RequestContext(request, {"flatpage": flatpage})
+ # Test with custom label
+ content = template.render(context)
+ self.assertEqual(content, frontendadmin_delete_html)
+ # Test without custom label
+ content = template_plain.render(context)
+ self.assertEqual(content, frontendadmin_delete_plain_html)
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import *
+import frontendadmin
+import frontendadmin_registry
+
+
+urlpatterns = patterns("",
+ url(r"^servee/", include("servee.urls")),
+ url(r"^servee/", include(frontendadmin.site.urls)),
+)

0 comments on commit 5467fce

Please sign in to comment.