Exemple #1
0
def StoreListings( listingObject ):

    #Prepare the secondary dictionaries
    listing_sales = listingObject.get( 'saleDetails' )
    lisitng_inspections = listingObject.get( 'inspectionDetails' )
    listing_prices = listingObject.get( 'priceDetails' )

    try:
        #Insert the raw listing first.
        #Get the listing_status
        listing_status = QueryWithSingleValue( 'listing_status_lkp', 'description', listingObject['status'], 'listing_status_id', True )

        #build the JSON from listingObject
        raw_listing_JSON = json.dumps( listingObject )

        #Get the value that will be used with listing_insert_statement
        listings_id = returnNextSerialID( 'listings', 'listings_id' )

        if lisitng_inspections is not None:
            isByAppointmentOnly = lisitng_inspections.get( 'isByAppointmentOnly' )
        else:
            isByAppointmentOnly = None

        cur.execute( """ INSERT INTO listings( domain_listings_id, headline, price_displayed, display_price, price, price_from, price_to, seo_url, listing_objective,
                                                            listing_status, land_area, building_area, energy_efficiency, is_new_development, date_updated, date_created,
                                                            entered_when, entered_by, raw_listing, inspection_appointment_only )
                         VALUES( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, to_timestamp( %s, 'YYYY-MM-DD HH24:MI:SS' ), to_timestamp( %s, 'YYYY-MM-DD HH24:MI:SS' ), current_timestamp, 1, %s, %s ) """,
                         ( listingObject.get( 'id' ), listingObject.get( 'headline' ), listing_prices.get( 'canDisplayPrice' ), listing_prices.get( 'displayPrice' ), listing_prices.get( 'price' ), 
                           listing_prices.get( 'priceFrom' ), listing_prices.get( 'priceTo' ), listingObject.get( 'seoUrl' ), listingObject.get( 'objective' ), listing_status, listingObject.get( 'landAreaSqm'), 
                           listingObject.get( 'buildingAreaSqm' ), listingObject.get( 'energyEfficiencyRating' ), listingObject.get( 'isNewDevelopment' ), convertJSONDate(listingObject['dateUpdated']),
                           convertJSONDate(listingObject['dateListed']), cleanForSQL(raw_listing_JSON), isByAppointmentOnly ) )

        #Insert the Features if the listing contains any.
        #Set the object type
        #Only do this if the listing already has a features object.
        if 'features' in listingObject:
            link_object_type = OBJECT_Listing
            for feature in listingObject['features']:
                storeFeatures( listings_id, link_object_type, feature )

        if 'media' in listingObject:
            #Store any media attached to the listing
            for media in listingObject['media']:
                mediaObject = File( FILE_TYPE_Images, OBJECT_Listing, str(listings_id), None, "listing_" + media['type'] )
                mediaObject.addImageDetails( None, None, media['url'] )
                mediaObject.storeFile( False )

        #Store Listing Sales Information.
        #First, we need to check if the listings has any sales information attached to it.
        if listing_sales is not None:
            storeListingSalesDetails( listing_sales, listings_id )

        #Store the Inspection information (if the listing_inspections array is not None)
        if lisitng_inspections is not None:
            storeInspectionDetails( listings_id, lisitng_inspections )

        return listings_id
    except(Exception, psycopg2.DatabaseError) as error:
        print("Error in INSERTING New Listing with Domain Listing ID " + "\n" + "Error: " + error )
        return None
    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