add AddressModel, AddressDAO class
This commit is contained in:
34
lib/DB/AddressDAO.py
Normal file
34
lib/DB/AddressDAO.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from .DbManager import DbManager
|
||||
import mariadb
|
||||
import json
|
||||
|
||||
|
||||
class AddressDAO:
|
||||
def __init__(self):
|
||||
self.__con = DbManager().getConnection()
|
||||
self.__cur = self.__con.cursor()
|
||||
|
||||
|
||||
def __importPlz(self):
|
||||
with open("import json filepath here", "r") as plz:
|
||||
postcodes = json.load(plz)
|
||||
try:
|
||||
for i in postcodes:
|
||||
test =i["plz_name"].split(",")
|
||||
for town in test:
|
||||
if "u.a" in town:
|
||||
town = town[:-4]
|
||||
town = town.strip()
|
||||
if town:
|
||||
print(f"PROCESSING {i['name']} {town}")
|
||||
self.__cur.callproc("addZipCodes", (i["name"], town,))
|
||||
except mariadb.OperationalError as e:
|
||||
print(f"Database Error: {e}")
|
||||
finally:
|
||||
self.__con.commit()
|
||||
print("FINISHED")#
|
||||
|
||||
def getAddressData(self, all = True, zipcode = None):
|
||||
self.__cur.callproc("getAddress", (all, zipcode,))
|
||||
self.__data = self.__cur.fetchall()
|
||||
return self.__data
|
||||
38
lib/DB/AddressModel.py
Normal file
38
lib/DB/AddressModel.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from PySide6.QtCore import QAbstractListModel, Qt, Slot, QModelIndex
|
||||
from .AddressDAO import AddressDAO
|
||||
|
||||
|
||||
class AddressModel(QAbstractListModel):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.__address_data = AddressDAO().getAddressData()
|
||||
|
||||
|
||||
|
||||
def rowCount(self, parent = QModelIndex()):
|
||||
return 10
|
||||
|
||||
|
||||
def data(self, index, role = Qt.DisplayRole):
|
||||
row = index.row()
|
||||
|
||||
if role == Qt.DisplayRole:
|
||||
|
||||
data= self.__address_data[row][2]
|
||||
|
||||
print(type(data))
|
||||
return data
|
||||
|
||||
|
||||
|
||||
@Slot(bool, str)
|
||||
def getAddresses(self, all, zipcode):
|
||||
data = AddressDAO().getAddressData(all, zipcode)
|
||||
return data
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .DbManager import DbManager
|
||||
import json
|
||||
import mariadb
|
||||
import time
|
||||
|
||||
|
||||
class BusinessDAO:
|
||||
def __init__(self):
|
||||
@@ -23,34 +23,6 @@ class BusinessDAO:
|
||||
print(str(e))
|
||||
|
||||
|
||||
def addPlz(self):
|
||||
count = 1
|
||||
with open("/home/dstoppek/Downloads/georef-germany-postleitzahl.json", "r") as plz:
|
||||
postcodes = json.load(plz)
|
||||
try:
|
||||
for i in postcodes:
|
||||
test =i["plz_name"].split(",")
|
||||
for town in test:
|
||||
|
||||
if "u.a" in town:
|
||||
town = town[:-4]
|
||||
town = town.strip()
|
||||
if town:
|
||||
|
||||
|
||||
print(f"PROCESSING {i['name']} {town}")
|
||||
self.__cur.callproc("addZipCodes", (i["name"], town,))
|
||||
|
||||
|
||||
#time.sleep(1)
|
||||
# self.__cur.execute(f"INSERT INTO postcode(postcode, addressid) VALUE ({i["name"]}, {count})")
|
||||
# count += 1
|
||||
|
||||
except mariadb.OperationalError as e:
|
||||
print(f"Database Error: {e}")
|
||||
finally:
|
||||
self.__con.commit()
|
||||
print("FINISHED")
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user