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

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

# -*- coding: utf-8 -*- 

# Copyright 2008-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

""" 

 

See test cases and examples in :doc:`/tutorials/human/index`. 

 

.. autosummary:: 

 

 

""" 

from __future__ import division 

from builtins import str 

from past.utils import old_div 

from builtins import object 

 

import logging 

logger = logging.getLogger(__name__) 

 

from django.db import models 

from django.core.exceptions import ValidationError 

from django.utils.translation import ugettext_lazy as _ 

from django.utils.translation import pgettext_lazy as pgettext 

from django.conf import settings 

 

from lino.utils import join_words 

from lino.api.dd import Genders 

 

from lino.core import fields 

from lino.core import model 

 

name_prefixes1 = set(("HET", "'T", 'VAN', 'DER', 'TER', 'DEN', 

                      'VOM', 'VON', 'OF', "DE", "DU", "EL", "AL", "DI")) 

name_prefixes2 = set(("VAN DEN", "VAN DER", "VAN DE", 

                      "IN HET", "VON DER", "DE LA")) 

 

NAME_PREFIXES = set([p + ' ' for p in name_prefixes1]) 

NAME_PREFIXES |= set([p + ' ' for p in name_prefixes2]) 

 

 

def strip_name_prefix(s): 

    """Strip name prefix from given family name `s`.""" 

    s = s.upper() 

    for p in NAME_PREFIXES: 

        if s.startswith(p): 

            s = s[len(p):] 

    return s 

 

 

 

def name2kw(s, last_name_first=True): 

    """Separate first name from last name.  Split a string that contains 

both last_name and first_name.  The caller must indicate whether the 

string contains last_name first (e.g. Saffre Luc) or first_name first 

(e.g. Luc Saffre). 

 

    """ 

    kw = {} 

    a = s.split(',') 

    if len(a) == 2: 

        if last_name_first: 

            return dict(last_name=a[0].strip(), first_name=a[1].strip()) 

    a = s.strip().split() 

    if len(a) == 0: 

        return dict() 

    elif len(a) == 1: 

        return dict(last_name=a[0]) 

    elif len(a) == 2: 

        if last_name_first: 

            return dict(last_name=a[0], first_name=a[1]) 

        else: 

            return dict(last_name=a[1], first_name=a[0]) 

    else: 

        # string consisting of more than 3 words 

        if last_name_first: 

            a01 = a[0] + ' ' + a[1] 

            if a01.upper() in name_prefixes2: 

                return dict( 

                    last_name=a01 + ' ' + a[2], 

                    first_name=' '.join(a[3:])) 

            elif a[0].upper() in name_prefixes1: 

                return dict( 

                    last_name=a[0] + ' ' + a[1], 

                    first_name=' '.join(a[2:])) 

            else: 

                return dict(last_name=a[0], 

                            first_name=' '.join(a[1:])) 

        else: 

            if len(a) >= 4: 

                pc = a[-3] + ' ' + a[-2]  # prefix2 candidate 

                if pc.upper() in name_prefixes2: 

                    return dict( 

                        last_name=pc + ' ' + a[-1], 

                        first_name=' '.join(a[:-3])) 

            pc = a[-2]  # prefix candidate 

            if pc.upper() in name_prefixes1: 

                return dict( 

                    last_name=pc + ' ' + a[-1], 

                    first_name=' '.join(a[:-2])) 

        return dict( 

            last_name=a[-1], 

            first_name=' '.join(a[:-1])) 

 

    return kw 

 

 

def upper1(s): 

    if ' ' in s: 

        return s  # don't change 

    return s[0].upper() + s[1:] 

 

 

def parse_name(text): 

    """Parse the given `text` and return a dict of `first_name` and 

    `last_name` value. 

 

    Extends :func:`name2kw` by raising a  ValidationError if necessary. 

 

    """ 

    kw = name2kw(text, last_name_first=False) 

    if len(kw) != 2: 

        raise ValidationError( 

            _("Cannot find first and last name in \"{0}\"").format(text)) 

    for k in ('last_name', 'first_name'): 

        if kw[k] and not kw[k].isupper(): 

            kw[k] = upper1(kw[k]) 

    return kw 

 

 

def get_salutation(gender, nominative=False): 

    """Returns "Mr" or "Mrs" or a translation thereof, depending on the 

    gender and the current language. 

     

    Note that the English abbreviations `Mr 

    <http://en.wikipedia.org/wiki/Mr.>`_ and `Mrs 

    <http://en.wikipedia.org/wiki/Mrs.>`_ are written either *with* 

    (AE) or *without* (BE) a dot. 

     

    The optional keyword argument `nominative` is used only in certain 

    languages like German: specifying ``nominative=True`` for a male 

    person will return the nominative or direct form "Herr" instead of 

    the default (accusative or indirect form) "Herrn". 

 

    """ 

    if not gender: 

        return '' 

    if gender == Genders.female: 

        return _("Mrs") 

    if nominative: 

        return pgettext("nominative salutation", "Mr") 

    return pgettext("indirect salutation", "Mr") 

 

 

from django.utils.encoding import python_2_unicode_compatible 

 

 

@python_2_unicode_compatible 

