Example #1
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)