Exemple #1
0
def api_document_put(document_name, document_id):
    '''Make changes to an existing document and save it to the database'''
    user = helpers.user_required(1)
    model = models.model_get(document_name)

    # get the dictionary of arguments
    args = flask.request.get_json()

    # get the document
    document = None
    try:
        document = model.objects.get(id=document_id)
    except mongoengine.errors.ValidationError:
        return helpers.api_error(message='No document found', code=404)

    # iterate through the args and screen them
    if args:
        for arg_name in args:
            print('PROCESSING ARG_NAME', arg_name)
            print('VALUE', args[arg_name])

    # update the document and return a success
    document.update(**args)
    document.reload()
    if document._after_put(user):
        document.save()
    return helpers.api_success(message='Document updated.')
Exemple #2
0
def api_document_post(document_name):
    '''Create a new document and insert it into the database'''
    user = helpers.user_required(1)
    model = models.model_get(document_name)

    # get the dictionary of arguments
    args = flask.request.get_json()

    # filter out fields based on access level
    try:
        for field_name in model._blacklist_postput[user.access]:
            args.pop(field_name, None)
    except IndexError:
        pass

    # some types need extra processing (e.g., datetime fields)
    model_fields = model._fields
    for arg_name in args:
        value = args[arg_name]
        if isinstance(model_fields.get(arg_name, None), mongoengine.fields.DateTimeField):
            args[arg_name] = dateutil.parser.parse(value)

    # put together the new document
    new = model(**args)

    # some models need special handling, so make sure to call the 'after' func
    new._after_post(user)

    # now save it and return it
    new.save()
    return helpers.api_success(data=new)
Exemple #3
0
def api_image_upload():
    # Require a user be logged in
    user = helpers.user_required()

    # Get the uploaded file from flask
    upload_file = flask.request.files['file']

    # Determine the new S3 filename
    upload_id = uuid.uuid4().hex

    s3_path_full = upload_id + '.jpg'
    s3_path_thumb = upload_id + '_thumb.jpg'

    # Process the image into full and thumbnail variants
    image_full, image_thumb = image.process_image(upload_file)

    # Write both to buffer and upload them to S3
    buffer_full = io.BytesIO()
    image_full.save(buffer_full, format='JPEG')
    s3_connection.upload(s3_path_full, buffer_full, 'clarityapp')

    buffer_thumb = io.BytesIO()
    image_thumb.save(buffer_thumb, format='JPEG')
    s3_connection.upload(s3_path_thumb, buffer_thumb, 'clarityapp')

    # return the final wallpaper document
    return helpers.api_success(data=upload_id)
Exemple #4
0
def api_document_delete(document_name, document_id):
    """Delete an existing document from the database"""
    user = helpers.user_required(1)
    model = models.model_get(document_name)

    # get the document
    document = None
    try:
        document = model.objects.get(id=document_id)
    except mongoengine.errors.ValidationError:
        return helpers.api_error(message='No document found', code=404)

    # delete it!
    document.delete()
    return helpers.api_success(message='Document deleted.')
Exemple #5
0
def user_get():
    """Return the currently logged in user's information (or not)."""
    return helpers.api_success(data=helpers.user_required())
Exemple #6
0
def user_logout():
    """Log a user out. That's it."""
    helpers.user_required()
    flask_login.logout_user()
    return helpers.api_success()