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

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

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

# Copyright 2009-2014 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

""" 

 

Garbles person names in the database so that it may be used for a demo. 

 

""" 

 

from optparse import make_option 

 

from django.conf import settings 

from django.core.management.base import BaseCommand, CommandError 

 

from lino.utils import dblogger 

from lino.utils import Cycler, join_words 

 

from lino import mixins 

from lino.api import dd, rt 

 

 

from lino.utils import confirm 

 

from lino.utils import demonames as demo 

 

 

class Distribution(object): 

    def __init__(self): 

        self.LAST_NAMES = Cycler(self.get_last_names()) 

        self.MALES = Cycler(self.get_male_first_names()) 

        self.FEMALES = Cycler(self.get_female_first_names()) 

 

    def before_save(self, obj): 

        pass 

 

 

class BelgianDistribution(Distribution): 

 

    def get_last_names(self): 

        yield demo.LAST_NAMES_BELGIUM 

        yield demo.LAST_NAMES_MUSLIM 

        yield demo.LAST_NAMES_BELGIUM 

        yield demo.LAST_NAMES_RUSSIA 

        yield demo.LAST_NAMES_BELGIUM 

        yield demo.LAST_NAMES_AFRICAN 

 

    def get_male_first_names(self): 

        yield demo.MALE_FIRST_NAMES_FRANCE 

        yield demo.MALE_FIRST_NAMES_MUSLIM 

        yield demo.MALE_FIRST_NAMES_FRANCE 

        yield demo.MALE_FIRST_NAMES_RUSSIA 

        yield demo.MALE_FIRST_NAMES_FRANCE 

        yield demo.MALE_FIRST_NAMES_AFRICAN 

 

    def get_female_first_names(self): 

        yield demo.FEMALE_FIRST_NAMES_FRANCE 

        yield demo.FEMALE_FIRST_NAMES_MUSLIM 

        yield demo.FEMALE_FIRST_NAMES_FRANCE 

        yield demo.FEMALE_FIRST_NAMES_RUSSIA 

        yield demo.FEMALE_FIRST_NAMES_FRANCE 

        yield demo.FEMALE_FIRST_NAMES_AFRICAN 

 

 

class EstonianDistribution(Distribution): 

 

    def __init__(self): 

        super(EstonianDistribution, self).__init__() 

        Country = rt.modules.countries.Country 

        Place = rt.modules.countries.Place 

        PlaceTypes = rt.modules.countries.PlaceTypes 

        self.tallinn = Place.objects.get( 

            type=PlaceTypes.town, name="Tallinn") 

        self.eesti = Country.objects.get(isocode="EE") 

        self.streets = Cycler(self.get_streets()) 

 

    def get_last_names(self): 

        yield demo.LAST_NAMES_ESTONIA 

 

    def get_male_first_names(self): 

        yield demo.MALE_FIRST_NAMES_ESTONIA 

 

    def get_female_first_names(self): 

        yield demo.FEMALE_FIRST_NAMES_ESTONIA 

 

    def get_streets(self): 

        Place = rt.modules.countries.Place 

        PlaceTypes = rt.modules.countries.PlaceTypes 

        for streetname, linnaosa in demo.streets_of_tallinn(): 

            t = PlaceTypes.township 

            try: 

                p = Place.objects.get(type=t, name__iexact=linnaosa) 

            except Place.DoesNotExist: 

                raise Exception("Unknown %s %r" % (t, linnaosa)) 

            yield p, streetname 

 

    def before_save(self, obj): 

        if obj.country and obj.country.isocode == 'BE': 

            obj.country = self.eesti 

            p, s = self.streets.pop() 

            obj.city = p 

            obj.zip_code = p.zip_code 

            obj.street = s 

 

 

class Command(BaseCommand): 

    args = '(no arguments)' 

    help = "Garbles person names in the database so that it " 

    "may be used for a demo." 

 

    option_list = BaseCommand.option_list + ( 

        make_option( 

            '--noinput', action='store_false', 

            dest='interactive', default=True, 

            help='Do not prompt for input of any kind.'), 

        make_option( 

            '--distribution', action='store', 

            dest='distribution', default='BE', 

            help='Distribution to use. Available dists are BE and EE.'), 

    ) 

 

    def handle(self, *args, **options): 

 

        dbname = settings.DATABASES['default']['NAME'] 

        if options.get('interactive'): 

            if not confirm("This is going to GARBLE your database (%s).\n" 

                           "Are you sure (y/n) ?" % dbname): 

                raise CommandError("User abort.") 

 

        def build_dist(k): 

            k = k.upper() 

            if k == 'BE': 

                return BelgianDistribution() 

            if k == 'EE': 

                return EstonianDistribution() 

            raise CommandError("Invalid distribution key %r." % k) 

 

        dist = build_dist(options.get('distribution')) 

 

        User = dd.resolve_model(settings.SITE.user_model) 

        Person = rt.modules.contacts.Person 

 

        for p in Person.objects.order_by('id'): 

            if User.objects.filter(partner=p).count() > 0: 

                # users keep their original name 

                pass 

            else: 

                p.last_name = dist.LAST_NAMES.pop() 

                if p.gender == dd.Genders.male: 

                    p.first_name = dist.MALES.pop() 

                    dist.FEMALES.pop() 

                else: 

                    p.first_name = dist.FEMALES.pop() 

                    dist.MALES.pop() 

                p.name = join_words(p.last_name, p.first_name) 

                dist.before_save(p) 

                p.save() 

                dblogger.info(p.get_address(', '))