Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
teleexit/message/edit.py
2020-11-07 15:15:44 +01:00

47 lines
1.5 KiB
Python

import re
from datetime import datetime
from telegram import Update, ParseMode
from telegram.ext import CallbackContext
import db
from callbackQuery.create import address_re
from callbackQuery.data import data
from main import local, messages
rex = {
"first_name": re.compile(r"^([a-zA-Z]| )+$"),
"last_name": re.compile(r"^([a-zA-Z]| )+$"),
"birth_date": re.compile(r"^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"),
"birth_city": re.compile(r"^[a-zA-Z]+(?:[\s-][a-zA-Z]+)*$"),
"address": address_re
}
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:
s = db.Session()
u = s.query(db.User).get(update["_effective_user"]["id"])
del messages[update.effective_chat.id][update["_effective_user"]["id"]]
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()
data(update, context)