[556] | 1 | from quills.app.browser.controllerView import FormControllerView, getToolByName |
---|
[538] | 2 | from quills.app.utilities import talkbackURL |
---|
[556] | 3 | from plone.memoize import instance |
---|
| 4 | from plone.app.content.browser.tableview import Table |
---|
| 5 | from zope.component import getMultiAdapter |
---|
| 6 | from zope.interface import implements |
---|
| 7 | import urllib, re |
---|
[538] | 8 | |
---|
[556] | 9 | |
---|
| 10 | talkback_url_extractor = re.compile("(.*)/talkback/\d+") |
---|
| 11 | |
---|
[538] | 12 | class ManageCommentsView(FormControllerView): |
---|
| 13 | """ The view class for the comments management form """ |
---|
| 14 | |
---|
| 15 | def __init__(self, context, request): |
---|
| 16 | super(ManageCommentsView, self).__init__(context, request) |
---|
| 17 | |
---|
| 18 | self.form_submitted = bool(request.get('form.submitted')) |
---|
| 19 | if bool(request.get('form.button.Delete')): |
---|
| 20 | self.mode = "delete" |
---|
| 21 | elif bool(request.get('form.button.Update')): |
---|
| 22 | self.mode = "update" |
---|
| 23 | elif bool(request.get('form.button.ResetFilter')): |
---|
| 24 | self.mode = "reset" |
---|
| 25 | elif bool(request.get('form.button.Publish')): |
---|
| 26 | self.mode = "publish" |
---|
| 27 | else: |
---|
| 28 | self.mode = 'display' |
---|
| 29 | |
---|
| 30 | self.author = request.get('form.field.author', '') |
---|
| 31 | self.subject = request.get('form.field.subject', '') |
---|
| 32 | self.review_state = request.get('form.field.review_state', '') |
---|
| 33 | self.selected_comments = request.get('selected_comments', []) |
---|
| 34 | self.portal_catalog = getToolByName(self.context,'portal_catalog') |
---|
| 35 | |
---|
| 36 | self.contentFilter = { |
---|
| 37 | 'portal_type' : 'Discussion Item', |
---|
| 38 | 'sort_on' : 'created', |
---|
| 39 | 'sort_order' : 'reverse', |
---|
| 40 | 'path' : {'query' : '/'.join(self.context.getPhysicalPath()),} |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | self.filtered = False |
---|
| 44 | if self.mode in ['delete', 'display', 'update']: |
---|
| 45 | if self.author: |
---|
| 46 | self.contentFilter['Creator'] = self.author |
---|
| 47 | self.filtered = True |
---|
| 48 | if self.subject: |
---|
| 49 | self.contentFilter['Title'] = self.subject |
---|
| 50 | self.filtered = True |
---|
[556] | 51 | if not self.review_state and self.contentFilter.has_key('review_state'): |
---|
| 52 | del self.contentFilter['review_state'] |
---|
| 53 | self.filtered = True |
---|
| 54 | elif self.review_state in ['published', 'private']: |
---|
| 55 | self.contentFilter['review_state'] = self.review_state |
---|
| 56 | self.filtered = True |
---|
| 57 | |
---|
[538] | 58 | if self.mode == "display": |
---|
| 59 | self.getComments() |
---|
| 60 | |
---|
| 61 | def validate(self): |
---|
| 62 | """ performs validation and returns an errors dictionary """ |
---|
| 63 | errors = {} |
---|
| 64 | if self.mode=='delete': |
---|
| 65 | if self.selected_comments == []: |
---|
| 66 | errors['status'] = 'failure' # errors must not be empty... |
---|
| 67 | self.setMessage('You must select at least one comment for deletion.') |
---|
| 68 | self.getComments() |
---|
| 69 | return errors |
---|
| 70 | |
---|
| 71 | def control(self): |
---|
| 72 | """ performs the actions after a successful validation possibly |
---|
| 73 | returning an errors dictionary """ |
---|
| 74 | |
---|
| 75 | if self.mode == "update": |
---|
| 76 | self.setMessage('Filter applied.') |
---|
| 77 | elif self.mode == "reset": |
---|
| 78 | self.setMessage('Filter reset.') |
---|
| 79 | self.author = None |
---|
| 80 | self.subject = None |
---|
| 81 | self.review_state = None |
---|
| 82 | elif self.mode == "delete": |
---|
| 83 | discussion_tool = getToolByName(self.context, 'portal_discussion') |
---|
| 84 | for path in self.selected_comments: |
---|
| 85 | self.deleteReply(discussion_tool, self.portal_catalog, path) |
---|
| 86 | self.setMessage('%s comments have been deleted.' % str(len(self.selected_comments))) |
---|
| 87 | elif self.mode == 'publish': |
---|
[556] | 88 | for path in self.selected_comments: |
---|
| 89 | self.publishReply(self.portal_catalog, path) |
---|
| 90 | self.setMessage('%s comments have been published.' % str(len(self.selected_comments))) |
---|
[538] | 91 | self.getComments() |
---|
| 92 | return {} |
---|
| 93 | |
---|
| 94 | def deleteReply(self, dtool, pcatalog, path): |
---|
| 95 | discussion_item = pcatalog(path=path)[0].getObject() |
---|
| 96 | obj = discussion_item.parentsInThread()[0] |
---|
| 97 | discussion = dtool.getDiscussionFor(obj) |
---|
| 98 | discussion.deleteReply(discussion_item.getId()) |
---|
| 99 | |
---|
[556] | 100 | def publishReply(self, pcatalog, path): |
---|
| 101 | """publish the discussion item""" |
---|
| 102 | discussion_item = pcatalog(path=path)[0].getObject() |
---|
| 103 | roles = ['Anonymous'] |
---|
| 104 | discussion_item.review_state = "published" |
---|
| 105 | discussion_item.manage_permission('View', roles, acquire=1) |
---|
| 106 | discussion_item._p_changed = 1 |
---|
| 107 | discussion_item.reindexObject() |
---|
[538] | 108 | |
---|
| 109 | def getComments(self): |
---|
| 110 | self.comment_brains = self.portal_catalog(self.contentFilter) |
---|
| 111 | self.num_of_comments = len(self.comment_brains) |
---|
| 112 | self.has_comments = self.num_of_comments > 0 |
---|
[556] | 113 | #return self.comment_brains |
---|
| 114 | plone_utils = getToolByName(self.context, 'plone_utils') |
---|
| 115 | plone_view = getMultiAdapter((self.context, self.request), name=u'plone') |
---|
| 116 | portal_workflow = getToolByName(self.context, 'portal_workflow') |
---|
| 117 | portal_properties = getToolByName(self.context, 'portal_properties') |
---|
| 118 | |
---|
| 119 | curl = self.context.absolute_url() |
---|
| 120 | results = [] |
---|
| 121 | for i, obj in enumerate(self.comment_brains): |
---|
| 122 | if (i + 1) % 2 == 0: |
---|
| 123 | table_row_class = "draggable even" |
---|
| 124 | else: |
---|
| 125 | table_row_class = "draggable odd" |
---|
| 126 | |
---|
| 127 | url = obj.getURL() |
---|
| 128 | path = obj.getPath or "/".join(obj.getPhysicalPath()) |
---|
| 129 | absolute_url = url[:url.find('/talkback')] |
---|
| 130 | url = "%s#%s" % (absolute_url, obj.id) |
---|
| 131 | icon = None |
---|
| 132 | |
---|
| 133 | review_state = obj.review_state |
---|
| 134 | state_class = 'state-' + plone_utils.normalizeString(review_state) |
---|
| 135 | relative_url = obj.getURL(relative=True) |
---|
| 136 | obj_type = obj.portal_type |
---|
| 137 | modified = plone_view.toLocalizedTime( |
---|
| 138 | obj.ModificationDate, long_format=1) |
---|
| 139 | view_url = url |
---|
| 140 | results.append(dict( |
---|
| 141 | url = url, |
---|
| 142 | Creator = obj.Creator, |
---|
| 143 | id = obj.getId, |
---|
| 144 | quoted_id = obj.getId, |
---|
| 145 | path = path, |
---|
| 146 | title_or_id = obj.Title, |
---|
| 147 | description = obj.Description, |
---|
| 148 | obj_type = obj_type, |
---|
| 149 | size = obj.getObjSize, |
---|
| 150 | modified = modified, |
---|
| 151 | icon = '', |
---|
| 152 | type_class = 'contenttype-discutionitem', |
---|
| 153 | wf_state = review_state, |
---|
| 154 | state_title = review_state, |
---|
| 155 | state_class = state_class, |
---|
| 156 | is_browser_default = 0, |
---|
| 157 | folderish = 0, |
---|
| 158 | relative_url = relative_url, |
---|
| 159 | view_url = view_url, |
---|
| 160 | table_row_class = table_row_class, |
---|
| 161 | is_expired = 0, |
---|
| 162 | )) |
---|
| 163 | self.table = Table(self.request, curl, curl+'/manage_comments', results) |
---|
[538] | 164 | return self.comment_brains |
---|
[556] | 165 | |
---|
| 166 | |
---|
[538] | 167 | def talkbackURL(self, item): |
---|
| 168 | return talkbackURL(item) |
---|