コード例 #1
0
ファイル: views.py プロジェクト: arturorc/opac
def article_detail_pdf(url_seg, url_seg_issue, url_seg_article, lang_code=''):
    issue = controllers.get_issue_by_url_seg(url_seg, url_seg_issue)

    if not issue:
        abort(404, _('Issue não encontrado'))

    article = controllers.get_article_by_issue_article_seg(
        issue.iid, url_seg_article)

    if not article:
        abort(404, _('Artigo não encontrado'))

    if lang_code not in article.languages:
        # Se não tem idioma na URL mostra o artigo no idioma original.
        lang_code = article.original_language

    if not article.is_public:
        abort(404, ARTICLE_UNPUBLISH + _(article.unpublish_reason))

    if not article.issue.is_public:
        abort(404, ISSUE_UNPUBLISH + _(article.issue.unpublish_reason))

    if not article.journal.is_public:
        abort(404, JOURNAL_UNPUBLISH + _(article.journal.unpublish_reason))

    journal = article.journal
    issue = article.issue

    articles = controllers.get_articles_by_iid(issue.iid, is_public=True)

    article_list = [_article for _article in articles]

    previous_article = utils.get_prev_article(article_list, article)
    next_article = utils.get_next_article(article_list, article)

    pdf_ssm_path = None

    if article.pdfs:
        try:
            pdf_url = [pdf for pdf in article.pdfs if pdf['lang'] == lang_code]

            if len(pdf_url) != 1:
                abort(404, _('PDF do Artigo não encontrado'))
            else:
                pdf_url = pdf_url[0]['url']

            pdf_url_parsed = urlparse(pdf_url)
            pdf_ssm_path = pdf_url_parsed.path

        except Exception:
            abort(404, _('PDF do Artigo não encontrado'))
    else:
        abort(404, _('PDF do Artigo não encontrado'))

    context = {
        'next_article': next_article,
        'previous_article': previous_article,
        'article': article,
        'journal': journal,
        'issue': issue,
        'pdf_ssm_path': pdf_ssm_path,
        'pdfs': article.pdfs,
        'article_lang': lang_code
    }

    return render_template("article/detail_pdf.html", **context)
コード例 #2
0
ファイル: views.py プロジェクト: arturorc/opac
def article_detail(url_seg, url_seg_issue, url_seg_article, lang_code=''):
    article_url = url_for('main.article_detail',
                          url_seg=url_seg,
                          url_seg_issue=url_seg_issue,
                          url_seg_article=url_seg_article)

    issue = controllers.get_issue_by_url_seg(url_seg, url_seg_issue)

    if not issue:
        abort(404, _('Issue não encontrado'))

    article = controllers.get_article_by_issue_article_seg(
        issue.iid, url_seg_article)

    if not article:
        article = controllers.get_article_by_aop_url_segs(
            issue.journal, url_seg_issue, url_seg_article)
        if not article:
            abort(404, _('Artigo não encontrado'))
        return redirect(
            url_for('main.article_detail',
                    url_seg=article.journal.acronym,
                    url_seg_issue=article.issue.url_segment,
                    url_seg_article=article.url_segment))

    if lang_code not in article.languages:
        # Se não tem idioma na URL mostra o artigo no idioma original.
        lang_code = article.original_language

    if not article.is_public:
        abort(404, ARTICLE_UNPUBLISH + _(article.unpublish_reason))

    if not article.issue.is_public:
        abort(404, ISSUE_UNPUBLISH + _(article.issue.unpublish_reason))

    if not article.journal.is_public:
        abort(404, JOURNAL_UNPUBLISH + _(article.journal.unpublish_reason))

    journal = article.journal
    issue = article.issue

    articles = controllers.get_articles_by_iid(issue.iid, is_public=True)

    article_list = [_article for _article in articles]

    previous_article = utils.get_prev_article(article_list, article)
    next_article = utils.get_next_article(article_list, article)

    pdf_urls_path = []

    if article.pdfs:
        try:
            pdf_urls = [pdf['url'] for pdf in article.pdfs]

            if not pdf_urls:
                abort(404, _('PDF do Artigo não encontrado'))
            else:
                pdf_urls_parsed = list(map(urlparse, pdf_urls))
                pdf_urls_path = [pdf.path for pdf in pdf_urls_parsed]

        except Exception:
            abort(404, _('PDF do Artigo não encontrado'))

    html_article = None

    text_versions = None
    if article.htmls:
        try:
            html_url = [
                html for html in article.htmls if html['lang'] == lang_code
            ]

            if len(html_url) != 1:
                abort(404, _('HTML do Artigo não encontrado'))
            else:
                html_url = html_url[0]['url']

            if html_url.startswith('http'):  # http:// ou https://
                html_url_parsed = urlparse(html_url)
                html_full_ssm_url = current_app.config[
                    'SSM_BASE_URI'] + html_url_parsed.path
            else:
                html_full_ssm_url = current_app.config[
                    'SSM_BASE_URI'] + html_url

            # Obtemos o html do SSM
            try:
                result = requests.get(html_full_ssm_url)
            except requests.exceptions.RequestException:
                abort(404, _('HTML do Artigo não encontrado ou indisponível'))
            else:
                if result.status_code == 200 and len(result.content) > 0:

                    # Criamos um objeto do tip soup
                    soup = BeautifulSoup(result.content.decode('utf-8'),
                                         'html.parser')

                    # Fatiamos o HTML pelo div com class: articleTxt
                    html_article = soup.find('div',
                                             {'id': 'standalonearticle'})
                else:
                    abort(404, _('Artigo não encontrado'))

        except IndexError:
            abort(404, _('Artigo não encontrado'))
        text_versions = sorted([(html['lang'],
                                 display_original_lang_name(html['lang']),
                                 url_for('main.article_detail',
                                         url_seg=journal.url_segment,
                                         url_seg_issue=issue.url_segment,
                                         url_seg_article=article.url_segment,
                                         lang_code=html['lang']))
                                for html in article.htmls])

    context = {
        'next_article':
        next_article,
        'previous_article':
        previous_article,
        'article':
        article,
        'journal':
        journal,
        'issue':
        issue,
        'html':
        html_article,
        'pdfs':
        article.pdfs,
        'pdf_urls_path':
        pdf_urls_path,
        'article_lang':
        lang_code,
        'text_versions':
        text_versions,
        'related_links':
        related_articles_urls.related_links(article_url, [article.title]),
    }

    return render_template("article/detail.html", **context)
