Ejemplo n.º 1
0
 def write_log(self):
     "Write a log entry for the change."
     log = dict(
         _id=utils.get_iuid(),
         doc=self.doc["_id"],
         doctype=self.doc[constants.DOCTYPE],
         changed=self.changed,
         modified=utils.timestamp(),
     )
     log[constants.DOCTYPE] = constants.LOG
     if self.rqh:
         # xheaders argument to HTTPServer takes care of X-Real-Ip
         # and X-Forwarded-For
         log["remote_ip"] = self.rqh.request.remote_ip
         try:
             log["user_agent"] = self.rqh.request.headers["User-Agent"]
         except KeyError:
             pass
     if self.account:
         try:
             log["account"] = self.account["email"]
         except (TypeError, AttributeError, KeyError):
             pass
         try:
             log["user_agent"] = self.account["user_agent"]
         except (TypeError, AttributeError, KeyError):
             pass
     self.db.put(log)
Ejemplo n.º 2
0
 def get_account_json(self, account, full=False):
     "JSON representation of account."
     URL = self.absolute_reverse_url
     result = dict()
     result["entity"] = "account"
     result["iuid"] = account["_id"]
     result["timestamp"] = utils.timestamp()
     result["links"] = dict(
         [
             ("self", {"href": URL("account_json", account["email"])}),
             ("display", {"href": URL("account", account["email"])}),
         ]
     )
     result["email"] = account["email"]
     result["name"] = account.get("name")  # May be absent.
     result["role"] = account["role"]
     result["status"] = account.get("disabled") and "disabled" or "enabled"
     result["login"] = account.get("login")
     if full:
         result["api_key"] = account.get("api_key")
         result["labels"] = labels = []
         for label in account["labels"]:
             links = dict()
             links["self"] = {"href": URL("label_json", label)}
             links["display"] = {"href": URL("label", label)}
             labels.append(dict([("value", label), ("links", links)]))
     result["created"] = account["created"]
     result["modified"] = account["modified"]
     return result
Ejemplo n.º 3
0
 def initialize(self):
     "Set the initial values for the new document."
     try:
         self.doc['account'] = self.account['email']
     except (TypeError, AttributeError, KeyError):
         self.doc['account'] = None
     self.doc['created'] = utils.timestamp()
Ejemplo n.º 4
0
 def get_label_json(self, label, publications=None, accounts=None):
     "JSON representation of label."
     URL = self.absolute_reverse_url
     result = dict()
     result["entity"] = "label"
     result["iuid"] = label["_id"]
     result["timestamp"] = utils.timestamp()
     result["links"] = links = dict()
     links["self"] = {"href": URL("label_json", label["value"])}
     links["display"] = {"href": URL("label", label["value"])}
     result["value"] = label["value"]
     if settings["TEMPORAL_LABELS"]:
         result["started"] = label.get("started")
         result["ended"] = label.get("ended")
     result["created"] = label["created"]
     result["modified"] = label["modified"]
     if accounts is not None:
         result["accounts"] = [
             self.get_account_json(account) for account in accounts
         ]
     if publications is None:
         try:
             result["publications_count"] = label["count"]
         except KeyError:
             pass
     else:
         result["publications_count"] = len(publications)
         result["publications"] = [
             self.get_publication_json(publication) for publication in publications
         ]
     return result
Ejemplo n.º 5
0
 def post(self):
     """Login to a account account. Set a secure cookie.
     Log failed login attempt and disable account if too many recent.
     """
     try:
         email = self.get_argument("email")
         password = self.get_argument("password")
     except tornado.web.MissingArgumentError:
         self.set_error_flash("Missing email or password argument.")
         self.see_other("login")
         return
     try:
         account = self.get_account(email)
         if utils.hashed_password(password) != account.get("password"):
             raise KeyError
     except KeyError:
         self.set_error_flash("No such account or invalid password.")
         self.see_other("login")
     else:
         self.set_secure_cookie(
             constants.USER_COOKIE,
             account["email"],
             expires_days=settings["LOGIN_MAX_AGE_DAYS"],
         )
         with AccountSaver(doc=account, rqh=self) as saver:
             saver["login"] = utils.timestamp()  # Set last login timestamp.
         self.redirect(self.reverse_url("home"))
