コード例 #1
0
ファイル: test_node.py プロジェクト: asidev/aybu-core
    def test_get_max_weight(self):
        node1 = Menu(id=1, weight=10)
        self.session.add(node1)
        node2 = Page(id=2, parent=node1, weight=20)
        self.session.add(node2)
        node3 = Page(id=3, parent=node2, weight=30)
        self.session.add(node3)

        self.assertEqual(Node.get_max_weight(self.session, parent=None), 10)
        self.assertEqual(Node.get_max_weight(self.session, parent=node1), 20)
        self.assertEqual(Node.get_max_weight(self.session, parent=node2), 30)
        # Node.get_max_weight return None when the query is empty.
        self.assertEqual(Node.get_max_weight(self.session, parent=node3), 0)
コード例 #2
0
    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
コード例 #3
0
ファイル: structure.py プロジェクト: asidev/aybu-controlpanel
    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