Beispiel #1
0
    def edit(self):
        try:
            message = "UNDEFINED"
            pageinfo = PageInfo.get(self.session,
                                    self.request.params['translation_id'])
            html = self.request.params['translation_html']
            pageinfo.content = html
            self.session.flush()

        except ValueError:
            message = self.prepare_response(status=400,
                                    message=u'Contenuto non valido')

        except KeyError:
            message = self.prepare_response(status=400,
                                    message=u'Parametri mancanti')

        except NoResultFound:
            message = self.prepare_response(status=404,
                                    message=u'Nessuna risorsa trovata')

        except Exception:
            self.log.exception('Unexpected error during content update')
            message = self.prepare_response(status=500,
                                    message=u"Errore nell'aggiornamento")

        else:
            message = self.prepare_response(
                        status=200,
                        message=u'Contenuto aggiornato correttamente',
                        page=pageinfo)

        finally:
            return dict(msg=message)
Beispiel #2
0
    def page_banners(self):
        res = dict(success=False)
        try:
            name = self.request.params['name']
            id_ = self.request.params['nodeinfo_id']
            source = self.request.params['file']
            page = PageInfo.get(self.session, id_)
            self.log.debug("Updating banner for page '{}'".format(page.label))
            for banner in page.node.banners:
                banner.delete()
            page.node.banners = []

            page.node.banners.append(
                Banner(source=source.file, name=name, session=self.session)
            )
            self.session.flush()

        except Exception:
            self.session.rollback()
            message = u"Errore nella rimozione del banner"
            res['success'] = False
            res['error'] = self.request.translate(message)
            self.log.exception("Error removing banner")

        else:
            res['success'] = True
            self.session.commit()
            self.proxy.invalidate(url=page.url)

        finally:
            return res
Beispiel #3
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)
Beispiel #4
0
 def test_show_page(self):
     from aybu.website.views import show_page
     self.add_test_data_to_db()
     self.ctx = PageInfo.get(self.session, 3)
     dummy_rendererer = self.configurator.testing_add_renderer(
                                     self.ctx.node.view.fs_view_path)
     show_page(self.ctx, self.req)
     dummy_rendererer.assert_(page=self.ctx, request=self.req)
Beispiel #5
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))
Beispiel #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)
Beispiel #7
0
    def test_redirect_to_homepage(self):
        from aybu.website.views import redirect_to_homepage
        self.add_test_data_to_db()
        hpage_it = PageInfo.get(self.session, 3)
        hpage_en = PageInfo.get(self.session, 4)

        with self.assertRaises(HTTPMovedPermanently) as cm:
            redirect_to_homepage(self.ctx, self.req)
        self.assertEqual(cm.exception.location, hpage_en.url)

        self.req.accept_language = "en-US,en"
        with self.assertRaises(HTTPMovedPermanently) as cm:
            redirect_to_homepage(self.ctx, self.req)
        self.assertEqual(cm.exception.location, hpage_en.url)

        self.req.accept_language = "it-IT,it"
        with self.assertRaises(HTTPMovedPermanently) as cm:
            redirect_to_homepage(self.ctx, self.req)
        self.assertEqual(cm.exception.location, hpage_it.url)

        self.session.commit()
Beispiel #8
0
    def test_content_links_parsing(self):
        self.populate(self.session)
        en = self.session.query(Language).filter(Language.lang == 'en').one()
        menu = self.session.query(Menu).first()
        home_info = self.session.query(PageInfo).filter(PageInfo.id == 2).one()
        home = home_info.node
        contact_info = self.session.query(PageInfo).filter(PageInfo.id == 4).one()
        contact = contact_info.node
        team = Page(home=True, parent=menu, weight=300)
        content = '<h2>'
        content += '<a href="{obj.url}">{obj.label}</a>'.format(obj=home_info)
        content += '</h2>'
        team_info = PageInfo(label="Team", title="The Team",
                             url_part="team",
                             content=content,
                             lang=en, node=team)
        self.session.flush()
        self.assertIn(home_info, team_info.links)
        self.assertNotIn(contact_info, team_info.links)

        content = '<a href="{obj.url}">{obj.label}</a>'.format(obj=contact_info)
        team_info.content = team_info.content + content
        self.session.flush()
        self.assertIn(home_info, team_info.links)
        self.assertIn(contact_info, team_info.links)
        self.assertIn(home_info.url, team_info.content)
        self.assertIn(contact_info.url, team_info.content)

        home.weight = 3000
        home.parent = self.session.query(Section).filter(Section.id == 4).one()
        log.debug('Flushing...')
        self.session.flush()
        self.assertIn(home_info.url, team_info.content)
        self.assertIn(contact_info.url, team_info.content)
        self.assertIn(home_info, team_info.links)
        self.assertIn(contact_info, team_info.links)
