Merge branch 'master' into feature/check_results

# Conflicts:
#	src/db/mongo_manager.py
This commit is contained in:
Lei PAN
2022-07-06 17:42:20 +02:00
45 changed files with 160 additions and 109 deletions
+102
View File
@@ -0,0 +1,102 @@
import datetime
import logging
from pymongo import MongoClient
from src.pojo.ReserveResultPojo import ReserveResultPojo
from src.pojo.black_contact import BlackContactPojo
from src.pojo.contact_pojo import ContactPojo
MONGO_DB_URL = "mongo.lpaconsulting.fr"
CAPTCHA_ERROR_COLLECTION_PREFIX = "CAPTCHA_ERROR_"
BLACK_LIST = "BLACK_LIST"
class MongoDbManager:
def __init__(self):
client = MongoClient(MONGO_DB_URL, username='appointment', password='Rdv@2022', authSource='appointment')
self.db = client.appointment
self.logger = logging.getLogger("mongoDb")
def insert_one(self, collection_name: str, dict: dict):
collection_to_use = self.db[collection_name]
collection_to_use.insert_one(dict)
def insert_reserve_result(self, collection_name, reserve: ReserveResultPojo):
try:
collection_to_use = self.db[collection_name]
collection_to_use.replace_one(filter={'_id': reserve.id, }, replacement=reserve.to_firestore_dict(),
upsert=True)
except Exception as Error:
self.logger.info(Error)
def insert_captcha_error_contact(self, contact: ContactPojo):
day = str(datetime.date.today())
collection_name = CAPTCHA_ERROR_COLLECTION_PREFIX + day
try:
collection_to_use = self.db[collection_name]
collection_to_use.replace_one(filter={'_id': contact.mail, }, replacement=contact.to_firestore_dict(),
upsert=True)
except Exception as error:
self.logger.info(error)
def insert_blacklist_contact(self, contact: ContactPojo):
collection_name = BLACK_LIST
black_contact = BlackContactPojo(contact.phone, contact.passport, contact.last_name, contact.first_name,
contact.mail)
try:
collection_to_use = self.db[collection_name]
collection_to_use.replace_one(filter={'_id': black_contact.mail, },
replacement=black_contact.to_firestore_dict(),
upsert=True)
except Exception as error:
self.logger.info(error)
def get_all_successful_items_for_day(self) -> list:
params.oracle_log_sender.send_read_db_event("get_all_successful_items_for_day")
collection_name = str(datetime.date.today())
result_list = []
cursor = self.db[collection_name]
for document in cursor.find():
result_list.append(ReserveResultPojo.from_firestore_dict(document))
return result_list
def get_all_successful_items_for_one_day(self, day_in_str: str) -> list:
params.oracle_log_sender.send_read_db_event("get_all_successful_items_for_one_day")
collection_name = day_in_str
result_list = []
cursor = self.db[collection_name]
for document in cursor.find():
result_list.append(ReserveResultPojo.from_firestore_dict(document))
return result_list
def update_reserve_result(self, reserve_id: str, result: ResultEnum):
collection_name = str(datetime.date.today())
collection = self.db[collection_name]
collection.find_one_and_update({'_id': reserve_id}, {"$set": {"accepted": result.name}}, upsert=False)
def get_captcha_error_contacts_for_current_day(self) -> list:
day = str(datetime.date.today())
collection_name = CAPTCHA_ERROR_COLLECTION_PREFIX + day
cursor = self.db[collection_name]
contact_list = []
for document in cursor.find():
contact_list.append(ContactPojo.from_firestore_dict(document))
return contact_list
def delete_captcha_error_contact_for_current_day(self, contact: ContactPojo):
day = str(datetime.date.today())
collection_name = CAPTCHA_ERROR_COLLECTION_PREFIX + day
collection = self.db[collection_name]
to_delete = {'_id': contact.mail}
try:
collection.delete_one(to_delete)
except Exception as error:
self.logger.info(error)
if __name__ == '__main__':
db_manager = MongoDbManager()
contact = ContactPojo(phone_number='0755667750', passport_number='123456789', last_name='PAN', first_name='Lei',
mail='panleicim@gmail.com')
print(db_manager.insert_blacklist_contact(contact))