@action(route="/radioepg/gallery/edit/<id>", template="radioepg/gallery/edit.html", methods=['POST', 'GET'])
@only_orga_admin_user()
def radioep_gallery_edit(request, id):
    """Edit a channel."""

    object = None
    errors = []

    if id != '-':
        object = PictureForEPG.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 = PictureForEPG(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, (name, i, ext) % '%s(%d)%s')

            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/radioepg/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.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

        # If no errors, save
        if not errors:

            if not object.id:
                db.session.add(object)
            else:
                import glob
                for f in glob.glob('media/uploads/radioepg/cache/*_L%s.png' % (object.id,)):
                    os.unlink(f)

            db.session.commit()

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

    if object:
        object = object.json

    return {'object': object, 'errors': errors}
dataset/ETHPy150Open ebu/radiodns-plugit/RadioDns-PlugIt/radioepg/actions.py/radioep_gallery_edit/swapped operands of `%`