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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
#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
class Agency:

    variables_set = False

    def __init__(self,
                 agency_banner,
                 agency_website,
                 agency_logo_standard,
                 agency_long,
                 agency_lat,
                 agency_description,
                 num_sale,
                 num_rent,
                 name,
                 domain_agency_id,
                 principal_agent_name,
                 raw_agency_json,
                 contact_details,
                 streetAddress1,
                 streetAddress2,
                 suburb,
                 agents=None,
                 principal_agent_id=None,
                 agency_id=None,
                 state=None,
                 postCode=None):

        self.agency_banner = agency_banner
        self.agency_website = agency_website
        self.agency_logo_standard = agency_logo_standard
        cleaned_agency_description = self.cleanAgencyDescription(
            agency_description)
        #We need to truncate the agency description if its larger then 1000
        if len(cleaned_agency_description) > 1000:
            self.agency_description = cleaned_agency_description[0:997] + "..."
        else:
            self.agency_description = cleaned_agency_description
        self.num_sale = num_sale
        self.num_rent = num_rent
        self.domain_agency_id = domain_agency_id
        self.name = name
        self.principal_agent_name = principal_agent_name
        self.raw_agency_json = raw_agency_json
        self.agents = agents

        #Set up the address object for the Agency Class
        street_name = streetAddress1 + ' ' + streetAddress2
        full_address = street_name + ' ' + suburb + ' ' + state + ' ' + str(
            postCode)
        #We need to get the street number
        street_number_object = re.search(r"\d+/?\d*", streetAddress1)
        if street_number_object is None:
            street_number_object = re.search(r"\d+/?\d*", streetAddress2)

        if street_number_object is not None:
            street_number = street_number_object.group(0)
        else:
            street_number = None

        self.address = Address(full_address, street_name, suburb,
                               street_number, None, None, agency_long,
                               agency_lat, state, postCode)

        self.contact_details = contact_details
        if principal_agent_id is not None:
            self.principal_agent_id = principal_agent_id
        else:
            self.principal_agent_id = None
        if agency_id is not None:
            self.agency_id = agency_id
        else:
            self.agency_id = None

        self.classesConverted = False

    def cleanAgencyDescription(self, cleanString):
        #Use unicodedata.normalize to get rid of the extra Unicode Data
        agency_description_unicode = unicodedata.normalize("NFKD", cleanString)
        remove_html_string = re.sub(r"\<\\?[^\>]+\>", "",
                                    agency_description_unicode)
        #str_length = len( agency_description_unicode )
        #final_step_string = None
        #count = 0
        #Find the position of the first span (We know that the main data will be contained inside a span)
        #first_span_position = agency_description_unicode.find( "<span" )
        #Slice the String and get the remainder.
        #first_step_string = agency_description_unicode[first_span_position:str_length]
        #Get the location of the >, signifiying the end of the first span tag.
        #end_first_span_position = first_step_string.find( ">" )
        #Slice the string again.
        #new_str_length = len(first_step_string)
        #second_step_string = first_step_string[end_first_span_position:new_str_length]
        #Now get the position for the closing span tag.
        #second_span_position = second_step_string.find("</span")
        #final_step_string = second_step_string[1:second_span_position]

        return remove_html_string

    def getPrincipalAgent(self):
        try:
            #Test to see if the Agent instance has a value for principal_agent_name
            if self.principal_agent_name == None:
                raise ValueError('Invalid Principal Agent Name')
            else:
                full_name = self.principal_agent_name.split()
                result = None
                if len(full_name) == 2:
                    cur.execute(
                        """ SELECT a.agent_id 
                                  FROM agents a 
                                  WHERE UPPER( a.first_name ) = UPPER( %s ) 
                                        AND UPPER( a.last_name ) = UPPER( %s )""",
                        (cleanForSQL(full_name[0]), cleanForSQL(full_name[1])))
                    result = cur.fetchone()
                if result is None:
                    self.principal_agent_id = None
                else:
                    self.principal_agent_id = result[0]
            return True
        except (Exception, psycopg2.DatabaseError, ValueError) as error:
            print("Error in Getting Principal Agent ID for Agency " +
                  self.name + "\n" + error)
            return False

    def hasAgencyID(self):
        return self.agency_id is not None

    #def checkForNewAgents( self, agents ):
    #Need to first check if self.agents is populated and that the Agnecy Object has its agency id populated.
    #    if self.agents is None and self.agency_id is not None:
    #        cur.execute( """SELECT a.agent_id
    #                        FROM agencies_agent a
    #                        WHERE a.agency_id = %s
    #                            AND a.deleted_when IS NULL""",
    #                        ( self.agency_id ) )

    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)

    def storeAgency(self):
        try:
            #If the Agency ID hasn't been set for this Agency, it means that hasn't been saved yet into the database...
            if self.agency_id is None:
                self.agency_id = returnNextSerialID('agencies', 'agency_id')

            if not self.getPrincipalAgent():
                return None
            #Convert the data inside the Agency object into Classes
            self.convertToClasses()

            #Save all contact details first.
            for contact in self.contact_details_converted:
                contact.storeContactDetails(False)

            #Store the links to the agency banner, website and standard logo.
            self.agency_website.storeContactDetails(False)
            self.agency_banner.storeFile(False)
            self.agency_logo_standard.storeFile(False)

            #Store the address first so that we can link the address to the correct row inside the agencies table.
            address_id = self.address.storeAddress()

            agency_json = json.dumps(self.raw_agency_json)

            cur.execute(
                """INSERT INTO agencies( description, num_sale, num_rent, entered_when, raw_agency_json, name, principal_agent, domain_agency_id, address_id )
                            VALUES( %s, %s, %s, current_timestamp, %s, %s, %s, %s, %s)""",
                (self.agency_description, self.num_sale, self.num_rent,
                 cleanForSQL(agency_json), self.name, self.principal_agent_id,
                 self.domain_agency_id, address_id))
            return self.agency_id
        except (Exception, psycopg2.DatabaseError) as error:
            print("Error in INSERTING New Agency for Agency " + self.name +
                  "\n" + error)
            return None
Example #7
0
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)
Example #8
0
# 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)
Example #9
0
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)