# -*- coding: utf-8 # Copyright 2006 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import FileField from Products.Archetypes.public import FileWidget from Products.Archetypes.public import OrderedBaseFolder, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Common import CommonMix as Common from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME from urllib2 import URLError schema = BaseSchema class EFolder(OrderedBaseFolder, Common): """ E-Folder. it'll be in drawer """ meta_type = "EFolder" archetype_name = "Folder" global_allow=0 exclude_from_nav = True filter_content_types = True allowed_content_types = ('File', 'Image', 'EFolder', 'Report', 'QCL', 'Photoalbum', 'Link') actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/drawer_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Access Denied',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Access Denied',), }, { 'id':'delete', 'name':'Delete', 'action':'string:${object_url}/delete_confirmation', 'permissions': ('Modify portal content',), }, { 'id':'competencies', 'name':'Associate with competency', 'action':'string:${object_url}/add_reference_to_competency?uid=${object/UID}', 'permissions': ('Modify portal content',), }, ) aliases = { '(Default)' : '', 'view' : 'drawer_view', 'edit' : 'base_edit', } def getLargeIconById(self, id): """ returns large icon name """ object = getattr(self, id) largeIcon = object.largeIcon if object.getLargeIcon(): if (object.getLargeIcon()=='drawer.gif' and object.meta_type!='Drawer'): if object.meta_type in ['Folder', 'ATFolder']: largeIcon = 'folder.gif' if object.meta_type in ['File', 'ATFile', 'ATImage']: if object.content_type in ['application/pdf']: largeIcon = 'pdffile.gif' elif object.content_type in ['text/html','text/xml','text/javascript','text/css']: largeIcon = 'htmlfile.gif' elif object.content_type in ['application/msword','text/rtf','text/richtext','application/rtf']: largeIcon = 'textfile.gif' elif object.content_type in ['application/vnd.ms-excel']: largeIcon = 'tablefile.gif' elif object.content_type in ['image/jpeg','image/png','image/gif','image/tiff','image/x-ms-bmp']: largeIcon = 'imagefile.gif' elif object.content_type in ['application/zip','application/x-tar']: largeIcon = 'zipfile.gif' return largeIcon def goBack(self, REQUEST): REQUEST.RESPONSE.redirect(self.absolute_url()) def addPhotoalbum(self, REQUEST): """ add photoalbum """ import time newId = 'photoalbum-'+str(time.time()).replace('.','') nid = self.invokeFactory("Photoalbum", id=newId) obj = getattr(self, nid) obj.setAlbum(REQUEST) return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/'+nid) registerType(EFolder, PROJECT_NAME) class Drawer(OrderedBaseFolder, Common): """ E-Portfolio Drawer folder for stuff ;) (files, documents, etc)""" meta_type = "Drawer" archetype_name = "Drawer" global_allow = 0 exclude_from_nav = True filter_content_types = True allowed_content_types = ('File', 'Image', 'EFolder', 'Report', 'QCL', 'Photoalbum', 'Link') security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/drawer_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Access Denied',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Access Denied',), }, ) aliases = { '(Default)' : '', 'view' : 'drawer_view', 'edit' : 'base_edit', } def __init__(self, id): self.id = id self.largeIcon = "drawer.gif" def allowRestricted(self): return True def allowAccepted(self): return False def getLargeIcon(self): """ returns large icon name """ return self.largeIcon def getLargeIconById(self, id): """ returns large icon name """ object = getattr(self, id) largeIcon = object.largeIcon if object.getLargeIcon(): if (object.getLargeIcon()=='drawer.gif' and object.meta_type!='Drawer'): if object.meta_type in ['Folder', 'ATFolder']: largeIcon = 'folder.gif' if object.meta_type in ['File', 'ATFile', 'ATImage']: if object.content_type in ['application/pdf']: largeIcon = 'pdffile.gif' elif object.content_type in ['text/html','text/xml','text/javascript','text/css']: largeIcon = 'htmlfile.gif' elif object.content_type in ['application/msword','text/rtf','text/richtext','application/rtf']: largeIcon = 'textfile.gif' elif object.content_type in ['application/vnd.ms-excel']: largeIcon = 'tablefile.gif' elif object.content_type in ['image/jpeg','image/png','image/gif','image/tiff','image/x-ms-bmp']: largeIcon = 'imagefile.gif' elif object.content_type in ['application/zip','application/x-tar']: largeIcon = 'zipfile.gif' return largeIcon def cutTitle(self, title, lenght): """ cut tail of title """ if len(title)==0: return "..." if len(title)>lenght: return title[:lenght]+'...' return title def RestrictItem(self, id, REQUEST, members='', groups=''): """ """ from Permissions import * ObjId = self._getOb(id) allow = REQUEST.get('access', '') if allow == '': last = REQUEST.get('last_status', 'public') if last == 'public': status = 'publish' if last == 'hidden': status = 'hide' return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/'+str(id)+'/restrict?last='+last) from config import TASK_TYPES viewers = ObjId.users_with_local_role('Viewer') # print viewers, '........' for v in viewers: ObjId.manage_delLocalRoles([v]) # print ObjId.users_with_local_role('Viewer'), ',,,,,,' ObjId.manage_permission(View, ('Manager','Owner','Viewer'), 0) if str(allow) == 'teachers': print 'teachers' user = str(self.REQUEST.AUTHENTICATED_USER) user_groups = self.REQUEST.AUTHENTICATED_USER.getGroups() user_groups.append(user) pc = getToolByName(self, 'portal_catalog') res = pc({'portal_type':TASK_TYPES, 'getTarget':user_groups}) gt = getToolByName(self, 'portal_groups') allgroups = gt.listGroups() lect = [] addedGroups = [] for x in res: task = x.getObject() target = task.getLecturer() print target for t in target: addedLect = 0 for l in lect: #print '--', l, t, '--' if str(l) == str(t): addedLect = 1 if addedLect == 0: lectgroup = 0 for g in allgroups: print g if str(g) == t: addedGroups.append(g) g_members = gt.getGroupMembers(g.id) #print g_members for m in g_members: added = 0 for l in lect: if str(l) == str(m): added = 1 if added == 0: lect.append(m) lectgroup = 1 if lectgroup == 0: lect.append(t) # print lect for user in lect: # print user ObjId.manage_setLocalRoles(str(user),('Viewer',)) if str(allow) == 'groupmembers': if isinstance(members, type('')): members = [members,] for user in members: # print user ObjId.manage_setLocalRoles(str(user),('Viewer',)) if str(allow) == 'groups': if isinstance(groups, type('')): groups = [groups,] for group in groups: # print group gt = getToolByName(self, 'portal_groups') groupObj = gt.getGroupById(group) groupmembers = groupObj.getGroupMembers() for user in groupmembers: # print user ObjId.manage_setLocalRoles(str(user),('Viewer',)) if str(allow) == 'community': print 'comm' ObjId.manage_permission(View, ('Manager','Owner','Viewer','Authenticated'), 0) return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/'+str(id)+'/view') def getDrawerItems(self, here): """ """ tmp = [] pm = getToolByName(self,'portal_membership') be = here.objectValues() for x in be: if pm.checkPermission('View', x): tmp.append(x) return tmp def getStatus(self, obj): """ """ object = self._getOb(obj) status = getToolByName(self, 'portal_workflow', None).getStatusOf('task_workflow',object) if status != None: state = status['review_state'] else: state = ' ' return state def getListOfSets(self, REQUEST): """ get flickr sets by username """ set_list = [] username = REQUEST.get('username', '') print 'uname', username if username == '': print 'none' return 'Insert username' import urllib url = "http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=3b123c3da3c5bd4ec58a33c0c411d821&username="+urllib.quote_plus(username) import urllib2 try: f = urllib2.urlopen(url) except URLError: print 'URLError' return '' html = f.read() print 'HTMl', html import xml.dom.minidom dom = xml.dom.minidom.parseString(html) user = dom.getElementsByTagName("user") print 'User:', user if user == []: error = dom.getElementsByTagName("err") error_msg = error[0].getAttribute("msg") return str(error_msg) user_id = user[0].getAttribute("id") photosets_url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=3b123c3da3c5bd4ec58a33c0c411d821&user_id="+str(user_id) try: f = urllib2.urlopen(photosets_url) except URLError: f = '' print 'URLError' return '' html = f.read() print 'List:', html dom = xml.dom.minidom.parseString(html) sets = dom.getElementsByTagName("photoset") for set in sets: set_id = set.getAttribute("id") title = set.getElementsByTagName("title") set_title = title[0].childNodes[0].data set_list.append([set_id, set_title]) return [user_id,set_list] def refreshPage(self, REQUEST): username = REQUEST.get('username') title = REQUEST.get('title') desc = REQUEST.get('description') REQUEST.RESPONSE.redirect('photoalbum_edit?service=flickr&username='+username+'&title='+title+'&desc='+desc) def goBack(self, REQUEST): REQUEST.RESPONSE.redirect(self.absolute_url()) def addPhotoalbum(self, REQUEST): """ add photoalbum """ import time newId = 'photoalbum-'+str(time.time()).replace('.','') nid = self.invokeFactory("Photoalbum", id=newId) obj = getattr(self, nid) obj.setAlbum(REQUEST) return self.REQUEST.RESPONSE.redirect(self.absolute_url()+'/'+nid) registerType(Drawer, PROJECT_NAME)