示例#1
0
def get_location(namestr, dbname=settings.MONGO_DATABASE_NAME, just_one=True, starts_with=False, postcodes=True, create_location=False):
    """
    namestr can be postcode, placename, or 'placename: district' for places with same name
    """
    # could do lat_lon search by testing namestr for '123.456, 123.456'
    # split on ','
    # check len=2
    # check parts 1 and 2 (trimmed) convert to real
    # return {'lat_lon': [real, real] }

    default = []
    namestr = namestr.strip()
    if not namestr:
        return default
    db = get_db()
    coll = db.location
    district =None
    pc = False
    pc_matcher = re.compile(POSTCODE_START_REGEX)

    if pc_matcher.match(namestr):
        name = namestr.upper().replace(' ', '')
        field = '_id'
        pc = True
    else:
        names = [n.strip() for n in namestr.split(':')]
        name = names[0]
        district = names[1] if len(names) > 1 else None
        field = 'place_name'
        coll.ensure_index([
            ('place_name', ASCENDING),
            ('country_code', ASCENDING),
            ('accuracy', DESCENDING)
            ])

    # removed this- supposed to be slow cos index not used
    # but seems ok, and improves usability
    # if starts_with:
    name = re.compile('^%s' % name, re.IGNORECASE)
    find_dict = {field: name}
    if district:
        find_dict['district'] = district
    if not postcodes and not pc:
        find_dict['postcode'] = None
    result = coll.find_one(find_dict) if just_one else coll.find(find_dict).limit(20)
    if result and (type(result) == dict or result.count() > 0):
        return result
    elif create_location:
        loc = Location.create_from(namestr)
        # print ' ###################### CREATED LOCATION ######################'
        if loc:
            return loc.to_mongo()

    return default
示例#2
0
 def clean_new_location(self):
     data = self.cleaned_data['new_location']
     if data:
         loc_ids = data.split(',')
         locs = list(Location.objects(id__in=loc_ids))
         new_locs = [l for l in loc_ids if l not in [loc.id for loc in locs]]
         new_locs_errors = []
         for new_loc in new_locs:
             loc = Location.create_from(new_loc)
             if loc:
                 locs.append(loc)
             else:
                 increment_failed_locations(new_loc)
                 new_locs_errors.append(new_loc)
         self.locations = locs
         if new_locs_errors:
             raise forms.ValidationError('Could not find these locations: %s' % ', '.join(new_locs_errors))
     return data
示例#3
0
 def clean_new_location(self):
     data = self.cleaned_data['new_location']
     if data:
         loc_ids = data.split(',')
         locs = list(Location.objects(id__in=loc_ids))
         new_locs = [l for l in loc_ids if l not in [loc.id for loc in locs]]
         new_locs_errors = []
         for new_loc in new_locs:
             loc = Location.create_from(new_loc)
             if loc:
                 locs.append(loc)
             else:
                 increment_failed_locations(new_loc)
                 new_locs_errors.append(new_loc)
         self.locations = locs
         if new_locs_errors:
             raise forms.ValidationError('Could not find these locations: %s' % ', '.join(new_locs_errors))
     return data