Coverage for portality/view/doaj.py: 66%
405 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 13:43 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 13:43 +0000
1import json
2import os.path
3import re
4import urllib.error
5import urllib.parse
6import urllib.request
8from flask import Blueprint, request, make_response
9from flask import render_template, abort, redirect, url_for, send_file, jsonify
10from flask_login import current_user, login_required
12from portality.ui import exceptions as ui_exceptions
13from portality.bll import exceptions as bll_exceptions
14from portality import constants
15from portality import dao
16from portality import models
17from portality.bll import DOAJ
18from portality.core import app
19from portality.lcc import lcc_jstree
20from portality.lib import plausible
21from portality.ui.messages import Messages
22from portality.ui import templates
23from portality.view.account import LoginForm
25# ~~DOAJ:Blueprint~~
26blueprint = Blueprint('doaj', __name__)
29@blueprint.route("/")
30def home():
31 news = models.News.latest(app.config.get("FRONT_PAGE_NEWS_ITEMS", 5))
32 recent_journals = models.Journal.recent(max=16)
33 stats = DOAJ.siteService().site_statistics()
34 return render_template(templates.PUBLIC_INDEX, news=news, recent_journals=recent_journals, statistics=stats)
37@blueprint.route('/login/')
38def login():
39 return redirect(url_for('account.login'))
42@blueprint.route("/dismiss_site_note")
43def dismiss_site_note():
44 cont = request.values.get("continue")
45 if cont is not None:
46 resp = redirect(cont)
47 else:
48 resp = make_response()
49 # set a cookie that lasts for one year
50 resp.set_cookie(app.config.get("SITE_NOTE_KEY"), app.config.get("SITE_NOTE_COOKIE_VALUE"),
51 max_age=app.config.get("SITE_NOTE_SLEEP"), samesite=None, secure=True)
52 return resp
55@blueprint.route("/news/")
56def news():
57 # NOTE: On live this is also handled by the nginx redirect map, but this will strip those with parameters supplied
58 return redirect("https://blog.doaj.org")
61@blueprint.route("/fqw_hit", methods=['POST'])
62def fqw_hit():
63 page = request.form.get('embedding_page')
64 if page is not None:
65 plausible.send_event(app.config.get('ANALYTICS_CATEGORY_FQW', 'FQW'),
66 action=app.config.get('ANALYTICS_ACTION_FQW', 'hit'),
67 label=request.form.get('embedding_page'))
69 # No content response, whether data there or not.
70 return '', 204
73@blueprint.route("/search/journals", methods=["GET"])
74def journals_search():
75 return render_template(templates.PUBLIC_JOURNAL_SEARCH, lcc_tree=lcc_jstree)
78@blueprint.route("/search/articles", methods=["GET"])
79def articles_search():
80 return render_template(templates.PUBLIC_ARTICLE_SEARCH, lcc_tree=lcc_jstree)
83@blueprint.route("/search", methods=['GET'])
84def search():
85 # If there are URL params, check if we need to redirect to articles rather than journals
86 if request.values:
87 # Flat search the query params as string so we don't have to traverse all the way down the decoded json.
88 if re.search(r'\"_type\"\s*:\s*\"article\"', request.values.get('source', '')):
89 return redirect(url_for("doaj.articles_search"), 301)
90 return redirect(url_for("doaj.journals_search"), 301)
93@blueprint.route("/search", methods=['POST'])
94def search_post():
95 """ Redirect a query from the box on the index page to the search page. """
96 if request.form.get('origin') != 'ui':
97 abort(400) # bad request - we must receive searches from our own UI
99 ref = request.form.get("ref")
100 if ref is None:
101 abort(400) # Referrer is required
103 ct = request.form.get("content-type")
104 kw = request.form.get("keywords")
105 field = request.form.get("fields")
107 if kw is None:
108 kw = request.form.get("q") # back-compat for the simple search widget
110 # lhs for journals, rhs for articles
111 field_map = {
112 "all": (None, None),
113 "title": ("bibjson.title", "bibjson.title"),
114 "abstract": (None, "bibjson.abstract"),
115 "subject": ("index.classification", "index.classification"),
116 "author": (None, "bibjson.author.name"),
117 "issn": ("index.issn.exact", None),
118 "publisher": ("bibjson.publisher.name", None),
119 "country": ("index.country", None)
120 }
121 default_field_opts = field_map.get(field, None)
122 default_field = None
124 route = ""
125 if not ct or ct == "journals":
126 route = url_for("doaj.journals_search")
127 if default_field_opts:
128 default_field = default_field_opts[0]
129 elif ct == "articles":
130 route = url_for("doaj.articles_search")
131 if default_field_opts:
132 default_field = default_field_opts[1]
133 else:
134 abort(400)
136 query = dao.Facetview2.make_query(kw, default_field=default_field, default_operator="AND")
138 return redirect(route + '?source=' + urllib.parse.quote(json.dumps(query)) + "&ref=" + urllib.parse.quote(ref))
141#############################################
143@blueprint.route("/csv")
144@plausible.pa_event(app.config.get('ANALYTICS_CATEGORY_JOURNALCSV', 'JournalCSV'),
145 action=app.config.get('ANALYTICS_ACTION_JOURNALCSV', 'Download'))
146def csv_data():
147 svc = DOAJ.journalService()
148 if current_user and not current_user.is_anonymous and current_user.has_role(constants.ROLE_PREMIUM_CSV):
149 jc = svc.get_premium_csv()
150 else:
151 jc = svc.get_free_csv()
153 if jc is None:
154 abort(404)
156 store_url = svc.get_temporary_url(jc)
158 if store_url.startswith("/"):
159 store_url = "/store" + store_url
161 return redirect(store_url, code=307)
164@blueprint.route("/sitemap.xml")
165def sitemap_legacy():
166 return redirect(url_for('doaj.sitemap', suffix="0"), 301)
168@blueprint.route("/sitemap_index.xml")
169def sitemap_index_legacy():
170 return redirect(url_for('doaj.sitemap', suffix="_index_0"), 301)
172@blueprint.route("/sitemap<suffix>.xml")
173def sitemap(suffix):
174 """
175 This route handles both sitemaps, of the form /sitemapN.xml, and sitemap indexes, of the form /sitemap_index_N.xml
176 :param suffix:
177 :return:
178 """
179 url = None
180 if suffix.startswith("_index_"):
181 n = suffix[len("_index_"):]
182 url = models.Cache.get_sitemap_index(n)
183 else:
184 url = models.Cache.get_sitemap(suffix)
185 if url is None:
186 abort(404)
187 if url.startswith("/"):
188 url = "/store" + url
189 return redirect(url, code=307)
192@blueprint.route("/public-data-dump/<record_type>")
193@login_required
194@plausible.pa_event(app.config.get('ANALYTICS_CATEGORY_PUBLICDATADUMP', 'PublicDataDump'),
195 action=app.config.get('ANALYTICS_ACTION_PUBLICDATADUMP', 'Download'))
196def public_data_dump_redirect(record_type):
197 if not current_user.has_role(constants.ROLE_PUBLIC_DATA_DUMP) and not current_user.has_role(constants.ROLE_PREMIUM_PDD):
198 abort(404)
200 svc = DOAJ.publicDataDumpService()
201 if current_user.has_role(constants.ROLE_PREMIUM_PDD):
202 dd = svc.get_premium_dump()
203 else:
204 dd = svc.get_free_dump()
206 if dd is None:
207 abort(404)
209 store_url = svc.get_temporary_url(dd, record_type)
211 if store_url.startswith("/"):
212 store_url = "/store" + store_url
214 return redirect(store_url, code=307)
216@blueprint.route("/store/<container>/<dir>/<filename>")
217def get_from_local_store_dir(container, dir, filename):
218 file = os.path.join(dir, filename)
219 return get_from_local_store(container, file)
221@blueprint.route("/store/<container>/<filename>")
222def get_from_local_store(container, filename):
223 if not app.config.get("STORE_LOCAL_EXPOSE", False):
224 abort(404)
226 from portality import store
227 localStore = store.StoreFactory.get(None)
228 file_handle = localStore.get(container, filename)
229 return send_file(file_handle, mimetype="application/octet-stream", as_attachment=True,
230 download_name=os.path.basename(filename))
233@blueprint.route('/autocomplete/<doc_type>/<field_name>', methods=["GET", "POST"])
234def autocomplete(doc_type, field_name):
235 prefix = request.args.get('q', '')
236 if not prefix:
237 return jsonify({'suggestions': [{"id": "",
238 "text": "No results found"}]}) # select2 does not understand 400, which is the correct code here...
240 m = models.lookup_model(doc_type)
241 if not m:
242 return jsonify({'suggestions': [{"id": "",
243 "text": "No results found"}]}) # select2 does not understand 404, which is the correct code here...
245 size = request.args.get('size', 5)
247 filter_field = app.config.get("AUTOCOMPLETE_ADVANCED_FIELD_MAPS", {}).get(field_name)
249 suggs = []
250 if filter_field is None:
251 suggs = m.autocomplete(field_name, prefix, size=size)
252 else:
253 suggs = m.advanced_autocomplete(filter_field, field_name, prefix, size=size, prefix_only=False)
255 return jsonify({'suggestions': suggs})
256 # you shouldn't return lists top-level in a JSON response:
257 # http://flask.pocoo.org/docs/security/#json-security
260def is_issn_by_identifier(identifier):
261 return len(identifier) == 9
264def find_correct_redirect_identifier(identifier, bibjson) -> str:
265 """
266 return None if identifier is correct and no redirect is needed
268 :param identifier:
269 :param bibjson:
270 :return:
271 """
272 if is_issn_by_identifier(identifier): # the journal is referred to by an ISSN
273 # if there is an E-ISSN (and it's not the one in the request), redirect to it
274 eissn = bibjson.get_one_identifier(bibjson.E_ISSN)
275 if eissn and identifier != eissn:
276 return eissn
278 # if there's no E-ISSN, but there is a P-ISSN (and it's not the one in the request), redirect to the P-ISSN
279 if not eissn:
280 pissn = bibjson.get_one_identifier(bibjson.P_ISSN)
281 if pissn and identifier != pissn:
282 return pissn
284 # The journal has neither a PISSN or an EISSN. Yet somehow
285 # issn_ref is True, the request was referring to the journal
286 # by its ISSN. Not sure how this could ever happen, but just
287 # continue loading the data and do nothing else in such a
288 # case.
290 else: # the journal is NOT referred to by any ISSN
292 # if there is an E-ISSN, redirect to it
293 # if not, but there is a P-ISSN, redirect to it
294 # if neither ISSN is present, continue loading the page
295 issn = bibjson.get_one_identifier(bibjson.E_ISSN)
296 if not issn:
297 issn = bibjson.get_one_identifier(bibjson.P_ISSN)
298 if issn:
299 return issn
301 # let it continue loading if we only have the hex UUID for the journal (no ISSNs)
302 # and the user is referring to the toc page via that ID
305@blueprint.route("/toc/<identifier>")
306def toc(identifier=None):
307 """ Table of Contents page for a journal. identifier may be the journal id or an issn """
308 # If this route is changed, update JOURNAL_TOC_URL_FRAG in settings.py (partial ToC page link for journal CSV)
309 if identifier is None:
310 abort(400)
312 journalSvc = DOAJ.journalService()
313 try:
314 journal = journalSvc.find_best(identifier)
315 except bll_exceptions.ArgumentException:
316 abort(400)
317 except bll_exceptions.TooManyJournals:
318 abort(500)
320 if journal is None:
321 abort(404)
323 if journal.is_in_doaj() is False:
324 raise ui_exceptions.JournalWithdrawn()
326 bibjson = journal.bibjson()
327 real_identifier = find_correct_redirect_identifier(identifier, bibjson)
328 current_info = {'next': url_for('publisher.update_request', journal_id=journal.id)}
329 form = LoginForm(request.form, csrf_enabled=False, **current_info)
330 if real_identifier:
331 return redirect(url_for('doaj.toc', identifier=real_identifier), 301)
332 else:
333 # now render all that information
334 return render_template(templates.PUBLIC_TOC_MAIN, journal=journal, bibjson=bibjson, tab="main", form=form)
337@blueprint.route("/toc/articles/<identifier>")
338def toc_articles_legacy(identifier=None):
339 return redirect(url_for('doaj.toc_articles', identifier=identifier, volume=1, issue=1), 301)
342@blueprint.route("/toc/<identifier>/articles")
343def toc_articles(identifier=None):
344 if identifier is None:
345 abort(400)
347 journalSvc = DOAJ.journalService()
348 try:
349 journal = journalSvc.find_best(identifier)
350 except bll_exceptions.ArgumentException:
351 abort(400)
352 except bll_exceptions.TooManyJournals:
353 abort(500)
355 if journal is None:
356 abort(404)
358 if journal.is_in_doaj() is False:
359 raise ui_exceptions.JournalWithdrawn()
361 bibjson = journal.bibjson()
362 articles_no = journal.article_stats()["total"]
363 real_identifier = find_correct_redirect_identifier(identifier, bibjson)
364 if real_identifier:
365 return redirect(url_for('doaj.toc_articles', identifier=real_identifier), 301)
366 else:
367 return render_template(templates.PUBLIC_TOC_ARTICLES, journal=journal, bibjson=bibjson, articles_no=articles_no, tab="articles")
370# ~~->Article:Page~~
371@blueprint.route("/article/<identifier>")
372def article_page(identifier=None):
373 # identifier must be the article id
374 article = models.Article.pull(identifier)
376 if article is None:
377 article = models.ArticleTombstone.pull(identifier)
378 if article:
379 raise ui_exceptions.TombstoneArticle()
380 else:
381 abort(404, description=Messages.ARTICLE_NOT_FOUND)
383 if not article.is_in_doaj():
384 raise ui_exceptions.ArticleFromWithdrawnJournal()
386 # find the related journal record
387 journal = article.get_journal()
388 if journal is None:
389 app.logger.exception(Messages.ARTICLE_ABANDONED_LOG.format(article_id=article.id))
390 abort(500, description=Messages.ARTICLE_ABANDONED_PUBLIC)
391 if journal.is_in_doaj() is False:
392 raise ui_exceptions.ArticleFromWithdrawnJournal()
394 # issns = article.bibjson().issns()
395 # more_issns = article.bibjson().journal_issns
396 # for issn in issns + more_issns:
397 # journals = models.Journal.find_by_issn(issn)
398 # if len(journals) == 0:
399 # app.logger.exception(Messages.ARTICLE_ABANDONED_LOG.format(article_id=article.id))
400 # abort(500, description=Messages.ARTICLE_ABANDONED_PUBLIC)
401 # try:
402 # journal = models.Journal.get_active_journal(journals)
403 # except exceptions.TooManyJournals:
404 # app.logger.exception(Messages.TOO_MANY_JOURNALS_LOG.format(identifier=identifier))
405 # abort(500, description=Messages.TOO_MANY_JOURNALS.format(identifier=identifier))
406 # except exceptions.JournalWithdrawn:
407 # raise exceptions.ArticleFromWithdrawnJournal
409 return render_template(templates.PUBLIC_ARTICLE, article=article, journal=journal, page={"highlight" : True})
412###############################################################
413# The various static endpoints
414###############################################################
416@blueprint.route("/googlebdb21861de30fe30.html")
417def google_webmaster_tools():
418 return 'google-site-verification: googlebdb21861de30fe30.html'
421@blueprint.route("/accessibility/")
422def accessibility():
423 return render_template(templates.STATIC_PAGE, page_frag="/legal/accessibility.html")
426@blueprint.route("/privacy/")
427def privacy():
428 return render_template(templates.STATIC_PAGE, page_frag="/legal/privacy.html")
431@blueprint.route("/contact/")
432def contact():
433 return render_template(templates.STATIC_PAGE, page_frag="/legal/contact.html")
436@blueprint.route("/terms/")
437def terms():
438 return render_template(templates.STATIC_PAGE, page_frag="/legal/terms.html")
441@blueprint.route("/code-of-conduct/")
442def conduct():
443 """
444 ~~Conduct:WebRoute~~
445 """
446 return render_template(templates.STATIC_PAGE, page_frag="/legal/code-of-conduct.html")
449@blueprint.route("/media/")
450def media():
451 """
452 ~~Media:WebRoute~~
453 """
454 return render_template(templates.STATIC_PAGE, page_frag="/legal/media.html")
457@blueprint.route("/support/")
458def support():
459 return render_template(templates.STATIC_PAGE, page_frag="/support/index.html")
462@blueprint.route("/support/sponsors/")
463def sponsors():
464 return render_template(templates.STATIC_PAGE, page_frag="/support/sponsors.html")
467@blueprint.route("/support/publisher-supporters/")
468def publisher_supporters():
469 return render_template(templates.STATIC_PAGE, page_frag="/support/publisher-supporters.html")
472@blueprint.route("/support/supporters/")
473def supporters():
474 return render_template(templates.STATIC_PAGE, page_frag="/support/supporters.html")
477@blueprint.route("/support/funders/")
478def funders():
479 return render_template(templates.STATIC_PAGE, page_frag="/support/funders.html")
482@blueprint.route("/support/thank-you/")
483def application_thanks():
484 return render_template(templates.STATIC_PAGE, page_frag="/support/thank-you.html")
487@blueprint.route("/apply/guide/")
488def guide():
489 return render_template(templates.STATIC_PAGE, page_frag="/apply/guide.html")
492@blueprint.route("/apply/transparency/")
493def transparency():
494 return render_template(templates.STATIC_PAGE, page_frag="/apply/transparency.html")
497@blueprint.route("/apply/why-index/")
498def why_index():
499 return render_template(templates.STATIC_PAGE, page_frag="/apply/why-index.html")
502@blueprint.route("/apply/publisher-responsibilities/")
503def publisher_responsibilities():
504 return render_template(templates.STATIC_PAGE, page_frag="/apply/publisher-responsibilities.html")
507@blueprint.route("/apply/copyright-and-licensing/")
508def copyright_and_licensing():
509 return render_template(templates.STATIC_PAGE, page_frag="/apply/copyright-and-licensing.html")
512@blueprint.route("/docs/oai-pmh/")
513def oai_pmh():
514 return render_template(templates.STATIC_PAGE, page_frag="/docs/oai-pmh.html")
517@blueprint.route('/docs/api/')
518def docs():
519 major_version = app.config.get("CURRENT_API_MAJOR_VERSION")
520 return redirect(url_for('api_v' + major_version + '.docs'))
523@blueprint.route("/docs/xml/")
524def xml():
525 return render_template(templates.STATIC_PAGE, page_frag="/docs/xml.html")
528@blueprint.route("/docs/widgets/")
529def widgets():
530 return render_template(templates.STATIC_PAGE, page_frag="/docs/widgets.html", base_url=app.config.get('BASE_URL'))
533@blueprint.route("/docs/public-data-dump/")
534def public_data_dump():
535 if current_user and not current_user.is_anonymous and (
536 current_user.has_role(constants.ROLE_PUBLIC_DATA_DUMP) or current_user.has_role(constants.ROLE_PREMIUM_PDD)):
537 dds = DOAJ.publicDataDumpService()
538 if current_user.has_role(constants.ROLE_PREMIUM_PDD):
539 dd = dds.get_premium_dump()
540 else:
541 dd = dds.get_free_dump()
542 return render_template(templates.STATIC_PAGE, page_frag="/docs/pdd-access.html", pdd=dd)
543 return render_template(templates.STATIC_PAGE, page_frag="/docs/pdd-contact.html")
546@blueprint.route("/docs/openurl/")
547def openurl():
548 return render_template(templates.STATIC_PAGE, page_frag="/docs/openurl.html")
551@blueprint.route("/docs/faq/")
552def faq():
553 return render_template(templates.STATIC_PAGE, page_frag="/docs/faq.html")
555@blueprint.route("/docs/journal-csv")
556def journal_csv():
557 svc = DOAJ.journalService()
558 if not current_user.is_anonymous and current_user.has_role(constants.ROLE_PREMIUM_CSV):
559 jc = svc.get_premium_csv()
560 else:
561 jc = svc.get_free_csv()
562 return render_template(templates.STATIC_PAGE, page_frag="/docs/journal-csv.html", jc=jc)
564@blueprint.route("/docs/premium")
565def premium():
566 return render_template(templates.STATIC_PAGE, page_frag="/docs/premium.html")
568@blueprint.route("/about/")
569def about():
570 return render_template(templates.STATIC_PAGE, page_frag="/about/index.html")
574@blueprint.route("/at-20/")
575def at_20():
576 return render_template(templates.STATIC_PAGE, page_frag="/about/at-20.html")
581@blueprint.route("/about/ambassadors/")
582def ambassadors():
583 return render_template(templates.STATIC_PAGE, page_frag="/about/ambassadors.html")
586@blueprint.route("/about/foundation-advisory-board/")
587def abc():
588 return render_template(templates.STATIC_PAGE, page_frag="/about/advisory-board-council.html")
591@blueprint.route("/about/advisory-board-council/")
592def abc_redirect():
593 return redirect("/about/foundation-advisory-board/", code=301)
596@blueprint.route("/about/editorial-policy-advisory-group/")
597def epag():
598 return render_template(templates.STATIC_PAGE, page_frag="/about/editorial-policy-advisory-group.html")
601@blueprint.route("/about/volunteers/")
602def volunteers():
603 return render_template(templates.STATIC_PAGE, page_frag="/about/volunteers.html")
606@blueprint.route("/about/team/")
607def team():
608 return render_template(templates.STATIC_PAGE, page_frag="/about/team.html")
611@blueprint.route("/preservation/")
612def preservation():
613 return render_template(templates.STATIC_PAGE, page_frag="/preservation/index.html")
616# LEGACY ROUTES
617@blueprint.route("/subjects")
618def subjects():
619 # return render_template("doaj/subjects.html", subject_page=True, lcc_jstree=json.dumps(lcc_jstree))
620 return redirect(url_for("doaj.journals_search"), 301)
623@blueprint.route("/application/new")
624def old_application():
625 return redirect(url_for("apply.public_application", **request.args), code=308)
628@blueprint.route("/<cc>/mejorespracticas")
629@blueprint.route("/<cc>/boaspraticas")
630@blueprint.route("/<cc>/bestpractice")
631@blueprint.route("/<cc>/editionsavante")
632@blueprint.route("/bestpractice")
633@blueprint.route("/oainfo")
634def bestpractice(cc=None):
635 return redirect(url_for("doaj.transparency", **request.args), code=308)
638@blueprint.route("/membership")
639def membership():
640 return redirect(url_for("doaj.support", **request.args), code=308)
643@blueprint.route("/publishermembers")
644def old_sponsors():
645 return redirect(url_for("doaj.sponsors", **request.args), code=308)
648@blueprint.route("/members")
649def members():
650 return redirect(url_for("doaj.supporters", **request.args), code=308)
653@blueprint.route('/features')
654def features():
655 return redirect(url_for("doaj.xml", **request.args), code=308)
658# @blueprint.route('/widgets')
659# def old_widgets():
660# return redirect(url_for("doaj.widgets", **request.args), code=308)
663# @blueprint.route("/public-data-dump/<record_type>")
664# def old_public_data_dump(record_type):
665# return redirect(url_for("doaj.public_data_dump", **request.args), code=308)
668@blueprint.route("/openurl/help")
669def old_openurl():
670 return redirect(url_for("doaj.openurl", **request.args), code=308)
673@blueprint.route("/faq")
674def old_faq():
675 return redirect(url_for("doaj.faq", **request.args), code=308)
678@blueprint.route("/publishers")
679def publishers():
680 return redirect(url_for("doaj.guide", **request.args), code=308)
683# Redirects necessitated by new templates
684@blueprint.route("/password-reset/")
685def new_password_reset():
686 return redirect(url_for('account.forgot'), code=301)
689@blueprint.route("/u/<alias>")
690@plausible.pa_event(app.config.get('ANALYTICS_CATEGORY_URLSHORT', 'Urlshort'),
691 action=app.config.get('ANALYTICS_ACTION_URLSHORT_REDIRECT', 'Redirect'))
692def shortened_url(alias):
693 short = DOAJ.shortUrlService().find_url_by_alias(alias)
694 if short:
695 return redirect(short.url)
697 app.logger.debug(f"Shortened URL not found: [{alias}]")
698 abort(404)