def import_article(): data = request.get_json() logger.info('data: %s', data) source_url = data.get('source_url') pretty_name = data.get('pretty_name') language = data.get('language') rss_id = data.get('id') track = RSSTrack.get(rss_id) try: resp = requests.get(track.url) resp.raise_for_status() except HTTPError as e: return {'status': 'error', 'details': str(e)}, resp.status_code hostname = urlparse(track.url).hostname article = Article(pretty_name=track.name, url=track.url, rss_id=rss_id, language=language) parser = ParsingRules.get(hostname) # TODO: Save in a better format! content = parser.parse(resp.content) article.content = content db.add(article) db.commit() return {'status': 'ok', 'article': article.to_json()}, 200
def create_article(): form = ArticleForm() if request.method == 'GET': return render_template('createarticle.html', form=form) if request.method == 'POST': if form.validate_on_submit(): article = Article(text=form.text.data, title=form.title.data, author_id=current_user.id) article.create_desc() db.session.add(article) db.session.commit() return jsonify({'message': 'Your article is now added'}) else: msg = "" for fieldName, errorMessages in form.errors.items(): for err in errorMessages: msg += (fieldName + ": " + err + " ") return jsonify({'message': msg})
def getArticleInfo(self,title=None,insertOnBd=False): payload = {'format': 'json','title':title} r = requests.get(self.baseUrl+ 'article/', params=payload) print r.status_code aux = r.json()['objects'] if len(aux) == 0 : stderr.write('Article with title: \"'+title + '\" doesn\'t exist in SICientifico') return None data = aux[0] article = Article(title= data['title'], abstract=data['abstract']) if insertOnBd : authors = data['authors'] for author in authors: a = self.getAuthorInfo(author['email'], insertOnBd) article.save() article.authors.add(a) return article, None # TODO descrever na doc da api este caso de nao devolver json return article, data
def getArticleInfo(self, title=None, insertOnBd=False): payload = {'format': 'json', 'title': title} r = requests.get(self.baseUrl + 'article/', params=payload) print r.status_code aux = r.json()['objects'] if len(aux) == 0: stderr.write('Article with title: \"' + title + '\" doesn\'t exist in SICientifico') return None data = aux[0] article = Article(title=data['title'], abstract=data['abstract']) if insertOnBd: authors = data['authors'] for author in authors: a = self.getAuthorInfo(author['email'], insertOnBd) article.save() article.authors.add(a) return article, None # TODO descrever na doc da api este caso de nao devolver json return article, data
def initdata(): fake = Faker('zh_CN') # 插入用户信息 for _ in range(10): new_user = User(username=fake.name(), password=convert_to_md5('adminadmin')) db.session.add(new_user) for i in range(10): new_article = Article(title='title{}'.format(i), content='content{}'.format(i), author_id=1) db.session.add(new_article) db.session.commit()
def add_article(): form = ArticleForm() orders = db.session.query(Order).all() order_refs = [(i.id, f"{i.reference} ({i.get_company_name()})") for i in orders] form.order.choices = order_refs if form.validate_on_submit(): article = Article(time=form.time.data, author_id=current_user.id, order_id=form.order.data) db.session.add(article) db.session.commit() return redirect(url_for('index')) if request.method == 'GET': return render_template('add_article.html', form=form)
def post(self): """ 新增文章 """ parse = reqparse.RequestParser() parse.add_argument('title', type = str, required = True) parse.add_argument('content', type = str, requird = True) parse.add_argument('author_id', type = int, required = True) args = parse.parse_args() article = Article( title = args.title, content = args.content, author_id = args.author_id ) db.session.add(article) db.session.commit() return { 'code': 20000 }
def try_create_new_article(title, topic_id, uri): topic = get_topic_or_404(topic_id) new_article = Article.create_article(title, uri, topic_id) topic.articles.append(new_article) return new_article
def get_pagination_in_topic(topic_id, page_number): topic = get_topic_or_404(topic_id) if topic: return Article.get_pagination_by_topic_id(topic_id, page_number)
def get_article_or_404(article_id): ret = Article.get_by_id(article_id) if ret is None: abort(404, message='Article not found.') return ret