Skip to content

Commit

Permalink
Refactor Date method to avoid redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
hvelarde committed Sep 30, 2014
1 parent 48a11ea commit fca4558
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 62 deletions.
25 changes: 23 additions & 2 deletions src/collective/cover/tiles/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-

# Basic implementation taken from
# http://davisagli.com/blog/using-tiles-to-provide-more-flexible-plone-layouts

from AccessControl import Unauthorized
from Acquisition import aq_base
from Acquisition import aq_inner
Expand Down Expand Up @@ -53,6 +51,7 @@
from zope.schema import getFieldsInOrder

import logging
import Missing

logger = logging.getLogger(PROJECTNAME)

Expand Down Expand Up @@ -336,6 +335,28 @@ def isAllowedToEdit(self, user=None):

return allowed

def Date(self, brain):
"""Return the date of publication of the object referenced by
brain. If the object has not been published yet, return its
modification date. If the object is an Event, then return the
start date.
:param brain: [required] brain of the cataloged object
referenced in the tile
:type brain: AbstractCatalogBrain
:returns: the object's publication/modification date or the
event's start date in case of an Event-like object
:rtype: str or DateTime
"""
calendar = api.portal.get_tool('portal_calendar')
# calendar_types lists all Event-like content types
if brain.portal_type not in calendar.calendar_types:
return brain.Date
else:
# an Event must have a start date
assert brain.start is not Missing.Value
return brain.start

@property
def has_image(self):
return self.data.get('image', None) is not None
Expand Down
20 changes: 2 additions & 18 deletions src/collective/cover/tiles/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from zope import schema
from zope.interface import implements

import Missing


class IBasicTile(IPersistentCoverTile):

Expand Down Expand Up @@ -77,23 +75,9 @@ def brain(self):
return result[0] if result else None

def Date(self):
"""Return the date of publication of the object; if it has not been
published yet, it will return its modification date. If the object is
an Event, then return the start date.
:returns: the object's publication/modification date or the event's
start date in case of an Event-like object
"""
# brain is None if the tile was populated by editing it
# self.brain is None when the tile was populated by editing it
if self.brain is not None:
calendar = api.portal.get_tool('portal_calendar')
# calendar_types lists all Event-like content types
if self.brain.portal_type not in calendar.calendar_types:
return self.brain.Date
else:
# Events must have a start date
assert self.brain.start is not Missing.Value
return self.brain.start
return super(BasicTile, self).Date(self.brain)

def is_empty(self):
return self.brain is None and \
Expand Down
22 changes: 0 additions & 22 deletions src/collective/cover/tiles/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collective.cover.tiles.base import IPersistentCoverTile
from collective.cover.tiles.base import PersistentCoverTile
from collective.cover.tiles.configuration_view import IDefaultConfigureForm
from plone import api
from plone.app.uuid.utils import uuidToObject
from plone.directives import form
from plone.memoize import view
Expand All @@ -18,8 +17,6 @@
from zope.interface import implements
from zope.schema import getFieldsInOrder

import Missing


class ICollectionTile(IPersistentCoverTile):

Expand Down Expand Up @@ -127,25 +124,6 @@ def is_empty(self):
return self.data.get('uuid', None) is None or \
uuidToObject(self.data.get('uuid')) is None

def Date(self, brain):
"""Return the date of publication of the object; if it has not been
published yet, it will return its modification date. If the object is
an Event, then return the start date.
:param brain: [required]
:type brain: catalog brain
:returns: the object's publication/modification date or the event's
start date in case of an Event-like object
"""
calendar = api.portal.get_tool('portal_calendar')
# calendar_types lists all Event-like content types
if brain.portal_type not in calendar.calendar_types:
return brain.Date
else:
# Events must have a start date
assert brain.start is not Missing.Value
return brain.start

def populate_with_object(self, obj):
super(CollectionTile, self).populate_with_object(obj) # check permission

Expand Down
21 changes: 1 addition & 20 deletions src/collective/cover/tiles/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from zope.schema import getFieldsInOrder

import logging
import Missing

logger = logging.getLogger(PROJECTNAME)

Expand Down Expand Up @@ -141,15 +140,6 @@ def is_empty(self):
return self.results() == []

def Date(self, obj):
"""Return the date of publication of the object; if it has not been
published yet, it will return its modification date. If the object is
an Event, then return the start date.
:param obj: [required]
:type obj: AT or DX-based content type object
:returns: the object's publication/modification date or the event's
start date in case of an Event-like object
"""
# XXX: different from Collection tile, List tile returns objects
# instead of brains in its `results` function. I think we
# were looking for some performace gains there but in this
Expand All @@ -162,16 +152,7 @@ def Date(self, obj):
catalog = api.portal.get_tool('portal_catalog')
brain = catalog(UID=self.get_uid(obj))
assert len(brain) == 1
brain = brain[0]

calendar = api.portal.get_tool('portal_calendar')
# calendar_types lists all Event-like content types
if brain.portal_type not in calendar.calendar_types:
return brain.Date
else:
# Events must have a start date
assert brain.start is not Missing.Value
return brain.start
return super(ListTile, self).Date(brain[0])

# TODO: get rid of this by replacing it with the 'count' field
def set_limit(self):
Expand Down

0 comments on commit fca4558

Please sign in to comment.