class UrlFormSchema(model.Schema): url = schema.ASCIILine(title=_(u"URL")) title = schema.TextLine(title=_(u"Title")) description = schema.Text(title=_(u"Description"), required=False) tags = schema.Tuple(title=_(u'Tags'), value_type=schema.TextLine(), required=False) folder = schema.Choice( title=_(u"Folder"), vocabulary="collective.readitlater.vocabulary.content")
class IReaditlaterSettings(interface.Interface): """Global configuration fo collective.readitlater add-on.""" folder_query = schema.ASCII( title=_(u"Folder query"), description=_(u"help_readitlater_folder_query", default=folder_query_desc), ) @interface.invariant def _folder_query_is_json(obj): try: json.loads(obj.folder_query) except ValueError: raise interface.Invalid("Not a valid JSON.")
def handleApply(self, action): data, errors = self.extractData() if errors: return folder = self._getFolder(data['folder']) if folder is None: error = interface.Invalid(_(u'Unknown folder.')) raise ActionExecutionError(error) try: self._createUrl(folder, data) except Unauthorized: error = interface.Invalid(_(u'Permission denied.')) raise ActionExecutionError(error) except ValueError: error = interface.Invalid(_(u'Could not add to folder.')) raise ActionExecutionError(error) else: self.request.response.redirect('@@collective_readitlater_urladded')
class ReaditlaterSettingsEditForm(controlpanel.RegistryEditForm): schema = IReaditlaterSettings label = _(u"collective.readitlater settings")
class UrlForm(AutoExtensibleForm, form.Form): schema = UrlFormSchema enableCSRFProtection = True label = _(u'Read it later') def update(self): super(UrlForm, self).update() self.widgets['url'].mode = 'hidden' #detect submit if self.widgets['url'].value: return #map widgets values with corresponding URL parameters url = self.request.form.get('url', '') self.widgets['url'].value = url title = self.request.form.get('title', '') self.widgets['title'].value = title description = self.request.form.get('description', '') if description != 'undefined': self.widgets['description'].value = description tags = self.request.form.get('tags', '') if tags != 'undefined': self.widgets['tags'].value = tags.replace(', ', '\n')\ .replace(',', '\n') @button.buttonAndHandler(_(u"Read it later"), name='submit') def handleApply(self, action): data, errors = self.extractData() if errors: return folder = self._getFolder(data['folder']) if folder is None: error = interface.Invalid(_(u'Unknown folder.')) raise ActionExecutionError(error) try: self._createUrl(folder, data) except Unauthorized: error = interface.Invalid(_(u'Permission denied.')) raise ActionExecutionError(error) except ValueError: error = interface.Invalid(_(u'Could not add to folder.')) raise ActionExecutionError(error) else: self.request.response.redirect('@@collective_readitlater_urladded') def _createUrl(self, folder, data): normalize = component.getUtility(IIDNormalizer) id = normalize.normalize(data['title']) if id in folder: i = 1 while '%s-%d' % (id, i) in folder: i += 1 id = '%s-%d' % (id, i) url = folder.invokeFactory(type_name='Link', id=id) link = folder[url] link.remoteUrl = data['url'] link.setTitle(data['title']) link.setDescription(data['description']) if hasattr(link, 'setSubject'): link.setSubject(data['tags']) else: link.subject = data['tags'] link.reindexObject() # Dexterity Only #url = createContent('Link') #url.remoteUrl = data['url'] #url.title = data['title'] #url.description = data['description'] #addContentToContainer(folder, url) def _getFolder(self, folder_uid): catalog = getToolByName(self.context, 'portal_catalog') brains = catalog.searchResults(UID={'query': folder_uid}, ) if len(brains) == 0: return None brain = brains[0] if brain is None: return None return brain.getObject()