def test_urlfy(self): url = ' Pagina Principale ' self.assertEqual(urlify(url), 'pagina_principale') url = ' Pagina\nPrincipale ' self.assertEqual(urlify(url), 'pagina_principale') url = ' Pagina\rPrincipale ' self.assertEqual(urlify(url), 'pagina_principale') url = ' Pagina Principale%&/' self.assertEqual(urlify(url), 'pagina_principale') xlate = { 0xc0: 'A', 0xc1: 'A', 0xc2: 'A', 0xc3: 'A', 0xc4: 'A', 0xc5: 'A', 0xc6: 'Ae', 0xc7: 'C', 0xc8: 'E', 0xc9: 'E', 0xca: 'E', 0xcb: 'E', 0xcc: 'I', 0xcd: 'I', 0xce: 'I', 0xcf: 'I', 0xd1: 'N', 0xd2: 'O', 0xd3: 'O', 0xd4: 'O', 0xd5: 'O', 0xd6: 'O', 0xd9: 'U', 0xda: 'U', 0xdb: 'U', 0xdc: 'U', 0xdd: 'Y', 0xe0: 'a', 0xe1: 'a', 0xe2: 'a', 0xe3: 'a', 0xe4: 'a', 0xe5: 'a', 0xe6: 'ae', 0xe7: 'c', 0xe8: 'e', 0xe9: 'e', 0xea: 'e', 0xeb: 'e', 0xec: 'i', 0xed: 'i', 0xee: 'i', 0xef: 'i', 0xf1: 'n', 0xf2: 'o', 0xf3: 'o', 0xf4: 'o', 0xf5: 'o', 0xf6: 'o', 0xf9: 'u', 0xfa: 'u', 0xfb: 'u', 0xfc: 'u', 0xfd: 'y', 0xff: 'y'} url = ' Pagina Principale ' for c in xlate.keys(): url = url + unichr(c) urlified = 'pagina_principale_' for c in xlate.keys(): urlified = urlified + xlate[c] urlified = urlified.lower() self.assertEqual(urlify(url), urlified)
def urlify(self): # FIXME change action name to urlify res = dict() name = self.request.params.get('name', '') try: res['name'] = urlify(name) except: res['name'] = '' res['success'] = False else: res['success'] = True finally: return res
def update(self): response = self._response.copy() try: id_ = self.request.matchdict['id'] # Convert JSON request param into dictionary. params = json.loads(self.request.params['dataset']) item = MediaItemPage.get(self.session, id_) item.weight = params['weight'] translation = params['translations'][0] info = MediaItemPageInfo.get(self.session, translation['id']) info.label = translation['label'] info.title = info.label info.url_part = urlify(info.title) info.content = translation['content'] except KeyError as e: self.log.exception('Not needed param in the request.') self.session.rollback() self.request.response.status = 400 response['msg'] = self.request.translate(str(e)) except NoResultFound as e: msg = "No MediaItemPage found: %s" % id_ self.log.exception(msg) self.session.rollback() self.request.response.status = 404 response['msg'] = self.request.translate(msg) except Exception as e: self.log.exception('Unknown error.') self.session.rollback() self.request.response.status = 500 response['msg'] = str(e) else: self.session.commit() response['success'] = True response['dataset'] = [] response['dataset_length'] = len(response['dataset']) response['msg'] = self.request.translate("MediaItemPage updated.") finally: return response
def urlify(self, name): return urlify(name)
def _create(self, params): parent_url = urlparse(params['parent_url']).path if parent_url.startswith('/admin'): parent_url = parent_url.replace('/admin', '', 1) parent_url = parent_url.rsplit('.', 1)[0] node_info = MediaCollectionPageInfo.get_by_url(self.session, parent_url) # Node attributes. enabled = params.get('enabled', 'on') enabled = True if enabled.lower() == 'on' else False hidden = params.get('hidden', 'off') hidden = True if hidden.lower() == 'on' else False max_weight = Node.get_max_weight(self.session, node_info.node) weight = params.get('weight', max_weight) #self.log.debug(max_weight) weight = int(weight) + 1 # Page attributes. home = params.get('home', 'off') home = True if home.lower() == 'on' else False sitemap_priority = params.get('sitemap_priority') sitemap_priority = int(sitemap_priority) if sitemap_priority else 1 # FIXME: add the right view needed by MediaItemPage rendering. view = View.get(self.session, 1) file_id = params.get('file', {}).get('id') node = MediaItemPage(enabled=enabled, hidden=hidden, parent=node_info.node, weight=weight, home=home, sitemap_priority=sitemap_priority, view=view, file_id=file_id) # NodeInfo attributes. translation = params.get('translations')[0] label = translation['label'] language = self.request.language # CommonInfo attributes. title = translation.get('title', label) url_part = translation.get('url_part', title).strip() url_part = urlify(url_part) meta_description = translation.get('meta_description') head_content = translation.get('head_content') # Page attributes. content = translation.get('content', '') node_info = MediaItemPageInfo(label=label, node=node, lang=language, title=title, url_part=url_part, meta_description=meta_description, head_content=head_content, content=content) self.session.add(node) self.session.add(node_info) node_info.translate(enabled_only=True) return node
def update(self): response = copy.deepcopy(self._response) try: language = self.request.language node_id = int(self.request.params.get("id")) node = Node.get(self.session, node_id) info = node.get_translation(language) # Node attributes. enabled = self.request.params.get("enabled", "off") if enabled is None: enabled = node.enabled else: enabled = True if enabled.lower() == "on" else False # NodeInfo attributes label = self.request.params.get("button_label", info.label) # CommonInfo attributes. title = self.request.params.get("title", info.title) url_part = self.request.params.get("url_part", info.url_part).strip() url_part = urlify(url_part) meta_description = self.request.params.get("meta_description", info.meta_description) head_content = self.request.params.get("head_content", info.head_content) node.enabled = enabled info.title = title info.label = label info.meta_description = meta_description info.head_content = head_content if isinstance(node, (Page, Section)) and info.url_part != url_part: log.debug("Change url_part from '%s' to '%s'.", info.url_part, url_part) info.url_part = url_part if isinstance(node, Page): home = self.request.params.get("home", "") home = True if home.lower() == "on" else False if home: Page.set_homepage(self.session, node) view_id = int(self.request.params.get("page_type_id", node.view.id)) node.view = View.get(self.session, view_id) elif isinstance(node, InternalLink): node.linked_to = Node.get(self.session, int(request.params.get("linked_to"))) elif isinstance(node, ExternalLink): ext_url = request.params.get("external_url", "") if not ext_url.startswith("http://"): ext_url = "http://" + ext_url info.ext_url = ext_url """ FIXME: purge cache. if routing_changed: reload_routing() else: aybu.cms.lib.cache.flush_all() """ except (TypeError, NoResultFound) as e: log.exception("Bad request params.") self.session.rollback() self.request.response.status = 400 response["errors"] = {} response["success"] = False response["errors"]["400"] = str(e) except Exception as e: log.exception("Unknown Error.") self.session.rollback() self.request.response.status = 500 response["errors"] = {} response["success"] = False response["errors"]["500"] = str(e) else: self.session.commit() self.request.response.status = 200 response["errors"] = {} response["dataset"] = [{"id": node.id}] response["dataset_len"] = 1 response["success"] = True return response
def create(self): response = copy.deepcopy(self._response) try: language = self.request.language type_ = self.request.params.get("type") # Specify model entity. # Node attributes. enabled = self.request.params.get("enabled", "off") enabled = True if enabled.lower() == "on" else False # FIXME: check JS to verify 'hidden' support. hidden = self.request.params.get("hidden", "off") hidden = True if hidden.lower() == "on" else False parent = self.request.params.get("parent_id") if not parent: parent = "1" parent = Node.get(self.session, parent) weight = self.request.params.get("weight", Node.get_max_weight(self.session, parent)) weight = int(weight) + 1 # NodeInfo attributes label = self.request.params.get("button_label") # CommonInfo attributes. title = self.request.params.get("title", "No title") url_part = self.request.params.get("url_part", title).strip() url_part = urlify(url_part) meta_description = self.request.params.get("meta_description") head_content = self.request.params.get("head_content") if type_ == "Section": node = Section(enabled=enabled, hidden=hidden, parent=parent, weight=weight) node_info = SectionInfo( label=label, node=node, lang=language, title=title, url_part=url_part, meta_description=meta_description, head_content=head_content, ) elif type_ in ("Page", "MediaCollectionPage"): # Page attributes. sitemap_priority = self.request.params.get("sitemap_priority") if not sitemap_priority: sitemap_priority = 1 sitemap_priority = int(sitemap_priority) view = self.request.params.get("page_type_id") if type_ == "MediaCollectionPage": # FIXME: # add supporto to restrict some views to a specific node. view = View.get_by_name(self.session, "MEDIA COLLECTION") view = view[0] if view else None else: view = View.get(self.session, view) if not Page.new_page_allowed: raise QuotaError("New pages are not allowed.") content = self.request.params.get("content", u"<h2>{}</h2>".format(title)) if type_ == "Page": node_cls = Page nodeinfo_cls = PageInfo elif type_ == "MediaCollectionPage": node_cls = MediaCollectionPage nodeinfo_cls = MediaCollectionPageInfo node = node_cls( enabled=enabled, hidden=hidden, parent=parent, weight=weight, sitemap_priority=sitemap_priority, view=view, ) node_info = nodeinfo_cls( label=label, node=node, lang=language, title=title, url_part=url_part, meta_description=meta_description, head_content=head_content, content=content, ) elif type_ == "InternalLink": linked_to = self.request.params.get("linked_to") linked_to = Page.get(self.session, linked_to) node = InternalLink(enabled=enabled, hidden=hidden, parent=parent, weight=weight, linked_to=linked_to) node_info = InternalLinkInfo(label=label, node=node, lang=language) elif type_ == "ExternalLink": ext_url = self.request.params.get("external_url") node = ExternalLink(enabled=enabled, hidden=hidden, parent=parent, weight=weight) node_info = ExternalLinkInfo(label=label, node=node, lang=language, ext_url=ext_url) else: raise NotImplementedError("Cannot create: %s" % type_) self.session.add(node) self.session.add(node_info) node_info.translate(enabled_only=True) self.session.flush() if type_ == "Page": home = self.request.params.get("home", "off") home = True if home.lower() == "on" else False if home: Page.set_homepage(self.session, node) except NotImplementedError as e: log.exception("Not Implemented.") self.session.rollback() self.request.response.status = 501 # HTTP 501 Not Implemented Error. response["errors"] = {} response["success"] = False response["errors"]["501"] = "Cannot create %s entity." % type_ except QuotaError as e: log.exception("Quota Error.") self.session.rollback() self.request.response.status = 500 response["errors"] = {} response["success"] = False response["errors"]["500"] = "Maximum pages number reached." except MultipleResultsFound as e: log.exception("Pages URls must be unique.") self.session.rollback() self.request.response.status = 409 response["errors"] = {} response["success"] = False response["errors"]["409"] = str(e) except Exception as e: log.exception("Unknown Error.") self.session.rollback() self.request.response.status = 500 response["errors"] = {} response["success"] = False response["errors"]["500"] = str(e) else: self.session.commit() self.request.response.status = 200 response["errors"] = {} response["dataset"] = [{"id": node.id}] response["dataset_len"] = 1 response["success"] = True return response