1818from transfers .util import read_csv , filter_to_valid_point_ids
1919from db import Thing , Contact , ThingContactAssociation , Email , Phone , Address
2020
21+ from schemas .contact import CreateContact
22+
2123
2224def extract_owner_role (comment ):
2325 # if comment is None:
@@ -32,6 +34,14 @@ def extract_owner_role(comment):
3234 return "Owner"
3335
3436
37+ """
38+ Developer's notes
39+
40+ Use Pydantic to perform model validations since all restrictions will
41+ be built into the models
42+ """
43+
44+
3545def transfer_contacts (session ):
3646
3747 odf = read_csv ("ownersdata.csv" )
@@ -41,96 +51,161 @@ def transfer_contacts(session):
4151 for i , row in odf .iterrows ():
4252 thing = session .query (Thing ).where (Thing .name == row .PointID ).first ()
4353 if thing is None :
44- print (f"Thing with PointID { row .PointID } not foaund . Skipping owner." )
54+ print (f"Thing with PointID { row .PointID } not found . Skipping owner." )
4555 continue
4656
4757 # TODO: extract role from OwnerComment
4858 # role = extract_owner_role(row.OwnerComment)
4959 role = "Owner"
60+ release_status = "private"
5061
5162 # TODO: put in guards for null values
52- # name OR organization must be defined, otherwise skip
53- if not (row .FirstName or row .LastName ) and not row .Company :
54- print (
55- f"Skipping first contact for PointID { row .PointID } due to missing name and organization."
56- )
57- else :
58- print (f"Transferring first contact for PointID { row .PointID } " )
59- contact1 = Contact (
60- name = f"{ row .FirstName } { row .LastName } " ,
61- role = role ,
62- contact_type = "Primary" ,
63- organization = row .Company , # assumes organization applies to both contacts
64- nma_pk_owners = row .OwnerKey ,
65- )
63+ try :
64+
65+ if row .FirstName is None and row .LastName is None :
66+ name = None
67+ elif row .FirstName is not None and row .LastName is None :
68+ name = row .FirstName
69+ elif row .FirstName is None and row .LastName is not None :
70+ name = row .LastName
71+ else :
72+ name = f"{ row .FirstName } { row .LastName } "
73+
74+ first_contact_data = {
75+ "thing_id" : thing .id ,
76+ "release_status" : release_status ,
77+ "name" : name ,
78+ "role" : role ,
79+ "contact_type" : "Primary" ,
80+ "organization" : row .Company ,
81+ "nma_pk_owners" : row .OwnerKey ,
82+ }
83+
84+ CreateContact .model_validate (first_contact_data )
85+
86+ first_contact_data .pop ("thing_id" )
87+ first_contact = Contact (** first_contact_data )
88+
6689 assoc = ThingContactAssociation ()
6790 assoc .thing = thing
68- assoc .contact = contact1
69- session .add (assoc )
70- session .add (contact1 )
91+ assoc .contact = first_contact
7192
7293 if row .Email :
73- contact1 .emails .append (Email (email = row .Email , email_type = "Primary" ))
94+ first_contact .emails .append (
95+ Email (
96+ email = row .Email ,
97+ email_type = "Primary" ,
98+ release_status = release_status ,
99+ )
100+ )
74101 if row .Phone :
75- contact1 .phones .append (
76- Phone (phone_number = row .Phone , phone_type = "Primary" )
102+ first_contact .phones .append (
103+ Phone (
104+ phone_number = row .Phone ,
105+ phone_type = "Primary" ,
106+ release_status = release_status ,
107+ )
77108 )
78109 if row .CellPhone :
79- contact1 .phones .append (
80- Phone (phone_number = row .CellPhone , phone_type = "Mobile" )
110+ first_contact .phones .append (
111+ Phone (
112+ phone_number = row .CellPhone ,
113+ phone_type = "Mobile" ,
114+ release_status = release_status ,
115+ )
81116 )
82117
83118 if row .MailingAddress :
84- contact1 .addresses .append (
119+ first_contact .addresses .append (
85120 Address (
86121 address_line_1 = row .MailingAddress ,
87122 city = row .MailCity ,
88123 state = row .MailState ,
89124 postal_code = row .MailZipCode ,
90125 address_type = "Mailing" ,
126+ release_status = release_status ,
91127 )
92128 )
93129
94- contact1 .addresses .append (
130+ first_contact .addresses .append (
95131 Address (
96132 address_line_1 = row .PhysicalAddress ,
97133 city = row .PhysicalCity ,
98134 state = row .PhysicalState ,
99135 postal_code = row .PhysicalZipCode ,
100136 address_type = "Physical" ,
137+ release_status = release_status ,
101138 )
102139 )
103140
104- # TODO: put in guards for null values
105- if not (row .SecondFirstName or row .SecondLastName ) and not row .Company :
141+ session .add (assoc )
142+ session .add (first_contact )
143+ session .commit ()
144+
145+ except Exception as e :
106146 print (
107- f"Skipping second contact for PointID { row .PointID } due to missing name and organization."
108- )
109- else :
110- print (f"Transferring second contact for PointID { row .PointID } " )
111- contact2 = Contact (
112- name = f"{ row .SecondFirstName } { row .SecondLastName } " ,
113- role = "Owner" ,
114- contact_type = "Secondary" ,
115- organization = row .Company , # Assumes organization applies to both contacts
116- nma_pk_owners = row .OwnerKey ,
147+ f"Skipping first contact for PointID { row .PointID } due to validation error: { e } "
117148 )
149+ from pprint import pprint
150+
151+ pprint (e )
152+ session .rollback ()
153+
154+ try :
155+ if row .SecondFirstName is None and row .SecondLastName is None :
156+ name = None
157+ elif row .SecondFirstName is not None and row .SecondLastName is None :
158+ name = row .SecondFirstName
159+ elif row .SecondFirstName is None and row .SecondLastName is not None :
160+ name = row .SecondLastName
161+ else :
162+ name = f"{ row .SecondFirstName } { row .SecondLastName } "
163+
164+ second_contact_data = {
165+ "thing_id" : thing .id ,
166+ "release_status" : release_status ,
167+ "name" : name ,
168+ "role" : "Owner" ,
169+ "contact_type" : "Secondary" ,
170+ "organization" : row .Company ,
171+ "nma_pk_owners" : row .OwnerKey ,
172+ }
173+
174+ CreateContact .model_validate (second_contact_data )
175+
176+ second_contact_data .pop ("thing_id" )
177+ second_contact = Contact (** second_contact_data )
178+
179+ assoc = ThingContactAssociation ()
180+ assoc .thing = thing
181+ assoc .contact = second_contact
182+
118183 if row .SecondCtctEmail :
119- contact2 .emails .append (
120- Email (email = row .SecondCtctEmail , email_type = "Primary" )
184+ second_contact .emails .append (
185+ Email (
186+ email = row .SecondCtctEmail ,
187+ email_type = "Primary" ,
188+ release_status = release_status ,
189+ )
121190 )
191+
122192 if row .SecondCtctPhone :
123- contact2 .phones .append (
124- Phone (phone_number = row .SecondCtctPhone , phone_type = "Primary" )
193+ second_contact .phones .append (
194+ Phone (
195+ phone_number = row .SecondCtctPhone ,
196+ phone_type = "Primary" ,
197+ release_status = release_status ,
198+ )
125199 )
126200
127- assoc = ThingContactAssociation ()
128- assoc .thing = thing
129- assoc .contact = contact2
130201 session .add (assoc )
131- session .add (contact2 )
202+ session .add (second_contact )
132203
133- session .commit ()
204+ except Exception as e :
205+ print (
206+ f"Skipping second contact for PointID { row .PointID } due to validation error: { e } "
207+ )
208+ session .rollback ()
134209
135210
136211# ============= EOF =============================================
0 commit comments