def post(): if request.is_json: json = request.get_json() else: return None, 300 if not validate_json_data(json): return None, 300 else: json['date'] = datetime.strptime(json['date'], '%Y-%m-%d %H:%M:%S%z') with get_session() as session: try: if session.query(Car).get(json['license_plate']) is None: car = Car(license_plate=json['license_plate'], chat_id=None) session.add(car) session.commit() accident = Accident(car_id=json['license_plate'], date_time=json['date'], temperature=json['temp'], fire=json['fire'], frontal=json['frontal'], tilt=json['tilt'], fall=json['fall'], lat=json['lat'], lng=json['lng'], reported=False) session.add(accident) session.commit() if session.query(Car).get(json['license_plate']).chat_id is not None: accident_data = {'car_id': json['license_plate'], 'date': json['date'], 'lat': json['lat'], 'lng': json['lng'], 'chat_id': accident.car.chat_id} accidentHandler.accident_message(accident_data) return None, 200 except Exception as e: logging.info(e) return None, 500
def delete_car(update: Update, context: CallbackContext): logging.info("Elimino auto") query = update.callback_query chat_id = update.effective_chat.id # recupero valori if query.data is not None: text = query.data else: text = update.message.text.upper() # controlli if len(text) > 7 or not re.match(pattern, text): logging.info(f"Targa {text} errata") update.message.reply_text("Formato targa errato \(Es\. AA111AA\).\nReinserisci \(o /cancel per terminare\):") return CHOOSE with get_session() as session: try: car = session.query(Car).get(text) if car is None or car.chat_id != chat_id: msg = f"Tra le tue auto non esite nessuna con targa *{text}*. (/lista per visualizzarle) " else: session.delete(car) session.commit() msg = f"❌ Auto con targa *{text}* eliminata correttamente" except Exception as e: logging.info(f"ERROR: {e}") msg = f"Errore durante l'eliminazione" context.bot.send_message(chat_id=chat_id, text=msg, parse_mode=ParseMode.MARKDOWN_V2) return ConversationHandler.END
def index(): with get_session() as session: try: accidents = session.query(Accident).all() for a in accidents: a.reported = False session.commit() except Exception as e: logging.info(f"ERROR: {e}") return render_template('index.html', google_api=google_api, development=Setting.DEBUG)
def get(): data = {} with get_session() as session: try: accidents = session.query(Accident).all() if len(accidents): data['accidents'] = [a.serialize_pos for a in accidents] return data, 200 else: return None, 204 except Exception as e: logging.info(e) return None, 300
def solved(accident_id): data = {} with get_session() as session: try: accident = session.query(Accident).get(accident_id) session.delete(accident) session.commit() logging.info(f"RIMUOVO INCIDENTE: {accident_id}") data['message'] = 'Accident resolved' return jsonify(data), 200 except Exception as e: data['message'] = e return jsonify(data), 300
def select_car(update: Update, context: CallbackContext) -> int: logging.info("Seleziona auto da eliminare") chat_id = update.effective_chat.id with get_session() as session: cars = session.query(Car).filter_by(chat_id=chat_id).all() if len(cars) == 0: msg = "Non hai memorizzato nessuna auto" context.bot.send_message(chat_id=chat_id, text=msg, parse_mode=ParseMode.MARKDOWN_V2) else: msg = "Scegli la targa dell'auto che vuoi eliminare \(o /cancel per terminare\):" keyboard = [[InlineKeyboardButton(c.license_plate, callback_data=c.license_plate) for c in cars]] reply_markup = InlineKeyboardMarkup(keyboard) context.bot.send_message(chat_id=chat_id, text=msg, parse_mode=ParseMode.MARKDOWN_V2, reply_markup=reply_markup) return CHOOSE
def refresh(accident_id): data = {} with get_session() as session: try: accident = session.query(Accident).get(accident_id) if accident is not None: data['data'] = accident.serialize return jsonify(data), 200 else: data[ 'message'] = "Ops! You are accessing something that does not exist" return jsonify(data), 300 except Exception as e: logging.info(data) data['message'] = e return jsonify(data), 300
def get_car(update: Update, context: CallbackContext): chat_id = update.effective_chat.id logging.info(f'Lista auto per : {chat_id}') with get_session() as session: try: cars = session.query(Car).filter_by(chat_id=chat_id).all() if not len(cars): msg = "Non hai memorizzato nessuna auto" else: msg = "Ecco la lista delle tue auto 🚗: \n" for idx, c in enumerate(cars): msg += f"{idx+1}\. *{c.license_plate}*\n" except Exception as e: logging.info(f"ERROR: {e}") msg = f"Errore\! Riprova più tardi" context.bot.send_message(chat_id=update.effective_chat.id, text=msg, parse_mode=ParseMode.MARKDOWN_V2)
def update(): data = {} with get_session() as session: try: accidents = session.query(Accident).filter_by(reported=False).all() if len(accidents): data['accidents'] = [a.serialize for a in accidents] for a in accidents: a.reported = True session.commit() return jsonify(data), 200 else: return {}, 204 except Exception as e: logging.info(data) data['message'] = e return jsonify(data), 300
def read_license_plate(update: Update, context: CallbackContext) -> int: # recupero il valore text = update.message.text text = text.upper() # controlli if len(text) > 7 or not re.match(pattern, text): logging.info(f"Targa {text} errata") msg = "Formato targa errato \(Es\. AA111AA\)\.\nReinserisci \(o /cancel per terminare\):" context.bot.send_message(chat_id=update.effective_chat.id, text=msg, parse_mode=ParseMode.MARKDOWN_V2) return TOTAL else: chat_id = update.effective_chat.id with get_session() as session: try: car = session.query(Car).get(text) if car is None: car = Car(license_plate=text, chat_id=chat_id) session.add(car) session.commit() msg = f"🚗 Auto con targa *{text}* aggiunta correttamente" val = ConversationHandler.END else: if car.chat_id is not None: msg = f"*ERRORE* 🚗 *{text}* già registrata \!" val = TOTAL else: car.chat_id = chat_id session.commit() msg = f"🚗 Auto con targa *{text}* aggiunta correttamente" val = ConversationHandler.END except Exception as e: logging.info(f"ERROR: {e}") msg = f"Errore durante inserimento" val = TOTAL context.bot.send_message(chat_id=chat_id, text=msg, parse_mode=ParseMode.MARKDOWN_V2) return val