1 # -*- coding: utf-8 -*-
2 #***************************************************************************
3 #* Copyright (C) 2015 Polytechnique.org *
4 #* http://opensource.polytechnique.org/ *
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. *
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. *
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
23 from __future__
import unicode_literals
26 import django
.utils
.timezone
36 """Transform a UTF-8 string to an ASCII representation suitable for an
39 >>> emailify('ABC def')
41 >>> emailify('gégé à l\'accent')
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
)
49 # Define a list of names so that they are always the same accross builds
70 FIRSTNAMES
= FIRSTNAMES_F
+ FIRSTNAMES_M
78 class AccountFactory(factory
.django
.DjangoModelFactory
):
80 model
= models
.Account
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
))