Exemple #1
0
def profile():
    my_form = ProfileForm()  #初始化表单
    my_data = Profile(
    )  #初始化数据,没这行,数据无法放在form中,给该表单提交的数据起名为my_data,即first_name那列数据就叫my_data.first_name
    my_data.remove_none_values()  #call the function
    if my_form.validate_on_submit():  #意思是如果表单提交成功
        my_data.first_name = request.form.get('first_name')
        my_data.last_name = request.form.get('last_name')
        #print("first_name", my_data.first_name)
        #print("last_name", my_data.last_name)
        file = request.files.get('file_photo')
        if file:
            orig_filename = secure_filename(file.filename)
            new_filename = str(uuid.uuid1())  #生成uuid
            my_data.file_photo_filename = orig_filename  #To save the orignal file name
            my_data.file_photo_code = new_filename
            #上面这行是为了存储uuid,目的是如果不同用户上传的不用文件,起了同一个名,靠uuid来区分

            # save to upload folder
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], new_filename))

            #save to database
            db.session.add(my_data)
            db.session.commit()
            print("my_data", my_data.id)

            #redirect to display page
            return redirect('/profile/' +
                            str(my_data.id))  #这个意思是每个数据的特定URL,比如profile/5...

    return render_template("profile.html", my_form=my_form, my_data=my_data)
Exemple #2
0
def profile():

    my_form = ProfileForm()

    my_data = Profile()
    my_data.remove_none_values()

    # print("my_form.validate_on_submit()", my_form.validate_on_submit())
    # print(my_form.errors)
    if my_form.validate_on_submit():

        # print("************ FORM SUBMITTED****")
        my_data.first_name = request.form.get('first_name')
        my_data.last_name = request.form.get('last_name')
        my_data.email = request.form.get('email')
        my_data.dob = request.form.get('dob')
        # print("first_name", my_data.first_name)
        # print("last_name", my_data.last_name)

        # process file
        file = request.files.get('file_photo')
        if file:
            orig_filename = secure_filename(file.filename)
            file_extension = os.path.splitext(orig_filename)
            file_extension = str(file_extension[1]).lower()

            new_filename = str(uuid.uuid1()) + file_extension

            # save to upload folder
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], new_filename))
            # print("file saved")

            my_data.file_photo_filename = orig_filename
            my_data.file_photo_code = new_filename

        # ---------------EXCEL/CSV - Load into table
        data_file = request.files.get('excel_file')
        print("data_file", data_file)
        if data_file:
            orig_filename = secure_filename(data_file.filename)
            file_extension = os.path.splitext(orig_filename)
            file_extension = str(file_extension[1]).lower()

            new_filename = str(uuid.uuid1()) + file_extension

            file_full_path = os.path.join(app.config['UPLOAD_FOLDER'],
                                          new_filename)

            # save to upload folder
            data_file.save(file_full_path)
            # print("file_full_path", file_full_path)
            my_data.file_data_filename = orig_filename
            my_data.file_data_code = new_filename

            # load the data in the table using pandas
            df = pd.read_csv(file_full_path)
            rest_list_raw = df.to_dict('records')
            rest_list = []
            for rest in rest_list_raw:
                my_rest = Restaurant()
                my_rest.bill = rest['bill']
                my_rest.tip = rest['tip']
                rest_list.append(my_rest)
            db.session.bulk_save_objects(rest_list)
            db.session.commit()

        # save to database
        db.session.add(my_data)
        db.session.commit()
        # print("my_data", my_data.id)

        # redirect to display page
        return redirect('/profile/' + str(my_data.id))  # profile/5

    return render_template('profile.html', my_form=my_form, my_data=my_data)