示例#1
0
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
示例#2
0
文件: routes.py 项目: Eriwi/noName
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})
示例#3
0
 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
示例#4
0
    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
示例#5
0
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()
示例#6
0
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)
示例#7
0
    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
        }
示例#8
0
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
示例#9
0
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)
示例#10
0
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