示例#1
0
    def post(self):
        uploaded_files = request.files.getlist("file")
        private = "private" == request.form.get("access_type")
        presigned_posts = []

        for file in uploaded_files:
            if (file.filename == ""):
                flash("You must select at least one image!", 'danger')
                return render_template("upload.html"), 400

            identifier = str(uuid.uuid4())
            image = ImageModel(session['username'], identifier,
                               datetime.datetime.now(), private)
            try:
                image.save_to_database()
            except Exception:
                return {
                    'message':
                    'An error occurred saving the image to the database.'
                }, 500

            new_post = create_presigned_post(S3_BUCKET_NAME, identifier)
            presigned_posts.append(new_post)

        pool = ThreadPool(processes=20)
        start = time.time()
        pool.starmap(upload, zip(presigned_posts, uploaded_files))
        end = time.time()
        print(end - start)
        return redirect("/personal/1"), 301
示例#2
0
 def get(self, page_num):
     public_images = ImageModel.get_public_images_by_username(
         session['username'], page_num)
     private_images = ImageModel.get_private_images_by_username(
         session['username'], page_num)
     return render_template("personal.html",
                            public_images=public_images,
                            private_images=private_images,
                            cloudfront=CLOUDFRONT), 200
def test_delete_image(client):
    register_test_user(client)
    login_test_user(client)
    upload_single_image(client)

    private_images = ImageModel.get_private_images_by_username('test_user', 1)
    assert private_images.total == 1

    res = edit_image(client, private_images.items[0].identifier, 'delete')
    print(res.status_code)
    private_images = ImageModel.get_private_images_by_username('test_user', 1)
    assert private_images.total == 0
    assert res.status_code == 301

    
def test_change_image_privacy(client):
    register_test_user(client)
    login_test_user(client)
    upload_single_image(client)

    private_images = ImageModel.get_private_images_by_username('test_user', 1)
    identifier = private_images.items[0].identifier
    assert private_images.items[0].private == True

    res = edit_image(client, identifier, 'make public')
    image = ImageModel.get_image_by_identifier(identifier)
    assert image.private == False
    assert res.status_code == 301

    # delete image from s3 and db
    edit_image(client, identifier, 'delete')
示例#5
0
def test_upload_single_image(client):
    register_test_user(client)
    login_test_user(client)
    res = upload_single_image(client)
    private_images = ImageModel.get_private_images_by_username('test_user', 1)
    assert private_images.total == 1
    assert res.status_code == 301
    
    # delete image from s3 and db
    edit_image(client, private_images.items[0].identifier, 'delete')
示例#6
0
def test_upload_multiple_images(client):
    register_test_user(client)
    login_test_user(client)
    res = upload_multiple_images(client)
    private_images = ImageModel.get_private_images_by_username('test_user', 1)
    assert private_images.total == 3
    assert res.status_code == 301

    # delete images from s3 and db
    for image in private_images.items:
        edit_image(client, image.identifier, 'delete')
示例#7
0
 def post(self, page_num):
     action = request.form.get('action')
     identifier = request.form['identifier']
     image = ImageModel.get_image_by_identifier(identifier)
     if action == "make public" or action == "privatize":
         image.change_privacy()
     elif action == "delete":
         delete_image(identifier)
         image.delete_from_database()
     elif action == "download":
         url = create_presigned_url(identifier)
         return Response(BytesIO(requests.get(url).content),
                         mimetype='image/jpeg',
                         headers={
                             "Content-Disposition":
                             "attachment;filename=" + identifier
                         }), 200
     return redirect('/personal/' + str(page_num)), 301
示例#8
0
 def get(self, page_num):
     images = ImageModel.get_all_public_images(page_num)
     return render_template('discover.html',
                            images=images,
                            cloudfront=CLOUDFRONT)