def get(self): label_id = self.get_argument('label') url = self.get_argument('url') #reason = self.get_argument('reason') label = self.session.query(Label).get(label_id) if label is None: raise tornado.web.HTTPError(400) # article already in db? art = self.session.query(Article).join(ArticleURL).\ filter(ArticleURL.url==url).first() 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. # enter the article with just the url and a fudged datetime url_objs = [ ArticleURL(url=url), ] art = Article("", url, datetime.datetime.utcnow(), url_objs) # 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() # ok, now we can add the label! if label not in [l.label for l in art.labels]: artlabel = ArticleLabel(creator=self.current_user) artlabel.label = label artlabel.article = art art.labels.append(artlabel) self.session.add( Action('label_add', self.current_user, article=art, label=label)) self.session.commit() # all done self.redirect("/art/%d" % (art.id, )) return
def get(self): label_id = self.get_argument('label') url = self.get_argument('url') #reason = self.get_argument('reason') label = self.session.query(Label).get(label_id) if label is None: raise tornado.web.HTTPError(400) # article already in db? art = self.session.query(Article).join(ArticleURL).\ filter(ArticleURL.url==url).first() 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. # enter the article with just the url and a fudged datetime url_objs = [ArticleURL(url=url),] art = Article("", url, datetime.datetime.utcnow(), url_objs) # 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() # ok, now we can add the label! if label not in [l.label for l in art.labels]: artlabel = ArticleLabel(creator=self.current_user) artlabel.label = label artlabel.article = art art.labels.append(artlabel) self.session.add(Action('label_add', self.current_user, article=art, label=label)) self.session.commit() # all done self.redirect("/art/%d" % (art.id,)) return
def post(self, art_id): art = self.session.query(Article).get(art_id) if art is None: raise tornado.web.HTTPError(404, "Article not found") label_id = self.get_argument('label') label = self.session.query(Label).get(label_id) if label is None: raise tornado.web.HTTPError(400) if label in [l.label for l in art.labels]: self.redirect("/art/%s" % (art.id,)) return artlabel = ArticleLabel(creator=self.current_user) artlabel.label = label artlabel.article = art art.labels.append(artlabel) self.session.add(Action('label_add', self.current_user, article=art, label=label)) self.session.commit() self.redirect("/art/%s" % (art.id,))
def post(self, art_id): art = self.session.query(Article).get(art_id) if art is None: raise tornado.web.HTTPError(404, "Article not found") label_id = self.get_argument('label') label = self.session.query(Label).get(label_id) if label is None: raise tornado.web.HTTPError(400) if label in [l.label for l in art.labels]: self.redirect("/art/%s" % (art.id, )) return artlabel = ArticleLabel(creator=self.current_user) artlabel.label = label artlabel.article = art art.labels.append(artlabel) self.session.add( Action('label_add', self.current_user, article=art, label=label)) self.session.commit() self.redirect("/art/%s" % (art.id, ))