Exemplo n.º 1
0
def create_picture(session, orga, name="testPicture"):
    picture = Picture(orga)
    picture.name = name
    picture.filename = name
    picture.radiotext = name
    picture.radiolink = name
    picture.image_url_prefix = name
    session.add(picture)
    session.commit()
    Channel.query.filter_by(id=1).first().default_picture_id = picture.id
    Channel.query.filter_by(id=2).first().default_picture_id = picture.id
    session.commit()
    time.sleep(config.SPI_GENERATION_INTERVAL * 3)
    return picture
Exemplo n.º 2
0
def radiovis_gallery_edit(request, id):
    """Edit a channel."""

    object = None
    errors = []

    plugitapi = PlugItAPI(config.API_URL)
    orga = plugitapi.get_orga(request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))

    sp = None
    if orga.codops:
        sp = ServiceProvider.query.filter_by(codops=orga.codops).order_by(ServiceProvider.codops).first()

    if id != '-':
        object = Picture.query.filter_by(orga=int(request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk')),
                                         id=int(id)).first()

    if request.method == 'POST':

        if not object:
            object = Picture(int(request.form.get('ebuio_orgapk')))

        if sp:
            object.image_url_prefix = sp.image_url_prefix

        object.name = request.form.get('name')
        object.radiotext = request.form.get('radiotext')
        object.radiolink = request.form.get('radiolink')

        def add_unique_postfix(fn):
            """__source__ = 'http://code.activestate.com/recipes/577200-make-unique-file-name/'"""
            if not os.path.exists(fn):
                return fn

            path, name = os.path.split(fn)
            name, ext = os.path.splitext(name)

            make_fn = lambda i: os.path.join(path, '%s(%d)%s' % (name, i, ext))

            for i in xrange(2, sys.maxint):
                uni_fn = make_fn(i)
                if not os.path.exists(uni_fn):
                    return uni_fn

            return None

        def unique_filename(fn):

            path, name = os.path.split(fn)
            name, ext = os.path.splitext(name)

            make_fn = lambda i: os.path.join(path, '%s%s' % (str(uuid.uuid4()), ext))

            for i in xrange(2, sys.maxint):
                uni_fn = make_fn(i)
                if not os.path.exists(uni_fn):
                    return uni_fn

            return None

        new_file = False
        if request.files:
            new_file = True
            file = request.files['file']
            if file:
                filename = secure_filename(file.filename)
                full_path = unique_filename('media/uploads/radiovis/gallery/' + filename)
                path, name = os.path.split(full_path)
                file.save(full_path)
                if object.filename:
                    try:
                        os.unlink(object.filename)
                    except:
                        pass
                object.filename = name

            # Check errors
            if object.name == '':
                errors.append("Please set a name")

            if object.radiotext == '':
                errors.append("Please set a text")

            if object.radiolink == '':
                errors.append("Please set a link")

            if object.filename == '' or object.filename is None:
                errors.append("Please upload an image")
            else:

                if imghdr.what(full_path) not in ['jpeg', 'png']:
                    errors.append("Image is not an png or jpeg image")
                    os.unlink(full_path)
                    object.filename = None
                else:
                    im = Image.open(full_path)
                    if im.size != (320, 240):
                        errors.append("Image must be 320x240")
                        del im
                        os.unlink(full_path)
                        object.filename = None

        pieces = urlparse.urlparse(object.radiolink)

        if pieces.scheme not in ['http', 'https', 'ftp']:
            errors.append("The link is not valid")

        # If no errors, save
        if not errors:

            # Upload to s3
            if new_file:
                try:
                    if config.STANDALONE:
                        send_image_to_mock_api({name: open(full_path, 'rb')})
                    else:
                        awsutils.upload_public_image(sp, name, full_path)
                except:
                    pass
            # TODO Clean up old images on S3

            if not object.id:
                db.session.add(object)

            db.session.commit()

            try:
                # Remove local copy
                os.unlink(full_path)
            except:
                pass

            return PlugItRedirect('radiovis/gallery/?saved=yes')

        try:
            # Remove local copy
            os.unlink(full_path)
        except:
            pass

    if object:
        object = object.json

    return {'object': object, 'errors': errors}
Exemplo n.º 3
0
def radiovis_gallery_edit(request, id):
    """Edit a channel."""

    object = None
    errors = []

    if id != '-':
        object = Picture.query.filter_by(orga=int(
            request.args.get('ebuio_orgapk')
            or request.form.get('ebuio_orgapk')),
                                         id=int(id)).first()

    if request.method == 'POST':

        if not object:
            object = Picture(int(request.form.get('ebuio_orgapk')))

        object.name = request.form.get('name')
        object.radiotext = request.form.get('radiotext')
        object.radiolink = request.form.get('radiolink')

        def add_unique_postfix(fn):
            """__source__ = 'http://code.activestate.com/recipes/577200-make-unique-file-name/'"""
            if not os.path.exists(fn):
                return fn

            path, name = os.path.split(fn)
            name, ext = os.path.splitext(name)

            make_fn = lambda i: os.path.join(path, '%s(%d)%s' % (name, i, ext))

            for i in xrange(2, sys.maxint):
                uni_fn = make_fn(i)
                if not os.path.exists(uni_fn):
                    return uni_fn

            return None

        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            full_path = add_unique_postfix('media/uploads/radiovis/gallery/' +
                                           filename)
            file.save(full_path)
            if object.filename:
                try:
                    os.unlink(object.filename)
                except:
                    pass
            object.filename = full_path

        # Check errors
        if object.name == '':
            errors.append("Please set a name")

        if object.radiotext == '':
            errors.append("Please set a text")

        if object.radiolink == '':
            errors.append("Please set a link")

        if object.filename == '' or object.filename is None:
            errors.append("Please upload an image")
        else:

            if imghdr.what(object.filename) not in ['jpeg', 'png']:
                errors.append("Image is not an png or jpeg image")
                os.unlink(object.filename)
                object.filename = None
            else:
                im = Image.open(object.filename)
                if im.size != (320, 240):
                    errors.append("Image must be 320x240")
                    del im
                    os.unlink(object.filename)
                    object.filename = None

        pieces = urlparse.urlparse(object.radiolink)

        if not pieces.scheme in ['http', 'https', 'ftp']:
            errors.append("The link is not valid")

        # If no errors, save
        if not errors:

            if not object.id:
                db.session.add(object)

            db.session.commit()

            return PlugItRedirect('radiovis/gallery/?saved=yes')

    if object:
        object = object.json

    return {'object': object, 'errors': errors}
Exemplo n.º 4
0
def radiovis_gallery_edit(request, id):
    """Edit a channel."""

    object = None
    errors = []

    plugitapi = PlugItAPI(config.API_URL)
    orga = plugitapi.get_orga(
        request.args.get('ebuio_orgapk') or request.form.get('ebuio_orgapk'))

    sp = None
    if orga.codops:
        sp = ServiceProvider.query.filter_by(codops=orga.codops).order_by(
            ServiceProvider.codops).first()

    if id != '-':
        object = Picture.query.filter_by(orga=int(
            request.args.get('ebuio_orgapk')
            or request.form.get('ebuio_orgapk')),
                                         id=int(id)).first()

    if request.method == 'POST':

        if not object:
            object = Picture(int(request.form.get('ebuio_orgapk')))

        if sp:
            object.image_url_prefix = sp.image_url_prefix

        object.name = request.form.get('name')
        object.radiotext = request.form.get('radiotext')
        object.radiolink = request.form.get('radiolink')

        def add_unique_postfix(fn):
            """__source__ = 'http://code.activestate.com/recipes/577200-make-unique-file-name/'"""
            if not os.path.exists(fn):
                return fn

            path, name = os.path.split(fn)
            name, ext = os.path.splitext(name)

            make_fn = lambda i: os.path.join(path, '%s(%d)%s' % (name, i, ext))

            for i in xrange(2, sys.maxint):
                uni_fn = make_fn(i)
                if not os.path.exists(uni_fn):
                    return uni_fn

            return None

        def unique_filename(fn):

            path, name = os.path.split(fn)
            name, ext = os.path.splitext(name)

            make_fn = lambda i: os.path.join(path, '%s%s' %
                                             (str(uuid.uuid4()), ext))

            for i in xrange(2, sys.maxint):
                uni_fn = make_fn(i)
                if not os.path.exists(uni_fn):
                    return uni_fn

            return None

        new_file = False
        if request.files:
            new_file = True
            file = request.files['file']
            if file:
                filename = secure_filename(file.filename)
                full_path = unique_filename('media/uploads/radiovis/gallery/' +
                                            filename)
                path, name = os.path.split(full_path)
                file.save(full_path)
                if object.filename:
                    try:
                        os.unlink(object.filename)
                    except:
                        pass
                object.filename = name

            # Check errors
            if object.name == '':
                errors.append("Please set a name")

            if object.radiotext == '':
                errors.append("Please set a text")

            if object.radiolink == '':
                errors.append("Please set a link")

            if object.filename == '' or object.filename is None:
                errors.append("Please upload an image")
            else:

                if imghdr.what(full_path) not in ['jpeg', 'png']:
                    errors.append("Image is not an png or jpeg image")
                    os.unlink(full_path)
                    object.filename = None
                else:
                    im = Image.open(full_path)
                    if im.size != (320, 240):
                        errors.append("Image must be 320x240")
                        del im
                        os.unlink(full_path)
                        object.filename = None

        pieces = urlparse.urlparse(object.radiolink)

        if pieces.scheme not in ['http', 'https', 'ftp']:
            errors.append("The link is not valid")

        # If no errors, save
        if not errors:

            # Upload to s3
            if new_file:
                try:
                    if config.STANDALONE:
                        send_image_to_mock_api({name: open(full_path, 'rb')})
                    else:
                        awsutils.upload_public_image(sp, name, full_path)
                except:
                    pass
            # TODO Clean up old images on S3

            if not object.id:
                db.session.add(object)

            db.session.commit()

            try:
                # Remove local copy
                os.unlink(full_path)
            except:
                pass

            return PlugItRedirect('radiovis/gallery/?saved=yes')

        try:
            # Remove local copy
            os.unlink(full_path)
        except:
            pass

    if object:
        object = object.json

    return {'object': object, 'errors': errors}