class Human(model.Model): 

    """Base class for all models that represent a human. 

 

    .. attribute:: title 

 

        An optional name prefix like "Dr." or "PhD", used to specify a 

        professional position or academic qualification. 

 

        If given, the content of this field comes always *between* 

        salutation and name.  It does not handle special cases like 

        titles which replace the salutation ("Br.", "Sr.") or which must 

        come at another position of the full name (e.g. "Cardinal", "Graf" 

        before the last name). 

 

        External links: `linguee.de 

        <http://www.linguee.de/englisch-deutsch/uebersetzung/mr.+dr..html>`__ 

        and `wikipedia.org <https://en.wikipedia.org/wiki/Title>`__ 

 

    .. attribute:: first_name 

 

        The first name, also known as given name. 

 

    .. attribute:: last_name 

 

        The last name, also known as family name. 

 

    .. attribute:: middle_name 

 

        A space-separated list of all `middle names 

        <http://en.wikipedia.org/wiki/Middle_name>`_. 

 

    .. attribute:: gender 

 

        The sex of this person (male or female).  Possible values are 

        defined in :class:`lino.modlib.lino.choicelists.Genders`. 

 

    """ 

 

    class Meta(object): 

        abstract = True 

 

    title = models.CharField( 

        pgettext("(of a human)", "Title"), 

        max_length=200, blank=True, 

        help_text=_( 

            "Text to print between salutation and name as part " 

            "of the first address line.")) 

 

    first_name = models.CharField( 

        _('First name'), max_length=200, blank=True, 

        help_text=_("First or given name.")) 

 

    middle_name = models.CharField( 

        _("Middle name"), max_length=200, blank=True, 

        help_text=_("Space-separated list of all middle names.")) 

 

    last_name = models.CharField( 

        _('Last name'), max_length=200, blank=True, 

        help_text=_("Last name (family name).")) 

 

    gender = Genders.field(blank=True) 

 

    def mf(self, m, f, u=None): 

        """ 

        Taking three parameters `m`, `f` and `u` of any type, returns one 

        of them depending on whether this Person is male, female or of 

        unknown gender. 

 

        See :ref:`lino.tutorial.human` for some examples. 

 

 

        """ 

        if self.gender is Genders.male: 

            return m 

        if self.gender is Genders.female: 

            return f 

        return u or m 

 

    def __str__(self): 

        return self.get_full_name(nominative=True) 

 

    def get_salutation(self, **salutation_options): 

        return get_salutation( 

            #~ translation.get_language(), 

            self.gender, **salutation_options) 

 

    def get_full_name( 

            self, salutation=True, upper=None, **salutation_options): 

        """Returns a one-line string composed of salutation, 

        :attr:`first_name` and :attr:`last_name`. 

 

        The optional keyword argument `salutation` can be set to 

        `False` to suppress salutations. 

 

        The optional keyword argument `upper` can be specified to 

        override the Site's default value 

        (:attr:`lino.core.site.Site.uppercase_last_name`). `True` 

        means to convert the last name to uppercase as is usually done 

        in French. 

 

        Any other keyword arguments are forwarded to 

        :func:`lino.mixins.human.get_salutation` (see there). 

 

        See :ref:`lino.tutorial.human` for some examples. 

 

        """ 

        words = [] 

 

        if salutation: 

            words.append(self.get_salutation(**salutation_options)) 

        if self.title: 

            words.append(self.title) 

        words.append(self.first_name) 

        if upper is None: 

            upper = settings.SITE.uppercase_last_name 

        if upper: 

            words.append(self.last_name.upper()) 

        else: 

            words.append(self.last_name) 

        return join_words(*words) 

    full_name = property(get_full_name) 

 

    def format_family_member(self, ar, other): 

        """used in `humanlinks.LinksByHuman` and in 

`households.SiblingsByPerson`. 

 

        """ 

        if other.last_name == self.last_name: 

            text = other.first_name 

        else: 

            text = other.first_name + ' ' + other.last_name.upper() 

        return ar.obj2html(other, text) 

 

 

class Born(model.Model): 

    """ 

    Abstract base class that adds a `birth_date` 

    field and a virtual field "Age". 

 

    .. attribute:: birth_date 

 

      An :class:`IncompleteDateField <lino.core.fields.IncompleteDateField>`. 

 

    .. attribute:: age 

 

      Virtual field displaying the age in years. 

 

    """ 

 

    class Meta(object): 

        abstract = True 

 

    birth_date = fields.IncompleteDateField( 

        blank=True, verbose_name=_("Birth date")) 

 

    def get_age(self, today=None): 

        """Return the age (in years) of this human. 

        See :meth:`lino.utils.IncompleteDateField.get_age`. 

        """ 

        if self.birth_date: 

            return self.birth_date.get_age(today or settings.SITE.today()) 

 

    def get_exact_age(self, today=None): 

        """ 

        Return the age as a :class:`datetime.timedelta` object. 

 

        Optional keyword argument `today` should be a 

        :class:`datetime.date` instance to replace the actual current 

        date. This is used if you want the age at a given date in the past 

        or the future. 

        The default value calls :meth:`dd.Site.today`. 

        """ 

        # print(20160202, self.birth_date, self) 

        if self.birth_date and self.birth_date.year: 

            if today is None: 

                today = settings.SITE.today() 

            try: 

                return today - self.birth_date.as_date() 

            except ValueError: 

                pass 

 

    @fields.displayfield(_("Age")) 

    def age(self, request, today=None): 

        a = self.get_exact_age(today) 

        if a is None: 

            return str(_('unknown')) 

        s = _("%d years") % (old_div(a.days, 365)) 

        if self.birth_date and self.birth_date.is_complete(): 

            return s 

        return u"±" + s