def end_appointment(item_id): db.inicialize() item = db.get(item_id) start_date = datetime.strptime(item["start_date"], '%Y-%m-%d %H:%M:%S') end_date = datetime.now() timedelta = end_date - start_date hours = (timedelta.seconds % 3600) + 1 price = hours * PRICE_BY_HOUR item["end_date"] = end_date.strftime("%Y-%m-%d %H:%M:%S") item["price"] = price if (db.save(item)): payload = { "id": item_id, "start_date": item["start_date"], "physician_id": item["physician_id"], "patient_id": item["patient_id"], "end_date": item["end_date"], "price": item["price"], } financial_payload = json.dumps({ "appointment_id": item_id, # id da consulta "total_price": item["price"], }) # register a RabbitMQ message credentials = pika.PlainCredentials('guest', 'guest') parameters = pika.ConnectionParameters('rabbit', 5672, '/', credentials) connection = pika.BlockingConnection(parameters) channel = connection.channel() channel.queue_declare(queue='financial_queue', durable=True) channel.basic_publish( exchange='', routing_key='financial_queue', body=financial_payload, properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) connection.close() return payload else: return False
def rep(): #Get delivery time from config config.read("app/config.ini") del_time = datetime.timedelta(days=int(config.get("Settings", "del_time"))) today = datetime.datetime.now().date() ordered_stock = Database.get("Stock") stock_out = [] for stock in ordered_stock: if stock[3] == None and stock[3] is not None: #Retrieve stock items that are out of stock stock_out.append(stock) for stock in stock_out: #If the delivery time has passed set the stock level as healthy print(stock[4]) if stock[4] - today >= del_time: Database.update("Stock", "stock_healthy", 1, "stock_healthy", "Null")
class DataStore(Resource): def __init__(self): self.database = Database() def get(self): result = {} http_status = 200 result['data'] = self.database.get(g.connection) response = make_response(jsonify(result), http_status) return response def post(self): result = {} http_status = 200 required_parameters = ['name'] parameters = request.form file_required_parameters = ['file'] file = request.files result = self.parameter_validation(parameters, required_parameters) if result['status'] == 0: result = self.parameter_validation(file, file_required_parameters) if result['status'] == 0: save_file_path = join(app.config['UPLOAD_FOLDER'], file['file'].filename) if self.save_file(file['file'], save_file_path): data = [parameters.get('name'), save_file_path] self.database.add(g.connection, data) result['status'] = 0 result['result'] = 'File uploaded successfully' else: http_status = 500 result['status'] = -1 result['error'] = 'File uploaded error' else: http_status = 400 else: http_status = 400 response = make_response(jsonify(result), http_status) return response def parameter_validation(self, parameters, required_parameters): response = {'status': 0, 'error': ''} for required_parameter in required_parameters: if required_parameter not in parameters: response['status'] = -1 # response['error'] = '' response['error'] += required_parameter + ', ' if response['error'] is not '': response['error'] = response['error'][0:-2] response['error'] = 'Required Field(s): ' + response['error'] else: del response['error'] return response def save_file(self, file, save_file_path): try: with open(save_file_path, 'ab') as f: f.write(file.stream.read()) return True except Exception as e: print('File Upload Error', e) return False