Beispiel #1
0
def picture_get(site, host, netloc, site_id, user_id, portfolio_id,
                picture_id):
    """
    Get portfolio url
    """

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id,
                                  Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site,
                              Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Images data
    site_img_url = '%s/%s/' % (host, IMAGE_DIR)
    original_url = site_img_url + picture.original_image
    normalized_url = site_img_url + picture.normalized_image
    thumbnail_url = site_img_url + picture.thumbnail_image

    # Return OK
    return dict(status=True,
                info='Imagem encontrada com sucesso',
                original=original_url,
                normalized=normalized_url,
                thumbnail=thumbnail_url,
                **picture.get_dictionary())
Beispiel #2
0
def picture_get(site, host, netloc, site_id, user_id, portfolio_id, picture_id):
    """
    Get portfolio url
    """

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id, Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site, Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Images data
    site_img_url = '%s/%s/' % (host, IMAGE_DIR)
    original_url = site_img_url + picture.original_image
    normalized_url = site_img_url + picture.normalized_image
    thumbnail_url = site_img_url + picture.thumbnail_image

    # Return OK
    return dict(status=True,
                info='Imagem encontrada com sucesso',
                original=original_url,
                normalized=normalized_url,
                thumbnail=thumbnail_url,
                **picture.get_dictionary())
Beispiel #3
0
    def delete(self, uuid):
        try:
            picture = Picture.get(Picture.uuid == uuid)
        except Picture.DoesNotExist:
            return None, NOT_FOUND

        picture.delete_instance()
        return None, NO_CONTENT
Beispiel #4
0
 def delete(self, picture_uuid):
     """Remove the picture specified by picture_uuid"""
     try:
         obj = Picture.get(Picture.uuid == picture_uuid)
     except Picture.DoesNotExist:
         return None, client.NOT_FOUND
     obj.delete_instance()
     return None, client.NO_CONTENT
Beispiel #5
0
def picture_update(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Edit picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    picture_id = int(request.POST.get('picture'))
    title = request.POST.get('title')
    description = request.POST.get('description')

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id, Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site, Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Update upload picture image
    if request.files.get('upload'):
        img_path = site.get_image_path(IMAGE_PATH)
        original_file = os.path.join(img_path, picture.original_image)
        normalized_file = os.path.join(img_path, picture.normalized_image)
        thumbnail_file = os.path.join(img_path, picture.thumbnail_image)
        upload_response = upload_image(request, original_file, normalized_file, thumbnail_file, ORIG_SIZE, NORM_SIZE, THUMB_SIZE)
        if not upload_response['status']:
            return upload_response

    # Upload data
    picture.title = title
    picture.description = description
    picture.save()

    # Images data
    img_url = '%s/%s/' % (host, IMAGE_DIR)
    original_url = img_url + picture.original_image
    normalized_url = img_url + picture.normalized_image
    thumbnail_url = img_url + picture.thumbnail_image

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html', site=site, host=host, csrf=csrf,
                                portfolio=portfolio, img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True, info='Atualizada com sucesso', picture_id=picture.get_id(),
                original=original_url, normalized=normalized_url, thumbnail=thumbnail_url,
                picture_list=picture_list)
Beispiel #6
0
    def get(self, picture_uuid):
        """Retrieve the picture specified by picture_uuid"""
        try:
            picture = Picture.get(Picture.uuid == picture_uuid)
        except Picture.DoesNotExist:
            return None, client.NOT_FOUND

        return send_from_directory(utils.get_image_folder(),
                                   picture.filename,
                                   as_attachment=True)
 def test_post_picture__success(self):
     item = Item.create(**TEST_ITEM)
     resp = self.app.post(
         '/items/{item_uuid}/pictures/'.format(item_uuid=item.uuid),
         data={'image': (BytesIO(b'my file contents'), 'testimage.jpg')},
         content_type='multipart/form-data')
     assert resp.status_code == client.CREATED
     assert len(Picture.select()) == 1
     picture = Picture.get()
     assert picture.item == item
     assert picture.extension == 'jpg'
     assert type(picture.uuid) == uuid.UUID
Beispiel #8
0
    def get(self, uuid):
        try:
            picture = Picture.get(Picture.uuid == uuid)
        except Picture.DoesNotExist:
            return None, NOT_FOUND

        return send_from_directory(
            os.path.join(
                current_app.config['UPLOADS_FOLDER'],
                'items',
                str(picture.item.uuid),
            ),
            secure_filename('.'.join([str(picture.uuid), picture.extension]))
        )
Beispiel #9
0
def picture_delete(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Delete picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    picture_id = int(request.POST.get('picture'))

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id,
                                  Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site,
                              Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Delete picture
    site_img_path = site.get_image_path(IMAGE_PATH)
    picture.delete_all(site_img_path)

    # Images url
    img_url = '%s/%s/' % (host, IMAGE_DIR)

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html',
                                site=site,
                                host=host,
                                csrf=csrf,
                                portfolio=portfolio,
                                img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True,
                info='Apagado com sucesso',
                picture_id=picture_id,
                picture_list=picture_list)
