Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

# -*- coding: UTF-8 -*- 

# Copyright 2014 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

"""This fixture imports all Estonian places from :mod:`commondata.ee` 

(which needs to be installed before loading this fixture). 

 

""" 

 

import logging 

logger = logging.getLogger(__name__) 

 

from commondata.ee.places import root 

 

from commondata.ee.places import (Village, SmallBorough, Borough, 

                                  Township, Town, Municipality, County) 

 

from lino.api import dd 

 

countries = dd.resolve_app('countries') 

 

 

def cd2type(p): 

    if isinstance(p, County): 

        return countries.PlaceTypes.county 

    if isinstance(p, Township): 

        return countries.PlaceTypes.township 

    if isinstance(p, Town): 

        return countries.PlaceTypes.town 

    if isinstance(p, Municipality): 

        return countries.PlaceTypes.municipality 

    if isinstance(p, Borough): 

        return countries.PlaceTypes.borough 

    if isinstance(p, SmallBorough): 

        return countries.PlaceTypes.smallborough 

    if isinstance(p, Village): 

        return countries.PlaceTypes.village 

 

 

def place2objects(country, place, parent=None): 

    t = cd2type(place) 

    if t is None: 

        logger.info("20140612 ignoring place %s", place) 

        return 

    obj = countries.Place( 

        country=country, type=t, name=place.name, 

        parent=parent, 

        zip_code=place.zip_code) 

 

    # We must save the parent before we can generate children. 

    try: 

        obj.full_clean() 

    except Exception as e: 

        raise Exception("Could not save %s : %r" % ( 

            dd.obj2str(obj), e)) 

    obj.save() 

    yield obj 

 

    for cp in place.children: 

        yield place2objects(country, cp, obj) 

 

 

def objects(): 

 

    eesti = root() 

    EE = countries.Country.objects.get(isocode="EE") 

    for p in eesti.children: 

        yield place2objects(EE, p)