コード例 #3
0
def article_detail(url_seg, url_seg_issue, url_seg_article, lang_code=''):
    issue = controllers.get_issue_by_url_seg(url_seg, url_seg_issue)

    if not issue:
        abort(404, _('Issue não encontrado'))

    article = controllers.get_article_by_issue_article_seg(
        issue.iid, url_seg_article)

    if not article:
        article = controllers.get_article_by_aop_url_segs(
            issue.journal, url_seg_issue, url_seg_article)
    if not article:
        abort(404, _('Artigo não encontrado'))

    lang_code = lang_code or article.original_language
    if lang_code not in article.languages + [article.original_language]:
        # Se não é idioma válido, redireciona
        return redirect(url_for('main.article_detail',
                                url_seg=article.journal.url_segment,
                                url_seg_issue=article.issue.url_segment,
                                url_seg_article=article.url_segment,
                                lang_code=article.original_language),
                        code=301)

    if not article.is_public:
        abort(404, ARTICLE_UNPUBLISH + _(article.unpublish_reason))

    if not article.issue.is_public:
        abort(404, ISSUE_UNPUBLISH + _(article.issue.unpublish_reason))

    if not article.journal.is_public:
        abort(404, JOURNAL_UNPUBLISH + _(article.journal.unpublish_reason))

    articles = controllers.get_articles_by_iid(issue.iid, is_public=True)

    article_list = [_article for _article in articles]

    previous_article = utils.get_prev_article(article_list, article)
    next_article = utils.get_next_article(article_list, article)

    pdf_urls_path = []

    if article.pdfs:
        try:
            pdf_urls = [pdf['url'] for pdf in article.pdfs]

            if not pdf_urls:
                abort(404, _('PDF do Artigo não encontrado'))
            else:
                pdf_urls_parsed = list(map(urlparse, pdf_urls))
                pdf_urls_path = [pdf.path for pdf in pdf_urls_parsed]

        except Exception:
            abort(404, _('PDF do Artigo não encontrado'))

    try:
        html, text_languages = render_html(article, lang_code)
    except (ValueError, NonRetryableError, RetryableError):
        abort(404, _('HTML do Artigo não encontrado ou indisponível'))

    text_versions = sorted([(lang, display_original_lang_name(lang),
                             url_for('main.article_detail',
                                     url_seg=article.journal.url_segment,
                                     url_seg_issue=article.issue.url_segment,
                                     url_seg_article=article.url_segment,
                                     lang_code=lang))
                            for lang in text_languages])
    context = {
        'next_article': next_article,
        'previous_article': previous_article,
        'article': article,
        'journal': article.journal,
        'issue': issue,
        'html': html,
        'pdfs': article.pdfs,
        'pdf_urls_path': pdf_urls_path,
        'article_lang': lang_code,
        'text_versions': text_versions,
        'related_links': controllers.related_links(article),
    }

    return render_template("article/detail.html", **context)