예제 #1
0
    def add_test_data_to_db(self):
        """ Add some pages and languages to the database """
        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        npage = Page(id=2, parent=menu, weight=2)
        hpage = Page(id=3, parent=menu, weight=1, home=True)
        dummy_view = View(name='dummy',
                          fs_view_path='test_templates/page.mako')
        hpage.view = dummy_view
        npage.view = dummy_view
        self.session.add(hpage)
        self.session.add(npage)
        it = Language(lang=u'it', country=u'it')
        self.session.add(it)
        en = Language(lang=u'en', country=u'gb')
        self.session.add(en)


        npage_info_it = PageInfo(id=1, label='Normal', title='Normal Page',
                               url_part='normal', node=npage, lang=en)
        npage_info_en = PageInfo(id=2, label='Normale', title='Pagina Normale',
                               url_part='normale', node=npage, lang=it)

        self.session.add(npage_info_it)
        self.session.add(npage_info_en)

        hpage_info_it = PageInfo(id=3, label='Home', title='Pagina Principale',
                                 url_part='index', node=hpage, lang=it)

        hpage_info_en = PageInfo(id=4, label='Home', title='Main Page',
                                 url_part='index', node=hpage, lang=en)

        self.session.add(hpage_info_it)
        self.session.add(hpage_info_en)
        self.session.commit()
예제 #2
0
    def test_is_last_page(self):
        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        page_1 = Page(id=2, parent=menu, weight=1)
        self.session.add(page_1)

        self.assertEqual(True, Page.is_last_page(self.session))

        page_2 = Page(id=3, parent=menu, weight=2)
        self.session.add(page_2)

        self.assertEqual(False, Page.is_last_page(self.session))
예제 #3
0
    def link_list(self):
        """ Return a javascript with tinyMCELinkList array.
        """
        self.request.response.content_type = "application/javascript"
        try:
            language = self.request.language

            items = []
            for page in Page.all(self.session):
                translation = page.get_translation(language)
                item = unicode('["{}","{}.html"]')
                item = item.format(translation.title.replace('"', "'"), translation.url)
                items.append(item)

            response = unicode("var tinyMCELinkList = new Array({});")
            response = response.format(", ".join(items))

        except Exception as e:
            log.exception(e)
            self.session.rollback()
            response = "/* " + str(e) + " */"
            self.request.response.status = 500

        else:
            self.session.commit()
            self.request.response.status = 200

        return response
예제 #4
0
    def test_set_homepage(self):
        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        page_1 = Page(id=2, parent=menu, weight=1, home=True)
        self.session.add(page_1)
        page_2 = Page(id=3, parent=menu, weight=2)
        self.session.add(page_2)
        page_3 = Page(id=4, parent=menu, weight=3)
        self.session.add(page_3)
        page_4 = Page(id=5, parent=menu, weight=4)
        self.session.add(page_4)

        self.assertEqual(page_1, Page.get_homepage(self.session))

        Page.set_homepage(self.session, page_2)
        self.assertEqual(page_2, Page.get_homepage(self.session))
예제 #5
0
    def test_get_homepage(self):
        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        page_1 = Page(id=2, parent=menu, weight=1)
        self.session.add(page_1)
        page_2 = Page(id=3, parent=menu, weight=2)
        self.session.add(page_2)
        page_3 = Page(id=4, parent=menu, weight=3)
        self.session.add(page_3)
        page_4 = Page(id=5, parent=menu, weight=4, home=True)
        self.session.add(page_4)

        it = Language(lang=u'it', country=u'it')
        self.session.add(it)
        en = Language(lang=u'en', country=u'gb')
        self.session.add(en)

        page_info_1 = PageInfo(id=1, label='Home', title='Pagina Principale',
                               url_part='index', content='',
                               node=page_4, lang=it)
        self.session.add(page_info_1)

        page_info_2 = PageInfo(id=2, label='Home', title='Main Page',
                               url_part='index', content='',
                               node=page_4, lang=en)
        self.session.add(page_info_2)

        page_info_3 = PageInfo(id=3, label='Home 2',
                               title='Pagina Principale 2',
                               url_part='index', content='',
                               node=page_1, lang=it)
        self.session.add(page_info_1)

        page_info_4 = PageInfo(id=4, label='Home 2', title='Main Page 2',
                               url_part='index', content='',
                               node=page_1, lang=en)
        self.session.add(page_info_2)

        self.session.flush()

        self.assertEqual(PageInfo.get_homepage(self.session, it), page_info_1)
        self.assertEqual(PageInfo.get_homepage(self.session, en), page_info_2)

        page_4.home = False

        self.assertEqual(PageInfo.get_homepage(self.session, it), page_info_3)
        self.assertEqual(PageInfo.get_homepage(self.session, en), page_info_4)