Beispiel #9
0
    def search(self):
        response = self._response.copy()
        try:
            url = urlparse(self.request.params['url'])
            path = url.path
            if path.startswith('/admin'):
                path = path.replace('/admin', '', 1)
            if path.endswith('.html'):
                path = path.replace('.html', '', 1)
            pageinfo = PageInfo.get_by_url(self.session, path)
            self.log.debug('Found %s using %s', pageinfo, path)
            pageinfo = pageinfo.dictify(excludes=('__class__',
                                                  'meta_description',
                                                  'title',
                                                  'head_content',
                                                  '_parent_url',
                                                  'label', 'content',
                                                  'url_part',
                                                  'discriminator'))

        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:
            self.log.exception('No PageInfo: %s.', path)
            self.session.rollback()
            self.request.response.status = 404
            response['msg'] = self.request.translate("No PageInfo found.")

        except Exception as e:
            self.log.exception('Uknown error.')
            self.session.rollback()
            self.request.response.status = 500
            response['msg'] = str(e)

        else:
            self.session.commit()
            response['success'] = True
            response['dataset'] = [pageinfo]
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("PageInfo found.")

        finally:
            return response
Beispiel #10
0
    def remove_page_banners(self):
        res = dict(success=False)
        try:
            page = PageInfo.get(self.session,
                                self.request.params['nodeinfo_id'])
            for banner in page.node.banners:
                banner.delete()

            self.session.flush()

        except Exception:
            self.session.rollback()
            message = u"Errore nella rimozione del banner"
            res['success'] = False
            res['error'] = self.request.translate(message)
            self.log.exception("Error removing banner")

        else:
            res['success'] = True
            self.session.commit()
            self.proxy.invalidate(url=page.url)

        finally:
            return res
Beispiel #11
0
def get_root_resource(request):

    path_info = request.path_info
    log.debug('get_root_resource: %s', path_info)

    # Getting url_parts and for each part associating a Resource
    # On request.path_info applying strip('/') remove the initial / so
    # with the following split('/') we obtain a list just with parts
    url_parts = [UrlPart(part=url_part, resource=Resource())
                 for url_part in path_info.strip('/').split('/')
                 if url_part]

    log.debug('url_parts: %s', url_parts)

    if not url_parts:
        # URL is '/'.
        log.debug('Return NoLanguage context.')
        return NoLanguage()

    need_auth = False
    if url_parts[0].part == 'admin':
        log.debug("Admin Panel Request: removing admin.")
        url_parts = url_parts[1:]
        path_info = path_info.replace('/admin', '')
        need_auth = True

    language = Language.get_by_lang(request.db_session, url_parts[0].part)
    if language is None:
        # language not found, return a 404
        log.debug("No language found.")
        raise HTTPNotFound()
    else:
        request.language = language

    if len(url_parts) == 1:
        # URL is like '/{lang}'.
        log.debug('Get Context by Language %s.', url_parts[0].part)
        url_parts[0] = UrlPart(part=url_parts[0].part,
                               resource=request.language)

    else:
        # URL is like '/{lang}/{node}/[...]/{page}[.ext]
        # Get the NodeInfo from database using path_info.
        log.debug('Get Context by NodeInfo %s.', path_info)
        try:
            # Remove '.ext' from the url.
            url_parts[-1] = url_parts[-1].part
            resource = PageInfo.get_by_url(request.db_session,
                                           path_info.rsplit('.', 1)[0])
            url_parts[-1] = UrlPart(part=url_parts[-1], resource=resource)
        except NoResultFound:
            raise HTTPNotFound()
        else:
            log.debug('Found: %s', url_parts[-1])

    log.debug('UrlParts: %s', url_parts)

    # Create the resources tree.
    # The last element in resources tree is the request context.
    tmp = root = Resource()
    parent = None
    acl = [(Allow, Everyone, ALL_PERMISSIONS)]
    if need_auth:
        acl = Authenticated.__acl__

    for url_part, resource in url_parts:
        log.debug('Urlpart: %s, Resource: %s', url_part, resource)
        resource.__parent__ = parent
        resource.__acl__ = acl
        parent = resource
        tmp[url_part] = resource
        tmp = tmp[url_part]
        log.debug("Resource: %s, acl: %s, parent: %s",
                  resource, resource.__acl__, resource.__parent__)

    log.debug(root)

    return root
Beispiel #12
0
def redirect_to_homepage(context, request):
    lang = get_negotiated_language(request)
    page = PageInfo.get_homepage(request.db_session, lang)
    raise HTTPMovedPermanently(location='%s.html' % page.url)
Beispiel #13
0
 def test_get_by_url(self):
     self.populate(self.session)
     url = u'/it/index'
     self.assertEqual(PageInfo.get_by_url(self.session, url).url, url)
     url = u'/en/index'
     self.assertEqual(PageInfo.get_by_url(self.session, url).url, url)