def create_order(): user_authenticated_id = get_jwt_identity() helper_id = request.form.get('helper_id') description = request.form.get('description') long_description = request.form.get('long_description') order = Order(description=description, long_description=long_description, helper_id=helper_id, status_id=1) order.save() documentURL = request.form.get('files') if documentURL: filename = 'Documents URL' document = Document(name=filename, url=documentURL, order=order, user_id=user_authenticated_id) document.save() DBManager.commitSession() orderSerialized = order.serialize() if orderSerialized: new_order_mail(order.helper, order) # Añadir los documentos al objeto orderSerialized["documents"] = list( map(lambda document: document.serialize(), order.documents)) return jsonify({"status": "ok", "order": orderSerialized})
def create_admin(): full_name = None while full_name is None: full_name = input('Full name: ') or None email = None while email is None: email = input('email: ') or None password = None while password is None: password = getpass('Password: '******'Phone: ') or None hashed_password = generate_password_hash(password, method='sha256') new_user = User( email=email, password=hashed_password, full_name=full_name, phone=phone, role_id=Role.ADMIN_ROLE_ID, is_active= True, ) new_user.save() DBManager.commitSession()
def set_order_approved(id): order = Order.query.get(id) order.status_id = Status.APPROVED_STATUS_ID order.save() DBManager.commitSession() order_status_update_mail(order) return jsonify(order.serializeForEditView()), 200
def save_order_addresses(id): user_authenticated_id = get_jwt_identity() pickup_address = Address(address=request.json.get('pickup').get('address'), city=request.json.get('pickup').get('city'), country=request.json.get('pickup').get('country'), cp=request.json.get('pickup').get('CP'), user_id=user_authenticated_id) pickup_address.save() delivery_address = Address( address=request.json.get('delivery').get('address'), city=request.json.get('delivery').get('city'), country=request.json.get('delivery').get('country'), cp=request.json.get('delivery').get('CP'), user_id=user_authenticated_id) delivery_address.save() order = Order.query.get(id) order.address_delivery = delivery_address order.address_pickup = pickup_address order.save() DBManager.commitSession() order_new_data_mail(order) return jsonify(order.serializeForEditView()), 200
def save_video(id): user_authenticated_id = get_jwt_identity() order = Order.query.get(id) if os.environ.get('AWS_S3_BUCKET_NAME'): files = request.files print("files", files) for key in files: file = files[key] if file: url_document = upload_file_to_s3( file, os.environ.get('AWS_S3_BUCKET_NAME')) if url_document: document = Document(name=file.filename, url=url_document, order=order, user_id=user_authenticated_id) document.save() else: print("Faltan las credenciales de AWS") DBManager.commitSession() order_new_data_mail(order) return jsonify(order.serializeForEditView()), 200
def create_order(): user_authenticated_id = get_jwt_identity() helper_id = request.form.get('helper_id') description = request.form.get('description') order = Order(description=description, helper_id=helper_id, status_id=1) order.save() if os.environ.get('AWS_S3_BUCKET_NAME'): files = request.files for key in files: file = files[key] if file: url_document = upload_file_to_s3( file, os.environ.get('AWS_S3_BUCKET_NAME')) if url_document: document = Document(name=file.filename, url=url_document, order=order, user_id=user_authenticated_id) document.save() else: print("Faltan las credenciales de AWS") DBManager.commitSession() orderSerialized = order.serialize() if orderSerialized: new_order_mail(order.helper, order) #Añadir los documentos al objeto orderSerialized["documents"] = list( map(lambda document: document.serialize(), order.documents)) return jsonify({"status": "ok", "order": orderSerialized})
def delete_user(id): user = User.query.get(id) if user is None: raise APIException('User not found', status_code=404) user.is_active = False DBManager.commitSession() return jsonify(user.serialize()), 200
def accept_order(id): order = Order.query.get(id) order.status_id = Status.PROCESSING_STATUS_ID order.save() order_acceptance_mail(order) DBManager.commitSession() return jsonify(order.serializeForEditView()), 200
def delete_order(id): order = Order.query.get(id) if order is None: raise APIException('Order not found', status_code=404) order.active = False print("ORDER ACTIVE", order.active) DBManager.commitSession() return jsonify(order.serializeForEditView()), 200
def save_video(id): user_authenticated_id = get_jwt_identity() video = request.json.get('video', None) order = Order.query.get(id) document = Document(name="Video", url=video, order=order, user_id=user_authenticated_id) document.save() DBManager.commitSession() return jsonify(order.serializeForEditView()), 200
def create_roles(): if not Role.query.get(Role.ADMIN_ROLE_ID): role = Role(id=Role.ADMIN_ROLE_ID, name="Admin") role.save() if not Role.query.get(Role.MANAGER_ROLE_ID): role = Role(id=Role.MANAGER_ROLE_ID, name="Manager") role.save() if not Role.query.get(Role.HELPER_ROLE_ID): role = Role(id=Role.HELPER_ROLE_ID, name="Helper") role.save() DBManager.commitSession() return
def send_password_email(): email = request.json.get('email', None) user = User.query.filter_by(email=email).first() if user: dt = datetime.datetime.today() today = dt.strftime("%Y-%m-%d") user.reset_password_token = generate_password_hash(user.email + today, method='sha256') user.save() DBManager.commitSession() new_password_email(user) return jsonify({"status": "ok"}), 200
def save_user(id): userData = request.json.get('user', None) user = User.query.get(id) user.email = userData["email"] user.full_name = userData["full_name"] user.phone = userData["phone"] user.role_id = userData["role_id"] user.save() DBManager.commitSession() return jsonify(user.serialize()), 200
def reset_password(): token = request.json.get('token', None) if token and len(token) > 0: user = User.query.filter_by(reset_password_token=token).first() if user: password = request.json.get('password', None) user.password = generate_password_hash(password=password, method='sha256') user.reset_password_token = None user.save() DBManager.commitSession() return jsonify({"status": "ok"}), 200 return jsonify({"status": "Bad request"}), 400
def save_order_files(id): user_authenticated_id = get_jwt_identity() order = Order.query.get(id) body = request.get_json() filename = 'Order #' + str(order.id) + ' files' document = Document(name=filename, url=body['files'], order=order, user_id=user_authenticated_id) document.save() DBManager.commitSession() order_new_data_mail(order) return jsonify(order.serializeForEditView()), 201
def update_order(id): order = Order.query.get(id) if request.form.get('helper_id') and int( request.form.get('helper_id')) != order.helper_id: order.status_id = Status.PROCESSING_STATUS_ID order.helper_id = request.form.get('helper_id') order.save() new_order_mail(order.helper, order) if not request.form.get('description') is None: order.description = request.form.get('description') order.save() DBManager.commitSession() orderSerialized = order.serializeForEditView() return jsonify(orderSerialized), 200
def reject_order(id): order = Order.query.get(id) order.status_id = Status.REJECTED_STATUS_ID order.save() response = DBManager.commitSession() order_rejection_mail(order) return jsonify(order.serializeForEditView()), 200
def create_user(): user_authenticated_id = get_jwt_identity() form = request.form.to_dict() form['password_user'] = generate_password_hash(form['password_user'], method='sha256') new_user = User(email=str(form["email_address"]), password=form['password_user'], full_name=str(form["full_name"]), phone=str(form["phone_number"]), is_active=True) new_user.role_id = form["role_id"] new_user.save() DBManager.commitSession() return jsonify("User created"), 201
def create_statuses(): if not Status.query.get(Status.PENDING_STATUS_ID): status = Status(id=Status.PENDING_STATUS_ID, name="Pending") status.save() if not Status.query.get(Status.REJECTED_STATUS_ID): status = Status(id=Status.REJECTED_STATUS_ID, name="Rejected") status.save() if not Status.query.get(Status.PROCESSING_STATUS_ID): status = Status(id=Status.PROCESSING_STATUS_ID, name="Processing") status.save() if not Status.query.get(Status.READY_STATUS_ID): status = Status(id=Status.READY_STATUS_ID, name="Ready") status.save() if not Status.query.get(5): status = Status(id=5, name="Complete") status.save() DBManager.commitSession() return
def save_order_addresses(id): user_authenticated_id = get_jwt_identity() # pickup_address = Address(address=request.json.get('pickup').get('address'), city=request.json.get('pickup').get('city'), country=request.json.get('pickup').get('country'), cp=request.json.get('pickup').get('CP'),user_id=user_authenticated_id) # pickup_address.save() # delivery_address = Address(address=request.json.get('delivery').get('address'), city=request.json.get('delivery').get('city'), country=request.json.get('delivery').get('country'), cp=request.json.get('delivery').get('CP'),user_id=user_authenticated_id) # delivery_address.save() order = Order.query.get(id) # order.address_delivery = delivery_address # order.address_pickup = pickup_address # order.save() order.status_id = Status.COMPLETED_STATUS_ID order.save() response = DBManager.commitSession() order_rejection_mail(order) order_new_data_mail(order) return jsonify(order.serializeForEditView()), 200
def delete_document(id): document = Document.query.get(id) document.delete() DBManager.commitSession() return jsonify(document.serialize()), 200
def set_order_ready(id): order = Order.query.get(id) order.status_id = Status.READY_STATUS_ID order.save() DBManager.commitSession() return jsonify(order.serializeForEditView()), 200