def create_folders(container, folder_tree, report_path, skip_existing=False): """ `container` - reference to a NyFolder object `folder_tree` - list of two-tuples; in each tuple, first element is name of (current) folder; second element is another list of two-tuples. """ folder_map = {} for kid_name, kid_tree in folder_tree: if not (skip_existing and container._getOb(slugify(kid_name), None)): kid_id = addNyFolder(container, title=kid_name, _send_notifications=False) else: kid_id = slugify(kid_name) kid_folder = container[kid_id] folder_map[kid_name] = kid_folder report_path(kid_id + '/') kid_report_path = lambda p: report_path('%s/%s' % (kid_id, p)) kid_folder_map = create_folders(kid_folder, kid_tree, kid_report_path, skip_existing) for sub_kid_name, folder in kid_folder_map.iteritems(): folder_map['%s/%s' % (kid_name, sub_kid_name)] = folder return folder_map
def _update(self, portal): context = portal.restrictedTraverse('who-who/destinet-users') cat = portal['portal_catalog'] acl_users = portal['acl_users'] users = acl_users.getUsers() auth_tool = portal.getAuthenticationTool() #manager = self.request.AUTHENTICATED_USER.getUserName() # first, cleanup wrongly created users wrong = [o for o in context.objectValues() if o._owner[1] == 'tibiadmin'] self.log.info("Deleting %s wrong contacts" % len(wrong)) for obj in wrong: cat.uncatalog_object(ofs_path(obj)) context.manage_delObjects([o.id for o in wrong]) self.log.info("Migration: start migration of contacts for old users") EmailTool.divert_mail() counter = 0 for user in users: fullname = auth_tool.getUserFullNameByID(user.name) contacts = cat.searchResults(path=ofs_path(context), contributor=user.name) if not contacts: counter += 1 id = uniqueId( slugify(user.name or 'contact', removelist=[]), lambda x: context._getOb(x, None) is not None) ob = _create_NyContact_object(context, id, user.name) ob.approveThis(1, user.name) # 1, manager ob.submitThis() ob.set_localpropvalue('title', 'en', fullname) ob.set_localpropvalue('description', 'en', "") #ob.release_date = DateTime() new_user = user.__of__(acl_users) ob.changeOwnership(user=new_user) ob.giveEditRights() context.recatalogNyObject(ob) #crashes with unicodedecodeerror: #notify(NyContentObjectAddEvent(ob, user.name, {})) #log post date auth_tool.changeLastPost(user.name) self.log.info("Migration: %s - added contact for user: %s", counter, id) EmailTool.divert_mail(False) self.log.info("Migration: end migration") return True
def manage_addWidget(klass, container, id="", title=None, REQUEST=None, **kwargs): """Add widget""" if not title: title = str(klass) if not id: # prevent any name clashes by using the 'w_' prefix id = 'w_' + slugify(title) idSuffix = '' while (id + idSuffix in container.objectIds() or getattr(container, id + idSuffix, None) is not None): idSuffix = genRandomId(p_length=4) id = id + idSuffix # Get selected language lang = None if REQUEST is not None: lang = REQUEST.form.get('lang', None) if not lang: lang = kwargs.get('lang', container.gl_get_selected_language()) widget = klass(id, title=title, lang=lang, **kwargs) container.gl_add_languages(widget) container._setObject(id, widget) widget = container._getOb(id) if REQUEST is not None: REQUEST.RESPONSE.redirect(REQUEST.HTTP_REFERER) return id
def manage_addWidget(klass, container, id="", title=None, REQUEST=None, **kwargs): """Add widget""" if not title: title = str(klass) if not id: # prevent any name clashes by using the 'w_' prefix id = 'w_' + slugify(title) idSuffix = '' while (id+idSuffix in container.objectIds() or getattr(container, id+idSuffix, None) is not None): idSuffix = genRandomId(p_length=4) id = id + idSuffix # Get selected language lang = None if REQUEST is not None: lang = REQUEST.form.get('lang', None) if not lang: lang = kwargs.get('lang', container.gl_get_selected_language()) widget = klass(id, title=title, lang=lang, **kwargs) container.gl_add_languages(widget) container._setObject(id, widget) widget = container._getOb(id) if REQUEST is not None: REQUEST.RESPONSE.redirect(REQUEST.HTTP_REFERER) return id
def addNyURL(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create an URL type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate(schema_raw_data.pop('releasedate', '')) schema_raw_data.setdefault('locator', '') id = uniqueId(slugify(id or schema_raw_data.get('title', '') or 'url', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyURL_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/url_add_html' % self.absolute_url()) return #process parameters if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'url_manage_add' or l_referer.find('url_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'url_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) else: # undefined state (different referer, called in other context) return ob return ob.getId()
def _create_NyStory_object(parent, id, contributor): id = uniqueId(slugify(id or "story", removelist=[]), lambda x: parent._getOb(x, None) is not None) ob = NyStory(id, contributor) parent.gl_add_languages(ob) parent._setObject(id, ob) ob = parent._getOb(id) ob.after_setObject() return ob
def get_document_or_create(site, title): folder = site[FOLDER["id"]] try: doc = folder[slugify(title, removelist=[])] except KeyError: doc_id = addNyDocument(folder, title=title, submitted=1) doc = folder[doc_id] return doc
def _create_NyFile_object(parent, id, title, file, precondition, contributor): id = uniqueId(slugify(id or title or 'file', removelist=[]), lambda x: parent._getOb(x, None) is not None) ob = NyFile_extfile(id, title, file, precondition, contributor) parent.gl_add_languages(ob) parent._setObject(id, ob) ob = parent._getOb(id) ob.after_setObject() return ob
def _create_NyURL_object(parent, id, contributor): id = uniqueId(slugify(id or 'url', removelist=[]), lambda x: parent._getOb(x, None) is not None) ob = NyURL(id, contributor) parent.gl_add_languages(ob) parent._setObject(id, ob) ob = parent._getOb(id) ob.after_setObject() return ob
def addNyStory(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a Story type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', '')) schema_raw_data.setdefault('body', '') schema_raw_data.setdefault('resourceurl', '') schema_raw_data.setdefault('source', '') schema_raw_data.setdefault('topitem', '') _frontpicture = schema_raw_data.pop('frontpicture', '') id = uniqueId( slugify(id or schema_raw_data.get('title', '') or 'story', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyStory_object(self, id, contributor) ob._setLocalPropValue('title', _lang, '') sortorder_widget = ob._get_schema().getWidget('sortorder') schema_raw_data.setdefault('sortorder', sortorder_widget.default) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate, _all_values=False) if form_errors: raise ValueError(form_errors.popitem()[1]) # pick a random error ob.setFrontPicture(_frontpicture) if kwargs.has_key('submitted'): ob.submitThis() self.recatalogNyObject(ob) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: if REQUEST.has_key('submitted'): ob.submitThis() l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'story_manage_add' or l_referer.find( 'story_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'story_add_html': self.setSession('referer', self.absolute_url()) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) return ob.getId()
def addNyStory(self, id="", REQUEST=None, contributor=None, **kwargs): """ Create a Story type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop("_lang", schema_raw_data.pop("lang", None)) _releasedate = self.process_releasedate(schema_raw_data.pop("releasedate", "")) schema_raw_data.setdefault("body", "") schema_raw_data.setdefault("resourceurl", "") schema_raw_data.setdefault("source", "") schema_raw_data.setdefault("topitem", "") _frontpicture = schema_raw_data.pop("frontpicture", "") id = uniqueId( slugify(id or schema_raw_data.get("title", "") or "story", removelist=[]), lambda x: self._getOb(x, None) is not None, ) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyStory_object(self, id, contributor) ob._setLocalPropValue("title", _lang, "") sortorder_widget = ob._get_schema().getWidget("sortorder") schema_raw_data.setdefault("sortorder", sortorder_widget.default) form_errors = ob.process_submitted_form( schema_raw_data, _lang, _override_releasedate=_releasedate, _all_values=False ) if form_errors: raise ValueError(form_errors.popitem()[1]) # pick a random error ob.setFrontPicture(_frontpicture) if kwargs.has_key("submitted"): ob.submitThis() self.recatalogNyObject(ob) # log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) # redirect if case if REQUEST is not None: if REQUEST.has_key("submitted"): ob.submitThis() l_referer = REQUEST["HTTP_REFERER"].split("/")[-1] if l_referer == "story_manage_add" or l_referer.find("story_manage_add") != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == "story_add_html": self.setSession("referer", self.absolute_url()) REQUEST.RESPONSE.redirect("%s/messages_html" % self.absolute_url()) return ob.getId()
def setupContentType(site): ptool = site.getPortletsTool() for topics_list in LISTS: list_id = topics_list['list_id'] list_title = topics_list['list_title'] itopics = getattr(ptool, list_id, None) if not itopics: ptool.manage_addRefTree(list_id, list_title) itopics = getattr(ptool, list_id, None) for list_item in topics_list['list_items']: itopics.manage_addRefTreeNode(slugify(list_item), list_item)
def process_add(self, REQUEST, **kwargs): """ """ schema_raw_data = dict(REQUEST.form) _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', ''), self.releasedate) parent = self.getParentNode() id = uniqueId(slugify(schema_raw_data.get('title', '') or ID_PLACEHOLDER, removelist=[]), lambda x: parent._getOb(x, None) is not None) schema_raw_data['title'] = schema_raw_data.get('title', '') schema_raw_data['description'] = schema_raw_data.get('description', '') schema_raw_data['body'] = schema_raw_data.get('body', '') #check mandatory fiels l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] form_errors = self.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) submitter_errors = submitter.info_check(self.aq_parent, REQUEST, self) form_errors.update(submitter_errors) if not form_errors: #replace the old id with the new one #(for absolute URLs or pictures) parent.manage_renameObjects([self.id], [id]) if self.checkPermissionSkipApproval(): approved = 1 approved_by = self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved = 0 approved_by = None self.approveThis(approved, approved_by) self.submitThis() self.recatalogNyObject(self) notify(NyContentObjectAddEvent(self, self.contributor, schema_raw_data)) self.setSession('referer', self.getParentNode().absolute_url()) return self.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.getParentNode().absolute_url()) else: #XXX: `l_referer` is not used l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] self._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/add_html' % self.absolute_url())
def process_add(self, REQUEST, **kwargs): """ """ schema_raw_data = dict(REQUEST.form) _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', ''), self.releasedate) _frontpicture = schema_raw_data.pop('frontpicture', '') parent = self.getParentNode() id = uniqueId(slugify(schema_raw_data.get('title', '') or 'story'), lambda x: parent._getOb(x, None) is not None) schema_raw_data['title'] = schema_raw_data['title'].replace( self.id, id) schema_raw_data['description'] = schema_raw_data[ 'description'].replace(self.id, id) schema_raw_data['body'] = schema_raw_data['body'].replace(self.id, id) #check mandatory fiels l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] form_errors = self.process_submitted_form( schema_raw_data, _lang, _override_releasedate=_releasedate) submitter_errors = submitter.info_check(self.aq_parent, REQUEST, self) form_errors.update(submitter_errors) if not form_errors: parent.manage_renameObjects([self.id], [id]) if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName( ) else: approved, approved_by = 0, None self.setFrontPicture(_frontpicture) self.approveThis(approved, approved_by) self.submitThis() self.recatalogNyObject(self) notify( NyContentObjectAddEvent(self, self.contributor, schema_raw_data)) self.setSession('referer', self.getParentNode().absolute_url()) return self.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.getParentNode().absolute_url()) else: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] self._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/add_html' % self.absolute_url())
def addNyStory(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a Story type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate(schema_raw_data.pop('releasedate', '')) schema_raw_data.setdefault('body', '') schema_raw_data.setdefault('resourceurl', '') schema_raw_data.setdefault('source', '') schema_raw_data.setdefault('topitem', '') _frontpicture = schema_raw_data.pop('frontpicture', '') id = uniqueId(slugify(id or schema_raw_data.get('title', '') or 'story', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyStory_object(self, id, contributor) ob._setLocalPropValue('title', _lang, '') sortorder_widget = ob._get_schema().getWidget('sortorder') schema_raw_data.setdefault('sortorder', sortorder_widget.default) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate, _all_values=False) if form_errors: raise ValueError(form_errors.popitem()[1]) # pick a random error ob.setFrontPicture(_frontpicture) if kwargs.has_key('submitted'): ob.submitThis() if self.discussion: ob.open_for_comments() self.recatalogNyObject(ob) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: if REQUEST.has_key('submitted'): ob.submitThis() l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'story_manage_add' or l_referer.find('story_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'story_add_html': self.setSession('referer', self.absolute_url()) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) return ob.getId()
def _update(self, portal): cat = portal['portal_catalog'] users = portal['acl_users'].getUsers() auth_tool = portal.getAuthenticationTool() context = portal.restrictedTraverse('who-who/destinet-users') #approved, approved_by = 1, self.request.AUTHENTICATED_USER.getUserName() approved, approved_by = 0, None self.log.info("Migration: start migration of contacts for old users") EmailTool.divert_mail() counter = 0 for user in users: fullname = auth_tool.getUserFullNameByID(user.name) contacts = cat.searchResults(meta_type='Naaya Contact', title=fullname) if not contacts: counter += 1 id = uniqueId(slugify(user.name or 'contact', removelist=[]), lambda x: context._getOb(x, None) is not None) ob = _create_NyContact_object(context, id, user.name) ob.approveThis(approved, approved_by) ob.submitThis() ob.title = auth_tool.getUserFullNameByID(id) ob.description = '' context.recatalogNyObject(ob) #crashes with unicodedecodeerror: #notify(NyContentObjectAddEvent(ob, user.name, {})) #log post date auth_tool.changeLastPost(user.name) self.log.info("Migration: %s - added contact for user: %s", counter, id) EmailTool.divert_mail(False) self.log.info("Migration: end migration") return True
def process_add(self, REQUEST, **kwargs): """ """ schema_raw_data = dict(REQUEST.form) _lang = schema_raw_data.pop("_lang", schema_raw_data.pop("lang", None)) _releasedate = self.process_releasedate(schema_raw_data.pop("releasedate", ""), self.releasedate) _frontpicture = schema_raw_data.pop("frontpicture", "") parent = self.getParentNode() id = uniqueId( slugify(schema_raw_data.get("title", "") or "story"), lambda x: parent._getOb(x, None) is not None ) schema_raw_data["title"] = schema_raw_data["title"].replace(self.id, id) schema_raw_data["description"] = schema_raw_data["description"].replace(self.id, id) schema_raw_data["body"] = schema_raw_data["body"].replace(self.id, id) # check mandatory fiels l_referer = REQUEST["HTTP_REFERER"].split("/")[-1] form_errors = self.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) submitter_errors = submitter.info_check(self.aq_parent, REQUEST, self) form_errors.update(submitter_errors) if not form_errors: parent.manage_renameObjects([self.id], [id]) if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None self.setFrontPicture(_frontpicture) self.approveThis(approved, approved_by) self.submitThis() self.recatalogNyObject(self) notify(NyContentObjectAddEvent(self, self.contributor, schema_raw_data)) self.setSession("referer", self.getParentNode().absolute_url()) return self.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect("%s/messages_html" % self.getParentNode().absolute_url()) else: l_referer = REQUEST["HTTP_REFERER"].split("/")[-1] self._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect("%s/add_html" % self.absolute_url())
def process_add(self, REQUEST, **kwargs): """ """ schema_raw_data = dict(REQUEST.form) _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate(schema_raw_data.pop('releasedate', ''), self.releasedate) _frontpicture = schema_raw_data.pop('frontpicture', '') parent = self.getParentNode() id = uniqueId(slugify(schema_raw_data.get('title', '') or 'story'), lambda x: parent._getOb(x, None) is not None) schema_raw_data['title'] = schema_raw_data['title'].replace(self.id, id) schema_raw_data['description'] = schema_raw_data['description'].replace(self.id, id) schema_raw_data['body'] = schema_raw_data['body'].replace(self.id, id) #check mandatory fiels l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] form_errors = self.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) submitter_errors = submitter.info_check(self.aq_parent, REQUEST, self) form_errors.update(submitter_errors) if not form_errors: parent.manage_renameObjects([self.id], [id]) if self.glCheckPermissionPublishObjects(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None self.setFrontPicture(_frontpicture) self.approveThis(approved, approved_by) self.submitThis() if self.discussion: self.open_for_comments() self.recatalogNyObject(self) notify(NyContentObjectAddEvent(self, self.contributor, schema_raw_data)) self.setSession('referer', self.getParentNode().absolute_url()) return self.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.getParentNode().absolute_url()) else: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] self._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/add_html' % self.absolute_url())
def get_category_location(site, geo_type): """ Based on geo_type (Category) value, returns the corresponding location in who-who, who-who/market-place, who-who/market-solutions or None if not found """ if not geo_type: return None widget = GeoTypeWidget('').__of__(site) title = widget.convert_to_user_string(geo_type).replace('&', 'and') slug = slugify(title, removelist=[]) who_who = site['who-who'] candidates = map(lambda x: (x, x.objectIds('Naaya Folder')), [who_who, who_who['market-place'], who_who['market-solutions']]) for (candidate_parent, candidate_ids) in candidates: if slug in candidate_ids: return candidate_parent[slug] return None
def get_category_location(site, geo_type): """ Based on geo_type (Category) value, returns the corresponding location in who-who, who-who/market-place, who-who/market-solutions or None if not found """ if not geo_type: return None widget = GeoTypeWidget('').__of__(site) title = widget.convert_to_user_string(geo_type).replace('&', 'and') slug = slugify(title, removelist=[]) who_who = site['who-who'] candidates = map( lambda x: (x, x.objectIds('Naaya Folder')), [who_who, who_who['market-place'], who_who['market-solutions']]) for (candidate_parent, candidate_ids) in candidates: if slug in candidate_ids: return candidate_parent[slug] return None
def manage_addSurveyReport(context, id="", title="", REQUEST=None, **kwargs): """ ZMI method that creates an object of this type. """ if not id: id = slugify(title) idSuffix = '' while id+idSuffix in context.objectIds(): idSuffix = genRandomId(p_length=4) id = id + idSuffix # Get selected language lang = REQUEST and REQUEST.form.get('lang', None) lang = lang or kwargs.get('lang', context.gl_get_selected_language()) ob = SurveyReport(id, lang=lang, title=title) context.gl_add_languages(ob) context._setObject(id, ob) if REQUEST is not None: context.manage_main(context, REQUEST, update_menu=1) return id
def addNyFile(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a File type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate(schema_raw_data.pop('releasedate', '')) _source = schema_raw_data.pop('source', 'file') _file = schema_raw_data.pop('file', '') _url = schema_raw_data.pop('url', '') _precondition = schema_raw_data.pop('precondition', '') title = schema_raw_data.get('title', '') #process parameters if _source=='file': id = cookId(id, title, _file)[0] #upload from a file id = uniqueId(slugify(id or title or 'file', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyFile_object(self, id, title, '', _precondition, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if hasattr(_file, 'read') and not _file.read() and not _url: form_errors['file'] = ['File upload or URL is mandatory'] if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/file_add_html' % self.absolute_url()) return #process parameters if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None ob.submitThis() _send_notif = kwargs.get('_send_notifications', True) ob.approveThis(approved, approved_by, _send_notifications=_send_notif) ob.handleUpload(_source, _file, _url) self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'file_manage_add' or l_referer.find('file_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'file_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) else: # undefined state (different referer, called in other context) return ob return ob.getId()
def addNyCaseStudy(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a `NyCaseStudy` type of object. """ #process parameters if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', '')) id = uniqueId( slugify(id or schema_raw_data.get('title', '') or 'case_study', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyCaseStudy_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) spatial_scale = schema_raw_data.get('spatial_scale') geographical_scope = schema_raw_data.get('geographical_scope', []) if (spatial_scale == 'regional-case-study-covers-more-one-country' and len(geographical_scope) < 2): form_errors.setdefault('geographical_scope', []) form_errors['geographical_scope'].append( 'For regional case studies please select at least 2 countries') if (spatial_scale and spatial_scale != 'regional-case-study-covers-more-one-country' and len(geographical_scope) > 1): form_errors.setdefault('geographical_scope', []) form_errors['geographical_scope'].append( 'For national or sub-national case studies please select exactly ' 'one country') if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) return REQUEST.RESPONSE.redirect('%s/case_study_add_html' % self.absolute_url()) return if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName( ) else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'case_study_manage_add' or l_referer.find( 'case_study_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'case_study_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) else: # undefined state (different referer, called in other context) return ob return ob.getId()
def addNyURL(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create an URL type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', '')) schema_raw_data.setdefault('locator', '') id = uniqueId( slugify(id or schema_raw_data.get('title', '') or 'url', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyURL_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/url_add_html' % self.absolute_url()) return # process parameters if self.checkPermissionSkipApproval(): approved, approved_by = (1, self.REQUEST.AUTHENTICATED_USER.getUserName()) else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) # log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) # redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if (l_referer == 'url_manage_add' or l_referer.find('url_manage_add') != -1): return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'url_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) else: # undefined state (different referer, called in other context) return ob return ob.getId()
def addNyFile(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a File type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', '')) _source = schema_raw_data.pop('source', 'file') _file = schema_raw_data.pop('file', '') _url = schema_raw_data.pop('url', '') _precondition = schema_raw_data.pop('precondition', '') title = schema_raw_data.get('title', '') #process parameters if _source == 'file': id = cookId(id, title, _file)[0] #upload from a file id = uniqueId(slugify(id or title or 'file', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyFile_object(self, id, title, '', _precondition, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if hasattr(_file, 'read') and not _file.read() and not _url: form_errors['file'] = ['File upload or URL is mandatory'] if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/file_add_html' % self.absolute_url()) return #process parameters if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName( ) else: approved, approved_by = 0, None ob.submitThis() _send_notif = kwargs.get('_send_notifications', True) ob.approveThis(approved, approved_by, _send_notifications=_send_notif) ob.handleUpload(_source, _file, _url) self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'file_manage_add' or l_referer.find( 'file_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'file_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) else: # undefined state (different referer, called in other context) return ob return ob.getId()
folder_map = create_folders(container, folder_tree, created_file_paths.add, overwrite) folder_map[''] = container for file_path, file_data in zip_files: if '/' in file_path: file_container_path, file_name = file_path.rsplit( '/', 1) else: file_container_path, file_name = '', file_path file_name = file_name.encode('utf-8') assert file_container_path in folder_map try: file_container = folder_map[file_container_path] if overwrite: filename = slugify(file_name).rsplit('.', 1)[0] if file_container._getOb(filename, None): file_container.manage_delObjects([filename]) file_ob_id = add_file(file_container, file_name, file_data) file_ob = file_container[file_ob_id] except Exception, e: errors.append( ((u"Error while creating file ${file_path}: " "${error}"), { 'file_path': file_path, 'error': force_to_unicode(str(e)) })) else: p = relative_object_path(file_ob, container) created_file_paths.add(p)
def addNyGeoPoint(self, id="", REQUEST=None, contributor=None, **kwargs): """ Create an Contact type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop("_lang", schema_raw_data.pop("lang", None)) _releasedate = self.process_releasedate(schema_raw_data.pop("releasedate", "")) title = schema_raw_data["title"] # process parameters id = uniqueId(slugify(id or title or "geopoint", removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyGeoPoint_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect("%s/geopoint_add_html" % self.absolute_url()) return # process parameters if self.glCheckPermissionPublishObjects(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() if ob.discussion: ob.open_for_comments() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) # log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) # redirect if case if REQUEST is not None: l_referer = REQUEST["HTTP_REFERER"].split("/")[-1] if l_referer == "geopoint_manage_add" or l_referer.find("geopoint_manage_add") != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == "geopoint_add_html": self.setSession("referer", self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect("%s/messages_html" % self.absolute_url()) return ob.getId()
folder_map = create_folders(container, folder_tree, created_file_paths.add, overwrite) folder_map[''] = container for file_path, file_data in zip_files: if '/' in file_path: file_container_path, file_name = file_path.rsplit('/', 1) else: file_container_path, file_name = '', file_path file_name = file_name.encode('utf-8') assert file_container_path in folder_map try: file_container = folder_map[file_container_path] if overwrite: filename = slugify(file_name).rsplit('.', 1)[0] if file_container._getOb(filename, None): file_container.manage_delObjects([filename]) file_ob_id = add_file(file_container, file_name, file_data) file_ob = file_container[file_ob_id] except Exception, e: errors.append(( (u"Error while creating file ${file_path}: " "${error}"), {'file_path': file_path, 'error': force_to_unicode(str(e))})) else: p = relative_object_path(file_ob, container) created_file_paths.add(p)
def assert_slug(self, initial_string, expected_slug): slug = slugify(initial_string) self.assertEqual(slug, expected_slug)
def addNyMediaFile(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a File type of object. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate(schema_raw_data.pop('releasedate', '')) _file = schema_raw_data.pop('file', None) _subtitle = schema_raw_data.pop('subtitle', '') _skip_videofile_check = schema_raw_data.pop('_skip_videofile_check', False) id = uniqueId(slugify(id or schema_raw_data.get('title', '') or 'mediafile', removelist=[]), lambda x: self._getOb(x, None) is not None) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyMediaFile_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if not _skip_videofile_check: video_errors = _check_video_file(_file) if video_errors: form_errors['mediafile'] = video_errors if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/mediafile_add_html' % self.absolute_url()) return #process parameters if self.glCheckPermissionPublishObjects(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() ob.handleMediaUpload(_file) ob._setLocalPropValue('subtitle', _lang, _subtitle) if ob.discussion: ob.open_for_comments() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) #log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) #redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if l_referer == 'mediafile_manage_add' or l_referer.find('mediafile_manage_add') != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'mediafile_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) return ob.getId()
def addNyTalkBackConsultation(self, id="", REQUEST=None, contributor=None, **kwargs): """ Create a Naaya TalkBack Consultation type of object. """ l_referer = "/" if REQUEST is not None: l_referer = REQUEST.get("HTTP_REFERER", "/").split("/")[-1] schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop("_lang", schema_raw_data.pop("lang", None)) _releasedate = self.process_releasedate(schema_raw_data.pop("releasedate", "")) id = uniqueId( slugify(id or schema_raw_data.get("title", ""), removelist=[]), lambda x: self._getOb(x, None) is not None ) if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyTalkBackConsultation_object(self, id, contributor) form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) if REQUEST is not None: submitter_errors = submitter.info_check(self, REQUEST, ob) form_errors.update(submitter_errors) if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: abort_transaction_keep_session(REQUEST) ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) return REQUEST.RESPONSE.redirect(l_referer) self.setSessionInfoTrans("TalkBack Consultation object created") # process parameters if self.checkPermissionSkipApproval(): approved, approved_by = 1, self.REQUEST.AUTHENTICATED_USER.getUserName() else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() ob.show_contributor_request_role = ob.public_registration self.recatalogNyObject(ob) self.notifyFolderMaintainer(self, ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) # log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) # redirect if case if REQUEST is not None: if l_referer == "talkbackconsultation_add" or l_referer.find("talkbackconsultation_manage_add") != -1: return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == "talkbackconsultation_add_html": self.setSession("referer", self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect("%s/messages_html" % self.absolute_url()) return ob.getId()