Exemplo n.º 1
0
    def read(self):

        response = self._response.copy()

        try:
            page_id = self.request.matchdict['page_id']
            page = Page.get(self.session, page_id)
            items = [dict(id=obj.banner.id,
                          page=page_id,
                          banner=obj.banner.to_dict())
                     for obj in page.banners]

        except KeyError as e:
            self.log.exception('Not URL param in the request.')
            self.session.rollback()
            self.request.response.status = 400
            response['msg'] = self.request.translate("Missing parameter: 'url'.")

        except NoResultFound as e:
            msg = "No Page found: %s" % page_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'] = items
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("Dataset found.")

        finally:
            return response
Exemplo n.º 2
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