示例#1
0
    def _list(self, session):

        language = self.request.language
        items = []

        for view in View.all(session):

            if view.name.lower() == 'media collection':
                continue

            item = dict(id=view.id, description=None)

            if not view.descriptions:
                raise RuntimeError('View %s has no description in any language',
                                    view.id)

            descriptions = [description
                            for description in view.descriptions
                            if description.language == language]

            if descriptions:
                item['description'] = descriptions[0].description

            else:
                item['description'] = view.descriptions[0].description

            items.append(item)

        log.debug(items)

        return items
示例#2
0
    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, 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
示例#4
0
    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