Beispiel #1
0
def save_post():
    """ Save the gitless post """
    post_id = request.args.get('post_id', None)
    data = request.get_json()
    post = (db_session.query(Post)
            .filter(Post.id == post_id)
            .first())
    new_post = False
    if not post:
        new_post = True
        path = "{}/{}.kp".format(data['project'],
                                 data['title'].encode('utf8').lower().replace(' ', '_'))
        if current_app.config.get('WEB_EDITOR_PREFIXES', None):
            # TODO: Include dropdown on webeditor to have user specify repo
            path = "{}/{}".format(current_app.config['WEB_EDITOR_PREFIXES'][0], path)

        post = (db_session.query(Post)
                          .filter(Post.path == path)
                          .first())
        if post:
            error_msg = "Post with project {} and title {} already exists!".format(data['project'], data['title'])

            json_str = json.dumps({'msg': error_msg, 'success': False})
            return json_str
        else:
            post = Post()
            post.path = path
    else:
        path = post.path

    # create the knowledge post
    kp = KnowledgePost(path=path)

    headers = {}
    headers['created_at'] = datetime.strptime(data['created_at'], '%Y-%m-%d')
    headers['updated_at'] = datetime.strptime(data['updated_at'], '%Y-%m-%d')
    headers['title'] = str(data['title'])
    headers['path'] = str(post.path)
    headers['project'] = str(data['project'])
    # TODO: thumbnail header not working currently, as feed image set with kp
    # method not based on header
    headers['thumbnail'] = str(data['feed_image'])
    headers['authors'] = [str(auth).strip() for auth in data['author']]
    headers['tldr'] = str(data['tldr'])
    headers['tags'] = [str(tag).strip() for tag in data['tags']]
    kp.write(urlunquote(str(data['markdown'])), headers=headers)

    # add to repo
    current_repo.add(kp, update=True)  # THIS IS DANGEROUS
    db_session.commit()

    # add to index
    post.update_metadata_from_kp(kp)
    if new_post:
        db_session.add(post)
    db_session.commit()

    return json.dumps({'post_id': str(post.id)})
Beispiel #2
0
def save_post():
    """ Save the post """

    data = request.get_json()
    path = data['path']

    prefixes = current_app.config['WEB_EDITOR_PREFIXES']
    if prefixes == []:
        raise Exception("Web editing is not configured")

    if prefixes is not None:
        if not any([path.startswith(prefix) for prefix in prefixes]):
            return json.dumps({
                'msg':
                ("Your post path must begin with one of {}").format(prefixes),
                'success':
                False
            })

    # TODO better handling of overwriting
    kp = None
    if path in current_repo:
        kp = current_repo.post(path)
        if current_user.identifier not in kp.headers[
                'authors'] and current_user.identifier not in current_repo.config.editors:
            return json.dumps({
                'msg':
                ("Post with path {} already exists and you are not an author!"
                 "\nPlease try a different path").format(path),
                'success':
                False
            })

    # create the knowledge post
    kp = kp or KnowledgePost(path=path)

    headers = {}
    headers['created_at'] = datetime.strptime(data['created_at'],
                                              '%Y-%m-%d').date()
    headers['updated_at'] = datetime.strptime(data['updated_at'],
                                              '%Y-%m-%d').date()
    headers['title'] = data['title']
    headers['path'] = data['path']
    # TODO: thumbnail header not working currently, as feed image set with kp
    # method not based on header
    headers['thumbnail'] = data.get('feed_image', '')
    headers['authors'] = [auth.strip() for auth in data['author']]
    headers['tldr'] = data['tldr']
    headers['tags'] = [tag.strip() for tag in data.get('tags', [])]
    if 'proxy' in data:
        headers['proxy'] = data['proxy']

    kp.write(unquote(data['markdown']), headers=headers)
    # add to repo
    current_repo.add(kp, update=True,
                     message=headers['title'])  # THIS IS DANGEROUS

    update_index()
    return json.dumps({'path': path})
def save_post():
    """ Save the post """

    data = request.get_json()
    path = data['path']

    # TODO better handling of overwriting
    kp = None
    if path in current_repo:
        kp = current_repo.post(path)
        if g.user.username not in kp.headers['authors']:
            return json.dumps({
                'msg':
                "Post with path already exists!".format(path),
                'success':
                False
            })

    # create the knowledge post
    kp = kp or KnowledgePost(path=path)

    headers = {}
    headers['created_at'] = datetime.strptime(data['created_at'],
                                              '%Y-%m-%d').date()
    headers['updated_at'] = datetime.strptime(data['updated_at'],
                                              '%Y-%m-%d').date()
    headers['title'] = data['title']
    headers['path'] = data['path']
    # TODO: thumbnail header not working currently, as feed image set with kp
    # method not based on header
    headers['thumbnail'] = data.get('feed_image', '')
    headers['authors'] = [auth.strip() for auth in data['author']]
    headers['tldr'] = data['tldr']
    headers['tags'] = [tag.strip() for tag in data.get('tags', [])]
    kp.write(urlunquote(data['markdown']), headers=headers)

    # add to repo
    current_repo.add(kp, update=True,
                     message=headers['title'])  # THIS IS DANGEROUS

    update_index()
    return json.dumps({'path': path})