1 | from zope import schema |
---|
2 | from zope.formlib import form |
---|
3 | from zope.interface import implements |
---|
4 | from zope.component import getMultiAdapter |
---|
5 | |
---|
6 | from plone.app.portlets.portlets import base |
---|
7 | from plone.portlets.interfaces import IPortletDataProvider |
---|
8 | |
---|
9 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from plone.memoize.instance import memoize |
---|
12 | |
---|
13 | # Quills imports |
---|
14 | from quills.core.interfaces import IBaseContent |
---|
15 | from quills.core.interfaces import IWeblogEnhanced |
---|
16 | from quills.core.interfaces import IWeblog |
---|
17 | from quills.app.utilities import recurseToInterface |
---|
18 | from quills.app import QuillsAppMessageFactory as _ |
---|
19 | |
---|
20 | # Local imports |
---|
21 | from quills.app.portlets.base import BasePortletRenderer |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | PORTLET_TITLE = _(u"Recent Author") |
---|
26 | PORTLET_DESC = _(u"This portlet shows recent author.") |
---|
27 | |
---|
28 | |
---|
29 | class IRecentAuthorPortlet(IPortletDataProvider): |
---|
30 | |
---|
31 | blog_path = schema.TextLine( |
---|
32 | title=_(u"Path to Blog"), |
---|
33 | description=_(u"Physical path to blog, from the plone object, 'blog' for ex."), |
---|
34 | required=True) |
---|
35 | |
---|
36 | class Assignment(base.Assignment): |
---|
37 | |
---|
38 | implements(IRecentAuthorPortlet) |
---|
39 | |
---|
40 | def __init__(self, blog_path='blog'): |
---|
41 | self.blog_path= blog_path |
---|
42 | |
---|
43 | @property |
---|
44 | def title(self): |
---|
45 | return PORTLET_TITLE |
---|
46 | |
---|
47 | |
---|
48 | class Renderer(BasePortletRenderer, base.Renderer): |
---|
49 | |
---|
50 | _template = ViewPageTemplateFile('recent_author.pt') |
---|
51 | |
---|
52 | @property |
---|
53 | def title(self): |
---|
54 | return PORTLET_TITLE |
---|
55 | |
---|
56 | @property |
---|
57 | def authors(self): |
---|
58 | weblog = self.getWeblog() |
---|
59 | return weblog.getAuthors() |
---|
60 | |
---|
61 | def getWeblog(self): |
---|
62 | pstate = getMultiAdapter((self.context, self.request), name=u'plone_portal_state') |
---|
63 | portal = pstate.portal() |
---|
64 | weblog = portal.restrictedTraverse(str(self.data.blog_path)) |
---|
65 | return IWeblog(weblog) |
---|
66 | |
---|
67 | @property |
---|
68 | def getEntries(self): |
---|
69 | weblog = self.getWeblog() |
---|
70 | return weblog.getEntries(maximum=1) |
---|
71 | |
---|
72 | @property |
---|
73 | def getRecentAuthor(self): |
---|
74 | return self.getEntries[0].Creator |
---|
75 | |
---|
76 | def getPortraitFor(self, author): |
---|
77 | mtool = getToolByName(self.context, 'portal_membership') |
---|
78 | return mtool.getPersonalPortrait(author) |
---|
79 | |
---|
80 | def getInfoFor(self, author): |
---|
81 | mtool = getToolByName(self.context, 'portal_membership') |
---|
82 | info = mtool.getMemberInfo(author) |
---|
83 | if info is None: |
---|
84 | info = { 'fullname' : author, |
---|
85 | 'description' : '', |
---|
86 | 'location' : '', |
---|
87 | 'language' : '', |
---|
88 | 'home_page' : '', |
---|
89 | 'username' : author, |
---|
90 | } |
---|
91 | if not info['fullname']: |
---|
92 | info['fullname'] = author |
---|
93 | if not info['username']: |
---|
94 | info['username'] = author |
---|
95 | return info |
---|
96 | |
---|
97 | def getAuthorURL(self, author_id): |
---|
98 | weblog = self.getWeblogContentObject() |
---|
99 | return "%s/authors/%s" % (weblog.absolute_url(), author_id) |
---|
100 | |
---|
101 | def getAuthorsURL(self): |
---|
102 | weblog = self.getWeblogContentObject() |
---|
103 | return "%s/authors" % weblog.absolute_url() |
---|
104 | |
---|
105 | |
---|
106 | class AddForm(base.AddForm): |
---|
107 | form_fields = form.Fields(IRecentAuthorPortlet) |
---|
108 | label = _(u'add-portlet', default=u"Add ${portlet-name} Portlet", mapping={u'portlet-name': PORTLET_TITLE}) |
---|
109 | description = PORTLET_DESC |
---|
110 | |
---|
111 | def create(self, data): |
---|
112 | return Assignment(blog_path = data.get('blog_path', True)) |
---|
113 | |
---|
114 | class EditForm(base.EditForm): |
---|
115 | form_fields = form.Fields(IRecentAuthorPortlet) |
---|
116 | label = _(u'edit-portlet', default=u"Edit ${portlet-name} Portlet", mapping={u'portlet-name': PORTLET_TITLE}) |
---|
117 | description = PORTLET_DESC |
---|