From 99895a28d1caa2e419affd9ee282b4d983603d81 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 3 Aug 2013 08:58:16 +0200 Subject: [PATCH] Fix get_absolute_url for deleted persons. Fix #845 --- openslides/utils/person/api.py | 14 ++++++++++++++ openslides/utils/person/models.py | 2 +- openslides/utils/templatetags/tags.py | 5 ++++- tests/person_api/models.py | 3 +++ tests/person_api/tests.py | 12 +++++++++++- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/openslides/utils/person/api.py b/openslides/utils/person/api.py index e063f13b5..312e70d12 100644 --- a/openslides/utils/person/api.py +++ b/openslides/utils/person/api.py @@ -51,6 +51,20 @@ class Person(object): """ return '' + def get_absolute_url(self, link='detail'): + """ + Return an absolute url for the person. + + The argument 'link' affects which url it is. Typically it is one of: + * detail + * edit + * delete + + You should raise an 'ValueError', if your person does not have + one of this links. + """ + raise ValueError('This person object has no url.') + class Persons(object): """ diff --git a/openslides/utils/person/models.py b/openslides/utils/person/models.py index b74f9c92d..bf271d890 100644 --- a/openslides/utils/person/models.py +++ b/openslides/utils/person/models.py @@ -81,7 +81,7 @@ class PersonMixin(object): % self) def __unicode__(self): - return 'MyPerson: %s' % self.person_id + return self.person_id def prepare_database_save(self, field): if type(field) is PersonField: diff --git a/openslides/utils/templatetags/tags.py b/openslides/utils/templatetags/tags.py index 8592696ef..fe1a535b0 100644 --- a/openslides/utils/templatetags/tags.py +++ b/openslides/utils/templatetags/tags.py @@ -36,7 +36,10 @@ def trans(value): @register.simple_tag def model_url(object, link='view'): # TODO: Rename to object_url - return object.get_absolute_url(link) + try: + return object.get_absolute_url(link) + except ValueError: + return '' @register.filter diff --git a/tests/person_api/models.py b/tests/person_api/models.py index 22fa3ad31..57793b8e2 100644 --- a/tests/person_api/models.py +++ b/tests/person_api/models.py @@ -13,6 +13,9 @@ class TestPerson(PersonMixin, models.Model): def __unicode__(self): return self.name + def get_absolute_url(self, link='detail'): + return 'absolute_url_of_test_person' + class TestPersonToPerson(object): def __init__(self, person_prefix_filter=None, id_filter=None): diff --git a/tests/person_api/tests.py b/tests/person_api/tests.py index d35b25369..e1e15c237 100644 --- a/tests/person_api/tests.py +++ b/tests/person_api/tests.py @@ -15,11 +15,12 @@ from django.db.models.query import EmptyQuerySet from django.contrib.auth.models import AnonymousUser from openslides.utils.test import TestCase +from openslides.utils.person.api import get_person from .models import TestPerson, TestModel -class ItemTest(TestCase): +class PersonTest(TestCase): def setUp(self): self.person1 = TestPerson.objects.create(name='test1') @@ -45,3 +46,12 @@ class ItemTest(TestCase): AttributeError, 'You can not save \'\' into a person field.'): TestModel.objects.create(person=5) + + def test_get_absolute_url_with_deleted_person(self): + person2 = TestPerson.objects.create(name='test2') + self.assertEqual(person2.get_absolute_url(), 'absolute_url_of_test_person') + person_id = person2.person_id + self.assertEqual(get_person(person_id).get_absolute_url(), 'absolute_url_of_test_person') + person2.delete() + with self.assertRaisesRegexp(ValueError, 'This person object has no url.'): + get_person(person_id).get_absolute_url()