示例#1
0
def add():
    form = ArtistFrom()
    if request.method == 'GET':
        return render_template('artist_management.html',
                               action='add',
                               form=form)
    elif request.method == 'POST':
        if form.validate_on_submit():
            artist_id = str(uuid4())

            # upload photo file
            file = form.photo.data
            photo_filename = ''
            uploadfailed = False

            if file:
                photo_filename = artist_id + '.' + file.filename.rsplit(
                    '.', 1)[1]
                if not oshelper.upload_file(
                        file, oshelper.get_artistphoto_upload_abspath(),
                        photo_filename):
                    photo_filename = ''
                    uploadfailed = True

            # add artist to db
            artist = Artist(artist_id, form.name.data, form.letter.data,
                            photo_filename, form.region.data,
                            form.category.data)
            db.session.add(artist)

            # flush data
            db.session.commit()

            flash(u'Add new artist success', 'success')

            if not uploadfailed:
                return redirect(url_for('bp_admin_artist.list'))
            else:
                # flash(u'Upload photo failed. Please try again!', 'warning')
                return redirect(url_for('bp_admin_artist.edit', id=artist_id))
        else:
            error_message = validator.catch_errors(form.errors)
            flash(error_message, 'error')
            return render_template('artist_management.html',
                                   action='add',
                                   form=form)
示例#2
0
def edit(id):
    artist = Artist.query.get(id)
    form = ArtistFrom(id=artist.id,
                      name=artist.name,
                      letter=artist.letter,
                      photo=artist.letter,
                      region=artist.region_id,
                      category=artist.category_id)

    photo_relative_path = artist.photo_relative_path + '?dummy=' + str(
        random())
    if request.method == 'GET':
        return render_template('artist_management.html',
                               action='edit',
                               form=form,
                               photo_path=photo_relative_path)
    elif request.method == 'POST':
        if form.validate_on_submit():
            # update artist
            artist.name = form.name.data
            artist.letter = form.letter.data
            artist.region_id = form.region.data
            artist.category_id = form.category.data

            # upload photo file
            file = form.photo.data
            if file:
                photo_filename = artist.id + '.' + file.filename.rsplit(
                    '.', 1)[1]
                if oshelper.upload_file(
                        file, oshelper.get_artistphoto_upload_abspath(),
                        photo_filename):
                    artist.photo = photo_filename

            # flush data
            db.session.commit()

            flash(u'Update artist success', 'success')
            return redirect(url_for('bp_admin_artist.edit', id=id))
        else:
            error_message = validator.catch_errors(form.errors)
            flash(error_message, 'error')
            return render_template('artist_management.html',
                                   action='edit',
                                   form=form,
                                   photo_path=artist.photo_relative_path)
示例#3
0
def edit(id):
    artist = Artist.query.get(id)
    form = ArtistFrom(
        id=artist.id,
        name=artist.name,
        letter=artist.letter,
        photo=artist.letter,
        region=artist.region_id,
        category=artist.category_id,
    )

    photo_relative_path = artist.photo_relative_path + "?dummy=" + str(random())
    if request.method == "GET":
        return render_template("artist_management.html", action="edit", form=form, photo_path=photo_relative_path)
    elif request.method == "POST":
        if form.validate_on_submit():
            # update artist
            artist.name = form.name.data
            artist.letter = form.letter.data
            artist.region_id = form.region.data
            artist.category_id = form.category.data

            # upload photo file
            file = form.photo.data
            if file:
                photo_filename = artist.id + "." + file.filename.rsplit(".", 1)[1]
                if oshelper.upload_file(file, oshelper.get_artistphoto_upload_abspath(), photo_filename):
                    artist.photo = photo_filename

            # flush data
            db.session.commit()

            flash(u"Update artist success", "success")
            return redirect(url_for("bp_admin_artist.edit", id=id))
        else:
            error_message = validator.catch_errors(form.errors)
            flash(error_message, "error")
            return render_template(
                "artist_management.html", action="edit", form=form, photo_path=artist.photo_relative_path
            )
示例#4
0
def add():
    form = ArtistFrom()
    if request.method == "GET":
        return render_template("artist_management.html", action="add", form=form)
    elif request.method == "POST":
        if form.validate_on_submit():
            artist_id = str(uuid4())

            # upload photo file
            file = form.photo.data
            photo_filename = ""
            uploadfailed = False

            if file:
                photo_filename = artist_id + "." + file.filename.rsplit(".", 1)[1]
                if not oshelper.upload_file(file, oshelper.get_artistphoto_upload_abspath(), photo_filename):
                    photo_filename = ""
                    uploadfailed = True

            # add artist to db
            artist = Artist(
                artist_id, form.name.data, form.letter.data, photo_filename, form.region.data, form.category.data
            )
            db.session.add(artist)

            # flush data
            db.session.commit()

            flash(u"Add new artist success", "success")

            if not uploadfailed:
                return redirect(url_for("bp_admin_artist.list"))
            else:
                # flash(u'Upload photo failed. Please try again!', 'warning')
                return redirect(url_for("bp_admin_artist.edit", id=artist_id))
        else:
            error_message = validator.catch_errors(form.errors)
            flash(error_message, "error")
            return render_template("artist_management.html", action="add", form=form)