Exemplo n.º 1
0
def migrate_s3():
    """
    Migrates existing Checkpoints that are stored locally to Amazon S3.
    """
    from db import db, Checkpoint
    local_checkpoints = Checkpoint.query.filter_by(img_location=None)
    
    for cp in local_checkpoints:
        
        #save to s3
        resources_dir = get_resources_abs_path()
        img_path = os.path.join(resources_dir,"uploads",str(cp.creator),cp.image)
        print img_path
        img_file = open(img_path, 'r') 
        img_name = save_to_s3(cp.creator, img_file.read(), resources_dir, False)
        
        img_path_optimized = os.path.join(resources_dir,"uploads",str(cp.creator),cp.image + "_optimized.jpg")
        img_file_optimized = open(img_path_optimized, 'r') 
        save_to_s3(cp.creator, img_file_optimized.read(), resources_dir, False)
        
        #update the checkpoint
        cp.img_location = "s3"
        cp.image = img_name    
        db.session.add(cp)
    
    db.session.commit()
Exemplo n.º 2
0
def new_checkpoint():
    """
    (PUT: checkpoint) *requires authorization
    creates a barebone checkpoint (just location and image)
    this checkpoint is not complete yet.
    """
    
    #req vars
    user_id = request.form.get("user_id")
    signature = request.form.get("signature")
    name = request.form.get("name")
    longitude = request.form.get("longitude")
    latitude = request.form.get("latitude")
    description = request.form.get("description", None)
    price = request.form.get("price", None)
    expiry = request.form.get("expiry", None)
    image_encoded = request.form.get("image", None)
    type = request.form.get("type")
    share = request.form.get("share", None)
    facebook_post = request.form.get("facebook_post", False)
    image = None
    if image_encoded is None:
        image = request.files["image"]
    
    #generated vars
    verb = "put"
    noun = "checkpoint"
    user = get_user(user_id)
    expiry_datetime = None
    if expiry:
        expiry_datetime = unserialize_json_datetime(expiry)
    
    if not authorize(verb, noun, user_id, signature):
        print "fail"
        return authorization_fail()
    
    #save image
    from util.fileupload import save_to_s3
    tmp_folder = get_resources_abs_path()
    
    if image_encoded is None:
        img_file_name = save_to_s3(user_id, image, tmp_folder, encoded=False)
    else:
        img_file_name = save_to_s3(user_id, image_encoded, tmp_folder) 
    
    checkpoint = add_checkpoint(user_id, name, type, img_file_name, longitude, latitude, description, price, expiry_datetime, img_location="s3")
    user_checkpoint  = add_checkpoint_to_user(user, checkpoint)
    
    #dispatch shares
    if not share is None:
        user_ids_to_share = simplejson.loads(share)
        for uid in user_ids_to_share:
            user_to = get_user(uid)
            share_checkpoint(user, user_to, user_checkpoint)
    
    #return success
    return jsonify({
                    "status": "ok",
                    "result": {
                               "user_checkpoint_id": user_checkpoint.id,
                               },
                    })