def convertToClasses(self): """ This Function converts all instance variables of the Agency Object that are meant to be classes...(i.e agency_banner) into their respective class. (These includes the Contact Details and Files Classes) """ #Check to see if the instance has its agency_id set or has already had its variables converted into classes. try: if self.agency_id is None or self.classesConverted == True: raise Exception( "Cannot convert classes due to them already being converted or Agency Instance having a Null Agency ID" ) else: agency_banner = File(FILE_TYPE_Images, OBJECT_Agency, self.agency_id, None, "agency_banner") agency_banner.addImageDetails(None, None, self.agency_banner) self.agency_banner = agency_banner self.agency_website = ContactDetails('agency_website', OBJECT_Agency, self.agency_id, self.agency_website) agency_logo_standard = File(FILE_TYPE_Images, OBJECT_Agency, self.agency_id, None, "agency_logo_standard") agency_logo_standard.addImageDetails(None, None, self.agency_logo_standard) self.agency_logo_standard = agency_logo_standard self.contact_details_converted = [] for contactType, detailsType in self.contact_details.items(): #For now, only store dictionaries. We can handle exceptions by logging them to a debug file. if isinstance(detailsType, dict): for contact, details in detailsType.items(): if details != '': self.contact_details_converted.append( ContactDetails( 'agency_' + contactType + '_' + contact, OBJECT_Agency, self.agency_id, details)) #else: # self.contact_details_converted.append( ContactDetails( 'agency_' + contactType + '_' + contact, self.agency_id, OBJECT_Agency, details ) ) self.classesConverted = True except (Exception) as error: print(error)
bourne_identity = Movie('The Bourne Identity', date(2002, 10, 11)) furious_7 = Movie('Furious 7', date(2015, 4, 2)) pain_and_gain = Movie('Pain & Gain', date(2013, 8, 23)) # 5 - creates actors matt_damon = Actor('Matt Damon', date(1970, 10, 8)) dwayne_johnson = Actor('Dwayne Johnson', date(1972, 5, 2)) mark_wahlberg = Actor('Mark Wahlberg', date(1971, 6, 5)) # 6 - add actors to movies bourne_identity.actors = [matt_damon] furious_7.actors = [dwayne_johnson] pain_and_gain.actors = [dwayne_johnson, mark_wahlberg] # 7 - add contact details to actors matt_contact = ContactDetails('415 555 2671', 'Burbank, CA', matt_damon) dwayne_contact = ContactDetails('423 555 5623', 'Glendale, CA', dwayne_johnson) dwayne_contact_2 = ContactDetails('421 444 2323', 'West Hollywood, CA', dwayne_johnson) mark_contact = ContactDetails('421 333 9428', 'Glendale, CA', mark_wahlberg) # 8 - create stuntmen matt_stuntman = Stuntman('John Doe', True, matt_damon) dwayne_stuntman = Stuntman('John Roe', True, dwayne_johnson) mark_stuntman = Stuntman('Richard Roe', True, mark_wahlberg) # 9 - persists data session.add(bourne_identity) session.add(furious_7) session.add(pain_and_gain)
bourne_identity = Movie('The Bourne Identity', date(2002, 10, 11)) furious_7 = Movie('Furious 7', date(2015, 4, 2)) pain_and_gain = Movie('Pain & Gain', date(2013, 8, 23)) # Actors matt_damon = Actor('Matt Damon', date(1970, 10, 8)) dwayne_johnson = Actor('Dwayne Johnson', date(1972, 5, 2)) mark_wahlberg = Actor('Mark Wahlberg', date(1971, 6, 5)) # Add actors to movies bourne_identity.actors = [matt_damon] furious_7.actors = [dwayne_johnson] pain_and_gain.actors = [dwayne_johnson, mark_wahlberg] # Add contacts matt_contact = ContactDetails("415 555 2671", "Burbank, CA", matt_damon) dwayne_contact = ContactDetails("423 555 5623", "Glendale, CA", dwayne_johnson) dwayne_contact_2 = ContactDetails("421 444 2323", "West Hollywood, CA", dwayne_johnson) mark_contact = ContactDetails("421 333 9428", "Glendale, CA", mark_wahlberg) # Create Stuntmen matt_stuntman = Stuntman("John Doe", True, matt_damon) dwayne_stuntman = Stuntman("John Roe", True, dwayne_johnson) mark_stuntman = Stuntman("Richard Roe", True, mark_wahlberg) # Persist data session.add(bourne_identity) session.add(furious_7) session.add(pain_and_gain) session.add(matt_contact)
#create actors actor_1 = Actor("Actor 1 Name", date(1950, 8, 28)) actor_2 = Actor("Actor 2 Name", date(1960, 8, 22)) actor_3 = Actor("Actor 3 Name", date(1970, 1, 12)) actor_4 = Actor("Actor 4 Name", date(1980, 8, 8)) actor_5 = Actor("Actor 5 Name", date(1990, 12, 1)) #add actors to movies movie_1.actors = [actor_1] movie_2.actors = [actor_2] movie_3.actors = [actor_3] movie_4.actors = [actor_4] movie_5.actors = [actor_5, actor_1] #add contact details to actors actor_1_contact = ContactDetails("12 1234 1234", "Address 1", actor_1) actor_2_contact = ContactDetails("12 1234 1234", "Address 2", actor_2) actor_3_contact = ContactDetails("12 1234 1234", "Address 3", actor_3) actor_4_contact = ContactDetails("12 1234 1234", "Address 4", actor_4) actor_5_contact = ContactDetails("12 1234 1234", "Address 5", actor_5) #create stuntmen actor_1_stuntman = Stuntman("Stuntman 1", True, actor_1) actor_2_stuntman = Stuntman("Stuntman 2", True, actor_2) actor_3_stuntman = Stuntman("Stuntman 3", True, actor_3) actor_4_stuntman = Stuntman("Stuntman 4", True, actor_4) actor_5_stuntman = Stuntman("Stuntman 5", True, actor_5) #persist data #SQLAlchemy uses the save-update cascade strategy #movie
def storeAgent(self, commit): try: self.agent_id = returnNextSerialID('agents', 'agent_id') #Stores information contained inside the Agent Object into the Agents table. cur.execute( """INSERT INTO agents( domain_agent_id, entered_when, first_name, last_name, profile_text ) VALUES( %s, current_timestamp, %s, %s, %s )""", (self.domain_agent_id, cleanForSQL(self.first_name), cleanForSQL(self.last_name), self.profile_text)) #Store the link between the Agent and the Agency inside the agencies_agent table. cur.execute( """INSERT INTO agencies_agent( agency_id, agent_id, entered_when ) VALUES( %s, %s, current_timestamp)""", (self.agency_id, self.agent_id)) if self.email is not None: #Store the agent's emaia contactDetails_email = ContactDetails("agent_email", OBJECT_Agent, self.agent_id, self.email) if not contactDetails_email.storeContactDetails(False): raise Exception(psycopg2.DatabaseError) if self.phone is not None: #Store the agent's phone number contactDetails_phone = ContactDetails("agent_phone_number", OBJECT_Agent, self.agent_id, self.phone) if not contactDetails_phone.storeContactDetails(False): raise Exception(psycopg2.DatabaseError) if self.facebook_url is not None: contactDetails_facebook = ContactDetails( "agent_facebook_url", OBJECT_Agent, self.agent_id, self.facebook_url) #Attempt to save the facebook details if not contactDetails_facebook.storeContactDetails(False): raise Exception(psycopg2.DatabaseError) if self.twitter_url is not None: contactDetails_twitter = ContactDetails( "agent_twitter_url", OBJECT_Agent, self.agent_id, self.twitter_url) #Attempt to save the twitter details if not contactDetails_twitter.storeContactDetails(False): raise Exception(psycopg2.DatabaseError) if self.photo is not None: #Save the mugshot and agent photo. file_agentPhoto = File(FILE_TYPE_Images, OBJECT_Agent, self.agent_id, None, "agent_photo") file_agentPhoto.addImageDetails(None, None, self.photo) if not file_agentPhoto.storeFile(False): raise Exception(psycopg2.DatabaseError) if self.mugshot_url is not None: file_agentMugShot = File(FILE_TYPE_Images, OBJECT_Agent, self.agent_id, None, "agent_mugshot") file_agentMugShot.addImageDetails(None, None, self.mugshot_url) if not file_agentMugShot.storeFile(False): raise Exception(psycopg2.DatabaseError) if commit: conn.commit() return self.agent_id except (Exception, psycopg2.DatabaseError) as error: print("Error in INSERTING New Agent " + self.first_name + " " + self.last_name + "\n" + error) return None
session = Session() #create movies bourne_identity = Movie('The Bourne Identity', date(2002, 10, 11)) furious_7 = Movie('Furious 7', date(2001, 10, 2)) # create actors matt_damon = Actor('Matt Damon', date(1970, 10, 8)) dwayne_johnson = Actor('Dwayne Johnson', date(1972, 10, 10)) #adding actors to movie bourne_identity.actors = [matt_damon] furious_7.actors = [dwayne_johnson, matt_damon] matt_contact = ContactDetails('23-12323', 'kathmandu', matt_damon) dwayne_contact = ContactDetails('234-125545', 'lalitpur', dwayne_johnson) dwayne_contact2 = ContactDetails('234-125545', 'chabahil', dwayne_johnson) matt_stuntman = StuntMan('John Doe', True, matt_damon) dwayne_stuntman = StuntMan('John Roe', True, dwayne_johnson) session.add(bourne_identity) session.add(furious_7) session.add(matt_contact) session.add(dwayne_contact) session.add(dwayne_contact2) session.add(matt_stuntman) session.add(dwayne_stuntman)
# Create actors skim_phew = Actor("Skim Phew", date(2000, 6, 9)) stop_theft = Actor("Stop Theft", date(1999, 10, 10)) short_face = Actor("Short Face", date(2004, 2, 29)) longer_nose = Actor("Longer Nose", date(1999, 4, 20)) # Add actors to movies born_funny.actors = [skim_phew] catchy.actors = [short_face] making_soda.actors = [short_face, longer_nose, stop_theft] # Add contact details to actors skim_contact = ContactDetails("29 29 29", "Front Street 3", skim_phew) stop_contact = ContactDetails("42 40 65 44", "Cool Desert 96", stop_theft) short_contact = ContactDetails("47 85 03 64", "Empty Waters 32", short_face) longer_contact = ContactDetails("84 43 58 00", "Empty Waters 32", longer_nose) stop_contact1 = ContactDetails("92 84 83 54", "Hidden Houses 54", stop_theft) # Create stuntman skim_stunt = Stuntman("Jumping Tree", True, skim_phew) stop_stunt = Stuntman("Fallen Roof", True, stop_theft) long_stunt = Stuntman("Biscuit Man", True, longer_nose) short_stunt = Stuntman("Walking Slipper", True, short_face) # Persist data session.add(born_funny)
bourne_identity = Movie("The Bourne Identity", date(2002, 10, 11)) furious_7 = Movie("Furious 7", date(2015, 4, 2)) pain_and_gain = Movie("Pain & Gain", date(2013, 8, 23)) # create actors matt_damon = Actor("Matt Damon", date(1970, 10, 8)) dwayne_johnson = Actor("Dwayne Johnson", date(1972, 5, 2)) mark_wahlberg = Actor("Mark Wahlberg", date(1971, 6, 5)) # add actors to movies bourne_identity.actors = [matt_damon] furious_7.actors = [dwayne_johnson] pain_and_gain.actors = [mark_wahlberg] # add contact details to actors matt_contact = ContactDetails("415 555 2543", "Burbank, CA", matt_damon) dwayne_contact = ContactDetails("234 23 2343", "Glendale, CA", dwayne_johnson) dwayne_contact_2 = ContactDetails("231 12 1212", "West Hollywood, CA", dwayne_johnson) mark_contact = ContactDetails("234 23 2545", "Glendale, CA", mark_wahlberg) # create stuntmen matt_stuntman = Stuntman("John Doe", True, matt_damon) dwayne_stuntman = Stuntman("John Roe", True, dwayne_johnson) mark_stuntman = Stuntman("Richard Doe", True, mark_wahlberg) # persist data session.add(bourne_identity) session.add(furious_7) session.add(pain_and_gain)