# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import FileField, TextField, ReferenceField, StringField from Products.Archetypes.public import FileWidget, StringWidget from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Products.Archetypes.public import AnnotationStorage from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME schema = BaseSchema class DynLink(BaseContent): """ DynLink """ meta_type = "DynLink" archetype_name = "DynLink" global_allow = 1 filter_content_types = True allowed_content_types = () security = ClassSecurityInfo() schema = schema + Schema(( ReferenceField('link', accessor = 'getRefsToLink', relationship = 'References', mutator = 'addRefsToLink', allowed_types = (), multiValued = False, widget = ReferenceBrowserWidget( force_close_on_insert = True, startup_directory = "eportfolio", label = "Links", label_msgid = "label_link", description="Choose object you want to link with", description_msgid='desc_link', i18n_domain = "eportfolio", visible = {'view':'visible', 'edit':'visible'},) ), StringField('link2', accessor="getLink2", widget=StringWidget( label="Alternate link", label_msgid='label_alternate_link', description="Fill this field only, if you point to object with text url not as object, hints for member type {member} and for school type {school}", description_msgid='desc_alternate_link', visible={'view':'invisible','edit':'visible'}, i18n_domain="eportfolio"), ), )) actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/dynlink_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Manage Portal',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Manage Portal',), }, ) def getLinkToObject(self): """ gets link to object """ object = self.getRefsToLink() if object: url = object.absolute_url() else: url = self.link2 return url def redirect(self,REQUEST): """ redirect to url """ url = self.getLinkToObject() member = str(REQUEST.AUTHENTICATED_USER) pm = getToolByName(self, 'portal_membership') memberObj = pm.getMemberById(member) print memberObj if memberObj == None: school = '/' else: school = str(memberObj.school) url = url.replace('{member}', member) url = url.replace('{school}', school) return REQUEST.RESPONSE.redirect(url) registerType(DynLink, PROJECT_NAME)