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): next = self.get_argument("next", None) form = LoginForm(TornadoMultiDict(self)) if next is None: del form.next if not form.validate(): self.render('login.html', form=form, next=next) return # user exists? user = self.session.query(UserAccount).filter( UserAccount.email == form.email.data).first() if user is not None: # password ok? if not user.check_password(form.password.data): user = None if user is None: form.email.errors.append( "Either your email address or password was not recognised. Please try again." ) self.render('login.html', form=form, next=next) return # logged in successfully if next is None: next = '/' self.set_secure_cookie("user", unicode(user.id)) self.redirect(next)
def get(self): url = self.get_argument('url',None) if url is None: # blank url - prompt for one form = SubmitArticleForm() self.render("addarticle.html", form=form, notice='') return # basic validation form = SubmitArticleForm(TornadoMultiDict(self)) if not form.validate(): self.render("addarticle.html", form=form, notice='') return # article already in db? art = self.session.query(Article).join(ArticleURL).\ filter(ArticleURL.url==url).first() print "ART: ",art if art is None: # nope. try scraping it. params = {'url': url} scrape_url = config.settings.scrapeomat + '/scrape?' + urllib.urlencode(params) http = tornado.httpclient.AsyncHTTPClient() response = yield tornado.gen.Task(http.fetch, scrape_url) try: art = scrape.process_scraped(url,response); except Exception as err: # uhoh... we weren't able to scrape it. If user wants article, they'll have to log # in and enter the details themselves... login_next_url = None enter_form = EnterArticleForm(url=url) if self.current_user is None: params = {'url': url} login_next_url = '/enterarticle?' + urllib.urlencode(params) notice = unicode(err) notice += " Please enter the details manually (or try again later)." self.render("enterarticle.html", form=enter_form, notice=notice, login_next_url=login_next_url) return # ok, add the new article to the db (with an action) user = self.current_user if user is None: user = self.get_anon_user() action = Action('art_add', user, article=art) self.session.add(art) self.session.add(action) self.session.commit() # all done self.redirect("/art/%d" % (art.id,)) return
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): next = self.get_argument("next", None) form = RegisterForm(TornadoMultiDict(self)) if next is None: del form.next if not form.validate(): self.render('register.html', form=form, next=next) return # user might already exist - people _do_ forget. # outwardly, we don't want to reflect that an email address is # already registered, but we can send a different email. user = self.session.query(UserAccount).filter( UserAccount.email == form.email.data).first() if user is None: token = Token.create_registration(email=form.email.data, password=form.password.data, next=next) email_template = 'email/confirm_register.txt' email_subject = "%s - confirm your account" % settings.site_name else: token = Token.create_login(user_id=user.id, next=next) email_template = 'email/forgotten_password.txt' email_subject = "%s - forgotten password" % settings.site_name self.session.add(token) self.session.commit() confirmation_url = "%s/t/%s" % (settings.root_url, token.name) email_body = self.render_string(email_template, confirmation_url=confirmation_url) if settings.bypass_email: self.render('token_sent.html', bypass_email=settings.bypass_email, confirmation_url=confirmation_url, email_subject=email_subject, email_body=email_body) return # send it mailer.send_email(addr_from=settings.site_email, addr_to=form.email.data, subject=email_subject, content=email_body) # redirect to avoid multiple emails due to refresh clicking! self.redirect('/emailsent')
def post(self): form = EnterArticleForm(TornadoMultiDict(self)) if not form.validate(): self.render("enterarticle.html", form=form, notice=None) return # done - add the article to the db url = form.url.data title = form.title.data pubdate = form.pubdate.data url_objs = [ArticleURL(url=url),] art = Article(title,url, pubdate, url_objs) action = Action('art_add', self.current_user, article=art) self.session.add(art) self.session.add(action) self.session.commit() # all done. phew. self.redirect("/art/%d" % (art.id,))
def post(self, action_id): action = self.session.query(Action).get(action_id) assert action is not None tok = self.session.query(TwitterAccessToken).filter_by( user=self.current_user).first() if tok is None: raise tornado.web.HTTPError( 500, "Unexpected lack of Twitter authorisation") form = TweetForm(TornadoMultiDict(self)) if form.validate(): message = form.message.data self.twitter_request("/statuses/update", post_args={"status": message}, access_token=json.loads(tok.token), callback=functools.partial(self._on_post, action=action)) return else: self.render('tweet.html', action=action, form=form)
def post(self): user = self.current_user form = EditProfileForm(TornadoMultiDict(self)) if not form.validate(): self.render('profile.html', form=form) return # username already taken? foo = self.session.query(UserAccount.id).filter( UserAccount.username == form.username.data).first() if foo is not None and foo.id != user.id: form.username.errors.append( "username already in use. Please pick another.") self.render('profile.html', user=user, form=form) return # update stuff. if form.password.data: user.set_password(form.password.data) user.username = form.username.data user.email = form.email.data # any photo uploaded? uploaded_photos = self.request.files.get('photo', []) photo = None if len(uploaded_photos) > 0: photo = UploadedFile.create(uploaded_photos[0], creator=user) self.session.add(photo) old_photo = user.photo user.photo = photo if old_photo: self.session.delete(old_photo) self.session.commit() self.redirect('/user/%d' % (user.id, ))
def post(self): form = ForgotForm(TornadoMultiDict(self)) if not form.validate(): self.render('forgot.html', form=form) return user = self.session.query(UserAccount).filter( UserAccount.email == form.email.data).first() # don't leak existence of email addresses. if user is not None: token = Token.create_login(user.id) email_template = 'email/forgotten_password.txt' email_subject = "%s - forgotten details" % settings.site_name self.session.add(token) self.session.commit() confirmation_url = "%s/t/%s" % (settings.root_url, token.name) email_body = self.render_string(email_template, confirmation_url=confirmation_url) if settings.bypass_email: self.render('token_sent.html', bypass_email=settings.bypass_email, confirmation_url=confirmation_url, email_subject=email_subject, email_body=email_body) return # send it mailer.send_email(addr_from=settings.site_email, addr_to=form.email.data, subject=email_subject, content=email_body) # redirect to avoid multiple emails due to refresh clicking! self.redirect('/emailsent')
def get(self): page = int(self.get_argument('p', 1)) filters_form = FiltersForm(TornadoMultiDict(self)) arts = self.session.query(Article) if not filters_form.validate(): # uh-oh - form is bad. if self.is_xhr(): # if ajax, just send the errors errs = {} for field in filters_form: if field.errors: errs[field.name] = field.errors self.finish({'status': 'badfilters', 'errors': errs}) return else: paged_results = SAPaginator() self.render("browse.html", filters=filters_form, paged_results=paged_results) return # build up the query date_def = date_defs.get(filters_form.date.data, None) day_from = None day_to = None if date_def: if date_def.get('from_delta', None) is not None: day_from = datetime.utcnow().date() - date_def['from_delta'] if date_def.get('to_delta', None) is not None: day_to = datetime.utcnow().date() - date_def['to_delta'] if date_def.get('use_date_range', False): day_from = filters_form.dayfrom.data day_to = filters_form.dayto.data if day_from: arts = arts.filter(cast(Article.pubdate, Date) >= day_from) if day_to: arts = arts.filter(cast(Article.pubdate, Date) <= day_to) if filters_form.help.data: arts = arts.filter(Article.help_reqs.any()) if filters_form.discussed.data: arts = arts.filter(Article.comments.any()) if filters_form.labeled.data: arts = arts.filter(Article.labels.any()) if filters_form.sourced.data == 'unsourced': arts = arts.filter(Article.needs_sourcing == True) elif filters_form.sourced.data == 'sourced': arts = arts.filter(Article.needs_sourcing == False) arts = arts.order_by(Article.pubdate.desc()) # paginate the results def page_url(page): """ generate url for the given page of this query""" params = {} # preserve all request params, and override page number for k in self.request.arguments: params[k] = self.get_argument(k) params['p'] = page url = "/browse?" + urllib.urlencode(params) return url paged_results = SAPaginator(arts, page, page_url, per_page=100) if self.is_xhr(): # if ajax, just render a new #searchresults instead of whole page module = uimodules.searchresults(self) html = module.render(filters=filters_form, paged_results=paged_results) self.finish({'status': 'ok', 'results_html': html}) else: if self.get_argument('fmt', '') == 'markdown': # type should be text/x-markdown really, but more convenient # for cut&paste if treated as plain text self.set_header("Content-Type", "text/plain; charset=UTF-8") self.render("browse_export.markdown", filters=filters_form, paged_results=paged_results) else: self.render("browse.html", filters=filters_form, paged_results=paged_results)
def get(self): form = EnterArticleForm(TornadoMultiDict(self)) self.render("enterarticle.html", form=form, notice=None)
def get(self): next = self.get_argument("next", None) form = RegisterForm(TornadoMultiDict(self)) if next is None: del form.next self.render('register.html', form=form, next=next)
def get(self): form = ForgotForm(TornadoMultiDict(self)) self.render('forgot.html', form=form)
def get(self): next = self.get_argument("next", None) form = LoginForm(TornadoMultiDict(self)) if next is None: del form.next self.render('login.html', form=form, next=next)