Permalink
Browse files

tidied urls, reverses and tests

  • Loading branch information...
evildmp committed Jul 24, 2013
1 parent 4e53395 commit b97af422a021684582f93ea4929a39ecdcf97606
@@ -1,4 +1,5 @@
-# this file exists to help avoid circular imports
+# this file exists to help avoid circular imports - it imports nothing from
+# Arkestra
from django.db import models
@@ -1,22 +1,29 @@
from django.db import models
from django.core.urlresolvers import reverse
+
from links.models import ExternalLink
class URLModelMixin(models.Model):
+ # for models that can have external (hosted elsewhere) items
class Meta:
abstract = True
- # url fields
+ # field no longer used, to be deleted
url = models.URLField(null=True, blank=True, verify_exists=True,
- help_text=u"To be used <strong>only</strong> for external items. Use with caution!")
+ help_text=u"Use <strong>only</strong> for external items.")
+
+ # url fields
external_url = models.ForeignKey(ExternalLink,
related_name="%(class)s_item",
on_delete=models.PROTECT,
blank=True, null=True,
help_text=u"Select an item from the External Links database."
)
- slug = models.SlugField(unique=True, max_length=60, blank=True, help_text=u"Do not meddle with this unless you know exactly what you're doing!", error_messages={"unique": "unique"})
+ slug = models.SlugField(unique=True, max_length=60, blank=True,
+ help_text=u"Do not meddle with this unless you know exactly what you're doing!",
+ error_messages={"unique": "unique"}
+ )
def __unicode__(self):
return self.title
@@ -31,12 +38,14 @@ def get_absolute_url(self):
class LocationModelMixin(models.Model):
+ # provides location fields, for people, contacts, events, etc
class Meta:
abstract = True
- # location fields
- precise_location = models.CharField(help_text=u"Precise location <em>within</em> the building, for visitors",
+ precise_location = models.CharField(
+ help_text=u"Location <em>within</em> the building, for visitors",
max_length=255, null=True, blank=True)
- access_note = models.CharField(help_text = u"Notes on access/visiting hours/etc",
+ access_note = models.CharField(
+ help_text = u"Notes on access/visiting hours/etc",
max_length=255, null=True, blank=True)
@@ -8,7 +8,8 @@
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.conf import settings
-from django.core.urlresolvers import reverse
+from django.core.urlresolvers import reverse, NoReverseMatch
+
from cms.models import Page, CMSPlugin
from cms.models.fields import PlaceholderField
@@ -53,18 +54,22 @@ def get_by_natural_key(self, slug):
return self.get(slug=slug)
class Building(models.Model):
- """Each Building is on a Site."""
+ # the Building model should really be named Place
objects=BuildingManager()
name = models.CharField(max_length=100, null=True, blank=True)
number = models.CharField(max_length=10, null=True, blank=True)
- street = models.CharField("Street name", max_length=100, null = True, blank=True)
+ street = models.CharField(
+ "Street name", max_length=100,
+ null = True, blank=True
+ )
additional_street_address = models.CharField(help_text=u"If required",
max_length=100, null=True, blank=True)
postcode = models.CharField(max_length=9, null=True, blank=True)
site = models.ForeignKey(Site,
on_delete=models.PROTECT,
related_name="place")
- slug = models.SlugField(blank=True, help_text=u"Please leave blank/amend only if required",
+ slug = models.SlugField(blank=True,
+ help_text=u"Please leave blank/amend only if required",
max_length=255, null=True, unique=True)
image = FilerImageField(on_delete=models.SET_NULL,
null=True, blank=True)
@@ -330,9 +335,9 @@ def get_absolute_url(self):
if self.external_url:
return self.external_url.url
elif self.get_website:
- return reverse("contact", kwargs={"slug":self.slug})
+ return reverse("contact-entity", kwargs={"slug":self.slug})
else:
- return reverse("contact_base")
+ return reverse("contact-entity-base")
@property
def _get_real_ancestor(self):
@@ -427,28 +432,17 @@ def get_related_info_page_url(self, kind):
If the entity is the base entity, doesn't add the entity slug to
the URL
"""
- kinds = [
- "contact",
- "news-and-events",
- "vacancies-and-studentships",
- "forthcoming-events",
- "news-archive",
- "previous-events"
- ]
# external entities don't have info pages
if self.external_url:
return ""
# info pages for base entity
elif self == Entity.objects.base_entity():
- print 'reverse(kind+"_base")'
- return reverse(kind+"_base")
try:
return reverse(kind+"_base")
except NoReverseMatch:
return "/%s/" % kind
# info pages for other entities
else:
- return reverse(kind,kwargs={"slug":self.slug})
try:
return reverse(kind,kwargs={"slug":self.slug})
except NoReverseMatch:
@@ -652,11 +646,10 @@ def __unicode__(self):
return u" ".join(name_part for name_part in [unicode(title), self.given_name, self.surname] if name_part)
def get_absolute_url(self):
- if self.active:
- if self.external_url:
- return self.external_url.url
- else:
- return reverse("contact_person", kwargs={"slug":self.slug})
+ if self.external_url:
+ return self.external_url.url
+ else:
+ return reverse("contact-person", kwargs={"slug":self.slug})
@property
def get_role(self):
@@ -48,7 +48,7 @@
<h{{ IN_BODY_HEADING_LEVEL }}>All people A-Z by surname</h{{ IN_BODY_HEADING_LEVEL }}>
<ul class= "index">
{% for initial in initials_list %}
- <li><a href="{% url contact_people_letter entity.slug,initial|lower %}">{{ initial }}</a></li>
+ <li><a href="{% url contact-people-letter entity.slug,initial|lower %}">{{ initial }}</a></li>
{% endfor %}
</ul>
{% endif %}
@@ -8,15 +8,15 @@
{% load entity_tags %}
<div class="row columns2">
<div class="column firstcolumn">
- <h{{ IN_BODY_HEADING_LEVEL }} style="clear:both">{% if letter %}People: surnames beginning {{letter|upper}}{% else %}All people by surname{% endif %}</h{{ IN_BODY_HEADING_LEVEL }}>
+ <h{{ IN_BODY_HEADING_LEVEL }} style="clear:both">{% if letter %}{% trans "People: surnames beginning" %} {{letter|upper}}{% else %}{% trans "All people by surname" %}{% endif %}</h{{ IN_BODY_HEADING_LEVEL }}>
{% people_with_roles letter %}
</div>
<div class="column lastcolumn">
{% if initials_list %}
- <h{{ IN_BODY_HEADING_LEVEL }}>Index of people by surname</h{{ IN_BODY_HEADING_LEVEL }}>
+ <h{{ IN_BODY_HEADING_LEVEL }}>{% trans "Index of people by surname" %}</h{{ IN_BODY_HEADING_LEVEL }}>
<ul class="index">
{% for initial in initials_list %}
- <li><a href="/people/{{ entity.slug }}/{{initial|lower}}/">{{ initial }}</a></li>
+ <li><a href="{% url contact-people-letter entity.slug initial|lower %}">{{ initial }}</a></li>
{% endfor %}
</ul>
{% endif %}
@@ -1,4 +1,5 @@
from django.test import TestCase
+from django.core.urlresolvers import reverse
from contacts_and_people.models import Site, Person, Building, Entity, Membership
@@ -164,7 +165,7 @@ def setUp(self):
self.web_editors.save()
# set up a Person - we will add memberships later in the tests
- self.smith = Person()
+ self.smith = Person(slug="smith")
self.smith.save()
@@ -279,6 +280,12 @@ class PersonTests(EntityTestObjectsMixin, TestCase):
circumstances
"""
+ def test_person_get_absolute_url(self):
+ self.assertEquals(
+ self.smith.get_absolute_url(),
+ "/person/smith/"
+ )
+
def test_person_with_no_memberships(self):
# smith has no Memberships
self.assertEquals(list(self.smith.real_entity_memberships), [])
@@ -384,4 +391,66 @@ def test_person_with_better_entity_membership(self):
self.assertEquals(self.smith.get_building, self.main_building)
self.assertEquals(self.smith.get_full_address, [self.school, u'Main Building', u"St Mary's Street", u'Cardiff'])
# check that his membership of school has been downgraded by the save()
- self.assertEquals(Membership.objects.get(pk=smith_school_membership.pk).importance_to_person, 4)
+ self.assertEquals(
+ Membership.objects.get(
+ pk=smith_school_membership.pk
+ ).importance_to_person,
+ 4
+ )
+
+
+class ReverseURLsTests(TestCase):
+ def test_person_reverse_url(self):
+ self.assertEqual(
+ reverse("contact-person", kwargs={"slug": "some-slug"}),
+ "/person/some-slug/"
+ )
+
+ def test_person_tab_reverse_url(self):
+ self.assertEqual(
+ reverse(
+ "contact-person-tab",
+ kwargs={"slug": "some-slug", "active_tab": "yibbers"}),
+ "/person/some-slug/yibbers/"
+ )
+
+ def test_place_reverse_url(self):
+ self.assertEqual(
+ reverse("contact-place", kwargs={"slug": "some-slug"}),
+ "/place/some-slug/"
+ )
+
+ def test_place_tab_reverse_url(self):
+ self.assertEqual(
+ reverse(
+ "contact-place-tab",
+ kwargs={"slug": "some-slug", "active_tab": "yibbers"}),
+ "/place/some-slug/yibbers/"
+ )
+
+ def test_contact_people_reverse_url(self):
+ self.assertEqual(
+ reverse("contact-people", kwargs={"slug": "some-slug"}),
+ "/people/some-slug/"
+ )
+
+ def test_contact_people_letter_reverse_url(self):
+ self.assertEqual(
+ reverse(
+ "contact-people-letter",
+ kwargs={"slug": "some-slug", "letter": "w"}),
+ "/people/some-slug/w/"
+ )
+
+ def test_contact_entity_reverse_url(self):
+ self.assertEqual(
+ reverse("contact-entity", kwargs={"slug": "some-slug"}),
+ "/contact/some-slug/"
+ )
+
+ def test_contact_base_entity_reverse_url(self):
+ self.assertEqual(
+ reverse("contact-entity-base"),
+ "/contact/"
+ )
+
@@ -3,21 +3,25 @@
urlpatterns = patterns('contacts_and_people.views',
# person
- (r"^person/(?P<slug>[-\w]+)/?$", "person", {}, "contact_person"),
- (r"^person/(?P<slug>[-\w]+)/(?P<active_tab>[-\w]*)/?$", "person", {}, "contact_person_tab"),
+ url(r"^person/(?P<slug>[-\w]+)/$", "person", name="contact-person"),
+ url(r"^person/(?P<slug>[-\w]+)/(?P<active_tab>[-\w]*)/$", "person",
+ name="contact-person-tab"),
# place
- (r"^place/(?P<slug>[-\w]+)/?$", "place", {}, "contact_place"),
- (r"^place/(?P<slug>[-\w]+)/(?P<active_tab>[-\w]*)/?$", "place", {}, "contact_place_tab"),
+ url(r"^place/(?P<slug>[-\w]+)/$", "place", name="contact-place"),
+ url(r"^place/(?P<slug>[-\w]+)/(?P<active_tab>[-\w]*)/$", "place",
+ name="contact-place-tab"),
# lists of people in an entity
- (r"^people/(?P<slug>[-\w]+)/$", "people", {}, "contact_people"),
- (r"^people/(?P<slug>[-\w]+)/(?P<letter>\w)/$", "people", {}, "contact_people_letter"),
+ url(r"^people/(?P<slug>[-\w]+)/$", "people", name="contact-people"),
+ url(r"^people/(?P<slug>[-\w]+)/(?P<letter>\w)/$", "people", name="contact-people-letter"),
# main contacts & people page
- (r'^contact/(?P<slug>[-\w]+)/$', "contacts_and_people", {}, "contact"),
# non-base entities
- (r'^contact/$', "contacts_and_people", {}, "contact_base"), # base entity only
+ url(r'^contact/(?P<slug>[-\w]+)/$', "contacts_and_people",
+ name="contact-entity"),
+ # base entity only
+ url(r'^contact/$', "contacts_and_people", name="contact-entity-base"),
# news, events, vacancies, studentships
(r'^', include('news_and_events.urls')),
@@ -26,14 +30,5 @@
# housekeeping
(r'^', include('housekeeping.urls')),
(r'^', include('arkestra_image_plugin.urls')),
-
-
)
-
- # the old ways
- # (r"^entity/(?P<slug>[-\w]+)/contact/people/(?P<letter>[a-z])/$", "contacts_and_people.views.people"),
- # (r"^entity/(?P<slug>[-\w]+)/contact/people/$", "contacts_and_people.views.people"),
- # (r"^entity/(?P<slug>[-\w]+)/contact/$", "contacts_and_people.views.contacts_and_people"),
- # )
-
-
+
Oops, something went wrong.

0 comments on commit b97af42

Please sign in to comment.