def get(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) self.render('add_paper.html', art=self.art, form=self.form) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) self.render('add_pr.html', art=self.art, form=self.form) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) self.render('add_other.html', art=self.art, form=self.form)
def post(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) if self.form.validate(): url = self.form.url.data if self.kind == SourceKind.PAPER: # if adding a paper, try getting metadata doi = self.form.get_as_doi() if doi is not None: # we were given a doi - yay! dx_url = 'http://dx.doi.org/' + doi self.find_doi(dx_url) else: self.find_doi(url) else: # otherwise, we're all done - add source and finish up action = self.create_source(self.kind, url=url) self.wrap_things_up(action) else: if self.is_xhr(): # ajax? # collect up the form errors errs = {} for field in self.form: if field.errors: errs[field.name] = field.errors self.write({'success': False, 'errors': errs}) self.finish() else: self.render('add_paper.html', art=self.art, form=self.form)
def post(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) if self.form.validate(): url = self.form.url.data if self.kind == SourceKind.PAPER: # if adding a paper, try getting metadata doi = self.form.get_as_doi() if doi is not None: # we were given a doi - yay! dx_url = 'http://dx.doi.org/' + doi self.find_doi(dx_url) else: self.find_doi(url) else: # otherwise, we're all done - add source and finish up action = self.create_source(self.kind, url=url) self.wrap_things_up(action) else: if self.is_xhr(): # ajax? # collect up the form errors errs = {} for field in self.form: if field.errors: errs[field.name] = field.errors self.write({'success':False, 'errors':errs}) self.finish() else: self.render('add_paper.html', art=self.art, form=self.form)
class AddSourceHandler(BaseHandler): def get(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) self.render('add_paper.html', art=self.art, form=self.form) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) self.render('add_pr.html', art=self.art, form=self.form) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) self.render('add_other.html', art=self.art, form=self.form) @tornado.web.authenticated @tornado.web.asynchronous def post(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) if self.form.validate(): url = self.form.url.data if self.kind == SourceKind.PAPER: # if adding a paper, try getting metadata doi = self.form.get_as_doi() if doi is not None: # we were given a doi - yay! dx_url = 'http://dx.doi.org/' + doi self.find_doi(dx_url) else: self.find_doi(url) else: # otherwise, we're all done - add source and finish up action = self.create_source(self.kind, url=url) self.wrap_things_up(action) else: if self.is_xhr(): # ajax? # collect up the form errors errs = {} for field in self.form: if field.errors: errs[field.name] = field.errors self.write({'success': False, 'errors': errs}) self.finish() else: self.render('add_paper.html', art=self.art, form=self.form) def find_doi(self, url): """ retreive doi and metadata from url """ self.url = url # save for later params = {'url': url} scrapeomat_url = "http://localhost:8889/doi?" + urllib.urlencode( params) http = tornado.httpclient.AsyncHTTPClient() http.fetch(scrapeomat_url, callback=self.on_got_doi_data) def on_got_doi_data(self, response): details = {'url': self.url} if not response.error: results = json.loads(response.body) if results['status'] == 0: #success! meta = results['metadata'] details['doi'] = meta['doi'] details['title'] = meta['title'] details['publication'] = meta['journal'] try: details['pubdate'] = datetime.datetime.strptime( meta['date'], '%Y-%m-%dZ').date() except ValueError: # TODO: sometimes there's a year, which we should grab details['pubdate'] = None action = self.create_source(self.kind, **details) self.wrap_things_up(action) def wrap_things_up(self, action): if self.is_xhr(): # ajax - just send back the source snippet m = uimodules.source(self) html = m.render(action.source, 'li') self.write({ 'success': True, 'new_source': { 'kind': action.source.kind, 'html': html } }) self.finish() else: self.redirect("/thanks/%d" % (action.id, )) def create_source(self, kind, **details): src = Source(article=self.art, creator=self.current_user, kind=kind, **details) self.session.add(src) if self.art.needs_sourcing: self.art.needs_sourcing = False action = Action('src_add', self.current_user, article=self.art, source=src) self.art.needs_sourcing = False self.session.add(action) self.session.commit() return action
class AddSourceHandler(BaseHandler): def get(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) self.render('add_paper.html', art=self.art, form=self.form) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) self.render('add_pr.html', art=self.art, form=self.form) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) self.render('add_other.html', art=self.art, form=self.form) @tornado.web.authenticated @tornado.web.asynchronous def post(self, art_id, kind): self.kind = kind self.art = self.session.query(Article).get(art_id) if self.art is None: raise tornado.web.HTTPError(404, "Article not found") if self.kind == SourceKind.PAPER: self.form = AddPaperForm(TornadoMultiDict(self)) elif self.kind == SourceKind.PR: self.form = AddPRForm(TornadoMultiDict(self)) elif self.kind == SourceKind.OTHER: self.form = AddOtherForm(TornadoMultiDict(self)) if self.form.validate(): url = self.form.url.data if self.kind == SourceKind.PAPER: # if adding a paper, try getting metadata doi = self.form.get_as_doi() if doi is not None: # we were given a doi - yay! dx_url = 'http://dx.doi.org/' + doi self.find_doi(dx_url) else: self.find_doi(url) else: # otherwise, we're all done - add source and finish up action = self.create_source(self.kind, url=url) self.wrap_things_up(action) else: if self.is_xhr(): # ajax? # collect up the form errors errs = {} for field in self.form: if field.errors: errs[field.name] = field.errors self.write({'success':False, 'errors':errs}) self.finish() else: self.render('add_paper.html', art=self.art, form=self.form) def find_doi(self,url): """ retreive doi and metadata from url """ self.url = url # save for later params = {'url': url} scrapeomat_url = "http://localhost:8889/doi?" + urllib.urlencode(params) http = tornado.httpclient.AsyncHTTPClient() http.fetch(scrapeomat_url, callback=self.on_got_doi_data) def on_got_doi_data(self,response): details = {'url':self.url} if not response.error: results = json.loads(response.body) if results['status']==0: #success! meta = results['metadata'] details['doi'] = meta['doi'] details['title'] = meta['title'] details['publication'] = meta['journal'] try: details['pubdate'] = datetime.datetime.strptime(meta['date'], '%Y-%m-%dZ').date() except ValueError: # TODO: sometimes there's a year, which we should grab details['pubdate'] = None action = self.create_source(self.kind, **details) self.wrap_things_up(action) def wrap_things_up(self,action): if self.is_xhr(): # ajax - just send back the source snippet m = uimodules.source(self) html = m.render(action.source,'li') self.write({'success':True, 'new_source': {'kind':action.source.kind, 'html':html} }) self.finish() else: self.redirect("/thanks/%d" % (action.id,)) def create_source(self, kind,**details): src = Source(article=self.art, creator=self.current_user, kind=kind, **details) self.session.add(src) if self.art.needs_sourcing: self.art.needs_sourcing = False action = Action('src_add', self.current_user, article=self.art, source=src) self.art.needs_sourcing = False self.session.add(action) self.session.commit() return action
def go(self, html, scrape_err=None): art = self.art if html is not None: highlight_spans = self.analyse_text(html) # mark up the html html = highlight.html_highlight(html, highlight_spans) # now find the unique matches for each kind uniq = collections.defaultdict(set) for (start, end, kind, name, url) in highlight_spans: uniq[kind].add((name, url)) rs = uniq['researcher'] institutions = uniq['institution'] journals = uniq['journal'] researchers = [] for (name, url) in rs: parts = name.split() initial = parts[0][0] surname = parts[-1] researchers.append({ 'name': name, 'search_value': '"%s %s"' % (initial, surname) }) else: # don't have any text available :-( html = '' researchers, institutions, journals = [], [], [] n_actions = 6 # show most recent N actions recent_actions = self.session.query(Action).\ filter(Action.article_id==art.id).\ filter(Action.what.in_(('src_add','src_remove','art_add','tag_add','tag_remove','mark_sourced','mark_unsourced','helpreq_open','helpreq_close','comment','label_add','label_remove'))).\ order_by(Action.performed.desc()).\ slice(0,n_actions+1).\ all() more_actions = False if len(recent_actions) > n_actions: more_actions = True recent_actions = recent_actions[:n_actions] recent_actions = reversed(recent_actions) add_paper_form = AddPaperForm() add_pr_form = AddPRForm() add_other_form = AddOtherForm() self.render( 'article.html', art=art, article_content=html, researchers=researchers, institutions=institutions, journals=journals, scrape_err=scrape_err, add_paper_form=add_paper_form, add_pr_form=add_pr_form, add_other_form=add_other_form, recent_actions=recent_actions, more_actions=more_actions, )