示例#1
0
def add_or_update_store_in_db(store_dict, region, retailer):
    store_name = store_dict['name']
    results = retailer.stores.filter(name=store_name)
    if len(results) > 0:
        # Update existing store
        store = results[0]
    else:
        # Create new store
        store = Store()
        store.retailer = retailer
        store.region = region
        store.name = store_name
    store.display_name = store_dict["display_name"]
    store.latitude = None if 'latitude' not in store_dict else store_dict["latitude"]
    store.longitude = None if 'longitude' not in store_dict else store_dict["longitude"]
    store.tel = None if 'tel' not in store_dict else store_dict["tel"]
    store.address = None if 'address' not in store_dict else store_dict["address"]
    store.working_hours = None if 'working_hours' not in store_dict else store_dict["working_hours"]
    store.active = True
    store.save()

    lasoo_id = store_dict['lasoo_id']
    props = []
    prop = StoreProperty()
    prop.name = make_internal_property_name('lasoo_id')
    prop.value = lasoo_id
    props.append(prop)
    if 'lasoo_url' in store_dict:
        lasoo_url = store_dict['lasoo_url']
        prop = StoreProperty()
        prop.name = make_internal_property_name('lasoo_url')
        prop.value = lasoo_url
        props.append(prop)
    __update_store_props_in_db(store, props)
    return store
示例#2
0
 def parse_store_from_location(self, response, retailer, location):
     store_name = retailer.display_name
     lat, lng, address = None, None, None  # no physical store, website only
     if ('lat' in location and len(location['lat']) > 0) \
             and ('lng' in location and len(location['lng']) > 0):
         lat = float(location['lat'])
         lng = float(location['lng'])
     if 'physical_address_plain_text' in location:
         address_value = str.strip(location['physical_address_plain_text'])
         if len(address_value) > 0:
             address_value = address_value.replace(
                 '\u00a0', ', ') if address_value is not None else None
             if len(address_value) > 512:
                 self.log(
                     'Product {0}: Store address is too long ({1}).'.format(
                         response.url, address))
             address = address_value[:512]
     website, tel = None, None
     for contact_elem in response.xpath('//div[@class="supplier-info"]/a'):
         if len(contact_elem.xpath(
                 './i[contains(@class, "fa-globe")]')) > 0:
             website = extract_first_value_with_xpath(
                 contact_elem, './@href')
         elif len(contact_elem.xpath(
                 './i[contains(@class, "fa-phone")]')) > 0:
             tel = extract_first_value_with_xpath(contact_elem, './text()')
     try:
         store = retailer.stores.get(name=store_name,
                                     latitude=lat,
                                     longitude=lng)
     except Store.DoesNotExist:
         store = Store()
     store.region = response.meta['region']
     store.retailer = retailer
     store.name = store_name
     store.latitude = lat
     store.longitude = lng
     store.address = address
     store.website = website
     store.tel = tel
     store.working_hours = location['public_opening_hours']
     store.save()
     return store
示例#3
0
def add_or_update_store_in_db(store_dict, region, retailer):
    store_name = store_dict['name']
    results = retailer.stores.filter(name=store_name)
    if len(results) > 0:
        # Update existing store
        store = results[0]
    else:
        # Create new store
        store = Store()
        store.retailer = retailer
        store.region = region
        store.name = store_name
    store.display_name = store_dict["display_name"]
    store.latitude = None if 'latitude' not in store_dict else store_dict[
        "latitude"]
    store.longitude = None if 'longitude' not in store_dict else store_dict[
        "longitude"]
    store.tel = None if 'tel' not in store_dict else store_dict["tel"]
    store.address = None if 'address' not in store_dict else store_dict[
        "address"]
    store.working_hours = None if 'working_hours' not in store_dict else store_dict[
        "working_hours"]
    store.active = True
    store.save()

    lasoo_id = store_dict['lasoo_id']
    props = []
    prop = StoreProperty()
    prop.name = make_internal_property_name('lasoo_id')
    prop.value = lasoo_id
    props.append(prop)
    if 'lasoo_url' in store_dict:
        lasoo_url = store_dict['lasoo_url']
        prop = StoreProperty()
        prop.name = make_internal_property_name('lasoo_url')
        prop.value = lasoo_url
        props.append(prop)
    __update_store_props_in_db(store, props)
    return store