Ejemplo n.º 6
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     accounts = kwargs["accounts"]
     result = dict()
     result["entity"] = "accounts"
     result["timestamp"] = utils.timestamp()
     result["accounts_count"] = len(accounts)
     result["accounts"] = [
         self.get_account_json(account, full=True) for account in accounts
     ]
     self.write(result)
Ejemplo n.º 7
0
 def render(self, template, researchers):
     "Override; ignores template, and outpus JSON instead of HTML."
     URL = self.absolute_reverse_url
     result = dict()
     result["entity"] = "researchers"
     result["timestamp"] = utils.timestamp()
     result["links"] = links = dict()
     links["self"] = {"href": URL("researchers_json")}
     links["display"] = {"href": URL("researchers")}
     result["researchers"] = [self.get_json(r) for r in researchers]
     self.write(result)
Ejemplo n.º 8
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     labels = kwargs["labels"]
     result = dict()
     result["entity"] = "labels"
     result["timestamp"] = utils.timestamp()
     result["links"] = links = dict()
     links["self"] = {"href": URL("labels_json")}
     links["display"] = {"href": URL("labels")}
     result["labels_count"] = len(labels)
     result["labels"] = [self.get_label_json(l) for l in labels]
     self.write(result)
Ejemplo n.º 9
0
 def get_publication_json(self, publication, full=True, single=False):
     "JSON representation of publication."
     URL = self.absolute_reverse_url
     result = dict()
     if full:
         result["entity"] = "publication"
         result["iuid"] = publication["_id"]
         if single:
             result["timestamp"] = utils.timestamp()
         result["links"] = dict(
             [
                 ("self", {"href": URL("publication_json", publication["_id"])}),
                 ("display", {"href": URL("publication", publication["_id"])}),
             ]
         )
     result["title"] = publication["title"]
     if full:
         result["authors"] = []
         for author in publication["authors"]:
             au = dict()
             au["family"] = author.get("family")
             au["given"] = author.get("given")
             au["initials"] = author.get("initials")
             if author.get("researcher"):
                 researcher = self.get_researcher(author["researcher"])
                 if researcher.get("orcid"):
                     au["orcid"] = researcher["orcid"]
                 au["researcher"] = {
                     "href": URL("researcher_json", author["researcher"])
                 }
             result["authors"].append(au)
         result["type"] = publication.get("type")
     result["published"] = publication.get("published")
     result["journal"] = publication.get("journal")
     # XXX Kludge: this is not stored in the publication, since it is
     # not obtained (or at least not parsed) from PubMed or Crossref.
     result["journal"]["issn-l"] = self.get_issn_l(result["journal"].get("issn"))
     if full:
         result["abstract"] = publication.get("abstract")
     result["doi"] = publication.get("doi")
     result["pmid"] = publication.get("pmid")
     result["labels"] = publication.get("labels") or []
     result["xrefs"] = publication.get("xrefs") or []
     if full:
         result["notes"] = publication.get("notes") or []
         result["created"] = publication["created"]
         result["modified"] = publication["modified"]
     return result
Ejemplo n.º 10
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     publications = kwargs["publications"]
     terms = kwargs["terms"]
     result = dict()
     result["entity"] = "publications search"
     result["timestamp"] = utils.timestamp()
     result["terms"] = terms
     result["links"] = links = dict()
     links["self"] = {"href": URL("search_json", terms=terms)}
     links["display"] = {"href": URL("search", terms=terms)}
     result["publications_count"] = len(publications)
     result["publications"] = [
         self.get_publication_json(publication) for publication in publications
     ]
     self.write(result)
Ejemplo n.º 11
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     publications = kwargs['publications']
     terms = kwargs['terms']
     result = OD()
     result['entity'] = 'publications search'
     result['timestamp'] = utils.timestamp()
     result['terms'] = terms
     result['links'] = links = OD()
     links['self'] = {'href': URL('search_json', terms=terms)}
     links['display'] = {'href': URL('search', terms=terms)}
     result['publications_count'] = len(publications)
     result['publications'] = [
         self.get_publication_json(publication)
         for publication in publications
     ]
     self.write(result)
Ejemplo n.º 12
0
 def get(self, identifier):
     "Display the researcher."
     try:
         researcher = self.get_researcher(identifier)
     except KeyError as error:
         raise tornado.web.HTTPError(404, reason="no such researcher")
     publications = self.get_docs("publication",
                                  "researcher",
                                  key=researcher["_id"])
     publications.sort(key=lambda i: i["published"], reverse=True)
     result = dict()
     result["entity"] = "researcher"
     result["timestamp"] = utils.timestamp()
     result.update(self.get_json(researcher))
     result["publications"] = [
         self.get_publication_json(publ) for publ in publications
     ]
     self.write(result)