예제 #6
0
    def test_before_flush(self):

        en = Language(id=2, lang="en", country="GB", enabled=True)
        menu = Menu(id=1, weight=1)
        self.session.add(menu)
        menu_info = MenuInfo(id=29, label=u"Main Menu", lang=en, node=menu)
        index = Page(id=2, home=True, parent=menu, weight=1)
        index_info = PageInfo(id=2, label="Home", title="Home Page",
                              url_part="index", content="<h2>Home Page</h2>",
                              lang=en, node=index)
        company = Page(id=3, home=True, parent=menu, weight=2)
        company_info = PageInfo(id=3, label="Company", title="Company",
                                url_part="company", content="<h2>Company</h2>",
                                lang=en, node=company)
        team = Page(id=4, home=True, parent=company, weight=3)
        team_info = PageInfo(id=4, label="Team", title="Team",
                              url_part="team", content="<h2>Team</h2>",
                              lang=en, node=team)
        dummy = Page(id=5, home=True, parent=menu, weight=4)

        parent_url = '/' + en.lang
        self.assertEqual(index_info.parent_url, parent_url)
        index_info_url = parent_url + '/' + index_info.url_part
        self.assertEqual(index_info.url, index_info_url)
        self.assertEqual(company_info.parent_url, parent_url)
        company_info_url = parent_url + '/' + company_info.url_part
        self.assertEqual(company_info.url, company_info_url)
        self.assertEqual(team_info.parent_url, company_info.url)
        team_info_url = company_info.url + '/' + team_info.url_part
        self.assertEqual(team_info.url, team_info_url)

        team_info = self.session.query(PageInfo).filter(PageInfo.id == 4).one()
        self.assertEqual(team_info.url, team_info_url)

        team.parent = menu
        self.session.flush()
        self.assertEqual(team_info.parent_url, parent_url)
        self.assertEqual(team_info.url, parent_url + '/' + team_info.url_part)

        team_info.node = dummy
        self.session.flush()
        team_info = self.session.query(PageInfo).filter(PageInfo.id == 4).one()
        self.assertEqual(team_info.url, parent_url + '/' + team_info.url_part)
예제 #7
0
    def test_new_page_allowed(self):

        max_pages = Setting(name=u'max_pages',
                            value=u'1',
                            ui_administrable=False,
                            type=SettingType(name=u'integer',
                                             raw_type=u'int'))
        self.session.add(max_pages)

        self.assertTrue(Page.new_page_allowed(self.session))

        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        page = Page(id=2, parent=menu, home=True, weight=1)
        self.session.add(page)

        self.assertFalse(Page.new_page_allowed(self.session))

        page_2 = Page(id=3, parent=menu, weight=2)
        self.session.add(page_2)

        self.assertFalse(Page.new_page_allowed(self.session))
예제 #8
0
    def test_get_translation(self):

        self.populate(self.session)

        it = self.session.query(Language).filter(Language.lang == 'it').one()
        en = self.session.query(Language).filter(Language.lang == 'en').one()
        es = self.session.query(Language).filter(Language.lang == 'es').one()

        home = Page.get_homepage(self.session)
        it_translation = home.get_translation(it)
        self.assertEqual(it_translation,
                         PageInfo.get_homepage(self.session, it))
        en_translation = home.get_translation(en)
        self.assertEqual(en_translation,
                         PageInfo.get_homepage(self.session, en))
        es_translation = home.get_translation(es)
        self.assertEqual(es_translation,
                         PageInfo.get_homepage(self.session, es))
예제 #9
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
예제 #10
0
    def test_get_homepage(self):
        menu = Menu(id=1, parent=None, weight=1)
        self.session.add(menu)
        page_1 = Page(id=2, parent=menu, weight=1, home=True)
        self.session.add(page_1)
        page_2 = Page(id=3, parent=menu, weight=2)
        self.session.add(page_2)
        page_3 = Page(id=4, parent=menu, weight=3)
        self.session.add(page_3)
        page_4 = Page(id=5, parent=menu, weight=4)
        self.session.add(page_4)

        self.assertEqual(page_1, Page.get_homepage(self.session))

        page_2.home = True

        with self.assertRaises(MultipleResultsFound):
            Page.get_homepage(self.session)

        page_1.home = False
        self.assertEqual(page_2, Page.get_homepage(self.session))

        page_2.home = False
        self.assertEqual(page_1, Page.get_homepage(self.session))

        page_1.home = False
        section = Section(id=6, parent=menu, weight=5)
        self.session.add(section)
        page_1.parent = section
        page_2.parent = section
        page_3.parent = section
        page_4.parent = section

        self.assertIn(Page.get_homepage(self.session), (page_1, page_2, page_3,
                                                        page_4))
예제 #11
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
예제 #12
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
예제 #13
0
    def list(self):

        response = copy.deepcopy(self._response)

        try:
            language = self.request.language

            response["dataset"] = [page.to_dict(language, recursive=False) for page in Page.all(self.session)]
            response["dataset_len"] = len(response["dataset"])

        except NoResultFound as e:
            log.exception("No language found.")
            self.request.response.status = 500
            response["dataset"] = []
            response["dataset_len"] = 0
            response["success"] = False
            response["message"] = str(e)

        else:
            self.request.response.status = 200
            response["success"] = True
            response["message"] = "Page list retrieved."

        return response