def post(self): data = request.form factory_name = data["factory_name"] username = data["logistic_name"] address = data["address"] email = data["email"] factory_hotline = data["factory_hotline"] delegate_phone = data["delegate_phone"] password = User.generate_pass() role = 2 img = request.files['factory_logo'] user = User.query.filter_by(email=email).first() if user: return redirect( url_for('base_blueprint.SignupFactory', error="FAILED: user with entered E-mail already exist!")) user = User.query.filter_by(username=username).first() if user: return redirect(url_for('base_blueprint.SignupFactory', error="user with entered name already exist!")) user = User.query.filter_by(phone=delegate_phone).first() if user: return redirect(url_for('base_blueprint.SignupFactory', error="user with entered phone already exist!")) user = User(username=username, email=email, role=role, password=password, phone=delegate_phone, ) db.session.add(user) fac = Factory.query.filter_by(name=factory_name).first() if fac: return redirect(url_for('base_blueprint.SignupFactory', error="factory with entered name already exist!")) fac = Factory.query.filter_by(address=address).first() if fac: return redirect( url_for('base_blueprint.SignupFactory', error="factory with entered address already exist!")) fac = Factory.query.filter_by(hotline=factory_hotline).first() if fac: return redirect( url_for('base_blueprint.SignupFactory', error="factory with entered hot line already exist!")) _, file_extension = os.path.splitext(img.filename) url = upload_file_to_s3(img, file_name=factory_name + file_extension, folder='factory_logo') fac = Factory(name=factory_name, delegate=user.id, address=address, hotline=factory_hotline, logo=url) db.session.add(fac) db.session.commit() admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Factory" message_body = "There are new Factory!" click_action = "/AdminDashboard/factory" result = notf_service.notify_single_device(registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body) return redirect(url_for('base_blueprint.login', message="Successfully Signed up, waiting for Admin approve then you will " "receive Accepted E-mail from us"))
def post(self): data = request.json try: car = Car.query.filter_by( user_id=current_user.id).first() # get current user car if not car.current_order_id: response_obj = { 'status': 'failed', 'message': "This truck don't have current order!" } return response_obj, 400 order_car_driver = OrderCarsAndDrivers.query.filter_by( order_id=car.current_order_id).filter_by( car_id=car.id).first() current_status = order_car_driver.status if current_status == 5: response_obj = { 'status': 'failed', 'message': "This order is already finished!" } return response_obj, 400 new_status = data.get('status') if new_status <= current_status: response_obj = { 'status': 'failed', 'message': "new status is less than or equal to current status!" } return response_obj, 400 order_car_driver.status = new_status db.session.commit() # get all cars that assigned to same order, we check all cars status before we update order status all_cars = OrderCarsAndDrivers.query.filter_by( order_id=car.current_order_id).all() order = Order.query.get(car.current_order_id) # check if all cars needed is assigned or not if order.num_of_cars == len( all_cars): # admin not assigned all cars final_status = min([x.status for x in all_cars]) if final_status > order.status: order.status = final_status order_history = OrderHistory(order_id=car.current_order_id, old_state=current_status, new_state=new_status) db.session.add(order_history) device_token = order.factory_object.delegate_opj.device_token message_title = "Order Status Update" message_body = "Your Order status has been updated,click for details!" message_data = { 'order_id': order.id, 'notf_type': "order_status_update", } result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, message_body=message_body, data_message=message_data) if new_status == 5: car.status = 'free' car.current_order_id = 0 order_car_driver.driver_opj.current_order_id = None db.session.commit() response_obj = { 'status': 'success', 'message': f"order status updated successfully from {orders_status[current_status]} to" f" {orders_status[new_status]}" } return response_obj, 200 except Exception as e: print('Exception in update order status: ', e) response_obj = { 'status': 'failed', 'message': 'Something Wrong, please try again later' } return response_obj, 500
def post(self): data = request.json try: from_latitude = data.get('from_latitude') # دوائر العرض from_longitude = data.get('from_longitude') # خطوط الطول to_latitude = data.get('to_latitude') # دوائر العرض to_longitude = data.get('to_longitude') # خطوط الطول pickup_location = data.get('pickup_location') dropoff_location = data.get('dropoff_location') factory_id = Factory.query.filter_by(_delegate_id=current_user.id).first().id num_of_trilla = data.get('trilla') num_of_maktura = data.get('maktura') num_of_cars = int(num_of_maktura) + int(num_of_trilla) order = Order(from_latitude=from_latitude, from_longitude=from_longitude, to_latitude=to_latitude, to_longitude=to_longitude, pickup_location=pickup_location, dropoff_location=dropoff_location, factory_id=factory_id, num_of_cars=num_of_cars) db.session.add(order) db.session.commit() for car_type in available_type: car = OrderCarsTypes(order_id=order.id, cars_num=data.get(car_type) or 0, car_type=car_type) db.session.add(car) db.session.commit() # # TODO REmove this todaay # """ For Test Only """ # # ------------------ # # ------------------ # car = Car.query.get(1) # order_id = order.id # new = OrderCarsAndDrivers(order_id=order_id, car_id=1, driver_id=1, company_id=car._owner) # car.current_order_id = order_id # car.status = 'busy' # db.session.add(new) # num_of_assigned_cars = OrderCarsAndDrivers.query.filter_by(order_id=order_id).count() # if order.num_of_cars == num_of_assigned_cars: # order.status += 1 # driver = Driver.query.get(1) # driver.current_order_id = order_id # db.session.commit() ###### End Of Test Part ##################### admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Order" message_body = "There are new order!" message_data = { 'factory_name': order.factory_object.name, 'notf_type': "new_order", "order_id": order.id, 'pickup_location_lat': order.from_latitude, 'pickup_location_lng': order.from_longitude, 'pickup_location_str': order.pickup_location, 'dropoff_location_lat': order.to_latitude, 'dropoff_location_lng': order.to_longitude, 'dropoff_location_str': order.dropoff_location } click_action = f"/AdminDashboard/OrderDetailsPage{order.id}" result = notf_service.notify_single_device(registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body, data_message=message_data) response_obj = { 'status': 'success', 'message': 'Successfully crate new order', 'order_id': order.id } return response_obj, 201 except Exception as e: print("Exception in new order:", e) response_obj = { 'status': 'failed', 'message': 'Something Wrong, please try again later' } return response_obj, 500
def post(self): data = request.form try: img = request.files['factory_logo'] factory_name = data.get('factory_name') username = data.get('username') email = data.get('email') password = User.generate_pass() # 'factory' address = data.get('address') factory_hotline = data.get('factory_hotline') delegate_phone = data.get('delegate_phone') role = 2 user = User.query.filter_by(email=email).first() # img = request.files['factory_logo'] if user: response_obj = { 'status': 'failed', 'message': 'user with entered E-mail already exist!' } return response_obj, 409 user = User.query.filter_by(username=username).first() if user: response_obj = { 'status': 'failed', 'message': 'user with entered name already exist!' } return response_obj, 409 user = User.query.filter_by(phone=delegate_phone).first() if user: response_obj = { 'status': 'failed', 'message': 'user with entered phone already exist!' } return response_obj, 409 user = User(username=username, email=email, role=role, password=password, phone=delegate_phone) db.session.add(user) fac = Factory.query.filter_by(name=factory_name).first() if fac: response_obj = { 'status': 'failed', 'message': 'factory with entered name already exist!' } return response_obj, 409 fac = Factory.query.filter_by(address=address).first() if fac: response_obj = { 'status': 'failed', 'message': 'factory with entered address already exist!' } return response_obj, 409 fac = Factory.query.filter_by(hotline=factory_hotline).first() if fac: response_obj = { 'status': 'failed', 'message': 'factory with entered hot line already exist!' } return response_obj, 409 _, file_extension = os.path.splitext(img.filename) url = upload_file_to_s3(img, file_name=factory_name + file_extension, folder='factory_logo') fac = Factory(name=factory_name, delegate=user.id, address=address, hotline=factory_hotline,logo=url) # fac = Factory(name=factory_name, delegate=user.id, address=address, hotline=factory_hotline) db.session.add(fac) db.session.commit() admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Factory" message_body = "There are new Factory!" click_action = "/AdminDashboard/factory" result = notf_service.notify_single_device(registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body) response_obj = { 'status': 'success', 'message': 'Successfully Signed up' } return response_obj, 201 except Exception as e: print('Exception in factory sign up:', e) response_obj = { 'status': 'failed', 'message': 'Something Wrong, please try again later' } return response_obj, 500
def post(self): try: data = request.form if current_user.isAdmin: factory_id = data['factory_id'] else: factory_id = Factory.query.filter_by( _delegate_id=current_user.id).first().id factory = Factory.query.get(factory_id) if not factory or factory.delegate_opj.account_status == -1: return redirect( url_for('orders_blueprint.route_error', error=f"No Factory exist with ID: {factory_id}")) from_latitude = data.get('from_lat') # دوائر العرض from_longitude = data.get('from_lng') # خطوط الطول to_latitude = data.get('to_lat') # دوائر العرض to_longitude = data.get('to_lng') # خطوط الطول pickup_location = data.get('pickup_location') dropoff_location = data.get('dropoff_location') num_of_trilla = data.get('trilla') num_of_maktura = data.get('maktura') num_of_cars = int(num_of_maktura) + int(num_of_trilla) order = Order(from_latitude=from_latitude, from_longitude=from_longitude, to_latitude=to_latitude, to_longitude=to_longitude, pickup_location=pickup_location, dropoff_location=dropoff_location, factory_id=factory_id, num_of_cars=num_of_cars) db.session.add(order) db.session.commit() for car_type in available_type: car = OrderCarsTypes(order_id=order.id, cars_num=data.get(car_type) or 0, car_type=car_type) db.session.add(car) db.session.commit() admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Order" message_body = "There are new order!" message_data = { 'factory_name': order.factory_object.name, 'notf_type': "new_order", "order_id": order.id, 'pickup_location_lat': order.from_latitude, 'pickup_location_lng': order.from_longitude, 'pickup_location_str': order.pickup_location, 'dropoff_location_lat': order.to_latitude, 'dropoff_location_lng': order.to_longitude, 'dropoff_location_str': order.dropoff_location } click_action = f"/AdminDashboard/OrderDetailsPage{order.id}" result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body, data_message=message_data) if current_user.isAdmin: return redirect(url_for('orders_blueprint.index')) else: return redirect( url_for('factoryOrders_blueprint.FactoryOrdersList')) except Exception as e: print("Exception in NewOrder: ", e) if current_user.isAdmin: return redirect( url_for( 'orders_blueprint.route_error', error="Some thing wrong happened please try again later" )) return redirect( url_for( 'factory_newOrder_blueprint.factory_route_error', error="Some thing wrong happened please try again later"))
def post(self): data = request.json order_id = data['order_id'] car_id = data['car_id'] driver_id = data['driver_id'] car = Car.query.get(car_id) if car._status == 1: response_obj = { 'status': "failed", 'message': "this car is busy, can't assign it!" } return response_obj, 400 order = Order.query.get(order_id) num_of_assigned_cars = OrderCarsAndDrivers.query.filter_by( order_id=order_id).count() if order.num_of_cars == num_of_assigned_cars: response_obj = { 'status': "failed", 'message': "all cars needed for this order is already assigned before!" } return response_obj, 400 cars_type_count = OrderCarsAndDrivers.query.filter_by( order_id=order_id).filter( OrderCarsAndDrivers.car_opj.has(_status=car._type)).count() needed_cars_type = OrderCarsTypes.query.filter( OrderCarsTypes.order_id == order_id, OrderCarsTypes._type == car._type).first().cars_num if cars_type_count == needed_cars_type: response_obj = { 'status': "failed", 'message': f"Order has enough cars of type: {car.car_type}" } return response_obj, 400 new = OrderCarsAndDrivers(order_id=order_id, car_id=car_id, driver_id=driver_id, company_id=car._owner) car.current_order_id = order_id car.status = 'busy' db.session.add(new) num_of_assigned_cars = OrderCarsAndDrivers.query.filter_by( order_id=order_id).count() if order.num_of_cars == num_of_assigned_cars: order.status += 1 driver = Driver.query.get(driver_id) driver.current_order_id = order_id db.session.commit() # Send notification to truck device device_token = car.user_obj.device_token message_title = "New Order" message_body = "You have new order, click to view details!" message_data = { 'factory_name': order.factory_object.name, 'notf_type': "new_order", "order_id": order.id, 'pickup_location_lat': order.from_latitude, 'pickup_location_lng': order.from_longitude, 'pickup_location_str': order.pickup_location, 'dropoff_location_lat': order.to_latitude, 'dropoff_location_lng': order.to_longitude, 'dropoff_location_str': order.dropoff_location } result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, message_body=message_body, data_message=message_data) return 'Car assigned successfully', 200
def post(self): data = request.form company_name = data.get('company_name') username = company_name # data.get('username') email = data.get('email') password = User.generate_pass() address = data.get('address') phone = data.get('company_phone') role = 1 user = User.query.filter_by(email=email).first() # img = request.files['company_logo'] if user: return redirect( url_for( 'base_blueprint.SignupCompany', error="FAILED: user with entered E-mail already exist!")) user = User.query.filter_by(username=username).first() if user: return redirect( url_for('base_blueprint.SignupCompany', error="FAILED: user with entered name already exist!")) user = User.query.filter_by(phone=phone).first() if user: return redirect( url_for( 'base_blueprint.SignupCompany', error="FAILED: user with entered phone already exist!")) user = User(username=username, email=email, role=role, password=password, phone=phone) db.session.add(user) com = Company.query.filter_by(name=company_name).first() if com: return redirect( url_for( 'base_blueprint.SignupCompany', error="FAILED: company with entered name already exist!")) com = Company.query.filter_by(address=address).first() if com: return redirect( url_for( 'base_blueprint.SignupCompany', error="FAILED: company with entered address already exist!" )) # _, file_extension = os.path.splitext(img.filename) # url = upload_file_to_s3(img, file_name=company_name + file_extension, folder='company_logo') com = Company(name=company_name, account=user.id, address=address) db.session.add(com) db.session.commit() message_title = "New Company" message_body = "There are New company signed up, check pending companies!" device_token = "fQQZG641vkY:APA91bH02cIkdvFru7j7n6zwZzitFqZLvrT-IPW6RLuQRJfdSjHRNzG-0HWxd3aL6FsBQMFmTl3X00GaB8NkcTyjQXTmBoaSk2KQJ2Qm2JYvaDdUXzOTomEPhoY_jzFcVILwDtMlUaSR" result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, message_body=message_body, click_action="/AdminDashboard/company") admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Company" message_body = "There are new Company!" click_action = "/AdminDashboard/company" result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body) return redirect( url_for( 'base_blueprint.login', message= "Successfully Signed up, waiting for Admin approve then you will " "receive Accepted E-mail from us"))
def post(self): data = request.json print(data) try: company_name = data.get('company_name') username = company_name # data.get('username') email = data.get('email') # TODO generate password, and send mail password = User.generate_pass( ) # 'company' # data.get('password') address = data.get('address') phone = data.get('phone') # img = request.files['company_logo'] role = 1 user = User.query.filter_by(email=email).first() if user: response_obj = { 'status': 'failed', 'message': 'user with entered E-mail already exist!' } return response_obj, 409 user = User.query.filter_by(username=username).first() if user: response_obj = { 'status': 'failed', 'message': 'user with entered name already exist!' } return response_obj, 409 user = User.query.filter_by(phone=phone).first() if user: response_obj = { 'status': 'failed', 'message': 'user with entered phone already exist!' } return response_obj, 409 user = User(username=username, email=email, role=role, password=password, phone=phone) db.session.add(user) com = Company.query.filter_by(name=company_name).first() if com: response_obj = { 'status': 'failed', 'message': 'company with entered name already exist!' } return response_obj, 409 com = Company.query.filter_by(address=address).first() if com: response_obj = { 'status': 'failed', 'message': 'company with entered address already exist!' } return response_obj, 409 # _, file_extension = os.path.splitext(img.filename) # url = upload_file_to_s3(img, file_name=company_name + file_extension, folder='company_logo') # com = Company(name=company_name, account=user.id, address=address, logo=url) com = Company(name=company_name, account=user.id, address=address) db.session.add(com) db.session.commit() admin_users = User.query.filter_by(role=3).all() for admin in admin_users: if admin.device_token: device_token = admin.device_token message_title = "New Company" message_body = "There are new Company!" click_action = "/AdminDashboard/company" result = notf_service.notify_single_device( registration_id=device_token, message_title=message_title, click_action=click_action, message_body=message_body) response_obj = { 'status': 'success', 'message': 'Successfully Signed up' } return response_obj, 201 except Exception as e: print('Exception in company sign up:', e) response_obj = { 'status': 'failed', 'message': 'Something Wrong, please try again later' } return response_obj, 500