Files
appointment_tool/db/DbManager.py
T

141 lines
5.3 KiB
Python

import datetime
import firebase_admin
import xlsxwriter as xlsxwriter
from firebase_admin import credentials, firestore
import definitions
import params
from pojo.MailPojo import MailPojo
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
from pojo.SimInfoPojo import SimInfoPojo
from pojo.contact_pojo import ContactPojo
from utils.excel_reader import ExcelHelper
from utils.operator import Operator
ERROR_COLLECTION_NAME = "error_items"
CONTACT_COLLECTION_NAME = "contact_list"
MAIL_COLLECTION_NAME = "mail_list"
SIM_INFOS = "sim_infos"
TIMEOUT = "timeout_items"
excel_reader = ExcelHelper()
class DataManager:
batch_size = 20
def __init__(self):
cred = credentials.Certificate(definitions.ROOT_DIR + "/appointment.json")
self._app = firebase_admin.initialize_app(cred)
self._db = firestore.client()
self._contact_list = excel_reader.read_contacts("/contact_all.xlsx")
def get_all_error_items(self):
pass
def get_all_sim_infos(self):
sim_info_collection = self._db.collection(SIM_INFOS)
return sim_info_collection
def get_all_successful_items(self):
return self.get_all_successful_items_for_day(str(datetime.date.today()))
def get_all_successful_items_for_day(self, day):
doc_ref = self._db.collection(day)
return doc_ref
def save_sim_info(self, sim_info: SimInfoPojo):
doc_ref = self._db.collection(SIM_INFOS).document(sim_info.phone)
doc_ref.set(sim_info.to_firestore_dict())
def save(self, result: ReserveResultPojo):
if result.type == PublishType.SUCCESS:
# get id
id = result.url.split("/")[-1]
result.id = id
document_name = str(datetime.date.today())
doc_ref = self._db.collection(document_name).document(result.id)
doc_ref.set(result.to_firestore_dict())
else:
if len(result.phone) > 0:
doc_ref = self._db.collection(ERROR_COLLECTION_NAME).document(result.phone)
doc_ref.set(result.to_firestore_dict())
def save_timeout_contact(self, contact: ContactPojo):
doc_ref = self._db.collection(TIMEOUT).document(str(contact.phone))
doc_ref.set(contact.to_firestore_dict())
def _delete_collection(self, coll_ref, batch_size):
docs = coll_ref.limit(batch_size).stream()
deleted = 0
for doc in docs:
print(f'Deleting doc {doc.id} => {doc.to_dict()}')
doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return self._delete_collection(coll_ref, batch_size)
def clear_all_sim_info(self):
coll_ref = self._db.collection(SIM_INFOS)
self._delete_collection(coll_ref, self.batch_size)
def save_to_excel(self):
# Start from the first cell. Rows and columns are zero indexed.
sim_info_list = []
for sim in self.get_all_sim_infos().stream():
print(sim)
sim_pojo = SimInfoPojo.from_firestore_dict(sim.to_dict())
contact_list_found = [contact for contact in self._contact_list if
contact.ccid.replace("F", "") == sim_pojo.ccid.replace("F", "")]
if len(contact_list_found) > 0:
sim_pojo.email = contact_list_found[0].mail
sim_pojo.name = contact_list_found[0].first_name + " " + contact_list_found[0].last_name
sim_pojo.passport = contact_list_found[0].passport
sim_info_list.append(sim_pojo)
row = 0
col = 0
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('sim_infos.xlsx')
worksheet = workbook.add_worksheet()
for info in sim_info_list:
# Iterate over the data and write it out row by row.
if info.operator == Operator.LYCAMOBILE.value:
worksheet.write(row, col, info.phone[2:len(info.phone)])
else:
worksheet.write(row, col, info.phone)
worksheet.write(row, col + 1, info.ccid)
worksheet.write(row, col + 2, info.name)
worksheet.write(row, col + 3, info.passport)
worksheet.write(row, col + 4, info.email)
worksheet.write(row, col + 5, info.position)
row += 1
workbook.close()
def upload_contact_list_to_cloud(self):
contacts = excel_reader.read_contacts()
collections = self._db.collection(CONTACT_COLLECTION_NAME)
for contact in contacts:
new_contact = collections.document(contact.passport)
new_contact.set(contact.to_firestore_dict())
def read_contacts_from_db(self) -> list:
contact_collection = self._db.collection(CONTACT_COLLECTION_NAME)
return contact_collection
def get_mail_list(self) -> list:
mail_collection = self._db.collection(MAIL_COLLECTION_NAME)
mail_list = []
for mail in mail_collection.stream():
mail_pojo = MailPojo.from_firestore_dict(mail.to_dict())
mail_list.append(mail_pojo)
return mail_list
if __name__ == '__main__':
# params.firebase_store_manager.upload_contact_list_to_cloud()
params.firebase_store_manager.save_to_excel()
# params.firebase_store_manager.clear_all_sim_info()
# print(params.firebase_store_manager.get_mail_list())