Esempio n. 1
0
    def post(self):
        class VoteRequestSchema(Schema):
            aid = ObjectIdField(required=True)

            def __init__(self, strict=True, **kwargs):
                super().__init__(strict=strict, **kwargs)

        if 'uid' not in session:
            return {'message': 'no uid'}, 401

        schema = VoteRequestSchema()
        try:
            vote_request = schema.load(request.get_json()).data
        except ValidationError as err:
            return {'message': format_messages(err.messages)}, 400
        except Exception:
            return {'message': 'invalid request'}, 400

        aid = vote_request['aid']
        a = get_article(aid)

        if not a:
            return {'message': 'article not existed'}, 400

        vote_article(a)
        return {'aid': str(aid)}, 200
Esempio n. 2
0
    def post(self):

        class VoteRequestSchema(Schema):
            aid = ObjectIdField(required=True)

            def __init__(self, strict=True, **kwargs):
                super().__init__(strict=strict, **kwargs)

        if 'uid' not in session:
            return {'message': 'no uid'}, 401

        schema = VoteRequestSchema()
        try:
            vote_request = schema.load(request.get_json()).data
        except ValidationError as err:
            return {'message': format_messages(err.messages)}, 400
        except Exception:
            return {'message': 'invalid request'}, 400

        aid = vote_request['aid']
        a = get_article(aid)

        if not a:
            return {'message': 'article not existed'}, 400

        vote_article(a)
        return {'aid': str(aid)}, 200
Esempio n. 3
0
def article(aid):
    a = get_article(aid)
    if a is None:
        raise NotFound(f'article[{aid}] not existed')

    def get_site_css(site):
        path = f'{app.static_folder}/css/site/{site}.css'
        if not os.path.isfile(path):
            return
        return f'{site}.css'

    def get_site_script(site):
        path = f'{app.static_folder}/script/site/{site}.script'
        if not os.path.isfile(path):
            return
        with open(path) as f:
            return f.read().strip()

    site = a.domain
    headlist = a.head
    sitecss = get_site_css(site)
    return render_template(
            'article.html',
            article=a,
            sitecss=sitecss,
            sitescript=None,
            headlist=headlist,
    )
Esempio n. 4
0
def article(aid):
    a = get_article(aid)
    if a is None:
        raise NotFound(f'article[{aid}] not existed')

    def get_site_css(site):
        path = f'{app.static_folder}/css/site/{site}.css'
        if not os.path.isfile(path):
            return
        return f'{site}.css'

    def get_site_script(site):
        path = f'{app.static_folder}/script/site/{site}.script'
        if not os.path.isfile(path):
            return
        with open(path) as f:
            return f.read().strip()

    site = a.domain
    headlist = a.head
    sitecss = get_site_css(site)
    return render_template(
        'article.html',
        article=a,
        sitecss=sitecss,
        sitescript=None,
        headlist=headlist,
    )
Esempio n. 5
0
def edit_post(id):
        # Edit an article
        # GET reloads the article from the DB and creates the form
        # POST edits in DB
	article = model.get_article(int(id))
	if request.method=='GET':
		return render_template('edit.html', article=article)
	elif request.method=='POST':
		model.edit_post(request.form['title'], request.form['abstract'], request.form['text'], request.form['tags'], article)
		return redirect(url_for('view_post', id=int(id)))
Esempio n. 6
0
def edit_post(id):
    # Edit an article
    # GET reloads the article from the DB and creates the form
    # POST edits in DB
    article = model.get_article(int(id))
    if request.method == 'GET':
        return render_template('edit.html', article=article)
    elif request.method == 'POST':
        model.edit_post(request.form['title'], request.form['abstract'],
                        request.form['text'], request.form['tags'], article)
        return redirect(url_for('view_post', id=int(id)))
Esempio n. 7
0
def article(cls, id):
    result = get_article(cls, id)
    if result == None:
        return render_template('404.html')
    rec = get_list_rand(0, 6, "all")
    list_rec = []
    for item in rec:
        list_rec.append({
            'id': item['id'],
            'title': chs_to_cht(item['title'])[:20],
            'class': item['class'],
            'digest': chs_to_cht(item['digest'])[:20] + "...",
            'cover': item['cover']
        })
    data = {
        'title': chs_to_cht(result['title']),
        'html': chs_to_cht(result['html']),
        'digest' : chs_to_cht(result['digest']),
        'list' : list_rec
    }
    return render_template('article.html', data=data)
Esempio n. 8
0
def view_post(id):
    # Display a post from its id
    article = model.get_article(id)
    return render_template('view.html', article=article)
Esempio n. 9
0
def unpublish(id):
    # Unpublish an article, setting the 'draft' status
    article = model.get_article(id)
    if session['logged']:
        model.unpublish(article)
    return redirect(url_for('view_post', id=int(id)))
Esempio n. 10
0
def view_post(id):
        # Display a post from its id
	article = model.get_article(id)
	return render_template('view.html', article=article)
Esempio n. 11
0
def unpublish(id):
        # Unpublish an article, setting the 'draft' status
        article = model.get_article(id)
        if session['logged']:
                model.unpublish(article)
        return redirect(url_for('view_post', id=int(id)))