Beispiel #1
0
def create():
    """
    Create a post.
    """
    random_token = None
    if app.config['USE_RANDOM_TOKEN']:
        # Check if client defined a random token, if not generate one
        if 'post[random_token]' in request.form:
            random_token = request.form['post[random_token]']
        else:
            random_token = helpers.generate_token(
                app.config['RANDOM_TOKEN_LENGTH'])

    # Create burn after date based off client paramters or generate one
    today = datetime.today()
    if 'post[seconds_until_burn]' in request.form:
        burn_after = timedelta(seconds=int(request.form['post[seconds_until_burn]'])) + \
            today
    else:
        burn_after = timedelta(seconds=app.config['POST_LIFETIME_MAX']) + \
            today

    # Set the privly application, use "PlainPost" as default
    privly_application = "PlainPost"
    if 'post[privly_application]' in request.form:
        privly_application = request.form['post[privly_application]']
        
    # Set the content of the post
    content, structured = (None, None)
    if 'post[content]' in request.form:
        content = request.form['post[content]']
    if 'post[structured_content]' in request.form:
        structured = request.form['post[structured_content]']

    # Create the Post    
    post = Post(
        random_id=helpers.generate_token(app.config['RANDOM_ID_LENGTH']),
        random_token=random_token, 
        content=content,
        structured_content=structured,
        burn_after=burn_after,
        privly_application=privly_application,
        public=True,
        user=user
    )

    post.save()

    # Create JSON response
    response = helpers.jsonify(status='created', location=post, _status=201)
    response.headers['X-Privly-Url'] = helpers.privly_URL(post)
    return response
Beispiel #2
0
def create():
    """
    Create a post.
    """
    random_token = None
    if app.config['USE_RANDOM_TOKEN']:
        # Check if client defined a random token, if not generate one
        if 'post[random_token]' in request.form:
            random_token = request.form['post[random_token]']
        else:
            random_token = helpers.generate_token(
                app.config['RANDOM_TOKEN_LENGTH'])

    # Create burn after date based off client paramters or generate one
    today = datetime.today()
    if 'post[seconds_until_burn]' in request.form:
        burn_after = timedelta(seconds=int(request.form['post[seconds_until_burn]'])) + \
            today
    else:
        burn_after = timedelta(seconds=app.config['POST_LIFETIME_MAX']) + \
            today

    # Set the privly application, use "PlainPost" as default
    privly_application = "PlainPost"
    if 'post[privly_application]' in request.form:
        privly_application = request.form['post[privly_application]']

    # Set the content of the post
    content, structured = (None, None)
    if 'post[content]' in request.form:
        content = request.form['post[content]']
    if 'post[structured_content]' in request.form:
        structured = request.form['post[structured_content]']

    # Create the Post
    post = Post(random_id=helpers.generate_token(
        app.config['RANDOM_ID_LENGTH']),
                random_token=random_token,
                content=content,
                structured_content=structured,
                burn_after=burn_after,
                privly_application=privly_application,
                public=True,
                user=user)

    post.save()

    # Create JSON response
    response = helpers.jsonify(status='created', location=post, _status=201)
    response.headers['X-Privly-Url'] = helpers.privly_URL(post)
    return response
Beispiel #3
0
def get_post(id):
    """
    Gets a single user's post
    """
    post = Post.get_post(id)

    # Check if post is valid
    if not post:
        abort(403)

    # Check if random token is accurate if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
        ('random_token' not in request.args or
        request.args.get('random_token') != post.random_token):
        abort(403)

    return helpers.jsonify(post.__json__(user.get_id() == post.user.get_id()))
Beispiel #4
0
def get_post(id):
    """
    Gets a single user's post
    """
    post = Post.get_post(id)

    # Check if post is valid
    if not post:
        abort(403)

    # Check if random token is accurate if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
        ('random_token' not in request.args or
        request.args.get('random_token') != post.random_token):
        abort(403)

    return helpers.jsonify(post.__json__(user.get_id() == post.user.get_id()))
Beispiel #5
0
def update(id):
    """
    Update a user's Post
    """

    # Load the user's post out of the database
    post = Post.get_user_post(user, id)

    # Check if post is valid
    if not post:
        abort(403)

    # Check if random token correct if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
            ('random_token' not in request.args or
            request.args.get('random_token') != post.random_token):
            abort(403)

    # Update the random token if the server is using random_token
    if app.config['USE_RANDOM_TOKEN'] and 'post[random_token]' in request.form:
        post.random_token = request.form['post[random_token]']

    # Update the `burn_after` date if `seconds_until_burn` is sent
    if 'post[seconds_until_burn]' in request.form:
        post.burn_after = datetime.today() + \
            timedelta(seconds=int(request.form['post[seconds_until_burn]']))

    # Update the Privly application if the `privly_application` string is sent
    if 'post[privly_application]' in request.form:
        post.privly_application = request.form['post[privly_application]']

    # Update `content` or `structured_content` if either is set
    if 'post[content]' in request.form:
        post.content = request.form['post[content]']
    if 'post[structured_content]' in request.form:
        post.structured_content = request.form['post[structured_content]']

    post.updated = datetime.now()

    # Save and update
    post.save()

    return helpers.jsonify(post)
Beispiel #6
0
def update(id):
    """
    Update a user's Post
    """

    # Load the user's post out of the database
    post = Post.get_user_post(user, id)

    # Check if post is valid
    if not post:
        abort(403)

    # Check if random token correct if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
            ('random_token' not in request.args or
            request.args.get('random_token') != post.random_token):
        abort(403)

    # Update the random token if the server is using random_token
    if app.config['USE_RANDOM_TOKEN'] and 'post[random_token]' in request.form:
        post.random_token = request.form['post[random_token]']

    # Update the `burn_after` date if `seconds_until_burn` is sent
    if 'post[seconds_until_burn]' in request.form:
        post.burn_after = datetime.today() + \
            timedelta(seconds=int(request.form['post[seconds_until_burn]']))

    # Update the Privly application if the `privly_application` string is sent
    if 'post[privly_application]' in request.form:
        post.privly_application = request.form['post[privly_application]']

    # Update `content` or `structured_content` if either is set
    if 'post[content]' in request.form:
        post.content = request.form['post[content]']
    if 'post[structured_content]' in request.form:
        post.structured_content = request.form['post[structured_content]']

    post.updated = datetime.now()

    # Save and update
    post.save()

    return helpers.jsonify(post)
Beispiel #7
0
def destroy(id):
    """
    Deletes a user's Post
    """
    # Load the user's post out of the database
    post = Post.get_user_post(user, id)

    # Is the post invalid?
    if not post:
        abort(403)

    # Check if random token correct if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
        ('random_token' not in request.args or
        request.args.get('random_token') != post.random_token):
        abort(403)

    post.delete()

    return helpers.jsonify(success=True)
Beispiel #8
0
def destroy(id):
    """
    Deletes a user's Post
    """
    # Load the user's post out of the database
    post = Post.get_user_post(user, id)

    # Is the post invalid?
    if not post:
        abort(403)

    # Check if random token correct if enabled
    if app.config['USE_RANDOM_TOKEN'] and \
        ('random_token' not in request.args or
        request.args.get('random_token') != post.random_token):
        abort(403)

    post.delete()

    return helpers.jsonify(success=True)