try to use firestore to save contact list
This commit is contained in:
+31
-1
@@ -7,8 +7,10 @@ import params
|
||||
from pojo.ReserveResultPojo import ReserveResultPojo, PublishType
|
||||
from pojo.SimInfoPojo import SimInfoPojo
|
||||
from pojo.contact_pojo import ContactPojo
|
||||
from utils.excel_reader import ExcelHelper
|
||||
|
||||
ERROR_COLLECTION_NAME = "error_items"
|
||||
CONTACT_COLLECTION_NAME = "contact_list"
|
||||
SIM_INFOS = "sim_infos"
|
||||
TIMEOUT = "timeout_items"
|
||||
|
||||
@@ -20,6 +22,11 @@ class DataManager:
|
||||
cred = credentials.Certificate("appointment.json")
|
||||
self._app = firebase_admin.initialize_app(cred)
|
||||
self._db = firestore.client()
|
||||
contact_collection = self._db.collection(CONTACT_COLLECTION_NAME)
|
||||
self._contact_list = []
|
||||
for contact in contact_collection.stream():
|
||||
contact_pojo = ContactPojo.from_firestore_dict(contact.to_dict())
|
||||
self._contact_list.append(contact_pojo)
|
||||
|
||||
def get_all_error_items(self):
|
||||
pass
|
||||
@@ -35,6 +42,16 @@ class DataManager:
|
||||
def save_sim_info(self, simInfoPojo: SimInfoPojo):
|
||||
doc_ref = self._db.collection(SIM_INFOS).document(simInfoPojo.phone)
|
||||
doc_ref.set(simInfoPojo.to_firestore_dict())
|
||||
contact_found = [contact for contact in self._contact_list if contact.position == simInfoPojo.position]
|
||||
if len(contact_found) > 0:
|
||||
# link the phone number with contact
|
||||
contact = contact_found[0]
|
||||
phone = simInfoPojo.phone[2:len(simInfoPojo.phone)]
|
||||
ccid = simInfoPojo.ccid
|
||||
self._db.collection(CONTACT_COLLECTION_NAME).document(contact.passport).update(
|
||||
{'phone': phone, 'ccid': ccid})
|
||||
else:
|
||||
print("error, contact not found")
|
||||
|
||||
def save(self, result: ReserveResultPojo):
|
||||
if result.type == PublishType.SUCCESS:
|
||||
@@ -89,7 +106,20 @@ class DataManager:
|
||||
row += 1
|
||||
workbook.close()
|
||||
|
||||
def upload_contact_list_to_cloud(self):
|
||||
excel_reader = ExcelHelper()
|
||||
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
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
params.firebase_store_manager.save_to_excel()
|
||||
params.firebase_store_manager.upload_contact_list_to_cloud()
|
||||
# params.firebase_store_manager.save_to_excel()
|
||||
# params.firebase_store_manager.clear_all_sim_info()
|
||||
|
||||
Reference in New Issue
Block a user