import re
import time
from Products.Five import BrowserView as BaseView
from AccessControl.SecurityInfo import ClassSecurityInfo
from AccessControl import getSecurityManager
from Globals import InitializeClass
from Products.Five.skin.standardmacros import Macros
from zope.component import getUtility
from Products.MetadataPortal.interfaces import IWaramu, iMDPortal, IUsers
from Products.MetadataPortal.config import perm_add_content
from Products.MetadataPortal.utils import getLanguage, translate
from Products.MetadataPortal.waramulib.Errors import AccessDenied, InvalidUID
RESOURES_ON_DESKTOP = 15
class MetadataPortalView(BaseView):
""" Metadata Portal view """
security = ClassSecurityInfo()
_rid = None
def __init__(self, context, request):
self.request = request
self.context = context
##w = self._getWaramu()
##if not w.isConnected():
## w.connect()
def _getWaramu(self):
pr = getUtility(iMDPortal, context=self.context)
w = getUtility(IWaramu, context=self.context).__of__(pr)
return w
def isLoggedIn(self):
u = getSecurityManager().getUser()
if u is None or u.getUserName() == 'Anonymous User':
return 0
return 1
def getUsername(self):
if self.isLoggedIn():
u = getSecurityManager().getUser()
return u.getUserName()
return "Anonymous"
def getRoles(self):
u = getSecurityManager().getUser()
uroles = u.getRolesInContext(self.context)
return uroles
def getGroups(self):
u = getSecurityManager().getUser()
try:
grs = u.getGroups()
except AttributeError:
return "no groups"
return grs
def has_role(self, role):
u = getSecurityManager().getUser()
uroles = u.getRolesInContext(self.context)
return role in uroles
def has_edit(self):
o = getSecurityManager().checkPermission(perm_add_content, self.context)
if o is None:
return False
return True
def test(self, cond, tru, fal):
if cond: return tru
return fal
def getResource(self, id):
""" get a resource """
w = self._getWaramu()
try:
return w.getResource(id)
except AccessDenied:
print "access denied:", id
return None
except InvalidUID:
print "invalid uid:", id
return None
def resolveFromId(self, id, src):
for s in src:
if s['id'] == id:
return s['title']
return "not found"
def getBrowseFields(self):
""" get a list of fields to browse by """
res = []
pr = getUtility(iMDPortal, context=self.context)
l = getattr(pr, '_browsable_fields', ())
w = self._getWaramu()
if w.isConnected():
fullist = w.getCombinedFieldsList()
for f in l:
res.append([f, fullist.get(f).get('printable')])
return res
def getAllFields(self):
fl = self._getWaramu().getCombinedFieldsList().items()
pr = getUtility(iMDPortal, context=self.context)
l = getattr(pr, '_browsable_fields', ())
res = []
for f, d in fl:
th = False
if f in l:
th = True
res.append([f, d, th])
return res
def linkToUserDesktop(self, uname):
""" returns a link to user's desktop. if user exists! """
um = getUtility(IUsers, context=self.context)
w = self._getWaramu()
suffix = ''
if '@' in uname:
suffix = uname[uname.index('@')+1:]
if w.getWaramuSuffix() != suffix:
return None
uname = uname[:uname.index('@')]
u = um.getUserByUsername(uname)
if u is None:
return None
return 'users/'+uname+'/desktop.html'
def formatTime(self, d):
""" format time """
# 2008-12-09 11:18:40.0 -> 2008-12-09 11:18:40
#d = re.sub('\..', '', d)
#dt = time.strptime(d, "%Y-%m-%d %H:%M:%S")
#dt = time.strptime(d, "%Y/%m/%d %H:%M:%S %Z")
#return time.strftime("%d.%m.%Y %H:%M", dt)
return d.strftime("%d.%m.%Y %H:%M")
def breadcrumbs(self, show_start=False):
if not show_start:
return ""
return u''+translate(self.context, 'start', getLanguage(self.request))+''
def stripSuffix(self, author):
return author[:author.index('@')]
InitializeClass(MetadataPortalView)
class ResourceView(MetadataPortalView):
""" resource view """
security = ClassSecurityInfo()
_rid = None
_tid = None
_currResource = None
def setRID(self, rid):
self._rid = rid
def getRID(self):
return self._rid
def setTypeID(self, tid):
self._tid = tid
def getTypeID(self):
return self._tid
def getResource(self):
if self.getRID() is not None:
if self._currResource is not None and self._currResource.getId() == self.getRID():
return self._currResource
self._currResource = MetadataPortalView.getResource(self, self.getRID())
return self._currResource
if self.getRID() is None and self._tid is None:
raise 'oops. creating new.. hacking.. or somethin\' wrong'
if self.getRID() is None and self._tid is not None:
from Products.MetadataPortal.Resource import Resource
w = self._getWaramu()
u = getSecurityManager().getUser()
#acl = ""+str(u)+""
#dat = " "+str(self.getTypeID())+""+acl+""
dat = None
um = getUtility(IUsers, context=self.context)
uobj = um.getUserByUsername(str(u))
ln = fn = org = email = ""
if uobj is not None:
ln = uobj.getLastName()
fn = uobj.getFirstName()
org = ""
email = uobj.getEmail()
defvcard = """BEGIN:VCARD
VERSION:3.0
N:%s;%s;;;
FN:%s
ORG:%s
EMAIL;TYPE=internet,pref:%s
END:VCARD""" % (ln, fn, fn+" "+ln, org, email)
r = Resource(w._getConnection(), dat)
r._acl['owner'] = u.getId()+'@'+w.getWaramuSuffix()
r.setTypeID(self.getTypeID())
r.setTypeDescription(w.getTypeDescription(self.getTypeID()))
r.setTypeInfos(w.getTypeInfos(self.getTypeID()))
r.set('vauthor', defvcard, None)
return r.__of__(self.context)
return None
def breadcrumbs(self):
return MetadataPortalView.breadcrumbs(self, True) + \
" > "+translate(self.context, 'resources', getLanguage(self.request))+" > " + self.getResource().getTitle()
InitializeClass(ResourceView)
class AlertView(MetadataPortalView):
_msg = ""
_color = "red"
_next = ""
def setMessage(self, msg):
""" set message to display to a user """
self._msg = msg
def getMessage(self):
return self._msg
def getColor(self):
return self._color
def setColor(self, c):
self._color = c
def getNext(self):
return self._next
def setNext(self, n):
self._next = n
def breadcrumbs(self):
return ""
InitializeClass(AlertView)
class StandardMacro(BaseView, Macros):
macro_pages = ('main_template',)