Populate account table
[vagrant-mail.git] / database / platal / factories.py
index b955ad2..e224e57 100644 (file)
@@ -22,6 +22,8 @@
 """
 from __future__ import unicode_literals
 
+import datetime
+import django.utils.timezone
 import factory
 import factory.django
 import re
@@ -40,65 +42,51 @@ def emailify(string):
     'gege-a-l-accent'
     """
     normstr = unicodedata.normalize('NFKD', string)
-    asciitext = normstr.encode('ascii', 'ignore').decode('ascii')
-    return re.sub(r'\W', '-', asciitext)
+    asciitext = normstr.encode('ascii', 'ignore').decode('ascii').lower()
+    return re.sub(r'[^-._a-zA-Z0-9]', '-', asciitext)
 
 
+# Define a list of names so that they are always the same accross builds
+FIRSTNAMES_F = (
+    'Elisabeth',
+    'Emmanuelle',
+    'Marie',
+    'Marie-Hélène',
+    'Marthe',
+)
+FIRSTNAMES_M = (
+    'Jean',
+    'Jean-Baptiste',
+    'Jacques',
+    'Joseph',
+    'Luc',
+    'Marc',
+    'Matthieu',
+    'Paul',
+    'Pierre',
+    'Simon',
+    'Thomas',
+)
+FIRSTNAMES = FIRSTNAMES_F + FIRSTNAMES_M
+LASTNAMES = (
+    'Bibi',
+    'Exemple',
+    'De La Machine',
+    'Le Testeur',
+)
+
 class AccountFactory(factory.django.DjangoModelFactory):
     class Meta:
         model = models.Account
 
-    FIRSTNAMES_F = (
-        'Elisabeth',
-        'Emmanuelle',
-        'Marie',
-        'Marie-Hélène',
-    )
-    FIRSTNAMES_M = (
-        'Jean',
-        'Jean-Baptiste',
-        'Jean-Pierre',
-        'Jacques',
-        'Joseph',
-        'Luc',
-        'Marc',
-        'Matthieu',
-        'Pierre',
-        'Simon',
-    )
-    FIRSTNAMES = FIRSTNAMES_F + FIRSTNAMES_M
-    LASTNAMES = (
-        'Bibi',
-        'Exemple',
-        'De La Machine',
-        'Le Testeur',
-    )
-
     firstname = factory.Sequence(lambda n: FIRSTNAMES[n % len(FIRSTNAMES)])
     lastname = factory.Sequence(lambda n: LASTNAMES[n % len(LASTNAMES)])
-    hruid = models.CharField(max_length=255, unique=True)
-    type = models.ForeignKey(AccountType, null=True, db_column='type', blank=True)
-    user_perms = models.CharField(max_length=288, blank=True)
-    is_admin = models.BooleanField()
-    state = models.CharField(max_length=24)
-    password = models.CharField(max_length=120, blank=True)
-    token = models.CharField(max_length=96, blank=True)
-    weak_password = models.CharField(max_length=768, blank=True)
-    registration_date = models.DateTimeField()
-    flags = models.CharField(max_length=15)
-    comment = models.CharField(max_length=765, blank=True)
-    email = models.CharField(max_length=765, blank=True)
-    firstname = models.CharField(max_length=765, blank=True)
-    lastname = models.CharField(max_length=765, blank=True)
-    full_name = models.CharField(max_length=765, blank=True)
-    directory_name = models.CharField(max_length=765, blank=True)
-    sort_name = models.CharField(max_length=765, blank=True)
-    display_name = models.CharField(max_length=765, blank=True)
-    sex = models.CharField(max_length=18)
-    email_format = models.CharField(max_length=12)
-    skin = models.ForeignKey(Skin, null=True, db_column='skin', blank=True)
-    last_version = models.CharField(max_length=48)
-    best_domain = models.ForeignKey(EmailVirtualDomain, null=True, db_column='best_domain', blank=True)
-    from_email = models.CharField(max_length=765)
-    from_format = models.CharField(max_length=12)
-
+    full_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
+    directory_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
+    sort_name = factory.LazyAttribute(lambda o: '%s %s' % (o.lastname, o.firstname))
+    display_name = factory.LazyAttribute(lambda o: '%s %s' % (o.firstname, o.lastname))
+    sex = factory.Sequence(lambda n: 'female' if n % len(FIRSTNAMES) < len(FIRSTNAMES_F) else 'male')
+    hruid = factory.LazyAttributeSequence(
+        lambda o, n: emailify('%s.%s.%d' % (o.firstname, o.lastname, n)))
+    registration_date = factory.Sequence(
+        lambda n: datetime.date(2000, 1, 1) + datetime.timedelta(days=n))