2020-11-07 00:45:05 +01:00
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from telegram import Update, ParseMode
|
|
|
|
from telegram.ext import CallbackContext
|
|
|
|
|
2020-11-07 15:15:44 +01:00
|
|
|
import db
|
2020-11-07 14:41:31 +01:00
|
|
|
from callbackQuery.create import address_re
|
2020-11-07 00:45:05 +01:00
|
|
|
from callbackQuery.data import data
|
2020-11-07 15:15:44 +01:00
|
|
|
from main import local, messages
|
2020-11-07 00:45:05 +01:00
|
|
|
|
|
|
|
rex = {
|
|
|
|
"first_name": re.compile(r"^([a-zA-Z]| )+$"),
|
|
|
|
"last_name": re.compile(r"^([a-zA-Z]| )+$"),
|
2020-11-07 14:41:31 +01:00
|
|
|
"birth_date": re.compile(r"^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"),
|
2020-11-07 00:45:05 +01:00
|
|
|
"birth_city": re.compile(r"^[a-zA-Z]+(?:[\s-][a-zA-Z]+)*$"),
|
2020-11-07 14:41:31 +01:00
|
|
|
"address": address_re
|
2020-11-07 00:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def check_date(date) -> bool:
|
|
|
|
try:
|
|
|
|
datetime.strptime(date, "%d/%m/%Y")
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def edit(update: Update, context: CallbackContext, data_edit: str):
|
|
|
|
name = data_edit.replace("edit_", "")
|
|
|
|
if not rex[name].fullmatch(update.message.text) or\
|
|
|
|
(name == "birth_date" and not check_date(update.message.text)):
|
|
|
|
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Invalid value for `{local[name]}` \!",
|
|
|
|
parse_mode=ParseMode.MARKDOWN_V2)
|
|
|
|
else:
|
2020-11-07 15:15:44 +01:00
|
|
|
s = db.Session()
|
|
|
|
u = s.query(db.User).get(update["_effective_user"]["id"])
|
2020-11-07 00:45:05 +01:00
|
|
|
del messages[update.effective_chat.id][update["_effective_user"]["id"]]
|
2020-11-07 15:15:44 +01:00
|
|
|
if name == "birth_date":
|
|
|
|
setattr(u, name, datetime.strptime(update.message.text, "%d/%m/%Y").date())
|
|
|
|
else:
|
|
|
|
setattr(u, name, update.message.text)
|
|
|
|
s.add(u)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
2020-11-07 00:45:05 +01:00
|
|
|
data(update, context)
|