Permalink
Browse files

eliminated import-time database query; added 'published' view test fo…

…r news
  • Loading branch information...
1 parent 412a26e commit 9924b3eb51df4dbb5e965b739c8ff0c8b6db1e39 @evildmp committed Nov 1, 2012
@@ -1,15 +1,20 @@
+from datetime import datetime
+
from django.utils.translation import ugettext_lazy as _
from django.db import models
-from arkestra_utilities.settings import PLUGIN_HEADING_LEVELS, PLUGIN_HEADING_LEVEL_DEFAULT
from django.contrib.contenttypes.models import ContentType
-from datetime import datetime
+
+from django.conf import settings
+
from cms.models.fields import PlaceholderField
from filer.fields.image import FilerImageField
+from arkestra_utilities.settings import PLUGIN_HEADING_LEVELS, PLUGIN_HEADING_LEVEL_DEFAULT
+
from links.models import ObjectLink
-from contacts_and_people.models import Entity, Person, default_entity_id, default_entity
+from contacts_and_people.models import Entity, Person
from contacts_and_people.templatetags.entity_tags import work_out_entity
class ArkestraGenericModel(models.Model):
@@ -32,7 +37,7 @@ class Meta:
image = FilerImageField(null=True, blank=True)
# universal plugin fields
- hosted_by = models.ForeignKey(Entity, default=default_entity_id,
+ hosted_by = models.ForeignKey(Entity, default=Entity.objects.default_entity_id(),
related_name='%(class)s_hosted_events', null=True, blank=True,
help_text=u"The entity responsible for publishing this item")
publish_to = models.ManyToManyField(
@@ -67,28 +72,31 @@ def get_importance(self):
@property
def get_hosted_by(self):
- return self.hosted_by or default_entity
+ return self.hosted_by or Entity.objects.base_entity()
@property
def get_template(self):
- return self.get_hosted_by.get_template()
+ if self.get_hosted_by:
+ return self.get_hosted_by.get_template()
+ else:
+ return settings.CMS_TEMPLATES[0][0]
@property
def get_entity(self):
"""
Real-world information, can be None
"""
- return self.hosted_by or Entity.objects.get(id=default_entity_id)
+ return self.hosted_by or Entity.objects.get(id=Entity.objects.base_entity())
- @property
- def get_website(self):
- """
- for internal Arkestra purposes only
- """
- if self.get_entity:
- return self.get_entity.get_website
- else:
- return None
+ # @property
+ # def get_website(self):
+ # """
+ # for internal Arkestra purposes only
+ # """
+ # if self.get_entity:
+ # return self.get_entity.get_website
+ # else:
+ # return None
@property
def links(self):
@@ -215,14 +215,14 @@ class EntityManager(TreeManager):
def get_by_natural_key(self, slug):
return self.get(slug=slug)
- def base_entity(self):
+ def base_entity(self):
try:
# are Entities available at all?
list(Entity.objects.all())
- # print "** Entity objects are available from the database"
+ print "** Entity objects are available from the database"
except:
# no - the database isn't ready
- # print "** Entity objects are not available from the database"
+ print "** Entity objects are not available from the database"
pass
else:
# we managed to get Entity.objects.all()
@@ -231,13 +231,20 @@ def base_entity(self):
entity = Entity.objects.get(id = base_entity_id)
# it can't be found, maybe because of a misconfiguation or because we haven't added any Entities yet
except (Entity.DoesNotExist, DatabaseError), e:
- # print "** Either the Entity does not exist, or I got a DatabaseError:"
- # print "**", e
+ print "** Either the Entity does not exist, or I got a DatabaseError:"
+ print "**", e
pass
else:
- # print "** I successfully found a default entity:", entity
+ print "** I successfully found a default entity:", entity
return entity
+ def default_entity_id(self):
+ if self.base_entity and not MULTIPLE_ENTITY_MODE:
+ return base_entity_id
+
+ def some_thing(self):
+ print "*********"
+
class Entity(MPTTModel, EntityLite, CommonFields):
objects=EntityManager()
short_name = models.CharField(blank=True, help_text="e.g. Haematology",
@@ -397,7 +404,7 @@ def get_website_url(self):
# try
return self.parent.get_website_url()
else: # except
- return default_entity.get_website
+ return Entity.objects.base_entity().get_website
def get_related_info_page_url(self, kind):
"""
@@ -407,7 +414,7 @@ def get_related_info_page_url(self, kind):
"""
if self.external_url:
return ""
- elif self == default_entity:
+ elif self == Entity.objects.base_entity():
return "/%s/" % kind
else:
return "/%s/%s/" % (kind, self.slug)
@@ -419,7 +426,7 @@ def get_template(self):
if self.get_website:
return self.get_website.get_template()
else:
- return default_entity.get_website.get_template()
+ return Entity.objects.base_entity().get_website.get_template()
def get_contacts(self):
@@ -820,11 +827,11 @@ class EntityMembersPluginEditor(CMSPlugin):
# default_entity_id is used to autofill the default entity where required, when MULTIPLE_ENTITY_MODE = False
# default_entity is used throughout the system
# make default_entity and default_entity_id available
-default_entity = Entity.objects.base_entity() # get it from the Entity custom manager method
-if default_entity and not MULTIPLE_ENTITY_MODE:
- default_entity_id = base_entity_id
-else:
- default_entity_id = None
+# default_entity = Entity.objects.base_entity() # get it from the Entity custom manager method
+# if default_entity and not MULTIPLE_ENTITY_MODE:
+# default_entity_id = base_entity_id
+# else:
+# default_entity_id = None
# crazymaniac's wild monkeypatch#
@@ -2,6 +2,30 @@
from contacts_and_people.models import Site, Person, Building, Entity, Membership
+class EntityManagerTests(TestCase):
+ def setUp(self):
+ pass
+
+ def test_base_entity_with_empty_database(self):
+ """
+ test EntityManager.base_entity
+ """
+ # no Entities, should be None
+ self.assertEquals(Entity.objects.base_entity(), None)
+
+ def test_base_entity_with_one_entity(self):
+ """
+ test EntityManager.base_entity
+ """
+ # one Entities, should be that
+ self.school = Entity(
+ name="School of Medicine",
+ slug="medicine",
+ )
+ self.school.save()
+ self.assertEquals(Entity.objects.base_entity(), self.school)
+
+
class EntityTestObjectsMixin(object):
"""
Create a set of inter-related objects that we'll use in a series of tests
@@ -2,7 +2,7 @@
import django.http as http
from django.template import RequestContext
from django.shortcuts import render_to_response, get_object_or_404
-from models import Person, Building, Membership, Entity, default_entity
+from models import Person, Building, Membership, Entity
from links.link_functions import object_links
from django.conf import settings
@@ -13,7 +13,7 @@
from publications.models import BibliographicRecord
from publications.models import Researcher # required for publications
-def contacts_and_people(request, slug=getattr(default_entity, "slug", None)):
+def contacts_and_people(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
# general values needed to set up and construct the page and menus
entity = Entity.objects.get(slug=slug)
# for the menu, because next we mess up the path
@@ -61,7 +61,7 @@ def contacts_and_people(request, slug=getattr(default_entity, "slug", None)):
RequestContext(request),
)
-def people(request, slug=getattr(default_entity, "slug", None), letter=None):
+def people(request, slug=getattr(Entity.objects.base_entity(), "slug", None), letter=None):
"""
Responsible for lists of people
"""
@@ -138,8 +138,8 @@ def person(request, slug, active_tab=""):
description = ", ".join((home_role.__unicode__(), entity.__unicode__()))
request.current_page = entity.get_website
else:
- description = default_entity.__unicode__()
- request.current_page = default_entity.get_website
+ description = Entity.objects.base_entity().__unicode__()
+ request.current_page = Entity.objects.base_entity().get_website
meta = {
"description": ": ".join((person.__unicode__(), description))
@@ -149,7 +149,7 @@ def person(request, slug, active_tab=""):
template = entity.get_template()
else: # no memberships, no useful information
# print "no memberships, no useful information"
- template = default_entity.get_template()
+ template = Entity.objects.base_entity().get_template()
tabs_dict = { # information for each kind of person tab
"default": {
@@ -302,7 +302,7 @@ def place(request, slug, active_tab=""):
meta = {
"description": meta_description_content,
}
- page = default_entity.get_website
+ page = Entity.objects.base_entity().get_website
request.current_page = page
template = page.get_template()
@@ -75,7 +75,6 @@ and @properties:
* ``links``
* ``external_url``
* ``get_hosted_by``
-* ``get_entity``
* ``get_website``
And you can add whatever fields of your own that are required and ignore the ones that are not.
@@ -14,7 +14,7 @@
from filer.fields.image import FilerImageField
-from contacts_and_people.models import Entity, Person, Building, default_entity_id
+from contacts_and_people.models import Entity, Person, Building
from links.models import ExternalLink
@@ -38,7 +38,8 @@ def save(self, *args, **kwargs):
super(NewsAndEvents, self).save(*args, **kwargs)
def link_to_more(self):
- return self.get_hosted_by.get_related_info_page_url("news-and-events")
+ if self.get_hosted_by:
+ return self.get_hosted_by.get_related_info_page_url("news-and-events")
class NewsArticle(NewsAndEvents):
url_path = "news"
View
@@ -1,7 +1,7 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
-from contacts_and_people.models import Entity, default_entity
+from contacts_and_people.models import Entity
from links.link_functions import object_links
from models import NewsAndEventsPlugin, Event, NewsArticle
@@ -11,7 +11,7 @@
def common_settings(request, slug):
- entity = Entity.objects.get(slug=slug) or default_entity
+ entity = Entity.objects.get(slug=slug) or Entity.objects.base_entity()
request.auto_page_url = request.path
# request.path = entity.get_website.get_absolute_url() # for the menu, so it knows where we are
request.current_page = entity.get_website
@@ -30,7 +30,7 @@ def common_settings(request, slug):
return instance, context, entity
-def news_and_events(request, slug=getattr(default_entity, "slug", None)):
+def news_and_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
instance, context, entity = common_settings(request, slug)
instance.type = "main_page"
@@ -59,7 +59,7 @@ def news_and_events(request, slug=getattr(default_entity, "slug", None)):
context,
)
-def previous_events(request, slug=getattr(default_entity, "slug", None)):
+def previous_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -87,7 +87,7 @@ def previous_events(request, slug=getattr(default_entity, "slug", None)):
context,
)
-def all_forthcoming_events(request, slug=getattr(default_entity, "slug", None)):
+def all_forthcoming_events(request, slug=getattr(Entity.objects.base_entity(), "slug", None)):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -115,7 +115,7 @@ def all_forthcoming_events(request, slug=getattr(default_entity, "slug", None)):
context,
)
-def news_archive(request, slug=getattr(default_entity,"slug", None)):
+def news_archive(request, slug=getattr(Entity.objects.base_entity(),"slug", None)):
instance, context, entity = common_settings(request, slug)
instance.type = "sub_page"
@@ -149,8 +149,10 @@ def newsarticle(request, slug):
"""
Responsible for publishing news article
"""
- newsarticle = get_object_or_404(NewsArticle, slug=slug)
-
+ if request.user.is_staff:
+ newsarticle = get_object_or_404(NewsArticle, slug=slug)
+ else:
+ newsarticle = get_object_or_404(NewsArticle, slug=slug, published = True)
return render_to_response(
"news_and_events/newsarticle.html",
{
Oops, something went wrong.

0 comments on commit 9924b3e

Please sign in to comment.