示例#1
0
文件: views.py 项目: WachaDev/Brocat
def upload_brocat():
    upload_form = UploadBrocatForm()
    if upload_form.validate_on_submit():
        img_folder = app.config['IMAGES_FOLDER']
        aud_folder = app.config['AUDIOS_FOLDER']

        title = upload_form.title.data
        thumbnail = upload_form.thumbnail.data
        audio = upload_form.audio.data
        description = upload_form.description.data

        thumbnail_filename = secure_filename(thumbnail.filename)
        audio_filename = secure_filename(audio.filename)

        thumb_path = os.path.join(img_folder, thumbnail_filename)
        aud_path = os.path.join(aud_folder, audio_filename)

        thumbnail.save(thumb_path)
        audio.save(aud_path)

        new_brocat = Brocat(title, thumbnail_filename, audio_filename,
                            description)

        try:
            db_session.add(new_brocat)
            db_session.commit()
        except:
            db_session.rollback()
            return 'Error in the db'

        flash('Uploaded Brocat')
        return redirect('/')

    return _render_template('upload_brocat.html', form=upload_form)
示例#2
0
文件: views.py 项目: WachaDev/Brocat
def post_a_brocat():
    brocat_data = brocat_schema.load(request.json, session=db_session)

    try:
        db_session.add(brocat_data)
        db_session.commit()
    except:
        db_session.rollback()
        return jsonify({'message': 'Error in the db'})

    return jsonify({"message": "Added"})
示例#3
0
文件: views.py 项目: WachaDev/Brocat
def delete_brocat(id):
    brocat_to_del = Brocat.query.filter_by(id=id).first()
    if not (brocat_to_del and brocat_to_del.id in current_user.brocats):
        return jsonify('Brocat not found!'), 404

    try:
        db_session.delete(brocat_to_del)
        db_session.commit()
    except:
        db_session.rollback()
        return jsonify({'message': 'Error in the db'})

    return jsonify({"message": "Brocat deleted!"})
示例#4
0
文件: views.py 项目: WachaDev/Brocat
def delete_user(id):
    user_to_del = User.query.filter_by(id=id).first()

    if not (user_to_del and user_to_del.id == current_user.id):
        return jsonify('User not found'), 404

    try:
        db_session.delete(user_to_del)
        db_session.commit()
    except:
        db_session.rollback()
        return jsonify({'message': 'Error in the db'})

    return jsonify({'message': 'User deleted!'})
示例#5
0
文件: views.py 项目: WachaDev/Brocat
def create_account():
    ca_form = CreateAccountForm()
    if ca_form.validate_on_submit():
        new_user = User(ca_form.email.data, ca_form.username.data,
                        ca_form.password.data)

        try:
            db_session.add(new_user)
            db_session.commit()
        except:
            db_session.rollback()
            return 'Error in the db'

        return redirect('/login')

    return _render_template('create_account.html', form=ca_form)
示例#6
0
文件: views.py 项目: WachaDev/Brocat
def patch_brocat(id):
    brocat = Brocat.query.get(id)
    if brocat and brocat.author.id == current_user.id:
        data = request.json
        for key in data:
            if key == 'title':
                brocat.title = data['title']
            if key == 'thumbnail':
                brocat.thumbnail = data['thumbnail']
            if key == 'audio':
                brocat.audio = data['audio']
            if key == 'description':
                brocat.description = data['description']

        try:
            db_session.commit()
        except:
            return jsonify({'message': 'Error in the db'})

        return jsonify({'message': 'Updated succesfully!'})

    return jsonify({'message': 'Brocat not found!'}), 404
示例#7
0
文件: views.py 项目: WachaDev/Brocat
def patch_user(id):
    user = User.query.get(id)
    if user and user.id == current_user.id:
        data = request.json
        for key in data:
            if key == 'email':
                user.email = data['email']
            if key == 'username':
                user.username = data['username']
            if key == 'password':
                # encode() is not necessary with SQLAlchemy String(convert_unicode=True) and
                # the @staticmethod hash_psw() is not necessary with a property
                user.password = User.hash_psw(data['password'])

        try:
            db_session.commit()
        except:
            return jsonify({'message': 'Error in the db'})

        return jsonify({'message': 'Updated succesfully'})

    return jsonify({'message': 'User not found'}), 404