def get_dbserver(): "Return the CouchDB2 handle for the CouchDB server." kwargs = dict(href=settings["DATABASE_SERVER"]) if settings.get("DATABASE_ACCOUNT") and settings.get("DATABASE_PASSWORD"): kwargs["username"] = settings["DATABASE_ACCOUNT"] kwargs["password"] = settings["DATABASE_PASSWORD"] return couchdb2.Server(**kwargs)
def __init__(self): """Open the connection to the email server. Raise ValueError if no email server host has been defined. """ try: host = settings["EMAIL"]["HOST"] if not host: raise ValueError self.email = settings.get("SITE_EMAIL") or settings["EMAIL"]["SENDER"] except (KeyError, TypeError): raise ValueError("email server host is not properly defined") port = settings["EMAIL"].get("PORT") or 0 if settings["EMAIL"].get("SSL"): self.server = smtplib.SMTP_SSL(host, port=port) else: self.server = smtplib.SMTP(host, port=port) if settings["EMAIL"].get("TLS"): self.server.starttls() self.server.ehlo() try: user = settings["EMAIL"]["USER"] password = settings["EMAIL"]["PASSWORD"] except KeyError: pass else: self.server.login(user, password)
def get(self): if self.is_admin(): account = self.get_argument("account", self.current_user["email"]) elif self.current_user: account = self.current_user["email"] else: account = None if settings.get("EMAIL") and settings["EMAIL"].get("HOST"): self.render("account_reset.html", account=account) else: self.set_error_flash( "Cannot reset password since" " no email server configuration." " Contact the system administrator." ) self.see_other("home")
def load_settings(filepath=None, log=True): """Load the settings. The file path first specified is used: 1) The argument to this procedure (possibly from a command line argument). 2) The environment variable PUBLICATIONS_SETTINGS. 3) The file '../site/settings.yaml' relative to this directory. If 'log' is True, activate logging according to DEBUG settings. Raise IOError if settings file could not be read. Raise KeyError if a settings variable is missing. Raise ValueError if a settings variable value is invalid. """ site_dir = settings["SITE_DIR"] if not os.path.exists(site_dir): raise IOError(f"The required site directory '{site_dir}' does not exist.") if not os.path.isdir(site_dir): raise IOError(f"The site directory path '{site_dir}' is not a directory.") # Find and read the settings file, updating the defaults. if not filepath: try: filename = os.environ["PUBLICATIONS_SETTINGS"] except KeyError: filepath = os.path.join(site_dir, "settings.yaml") with open(filepath) as infile: settings.update(yaml.safe_load(infile)) settings["SETTINGS_FILE"] = filepath # Setup logging. if settings.get("LOGGING_DEBUG"): kwargs = dict(level=logging.DEBUG) else: kwargs = dict(level=logging.INFO) try: kwargs["format"] = settings["LOGGING_FORMAT"] except KeyError: pass try: kwargs["filename"] = settings["LOGGING_FILEPATH"] except KeyError: pass else: try: kwargs["filemode"] = settings["LOGGING_FILEMODE"] except KeyError: pass settings["LOG"] = log if log: logging.basicConfig(**kwargs) logging.info(f"Publications version {constants.VERSION}") logging.info(f"ROOT: {constants.ROOT}") logging.info(f"SITE_DIR: {settings['SITE_DIR']}") logging.info(f"settings: {settings['SETTINGS_FILE']}") logging.info(f"logging debug: {settings['LOGGING_DEBUG']}") logging.info(f"tornado debug: {settings['TORNADO_DEBUG']}") # Check some settings. for key in ["BASE_URL", "PORT", "DATABASE_SERVER", "DATABASE_NAME"]: if key not in settings: raise KeyError(f"No settings['{key}'] item.") if not settings[key]: raise ValueError(f"Settings['{key}'] has invalid value.") if len(settings.get("COOKIE_SECRET") or "") < 10: raise ValueError("settings['COOKIE_SECRET'] not set, or too short.") if len(settings.get("PASSWORD_SALT") or "") < 10: raise ValueError("Settings['PASSWORD_SALT'] not set, or too short.") for key in ["PUBMED_DELAY", "PUBMED_TIMEOUT", "CROSSREF_DELAY", "CROSSREF_TIMEOUT"]: if not isinstance(settings[key], (int, float)) or settings[key] <= 0.0: raise ValueError(f"Invalid '{key}' value: must be positive number.") # Set up the xref templates URLs. settings["XREF_TEMPLATE_URLS"] = NocaseDict(settings["XREF_TEMPLATE_URLS"]) settings["XREF_TEMPLATE_URLS"]["URL"] = "%s"
def get_application(): url = tornado.web.url handlers = [ url(r"/", publications.home.Home, name="home"), url(r"/status", publications.home.Status, name="status"), url(r"/docs/([^/]+)", publications.home.Doc, name="doc"), url( r"/site/([^/]+)", tornado.web.StaticFileHandler, {"path": settings["SITE_STATIC_DIR"]}, name="site", ), url( r"/publication/([^/]{32,32})", publications.publication.Publication, name="publication", ), url( r"/publication/([^/]{32,32}).json", publications.publication.PublicationJson, name="publication_json", ), url( r"/publications/(\d{4})", publications.publication.Publications, name="publications_year", ), url( r"/publications/(\d{4}).json", publications.publication.PublicationsJson, name="publications_year_json", ), url(r"/publications", publications.publication.Publications, name="publications"), url( r"/publications.json", publications.publication.PublicationsJson, name="publications_json", ), url( r"/publications/csv", publications.publication.PublicationsCsv, name="publications_csv", ), url( r"/publications/xlsx", publications.publication.PublicationsXlsx, name="publications_xlsx", ), url( r"/publications/txt", publications.publication.PublicationsTxt, name="publications_txt", ), url( r"/publications/table/(\d{4})", publications.publication.PublicationsTable, name="publications_table_year", ), url( r"/publications/table", publications.publication.PublicationsTable, name="publications_table", ), url( r"/publications/no_pmid", publications.publication.PublicationsNoPmid, name="publications_no_pmid", ), url( r"/publications/no_pmid.json", publications.publication.PublicationsNoPmidJson, name="publications_no_pmid_json", ), url( r"/publications/no_doi", publications.publication.PublicationsNoDoi, name="publications_no_doi", ), url( r"/publications/no_doi.json", publications.publication.PublicationsNoDoiJson, name="publications_no_doi_json", ), url( r"/publications/no_label", publications.publication.PublicationsNoLabel, name="publications_no_label", ), url( r"/publications/no_label.json", publications.publication.PublicationsNoLabelJson, name="publications_no_label_json", ), url( r"/publications/duplicates", publications.publication.PublicationsDuplicates, name="publications_duplicates", ), url( r"/publications/modified", publications.publication.PublicationsModified, name="publications_modified", ), url( r"/edit/([^/]{32,32})", publications.publication.PublicationEdit, name="publication_edit", ), url( r"/researchers/([^/]{32,32})", publications.publication.PublicationResearchers, name="publication_researchers", ), url( r"/xrefs/([^/]{32,32})", publications.publication.PublicationXrefs, name="publication_xrefs", ), url(r"/add", publications.publication.PublicationAdd, name="publication_add"), url( r"/fetch", publications.publication.PublicationFetch, name="publication_fetch", ), url(r"/blacklist/([^/]+)", publications.blacklist.Blacklist, name="blacklist"), url(r"/blacklisted", publications.blacklist.Blacklisted, name="blacklisted"), url( r"/update/([^/]{32,32})/pmid", publications.publication.PublicationUpdatePmid, name="publication_update_pmid", ), url( r"/update/([^/]{32,32})/find_pmid", publications.publication.PublicationFindPmid, name="publication_find_pmid", ), url( r"/update/([^/]{32,32})/doi", publications.publication.PublicationUpdateDoi, name="publication_update_doi", ), url(r"/researcher", publications.researcher.ResearcherAdd, name="researcher_add"), url( r"/researchers_json", publications.researcher.ResearchersJson, name="researchers_json", ), url(r"/researchers", publications.researcher.Researchers, name="researchers"), url( r"/researcher/([^/]+).json", publications.researcher.ResearcherJson, name="researcher_json", ), url( r"/researcher/([^/]+)", publications.researcher.Researcher, name="researcher", ), url( r"/researcher/([^/]+)/edit", publications.researcher.ResearcherEdit, name="researcher_edit", ), url( r"/researcher/([^/]+)/publications.csv", publications.researcher.ResearcherPublicationsCsv, name="researcher_publications_csv", ), url( r"/researcher/([^/]+)/publications.xlsx", publications.researcher.ResearcherPublicationsXlsx, name="researcher_publications_xlsx", ), url( r"/researcher/([^/]+)/publications.txt", publications.researcher.ResearcherPublicationsTxt, name="researcher_publications_txt", ), url( r"/researcher/([^/]+)/publications/edit", publications.researcher.ResearcherPublicationsEdit, name="researcher_publications_edit", ), url(r"/journals", publications.journal.Journals, name="journals"), url(r"/journals.json", publications.journal.JournalsJson, name="journals_json"), url( r"/journal/([^/]+).json", publications.journal.JournalJson, name="journal_json", ), url(r"/journal/([^/]+)", publications.journal.Journal, name="journal"), url( r"/journal/([^/]+)/edit", publications.journal.JournalEdit, name="journal_edit", ), url(r"/labels", publications.label.LabelsList, name="labels"), url(r"/labels.json", publications.label.LabelsJson, name="labels_json"), url(r"/labels/table", publications.label.LabelsTable, name="labels_table"), # These two label path patterns need to be checked first. url(r"/label/([^\.]+)/edit", publications.label.LabelEdit, name="label_edit"), url(r"/label/([^\.]+)/merge", publications.label.LabelMerge, name="label_merge"), url(r"/label/([^\.]+).json", publications.label.LabelJson, name="label_json"), url(r"/label/([^\.]+)", publications.label.Label, name="label"), url(r"/label", publications.label.LabelAdd, name="label_add"), url(r"/account/reset", publications.account.AccountReset, name="account_reset"), url( r"/account/password", publications.account.AccountPassword, name="account_password", ), url( r"/account/([^/]+).json", publications.account.AccountJson, name="account_json", ), url(r"/account/([^/]+)", publications.account.Account, name="account"), url( r"/account/([^/]+)/edit", publications.account.AccountEdit, name="account_edit", ), url( r"/account/([^/]+)/disable", publications.account.AccountDisable, name="account_disable", ), url( r"/account/([^/]+)/enable", publications.account.AccountEnable, name="account_enable", ), url(r"/accounts", publications.account.Accounts, name="accounts"), url(r"/accounts.json", publications.account.AccountsJson, name="accounts_json"), url(r"/account", publications.account.AccountAdd, name="account_add"), url(r"/search", publications.search.Search, name="search"), url(r"/search.json", publications.search.SearchJson, name="search_json"), url(r"/subset", publications.subset.SubsetDisplay, name="subset"), url(r"/logs/([^/]+)", publications.log.Logs, name="logs"), url(r"/contact", publications.home.Contact, name="contact"), url(r"/settings", publications.home.Settings, name="settings"), url(r"/software", publications.home.Software, name="software"), url(r"/login", publications.login.Login, name="login"), url(r"/logout", publications.login.Logout, name="logout"), url( r"/api/publication", publications.publication.ApiPublicationFetch, name="api_publication_fetch", ), url( r"/api/publication/([^/]{32,32})/labels", publications.publication.ApiPublicationLabels, name="api_publication_labels", ), ] return tornado.web.Application( handlers=handlers, debug=settings.get("TORNADO_DEBUG", False), cookie_secret=settings["COOKIE_SECRET"], xsrf_cookies=True, ui_modules=uimodules, template_path=os.path.join(constants.ROOT, "templates"), static_path=os.path.join(constants.ROOT, "static"), login_url=r"/login", )
def main(): args = get_args() utils.load_settings(filepath=args.settings) utils.initialize() url = tornado.web.url handlers = [ url(r'/', Home, name='home'), url(r'/site/([^/]+)', tornado.web.StaticFileHandler, {'path': settings['SITE_DIR']}, name='site'), url(r'/publication/(.+).json', PublicationJson, name='publication_json'), url(r'/publication/(.+)', Publication, name='publication'), url(r'/publications/(\d{4})', Publications, name='publications_year'), url(r'/publications/(\d{4}).json', PublicationsJson, name='publications_year_json'), url(r'/publications', Publications, name='publications'), url(r'/publications.json', PublicationsJson, name='publications_json'), url(r'/publications/csv', PublicationsCsv, name='publications_csv'), url(r'/publications/xlsx', PublicationsXlsx, name='publications_xlsx'), url(r'/publications/table/(\d{4})', PublicationsTable, name='publications_table_year'), url(r'/publications/table', PublicationsTable, name='publications_table'), url(r'/publications/acquired', PublicationsAcquired, name='publications_acquired'), url(r'/publications/no_pmid', PublicationsNoPmid, name='publications_no_pmid'), url(r'/publications/no_doi', PublicationsNoDoi, name='publications_no_doi'), url(r'/publications/no_label', PublicationsNoLabel, name='publications_no_label'), url(r'/publications/duplicates', PublicationsDuplicates, name='publications_duplicates'), url(r'/publications/modified', PublicationsModified, name='publications_modified'), url(r'/edit/([^/]+)', PublicationEdit, name='publication_edit'), url(r'/xrefs/([^/]+)', PublicationXrefs, name='publication_xrefs'), url(r'/add', PublicationAdd, name='publication_add'), url(r'/fetch', PublicationFetch, name='publication_fetch'), url(r'/blacklist/([^/]+)', PublicationBlacklist, name='publication_blacklist'), url(r'/acquire/([^/]+)', PublicationAcquire, name='publication_acquire'), url(r'/release/([^/]+)', PublicationRelease, name='publication_release'), url(r'/qc/([^/]+)', PublicationQc, name='publication_qc'), url(r'/update/([^/]+)/pmid', PublicationUpdatePmid, name='publication_update_pmid'), url(r'/update/([^/]+)/doi', PublicationUpdateDoi, name='publication_update_doi'), url(r'/journals', Journals, name='journals'), url(r'/journals.json', JournalsJson, name='journals_json'), url(r'/journal/([^/]+).json', JournalJson, name='journal_json'), url(r'/journal/([^/]+)', Journal, name='journal'), url(r'/journal/([^/]+)/edit', JournalEdit, name='journal_edit'), url(r'/labels', LabelsList, name='labels'), url(r'/labels.json', LabelsJson, name='labels_json'), url(r'/labels/table', LabelsTable, name='labels_table'), # These two label path patterns need to be checked first. url(r'/label/([^\.]+)/edit', LabelEdit, name='label_edit'), url(r'/label/([^\.]+)/merge', LabelMerge, name='label_merge'), url(r'/label/([^\.]+).json', LabelJson, name='label_json'), url(r'/label/([^\.]+)', Label, name='label'), url(r'/label', LabelAdd, name='label_add'), url(r'/account/reset', AccountReset, name='account_reset'), url(r'/account/password', AccountPassword, name='account_password'), url(r'/account/([^/]+).json', AccountJson, name='account_json'), url(r'/account/([^/]+)', Account, name='account'), url(r'/account/([^/]+)/edit', AccountEdit, name='account_edit'), url(r'/account/([^/]+)/disable', AccountDisable, name='account_disable'), url(r'/account/([^/]+)/enable', AccountEnable, name='account_enable'), url(r'/accounts', Accounts, name='accounts'), url(r'/accounts.json', AccountsJson, name='accounts_json'), url(r'/account', AccountAdd, name='account_add'), url(r'/search', Search, name='search'), url(r'/search.json', SearchJson, name='search_json'), url(r'/logs/([^/]+)', Logs, name='logs'), url(r'/contact', Contact, name='contact'), url(r'/settings', Settings, name='settings'), url(r'/login', Login, name='login'), url(r'/logout', Logout, name='logout'), url(r'/api/publication', ApiPublicationFetch, name='api_publication_fetch'), ] os.chdir(settings['ROOT']) application = tornado.web.Application( handlers=handlers, debug=settings.get('TORNADO_DEBUG', False), cookie_secret=settings['COOKIE_SECRET'], xsrf_cookies=True, ui_modules=uimodules, template_path='templates', static_path='static', login_url=r'/login') application.listen(settings['PORT'], xheaders=True) pid = os.getpid() logging.info("web server PID %s at %s", pid, settings['BASE_URL']) if args.pidfile: with open(args.pidfile, 'w') as pf: pf.write(str(pid)) tornado.ioloop.IOLoop.instance().start()