Beispiel #10
0
    def test_delete_item__pictures_cascade(self):
        """
        delete a selected item and all its binded pictures
        """
        test_utils.setup_images()
        item = Item.create(**TEST_ITEM)
        item2 = Item.create(**TEST_ITEM2)
        picture = Picture.create(item=item, **TEST_PICTURE)
        picture2 = Picture.create(item=item, **TEST_PICTURE2)
        picture3 = Picture.create(item=item2, **TEST_PICTURE3)
        imgfolder = utils.get_image_folder()
        path_pic = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture.uuid,
            extension=picture.extension))
        path_pic2 = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture2.uuid,
            extension=picture2.extension))
        path_pic3 = os.path.join(imgfolder, "{uuid}.{extension}".format(
            uuid=picture3.uuid,
            extension=picture3.extension))
        open(path_pic, "wb")
        open(path_pic2, "wb")
        open(path_pic3, "wb")

        resp = self.app.delete('/items/{item_uuid}'.format(
            item_uuid=item.uuid))

        assert resp.status_code == client.NO_CONTENT
        assert Picture.select().count() == 1
        assert Item.select().count() == 1
        item2 = Item.get()
        pic = Picture.get()
        assert pic == picture3
        assert os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture3.uuid,
            extension=picture3.extension))
        assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture.uuid,
            extension=picture.extension))
        assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format(
            path=utils.get_image_folder(),
            picture_uuid=picture2.uuid,
            extension=picture2.extension))
        test_utils.clean_images()
Beispiel #11
0
def picture_delete(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Delete picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    picture_id = int(request.POST.get('picture'))

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id, Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site, Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Delete picture
    site_img_path = site.get_image_path(IMAGE_PATH)
    picture.delete_all(site_img_path)

    # Images url
    img_url = '%s/%s/' % (host, IMAGE_DIR)

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html', site=site, host=host, csrf=csrf,
                                portfolio=portfolio, img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True, info='Apagado com sucesso', picture_id=picture_id, picture_list=picture_list)
Beispiel #12
0
def tmb(request, key):
    img = Picture.get(key)
    if img.thumbnail:
        return HttpResponse(img.thumbnail, mimetype="image/jpeg")
    else:
        return redirect('/static/noimage_tmb.jpg')
Beispiel #13
0
def tmb(request, key):
    img = Picture.get(key)
    if img.thumbnail:
        return HttpResponse(img.thumbnail, mimetype="image/jpeg")
    else:
        return redirect('/static/noimage_tmb.jpg')
Beispiel #14
0
def picture_update(site, host, netloc, csrf, logged_in, user_id_logged_in):
    """
    Edit picture post url
    """

    # POST parameters
    site_id = int(request.POST.get('site'))
    user_id = int(request.POST.get('user'))
    portfolio_id = int(request.POST.get('portfolio'))
    picture_id = int(request.POST.get('picture'))
    title = request.POST.get('title')
    description = request.POST.get('description')

    # Portfolio
    try:
        portfolio = Portfolio.get(Portfolio.id == portfolio_id,
                                  Portfolio.site == site)
    except Portfolio.DoesNotExist:
        return dict(status=False, info='Portfólio não encontrado')

    # Picture
    try:
        picture = Picture.get(Picture.id == picture_id, Picture.site == site,
                              Picture.portfolio == portfolio)
    except Picture.DoesNotExist:
        return dict(status=False, info='Imagem não encontrada')

    # Update upload picture image
    if request.files.get('upload'):
        img_path = site.get_image_path(IMAGE_PATH)
        original_file = os.path.join(img_path, picture.original_image)
        normalized_file = os.path.join(img_path, picture.normalized_image)
        thumbnail_file = os.path.join(img_path, picture.thumbnail_image)
        upload_response = upload_image(request, original_file, normalized_file,
                                       thumbnail_file, ORIG_SIZE, NORM_SIZE,
                                       THUMB_SIZE)
        if not upload_response['status']:
            return upload_response

    # Upload data
    picture.title = title
    picture.description = description
    picture.save()

    # Images data
    img_url = '%s/%s/' % (host, IMAGE_DIR)
    original_url = img_url + picture.original_image
    normalized_url = img_url + picture.normalized_image
    thumbnail_url = img_url + picture.thumbnail_image

    # Lista de pictures atualizada
    try:
        picture_list = template('pictures_admin_list.html',
                                site=site,
                                host=host,
                                csrf=csrf,
                                portfolio=portfolio,
                                img_url=img_url)
    except Exception as exp:
        return dict(status=False, info='%s' % exp)

    # Return OK
    return dict(status=True,
                info='Atualizada com sucesso',
                picture_id=picture.get_id(),
                original=original_url,
                normalized=normalized_url,
                thumbnail=thumbnail_url,
                picture_list=picture_list)