source: 4.0/enpraxis.educommons/trunk/enpraxis/educommons/portlet/simplenavportlet.py @ 759

Revision 759, 6.0 KB checked in by brent, 4 years ago (diff)

Skin changes, default to schools, icon support for templates, fixed eduCommons id clash, course ordering fixes, misc other changes.

Line 
1##################################################################################
2#    Copyright (c) 2004-2009 Utah State University, All rights reserved.
3#    Portions copyright 2009 Massachusetts Institute of Technology, All rights reserved.
4#                                                                                 
5#    This program is free software; you can redistribute it and/or modify         
6#    it under the terms of the GNU General Public License as published by         
7#    the Free Software Foundation, version 2.                                     
8#                                                                                 
9#    This program is distributed in the hope that it will be useful,             
10#    but WITHOUT ANY WARRANTY; without even the implied warranty of               
11#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
12#    GNU General Public License for more details.                                 
13#                                                                                 
14#    You should have received a copy of the GNU General Public License           
15#    along with this program; if not, write to the Free Software                 
16#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   
17#                                                                                 
18##################################################################################
19
20__author__  = '''Brent Lambert, David Ray, Jon Thomas'''
21__version__   = '$ Revision 0.0 $'[11:-2]
22
23
24from zope.interface import implements
25from zope.component import getUtility
26from zope.formlib import form
27from plone.portlets.interfaces import IPortletDataProvider
28from plone.app.portlets.portlets import base
29from enpraxis.educommons import eduCommonsMessageFactory as _
30from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
31from enpraxis.educommons.utilities.interfaces import IECUtility
32from Products.CMFPlone.utils import getToolByName
33
34
35class ISimpleNavPortlet(IPortletDataProvider):
36    """ A simple navigation portlet. """
37
38
39class Assignment(base.Assignment):
40
41    implements(ISimpleNavPortlet)
42
43    title = _(u'eduCommons Navigation')
44
45
46class Renderer(base.Renderer):
47    render = ViewPageTemplateFile('simplenav.pt')
48
49    def __init__(self, context, request, view, manager, data):
50        super(Renderer, self).__init__(context, request, view, manager, data)
51        self.ecutil = getUtility(IECUtility)
52        self.ecparent = self.ecutil.FindECParent(context)
53
54    def getNavObjects(self):
55        """ Get navigable objects. """
56        if self.isCourseObject():
57            path = {'path':{'query':'/'.join(self.ecparent.getPhysicalPath())+'/'},
58                    'getExcludeFromNav':False,
59                    'sort_on':'getObjPositionInCourse'}
60            brains = self.context.portal_catalog.searchResults(path)
61
62        elif self.isDivisionObject():
63            parent = self.getDivisionParent()
64            path = {'path':{'query':'/'.join(parent.getPhysicalPath())+'/'},
65                    'getExcludeFromNav':False,
66                    'portal_type':'Division',
67                    'sort_on':'sortable_title'}           
68            brains = self.context.portal_catalog.searchResults(path)
69                                                                   
70        else:
71            ecprops = self.context.portal_properties.educommons_properties
72            if ecprops.getProperty('school_enable'):
73                brains = self.context.portal_catalog.searchResults(
74                    Type='School',
75                    sort_on='sortable_title')                                                                   
76            else:
77                brains = self.context.portal_catalog.searchResults(
78                    Type='Division',
79                    sort_on='sortable_title')                                                                   
80                   
81        return [obj for obj in brains if not getattr(obj.aq_explicit, 'exclude_from_nav', True)]
82
83    def isCourseObject(self):
84        """ Check if this object is in a course. """
85        return 'Course' == self.ecparent.Type()
86
87    def isDivisionObject(self):
88        """ Check if this object is in a Division. """
89        return 'Division' == self.ecparent.Type()
90
91    def getDivisionParent(self):
92        """ Check if parent is Division of Portal. """
93        parent = self.context.aq_inner.aq_parent
94        if 'School' == parent.portal_type:
95            return parent
96        else:
97            return self.context.portal_url.getPortalObject()
98
99    def isSelected(self, item):
100        """ Check if the navigation item is the one being displayed. """
101        if '/'.join(item.getPath().split('/')[1:]) == self.context.virtual_url_path():
102            return 'portletItem portletItemSelected'
103        else:
104            return 'portletItem'
105       
106    def getDescriptor(self):
107        ts = getToolByName(self.context, 'translation_service')
108        ecprops = self.context.portal_url.portal_properties.educommons_properties
109        if self.isCourseObject():
110            return ts.translate(ecprops.course_descriptor)
111        elif self.isDivisionObject():
112            return ecprops.division_descriptor           
113        else:
114            ptypes = self.context.portal_url.portal_types
115            if ecprops.getProperty('school_enable'):
116                return ecprops.school_descriptor
117            return ecprops.division_descriptor
118           
119    def get_view_url(self, item):
120        props = self.context.portal_url.portal_properties
121        stp = props.site_properties
122        view_action_types = stp.getProperty('typesUseViewActionInListings', ())
123       
124        item_url = item.getURL()
125
126        if item.portal_type in view_action_types:
127            item_url += '/view'
128        elif item.portal_type == 'Link':
129            item_url = item.getRemoteUrl
130
131        return item_url
132       
133
134class AddForm(base.NullAddForm):
135    form_fields = form.Fields(ISimpleNavPortlet)
136   
137    def create(self):
138        return Assignment()
139
140
Note: See TracBrowser for help on using the repository browser.