@action(route="/radiovis/gallery/edit/<id>", template="radiovis/gallery/edit.html", methods=['POST', 'GET'])
@only_orga_admin_user()
def radiovis_gallery_edit(request, id):

    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 pieces.scheme not 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}
Get allowed users
dataset/ETHPy150Open ebu/radiodns-plugit/RadioDns-PlugIt/radiovis/actions.py/radiovis_gallery_edit--crossover with--dataset/ETHPy150Open nvbn/coviolations_web/projects/models.py/Project.get_allowed_users