예제 #1
0
def markdownsave():
    # read the data which was sent from the editor.js
    markdowntxt = request.files['file'].read()
    htmltxt = request.files['htmlfile'].read()

    # some encoding
    markdowntxt = markdowntxt.decode('utf-8')
    htmltxt = htmltxt.decode('utf-8')

    article_id = int((request.form['article_id']))
    article_title = (request.form['article_title'])
    article_abstract = (request.form['article_abstract'])
    article_title_html = (request.form['article_title_html'])
    article_abstract_html = (request.form['article_abstract_html'])

    # save the data
    #   new Article
    if article_id <= -1:
        # check the Article do not exist
        a = Article.query.filter_by(
            title=article_title).join(WriterRelationship).filter(
                WriterRelationship.writer_id == current_user.id).first()
        if a is not None:
            # we should return something more meaningfull ???
            abort(401)

        # create new Artilce
        a = Article(title=article_title,
                    abstract=article_abstract,
                    markdown=markdowntxt,
                    html=htmltxt,
                    abstracthtml=article_abstract_html,
                    titlehtml=article_title_html,
                    status="editing")
        db.session.add(a)
        # get the new object id
        db.session.flush()
        # save in DB
        db.session.commit()

        # get the user also
        user = User.query.filter_by(id=current_user.id).first_or_404()

        # add Writerrelationship
        w = WriterRelationship(article_id=a.id, writer_id=current_user.id)
        db.session.add(w)
        db.session.commit()

        return jsonify({'result': "OK", 'id': a.id})
    else:
        a = Article.query.filter_by(id=article_id).first_or_404()

        if a.status is not None and a.status != 'editing':
            raise RuntimeError("The  Article status is not 'edited'! ")
        elif a.status is None:
            a.status = 'editing'

        # update if somethings changed
        change = False
        if a.title != article_title:
            change = True
            a.title = article_title
        if a.html != htmltxt:
            change = True
            a.html = htmltxt
        if a.abstract != article_abstract:
            change = True
            a.abstract = article_abstract
        if a.markdown != markdowntxt:
            change = True
            a.markdown = markdowntxt
        if a.titlehtml != article_title_html:
            change = True
            a.titlehtml = article_title_html
        if a.abstracthtml != article_abstract_html:
            change = True
            a.abstracthtml = article_abstract_html
        if a.language != g.locale:
            change = True
            a.language = g.locale

        # if anything changed
        if change:
            a.timestamp = datetime.datetime.utcnow()
            db.session.commit()

    # return a OK json
    flash(_l("Your changes have been saved."))
    return jsonify(result="OK")