def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data, 'profile_pics') current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data current_user.about_me = form.about_me.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email form.about_me.data = current_user.about_me image_file = url_for('static', filename='profile_pics/' + current_user.image_file) page = request.args.get('page', 1, type=int) per_page = app.config['POSTS_PER_PAGE'] user = User.query.filter_by(username=current_user.username).first_or_404() posts = Recipe.query.filter_by(author=current_user)\ .order_by(Recipe.timestamp.desc())\ .paginate(page=page, per_page=per_page) return render_template('account.html', title='Account', image_file=image_file, form=form, posts=posts, user=user)
def api_add_product(): """ Add product { "name": "Paracetemol", "description": "Paracetemol is a very good medicine", "price": 20, "quantity": 5, "pictures": [ "base64 encoded image string", "base64 encoded image string", "base64 encoded image string" ] } """ try: data = request.get_json() name = data['name'] description = data['description'] price = data['price'] quantity = data['quantity'] pictures = data['pictures'] product = Product(name=name, description=description, price=price, quantity=quantity) db.session.add(product) db.session.commit() for picture in pictures: imgdata = base64.b64decode(picture.split(',')[1]) filename = save_picture(img=imgdata, folder="product_picture") product_picture = ProductPicture(product_id=product.id, picture=filename) db.session.add(product_picture) db.session.commit() except: return jsonify({"message": "error"}), 403 output = {} output['id'] = product.id return jsonify(output, {"message": "success"}), 200
def create_post(): form = LoginFoodForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data, 'post_pics') title = Recipe(title=form.title.data, time=form.time.data, temperature=form.temp.data, recipe=form.recipe.data, author=current_user, image_file=picture_file) db.session.add(title) db.session.commit() flash('New Post created!') return redirect(url_for('index')) return render_template('new_post.html', title='New Recipes', form=form)
def profili(): form = ProfileForm() if form.validate_on_submit(): if form.foto.data: profile_pic = save_picture(form.foto.data) current_user.image_file = profile_pic current_user.emri_perdoruesit = form.emri_perdoruesit.data current_user.email = form.email.data db.session.commit() flash('Profili juaj u perditsua', 'success') return redirect(url_for('profili')) elif request.method == 'GET': form.emri_perdoruesit.data = current_user.emri_perdoruesit form.email.data = current_user.email image_loc = url_for('static', filename='fotot/' + current_user.image_file) return render_template('profili.html', title='Profili', image_loc=image_loc, form=form)
def account(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash('Your account has been updated!', 'success') return redirect(url_for('user.account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email image_file = url_for('static', filename='profile_pics/' + current_user.image_file) return render_template('account.html', title='账户信息', image_file=image_file, form=form)
def api_add_user(): """Add new user Will receive data like this { "email": "*****@*****.**", "date_of_birth": "1999-06-18", "role": "patient" or "doctor" "super_admin" "password": "******", "full_name": "Farhad Hossain", "address": "Stadium para, Maijdee court", "contact_no": "01983495680", "age": 21, "profile_pic": "base64 encoded image string" } """ data = request.get_json() username = secrets.token_hex(8) email = data['email'].lower() password = bcrypt.generate_password_hash(data['password']).decode('utf-8') date_of_birth = data['date_of_birth'] if 'role' not in data: role = 'patient' else: role = data['role'] full_name = data['full_name'] address = data['address'] contact_no = data['contact_no'] age = data['age'] if 'profile_pic' in data: profile_pic = data['profile_pic'] imgdata = base64.b64decode(profile_pic.split(',')[1]) filename = save_picture(img=imgdata, folder="profile_pics") # check if email already exists user = User.query.filter_by(email=email).first() if user: return jsonify({"message": "user exists"}), 403 user = User(username=username, email=email, password=password, date_of_birth=date_of_birth, role=role) db.session.add(user) db.session.commit() if role == "patient": # now add the patient infos if 'profile_pic' in data: # now add the patient infos patient = Patient(full_name=full_name, address=address, contact_no=contact_no, age=age, profile_pic=filename, user_id=user.id) else: patient = Patient(full_name=full_name, address=address, contact_no=contact_no, age=age, user_id=user.id) db.session.add(patient) db.session.commit() elif role == "doctor": # now add the doctor infos if 'profile_pic' in data: # now add the patient infos doctor = Doctor(full_name=full_name, address=address, contact_no=contact_no, age=age, profile_pic=filename, user_id=user.id) else: doctor = Doctor(full_name=full_name, address=address, contact_no=contact_no, age=age, user_id=user.id) db.session.add(doctor) db.session.commit() elif role == "super_admin": # now add the super_admin infos if 'profile_pic' in data: # now add the patient infos super_admin = SuperAdmin(full_name=full_name, address=address, contact_no=contact_no, age=age, profile_pic=filename, user_id=user.id) else: super_admin = SuperAdmin(full_name=full_name, address=address, contact_no=contact_no, age=age, user_id=user.id) db.session.add(super_admin) db.session.commit() else: return jsonify({"message": "invalid role"}), 403 return jsonify({"message": "success"}), 201
def api_edit_product(): """ Edit product { "id": 2, "name": "Paracetemol", "description": "Paracetemol is a very good medicine", "price": 20, "quantity": 5, "pictures": [ "base64 encoded image string", "base64 encoded image string", "base64 encoded image string" ] } """ try: data = request.get_json() product_id = data['id'] name = data['name'] description = data['description'] price = data['price'] quantity = data['quantity'] pictures = data['pictures'] product = Product.query.get(product_id) if not product: return jsonify({'message': "not found"}), 404 product.id = product_id product.name = name product.description = description product.price = price product.quantity = quantity # remove old pictures and then add new for picture in product.pictures: db.session.delete(product) db.session.commit() for picture in pictures: imgdata = base64.b64decode(picture.split(',')[1]) filename = save_picture(img=imgdata, folder="product_picture") product_picture = ProductPicture(product_id=product.id, picture=filename) db.session.add(product_picture) db.session.commit() except: return jsonify({"message": "error"}), 403 output = {} output['id'] = product.id return jsonify(output, {"message": "success"}), 200
def api_edit_user(user_id): """Edit new user Will receive data like this { "email": "*****@*****.**", "date_of_birth": "1999-06-18", "role": "patient" or "doctor" "super_admin" "password": "******", "full_name": "Farhad Hossain", "address": "Stadium para, Maijdee court", "contact_no": "01983495680", "age": 21 } """ user = User.query.get(user_id) if not user: return jsonify({"message": "not found"}), 404 data = request.get_json() username = secrets.token_hex(8) email = data['email'].lower() password = bcrypt.generate_password_hash(data['password']).decode('utf-8') date_of_birth = data['date_of_birth'] role = data['role'] full_name = data['full_name'] address = data['address'] contact_no = data['contact_no'] age = data['age'] profile_pic = data['profile_pic'] if 'profile_pic' in data: imgdata = base64.b64decode(profile_pic.split(',')[1]) filename = save_picture(img=imgdata, folder="profile_pics") # check if email already exists if user.email != email: user = User.query.filter_by(email=email).first() if user: return jsonify({"message": "user exists"}), 403 user.username = username user.email = email user.password = password user.date_of_birth = date_of_birth user.role = role db.session.commit() if role == "patient": # now edit the patient infos patient = user.patient patient.full_name = full_name patient.address = address patient.contact_no = contact_no patient.age = age patient.profile_pic = filename patient.user_id = user.id db.session.commit() elif role == "doctor": # now edit the doctor infos doctor = user.doctor doctor.full_name = full_name doctor.address = address doctor.contact_no = contact_no doctor.age = age doctor.profile_pic = filename doctor.user_id = user.id db.session.commit() elif role == "super_admin": # now edit the super_admin infos super_admin = user.super_admin super_admin.full_name = full_name super_admin.address = address super_admin.contact_no = contact_no super_admin.age = age super_admin.profile_pic = filename super_admin.user_id = user.id db.session.commit() else: return jsonify({"message": "invalid role"}), 403 return jsonify({"message": "success"}), 200