Changeset 584

Show
Ignore:
Timestamp:
02/17/10 16:38:55 (5 months ago)
Author:
olivier
Message:

merge r582 and r583 into telemeta 0.5.0 tag

Location:
tags/telemeta-0.5.0
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • tags/telemeta-0.5.0

  • tags/telemeta-0.5.0/telemeta/management/commands/telemeta-geocode.py

    r581 r584  
    2020        except IOError: 
    2121            raise CommandError("Unable to open %s" % datafile) 
    22              
    23         locations = [l for l in Location.objects.all().current().filter(type=Location.COUNTRY)] 
     22         
     23        locations = {} 
     24        for l in Location.objects.all().current().filter(type=Location.COUNTRY): 
     25            locations[l] = [a.alias for a in l.aliases.all()] 
    2426 
    2527        i = 0 
    2628        geocoded = 0 
    2729        total = len(locations) 
     30        found_by_alias = {} 
    2831        for line in datafile: 
    2932            (geonameid, name, asciiname, alternatenames, latitude, longitude, feature_class, 
     
    4447                        geocoded += 1 
    4548                        found.append(l) 
     49                    else: 
     50                        for a in locations[l]: 
     51                            if unaccent(a).lower() in names: 
     52                                found_by_alias[l] = float(latitude), float(longitude) 
     53                                break 
     54                             
    4655 
    4756                for l in found: 
    48                     locations.remove(l) 
     57                    locations.pop(l) 
    4958 
    5059            i += 1 
    5160 
    5261            if i % 200000 == 0: 
    53                 print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) 
     62                print "Geocoded %d (%d by alias) out of %d countries (parsed %d geonames)" % (geocoded, len(found_by_alias), total, i) 
    5463 
    5564            if total == geocoded: 
    5665                break 
    5766 
    58         print "Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) 
     67        for l in locations: 
     68            if found_by_alias.has_key(l): 
     69                l.latitude, l.longitude = found_by_alias[l] 
     70                l.save() 
     71                geocoded += 1 
     72 
     73        print "Done. Geocoded %d out of %d countries (parsed %d geonames)" % (geocoded, total, i) 
    5974        datafile.close()                 
    6075 
  • tags/telemeta-0.5.0/telemeta/models/location.py

    r552 r584  
    7878            q &= Q(ancestor_relations__is_direct=True) 
    7979        return Location.objects.filter(q)            
     80 
     81    def apparented(self): 
     82        return Location.objects.filter( 
     83                Q(pk=self.id) |  
     84                Q(ancestor_relations__ancestor_location=self) |  
     85                Q(current_location=self.id)).distinct() 
    8086 
    8187    def add_child(self, other): 
  • tags/telemeta-0.5.0/telemeta/models/query.py

    r581 r584  
    8484    def by_location(self, location): 
    8585        "Find items by location" 
    86         return self.filter(Q(location=location) | Q(location__in=location.descendants())) 
     86        return self.filter(location__in=location.apparented()) 
    8787            
    8888    @staticmethod 
     
    9393        from telemeta.models import Location, LocationRelation 
    9494        l = self.values('location') 
     95        c = self.values('location__current_location') 
    9596        r = LocationRelation.objects.filter(location__in=l).values('ancestor_location') 
    96         return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r)) 
     97        return Location.objects.filter(Q(pk__in=l) | Q(pk__in=r) | Q(pk__in=c)) 
    9798 
    9899    def countries(self, group_by_continent=False): 
     
    102103            location = Location.objects.get(pk=id) 
    103104            for l in location.countries(): 
    104                 if not l in countries: 
    105                     countries.append(l) 
     105                c = l.current_location 
     106                if not c in countries: 
     107                    countries.append(c) 
    106108 
    107109        if group_by_continent: 
     
    226228    def by_location(self, location): 
    227229        "Find collections by location" 
    228         return self.filter(Q(items__location=location) | Q(items__location__in=location.descendants())).distinct() 
     230        return self.filter(items__location__in=location.apparented()).distinct() 
    229231     
    230232    def by_recording_year(self, from_year, to_year=None): 
     
    352354        return map 
    353355 
    354     def current(self, is_current=True): 
    355         if is_current: 
    356             where = ["locations.id = locations.current_location_id"] 
    357         else: 
    358             where = ["locations.id <> locations.current_location_id"] 
    359  
    360         return self.extra(where = where); 
     356    def current(self): 
     357        return self.filter(id__in=self.values_list('current_location_id', flat=True)).distinct() 
    361358 
    362359class LocationManager(CoreManager): 
  • tags/telemeta-0.5.0/telemeta/web/base.py

    r575 r584  
    240240            func = switch.get(key) 
    241241            if func and value and value != "0": 
    242                 res = func(value) 
    243                 if len(res) > 2: 
    244                     collections, items, value = res 
    245                 else:  
    246                     collections, items = res 
     242                try: 
     243                    res = func(value) 
     244                    if len(res) > 2: 
     245                        collections, items, value = res 
     246                    else:  
     247                        collections, items = res 
     248                except ObjectDoesNotExist: 
     249                    collections = collections.none() 
     250                    items = items.none() 
     251 
    247252                criteria[key] = value 
    248253