Ejemplo n.º 13
0
 def post(self, identifier):
     try:
         publication = self.get_publication(identifier)
         self.check_deletable(publication)
     except (KeyError, ValueError) as error:
         self.see_other("home", error=str(error))
         return
     blacklist = {
         constants.DOCTYPE: constants.BLACKLIST,
         "_id": utils.get_iuid(),
         "title": publication["title"],
         "pmid": publication.get("pmid"),
         "doi": publication.get("doi"),
         "created": utils.timestamp(),
         "owner": self.current_user["email"],
     }
     self.db.put(blacklist)
     self.delete_entity(publication)
     self.see_other("home")
Ejemplo n.º 14
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     journals = kwargs["journals"]
     result = dict()
     result["entity"] = "journals"
     result["timestamp"] = utils.timestamp()
     result["links"] = links = dict()
     links["self"] = {"href": URL("journals_json")}
     links["display"] = {"href": URL("journals")}
     result["journals_count"] = len(journals)
     result["journals"] = items = []
     for journal in journals:
         item = dict()
         item["title"] = journal["title"]
         item["issn"] = journal.get("issn")
         item["publications_count"] = journal["count"]
         item["links"] = links = dict()
         links["self"] = {"href": URL("journal_json", journal["title"])}
         links["display"] = {"href": URL("journal", journal["title"])}
         items.append(item)
     self.write(result)
Ejemplo n.º 15
0
 def render(self, template, **kwargs):
     URL = self.absolute_reverse_url
     journal = kwargs["journal"]
     publications = kwargs["publications"]
     result = dict()
     result["entity"] = "journal"
     result["iuid"] = journal["_id"]
     result["timestamp"] = utils.timestamp()
     result["links"] = links = dict()
     links["self"] = {"href": URL("journal_json", journal["title"])}
     links["display"] = {"href": URL("journal", journal["title"])}
     result["title"] = journal["title"]
     result["issn"] = journal.get("issn")
     result["issn-l"] = journal.get("issn-l")
     result["publications_count"] = len(publications)
     result["publications"] = [
         self.get_publication_json(publication)
         for publication in publications
     ]
     result["created"] = journal["created"]
     result["modified"] = journal["modified"]
     self.write(result)
Ejemplo n.º 16
0
 def post(self):
     try:
         email = self.get_argument("account")
         password = self.get_argument("password")
         code = self.get_argument("code", "")
         account = self.get_account(email)
         if not self.is_admin() and code != account.get("code"):
             raise ValueError
         with AccountSaver(account, rqh=self) as saver:
             saver.set_password(password)
             # Login directly if not already logged in
             if not self.current_user:
                 self.set_secure_cookie(
                     constants.USER_COOKIE,
                     account["email"],
                     expires_days=settings["LOGIN_MAX_AGE_DAYS"],
                 )
                 saver["login"] = utils.timestamp()
     except (tornado.web.MissingArgumentError, KeyError, ValueError):
         self.set_error_flash("Missing or wrong data in field(s).")
         self.see_other("account_password")
     else:
         self.see_other("account", account["email"])
Ejemplo n.º 17
0
 def write_log(self):
     "Write a log entry for the change."
     # utils.write_log(self.db, self.rqh, self.doc, changed=self.changed)
     log = dict(_id=utils.get_iuid(),
                doc=self.doc['_id'],
                doctype=self.doc[constants.DOCTYPE],
                changed=self.changed,
                modified=utils.timestamp())
     log[constants.DOCTYPE] = constants.LOG
     if self.rqh:
         # xheaders argument to HTTPServer takes care of X-Real-Ip
         # and X-Forwarded-For
         log['remote_ip'] = self.rqh.request.remote_ip
         try:
             log['user_agent'] = self.rqh.request.headers['User-Agent']
         except KeyError:
             pass
     if self.account:
         try:
             log['account'] = self.account['email']
         except (TypeError, AttributeError, KeyError):
             pass
     self.db.save(log)
Ejemplo n.º 18
0
 def finalize(self):
     "Perform any final modifications before saving the document."
     self.doc['modified'] = utils.timestamp()