def refine_entry(self, entry): """ Vypilovani zaznamu o obci. Nyni aktualizujeme vsechny jeji udaje do finalni podoby. """ ei = self.ei old_entry = self.old_entry if old_entry: exists = True # novy zaznam podedi nektere vlastnosti stareho zaznamu slug = old_entry.slug # stejny slug public = old_entry.public # viditelnost # stary zaznam obce odstavime na vedlejsi kolej old_entry.slug = "%s-%i" % (old_entry.slug, old_entry.id) old_entry.public = False old_entry.save() else: # pro novy zaznam vymyslime unikatni slug slug, exists = get_unique_slug(ei['town'][:90]) # TODO: neopakuje se mi to? ta :90 if exists: point = Point(ei['pos']['lon'], ei['pos']['lat'], srid=4326) if Entry.objects.filter(slug__startswith=slugify(ei['town'][:90]), dpoint__distance_lte=(point, D(km=KM20))).exists(): # duplicitni zaznam s jinym zdrojem dat; tohle skryjem public = False else: # jde pouze o shodu jmena obce; normalne ji zverejnime # (slug sice bude mit cislo, ale jinak to stejne nejde) public = True else: public = True # data pro aktualizovani zaznamu Entry data = { 'slug': slug, 'population': ei['population'] and int(ei['population']) or None, 'area': ei['area'] and int(ei['area']) or None, 'wikipedia': ei['wikipedia_url'], 'public': public, 'email': self.cleaned_data['email'], 'description': '' } if old_entry: # pokud nam v "ei" datech neco chybi, doplnime to ze stareho zaznamu data['population'] = data['population'] or old_entry.population data['area'] = data['area'] or old_entry.area data['wikipedia'] = data['wikipedia'] or old_entry.wikipedia data['description'] = data['description'] or old_entry.description # ulozeni objektu do DB for k, v in data.iteritems(): setattr(entry, k, v) entry.recalculate_denormalized_values(True) entry.save() cache.clear() return entry, exists
def load(data_or_filename): """ Vytvori komplet novy zaznam o jednom Entry podle dodanych dat. Data mohou byt zadana ve forme slovniku nebo cesty k pickle souboru (produkt Entry.dump). Nove naimportovana data budou mit jina ID, a zaznam Entry mozna i jiny slug. NOTE: Bacha! Pokud je parametr `data_or_filename` slovnik, bude jeho struktura mirne modifikovana (zaznamy zones dostanou novy klic 'new_id' s nove vytvorenym Zone.id). """ # data jsou predana budto jako slovnik nebo cesta k pickle souboru if type(data_or_filename) in [type(''), type(u'')]: f = open(data_or_filename, 'rb') data = pickle.load(f) f.close() else: data = data_or_filename # zaznam Entry slug, exists = get_unique_slug(data['entry']['title']) entry = Entry.objects.create( title=data['entry']['title'], slug=slug, description=data['entry']['description'], population=data['entry']['population'], area=data['entry']['area'], wikipedia=data['entry']['wikipedia'], hell_url=data['entry']['hell_url'], hell_kml=data['entry']['hell_kml'], building_url=data['entry']['building_url'], building_kml=data['entry']['building_kml'], public=data['entry']['public'], created=data['entry']['created'], dperc=data['entry']['dperc'], dhell_count=data['entry']['dhell_count'], dok_hell_count=data['entry']['dok_hell_count'], dper_population=data['entry']['dper_population'], dper_area=data['entry']['dper_area'], dpoint=Point(data['entry']['dpoint'][0], data['entry']['dpoint'][1], srid=4326)) # zones for zone_id, zone in data['zones'].iteritems(): z = Zone.objects.create(poly=Polygon(zone['poly'], srid=4326)) data['zones'][zone_id]['new_id'] = z.id # herny for hell_id, hell in data['hells'].iteritems(): h = Hell.objects.create( title=hell['title'], description=hell['description'], slug=hell['slug'], entry=entry, point=Point(hell['point'][0], hell['point'][1], srid=4326), uzone=hell['uzone'] and Zone.objects.get(id=data['zones'][hell['uzone']]['new_id']) or None, zones_calculated=hell['zones_calculated']) zones = [data['zones'][i]['new_id'] for i in hell['zones']] h.zones.add(*Zone.objects.filter(id__in=zones)) # budovy for building_id, building in data['buildings'].iteritems(): b = Building.objects.create( title=building['title'], description=building['description'], slug=building['slug'], entry=entry, zone=Zone.objects.get( id=data['zones'][building['zone']]['new_id']), poly=Polygon(building['poly'], srid=4326))
def load(data_or_filename): """ Vytvori komplet novy zaznam o jednom Entry podle dodanych dat. Data mohou byt zadana ve forme slovniku nebo cesty k pickle souboru (produkt Entry.dump). Nove naimportovana data budou mit jina ID, a zaznam Entry mozna i jiny slug. NOTE: Bacha! Pokud je parametr `data_or_filename` slovnik, bude jeho struktura mirne modifikovana (zaznamy zones dostanou novy klic 'new_id' s nove vytvorenym Zone.id). """ # data jsou predana budto jako slovnik nebo cesta k pickle souboru if type(data_or_filename) in [type(''), type(u'')]: f = open(data_or_filename, 'rb') data = pickle.load(f) f.close() else: data = data_or_filename # zaznam Entry slug, exists = get_unique_slug(data['entry']['title']) entry = Entry.objects.create( title = data['entry']['title'], slug = slug, description = data['entry']['description'], population = data['entry']['population'], area = data['entry']['area'], wikipedia = data['entry']['wikipedia'], hell_url = data['entry']['hell_url'], hell_kml = data['entry']['hell_kml'], building_url = data['entry']['building_url'], building_kml = data['entry']['building_kml'], public = data['entry']['public'], created = data['entry']['created'], dperc = data['entry']['dperc'], dhell_count = data['entry']['dhell_count'], dok_hell_count = data['entry']['dok_hell_count'], dper_population = data['entry']['dper_population'], dper_area = data['entry']['dper_area'], dpoint = Point(data['entry']['dpoint'][0], data['entry']['dpoint'][1], srid=4326) ) # zones for zone_id, zone in data['zones'].iteritems(): z = Zone.objects.create(poly=Polygon(zone['poly'], srid=4326)) data['zones'][zone_id]['new_id'] = z.id # herny for hell_id, hell in data['hells'].iteritems(): h = Hell.objects.create( title = hell['title'], description = hell['description'], slug = hell['slug'], entry = entry, point = Point(hell['point'][0], hell['point'][1], srid=4326), uzone = hell['uzone'] and Zone.objects.get(id=data['zones'][hell['uzone']]['new_id']) or None, zones_calculated = hell['zones_calculated'] ) zones = [data['zones'][i]['new_id'] for i in hell['zones']] h.zones.add(*Zone.objects.filter(id__in=zones)) # budovy for building_id, building in data['buildings'].iteritems(): b = Building.objects.create( title = building['title'], description = building['description'], slug = building['slug'], entry = entry, zone = Zone.objects.get(id=data['zones'][building['zone']]['new_id']), poly = Polygon(building['poly'], srid=4326) )