Merge pull request #850 from ostcar/absolute_url-filter

Absolute url filter
This commit is contained in:
Oskar Hahn 2013-08-19 08:44:06 -07:00
commit ef000829e0
11 changed files with 61 additions and 2 deletions

View File

View File

@ -10,6 +10,8 @@
:license: GNU GPL, see LICENSE for more details.
"""
import warnings
from django import template
from django.utils.translation import ugettext as _
from openslides.config.api import config
@ -35,10 +37,25 @@ def trans(value):
@register.simple_tag
def model_url(object, link='view'):
# TODO: Rename to object_url
warnings.warn("model_url is deprecated; use absolute_url instead",
DeprecationWarning)
return object.get_absolute_url(link)
@register.filter
def absolute_url(model, link='detail'):
"""
Returns the absolute_url to a model. The 'link' argument decides which url
will be returned. See get_absolute_url() in the model.
Example: {{ motion|absolute_url:'delete' }}
"""
try:
return model.get_absolute_url(link)
except ValueError:
return ''
@register.filter
def first_line(text):
try:

View File

@ -111,7 +111,6 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django.contrib.humanize',
'mptt',
'openslides.utils',
'openslides.poll',
'openslides.core',
'openslides.account',

12
tests/core/models.py Normal file
View File

@ -0,0 +1,12 @@
from django.db import models
class TestModel(models.Model):
name = models.CharField(max_length='255')
def get_absolute_url(self, link='detail'):
if link == 'detail':
return 'detail-url-here'
if link == 'delete':
return 'delete-url-here'
raise ValueError('No URL for %s' % link)

View File

@ -13,6 +13,8 @@ from django.template import Template, Context
from openslides.utils.test import TestCase
from openslides.config.api import config
from .models import TestModel
class ConfigTagAndFilter(TestCase):
def test_config_tag(self):
@ -37,3 +39,32 @@ class ConfigTagAndFilter(TestCase):
template = Template(template_code)
self.assertTrue('FdgfkR04jtg9f8bq' in template.render(Context({})))
self.assertFalse('bad_e0fvkfHFD' in template.render(Context({})))
class AbsoluteUrlFilter(TestCase):
def setUp(self):
self.model = TestModel.objects.create(name='test_model')
def test_default_argument(self):
"""
Test to call absolute_url without an argument.
"""
t = Template("{% load tags %}URL: {{ model|absolute_url }}")
html = t.render(Context({'model': self.model}))
self.assertEqual(html, 'URL: detail-url-here')
def test_with_argument(self):
"""
Test to call absolute_url with an argument.
"""
t = Template("{% load tags %}URL: {{ model|absolute_url:'delete' }}")
html = t.render(Context({'model': self.model}))
self.assertEqual(html, 'URL: delete-url-here')
def test_wrong_argument(self):
"""
Test to call absolute_url with a non existing argument.
"""
t = Template("{% load tags %}URL: {{ model|absolute_url:'wrong' }}")
html = t.render(Context({'model': self.model}))
self.assertEqual(html, 'URL: ')