Exemplo n.º 1
0
def article_editor(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    code = req.params.get('code', None)  # update

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    if site.type != 'publication':
        raise HTTPNotFound

    publication = site.instance
    if code:
        article = publication.articles.where(Article.code == code).get()
    else:
        code = Article.grab_unique_code()  # as path
        article = Article(path=code, code=code)

    editor_form = build_article_editor_form(req, article)
    settings_form = build_article_settings_form(req, article)

    return dict(
        editor_form=editor_form,
        settings_form=settings_form,
        project=project,
        site=site,
        publication=publication,
        article=article,
    )
Exemplo n.º 2
0
def chapter_edit(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')
    chapter_slug = req.matchdict.get('chapter_slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    if site.type != 'publication':
        raise HTTPNotFound

    publication = site.instance
    try:
        chapter = Chapter.select(
            Chapter,
            fn.COUNT(Article.id).alias('articles_count')).join(
                Article, JOIN.LEFT_OUTER).join(
                    Publication,
                    on=(Chapter.publication_id == publication.id)).where(
                        Chapter.slug == chapter_slug).group_by(
                            Chapter.id).order_by(Chapter.name.asc(), ).get()
    except Chapter.DoesNotExist:
        raise HTTPNotFound

    return dict(project=project,
                site=site,
                publication=publication,
                chapter=chapter)
Exemplo n.º 3
0
def article_list(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    if site.type != 'publication':
        raise HTTPNotFound

    publication = site.instance
    articles = Article.select(
        Article, Chapter.name, Chapter.slug,
    ).join(Chapter).join(Publication, on=(
        Article.publication_id == publication.id
    )).order_by(
        Article.updated_at.desc(),
        Article.progress_state.desc(),
        Article.title,
    )

    return dict(
        project=project,
        site=site,
        publication=publication,
        articles=articles)
Exemplo n.º 4
0
def site_settings_badges(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    return dict(project=project, site=site, instance=site.instance)
Exemplo n.º 5
0
def site_overview(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    req.override_renderer = tpl('overview.mako', type_=site.type)
    return dict(project=project, site=site, instance=site.instance)
Exemplo n.º 6
0
def site_insights(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    component_type = 'metrics'
    if 'tab' in req.params and req.params['tab'] == 'logs':
        component_type = 'logs'

    # only application
    return dict(project=project, site=site, instance=site.instance,
                component_type=component_type)
Exemplo n.º 7
0
def site_settings_widgets(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    if site.type != 'application':
        raise HTTPNotFound

    manager = req.find_service(iface=IManager, name='credentials')
    manager.assign(obj=site)

    # only application
    return dict(project=project, site=site, instance=site.instance,
                credentials_state=manager.validate('read'))
Exemplo n.º 8
0
def handle_post(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    code = req.params.get('code')  # update

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    if site.type != 'publication':
        raise HTTPNotFound

    publication = site.instance
    if code:
        try:
            article = publication.articles.where(
                Article.code == code).get()
        except Article.DoesNotExist:
            article = None

    if not article:
        code = Article.grab_unique_code()
        article = Article(
            code=code,
            path=code,
            title='Untitled',
            copyright='',
            publication=publication)

    context = req.params.get('context', None)
    _ = req.translate
    if context == 'editor':
        (article, errors) = save_content(req, article)
    elif context == 'settings':
        (article, errors) = save_meta(req, article)
    else:
        raise HTTPNotFound

    message = _('article.save.failure')
    if not errors:
        message = _('article.save.success')

    return dict(
        status='ok',
        code=article.code,
        errors=errors,
        message=message)
Exemplo n.º 9
0
def site_settings(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    project = get_project(namespace, user=req.user)
    site = get_site(slug, project=project)

    # update
    form = build_site_form(req, site)
    if 'submit' in req.POST:
        _ = req.translate
        if form.validate():
            update_slug = False

            with req.db.cardinal.atomic():
                instance = build_instance_of(site, form.instance.form)
                instance.save()

                if site.slug != form.slug.data:
                    update_slug = True
                    site.slug = form.slug.data

                if site.type == 'application':
                    site.domain = form.domain.data

                site.save()

            manager = req.find_service(iface=IManager, name='credentials')
            manager.assign(obj=site)
            manager.sync()

            req.session.flash(_('site.{:s}.update.success'.format(site.type)),
                              queue='success', allow_duplicate=False)
            if update_slug:
                next_path = req.route_path(
                    'console.site.settings', namespace=project.namespace,
                    slug=site.slug)
                return HTTPFound(location=next_path)
        else:
            req.session.flash(_('site.{:s}.update.failure'.format(site.type)),
                              queue='failure', allow_duplicate=False)

    req.override_renderer = tpl('settings.mako', type_=site.type)
    return dict(project=project, site=site, form=form, instance=site.instance)
Exemplo n.º 10
0
def api_application_insights_data(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    try:
        project = get_project(namespace, user=req.user)
        site = get_site(slug, project=project)
    except HTTPNotFound:
        return Response(
            status=404,
            json_body={'error': 'The project or site was not found'})

    q = ReadingResult.fetch_data_by_path(project.access_key_id, site.id)
    pq = PaginatedQuery(q, str(req.params.get('page', 1)), ITEMS_PER_PAGE)
    return {
        'data': list(pq.get_objects()),
        'page': pq.page,
        'page_count': pq.page_count
    }
Exemplo n.º 11
0
def api_article_progress_states(req):
    try:
        namespace = req.matchdict.get('namespace')
        slug = req.matchdict.get('slug')
        code = req.matchdict.get('code')

        project = get_project(namespace, user=req.user)
        site = get_site(slug, project=project)

        if site.type != 'publication':
            raise HTTPNotFound

        publication = site.instance

        article = publication.articles.where(
            Article.code == code).get()
    except (HTTPNotFound, Article.DoesNotExist):
        article = Article(progress_state='draft')

    states = article.available_progress_states_as_choices
    current_state = article.progress_state

    data = {}
    for (i, v) in enumerate(Article.progress_states):
        n = str(i)
        data[v] = {'value': n, 'label': v}
        if (n, v) not in states:
            data[v]['disabled'] = True
        elif v == current_state:
            data[v]['selected'] = True

    return dict(
        status='ok',
        data=data,
        errors=[],
        message='')
Exemplo n.º 12
0
def api_application_insights_metrics(req):
    namespace = req.matchdict.get('namespace')
    slug = req.matchdict.get('slug')

    try:
        project = get_project(namespace, user=req.user)
        _ = get_site(slug, project=project)
    except HTTPNotFound:
        return Response(
            status=404,
            json_body={'error': 'The project or site was not found'})

    # TODO
    # dummy
    data = [
        {
            'duration': 20,
            'rate': 3,
            'date': '20180631102324'
        },
        {
            'duration': 123,
            'rate': 10,
            'date': '20180631113032'
        },
        {
            'duration': 11,
            'rate': 4,
            'date': '20180631113500'
        },
        {
            'duration': 222,
            'rate': 18,
            'date': '20180631122412'
        },
        {
            'duration': 150,
            'rate': 11,
            'date': '20180701001239'
        },
        {
            'duration': 556,
            'rate': 33,
            'date': '20180701114134'
        },
        {
            'duration': 25,
            'rate': 3,
            'date': '20180702113134'
        },
        {
            'duration': 15,
            'rate': 2,
            'date': '20180702133134'
        },
        {
            'duration': 33,
            'rate': 9,
            'date': '20180702143134'
        },
        {
            'duration': 123,
            'rate': 11,
            'date': '20180702143134'
        },
        {
            'duration': 150,
            'rate': 18,
            'date': '20180703143134'
        },
        {
            'duration': 9,
            'rate': 1,
            'date': '20180703153134'
        },
        {
            'duration': 38,
            'rate': 5,
            'date': '20180703113134'
        },
        {
            'duration': 800,
            'rate': 90,
            'date': '20180704113134'
        },
        {
            'duration': 700,
            'rate': 60,
            'date': '20180704222124'
        },
        {
            'duration': 120,
            'rate': 15,
            'date': '20180704232124'
        },
        {
            'duration': 411,
            'rate': 30,
            'date': '20180704242124'
        },
        {
            'duration': 800,
            'rate': 66,
            'date': '20180704243124'
        },
        {
            'duration': 180,
            'rate': 20,
            'date': '20180704243124'
        },
        {
            'duration': 105,
            'rate': 18,
            'date': '20180635102334'
        },
        {
            'duration': 605,
            'rate': 67,
            'date': '20180704113134'
        },
        {
            'duration': 405,
            'rate': 20,
            'date': '20180636102334'
        },
        {
            'duration': 12,
            'rate': 2,
            'date': '20180704113134'
        },
        {
            'duration': 24,
            'rate': 9,
            'date': '20180704113134'
        },
        {
            'duration': 11,
            'rate': 1,
            'date': '20180704113134'
        },
        {
            'duration': 19,
            'rate': 3,
            'date': '20180704113134'
        },
        {
            'duration': 4,
            'rate': 2,
            'date': '20180704113134'
        },
        {
            'duration': 3,
            'rate': 1,
            'date': '20180637102334'
        },
        {
            'duration': 5,
            'rate': 1,
            'date': '20180638102334'
        },
        {
            'duration': 2,
            'rate': 1,
            'date': '20180639102334'
        },
    ]

    return {'data': data}