Install build-essential to build pfixtools
[vagrant-mail.git] / database / platal / factories.py
1 # -*- coding: utf-8 -*-
2 #***************************************************************************
3 #* Copyright (C) 2015 Polytechnique.org *
4 #* http://opensource.polytechnique.org/ *
5 #* *
6 #* This program is free software; you can redistribute it and/or modify *
7 #* it under the terms of the GNU General Public License as published by *
8 #* the Free Software Foundation; either version 2 of the License, or *
9 #* (at your option) any later version. *
10 #* *
11 #* This program is distributed in the hope that it will be useful, *
12 #* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 #* GNU General Public License for more details. *
15 #* *
16 #* You should have received a copy of the GNU General Public License *
17 #* along with this program; if not, write to the Free Software *
18 #* Foundation, Inc., *
19 #* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 #***************************************************************************/
21 """FactoryBoy classes aimed to ease populating the database
22 """
23 from __future__ import unicode_literals
24
25 import datetime
26 import django.utils.timezone
27 import factory
28 import factory.django
29 import re
30 import unicodedata
31
32 from . import models
33
34
35 def emailify(string):
36 """Transform a UTF-8 string to an ASCII representation suitable for an
37 email address
38
39 >>> emailify('ABC def')
40 'abc-def'
41 >>> emailify('gégé à l\'accent')
42 'gege-a-l-accent'
43 """
44 normstr = unicodedata.normalize('NFKD', string)
45 asciitext = normstr.encode('ascii', 'ignore').decode('ascii').lower()
46 return re.sub(r'[^-._a-zA-Z0-9]', '-', asciitext)
47
48
49 # Define a list of names so that they are always the same accross builds
50 FIRSTNAMES_F = (
51 'Elisabeth',
52 'Emmanuelle',
53 'Marie',
54 'Marie-Hélène',
55 'Marthe',
56 )
57 FIRSTNAMES_M = (
58 'Jean',
59 'Jean-Baptiste',
60 'Jacques',
61 'Joseph',
62 'Luc',
63 'Marc',
64 'Matthieu',
65 'Paul',
66 'Pierre',
67 'Simon',
68 'Thomas',
69 )
70 FIRSTNAMES = FIRSTNAMES_F + FIRSTNAMES_M
71 LASTNAMES = (
72 'Bibi',
73 'Exemple',
74 'De La Machine',
75 'Le Testeur',
76 )
77
78 class AccountFactory(factory.django.DjangoModelFactory):
79 class Meta:
80 model = models.Account
81
82 firstname = factory.Sequence(lambda n: FIRSTNAMES[n % len(FIRSTNAMES)])
83 lastname = factory.Sequence(lambda n: LASTNAMES[n % len(LASTNAMES)])
84 full_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
85 directory_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
86 sort_name = factory.LazyAttribute(lambda o: '%s %s' % (o.lastname, o.firstname))
87 display_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
88 sex = factory.Sequence(lambda n: 'female' if n % len(FIRSTNAMES) < len(FIRSTNAMES_F) else 'male')
89 hruid = factory.LazyAttributeSequence(
90 lambda o, n: emailify('%s.%s.%d' % (o.firstname, o.lastname, n)))
91 registration_date = factory.Sequence(
92 lambda n: datetime.date(2000, 1, 1) + datetime.timedelta(days=n))