From d171e6c7a2de5bd595ca9db94d15c00b575097d0 Mon Sep 17 00:00:00 2001 From: Nicolas Iooss Date: Thu, 24 Sep 2015 15:06:53 +0200 Subject: [PATCH] Populate account table --- database/Makefile | 9 +- database/platal/factories.py | 96 ++++++++++------------ database/platal/management/__init__.py | 0 database/platal/management/commands/__init__.py | 0 .../commands/fake_email_accounts.py} | 17 +++- database/platal/management/commands/populate_db.py | 47 +++++++++++ database/platal/models.py | 4 +- database/requirements.txt | 2 +- database/settings_sqlitedb.py | 14 ++-- 9 files changed, 120 insertions(+), 69 deletions(-) create mode 100644 database/platal/management/__init__.py create mode 100644 database/platal/management/commands/__init__.py rename database/platal/{static_data.py => management/commands/fake_email_accounts.py} (75%) create mode 100644 database/platal/management/commands/populate_db.py diff --git a/database/Makefile b/database/Makefile index eed59b6..c293fc1 100644 --- a/database/Makefile +++ b/database/Makefile @@ -2,11 +2,16 @@ PIP ?= pip PYTHON ?= python RM ?= rm -f +all: + $(PYTHON) manage.py migrate --noinput + $(PYTHON) manage.py populate_db + $(PYTHON) manage.py fake_email_accounts + install-deps: $(PIP) install -r requirements.txt -all: - $(PYTHON) manage.py syncdb --noinput +shell: + $(PYTHON) manage.py shell clean: $(RM) db.sqlite diff --git a/database/platal/factories.py b/database/platal/factories.py index b955ad2..e224e57 100644 --- a/database/platal/factories.py +++ b/database/platal/factories.py @@ -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)) diff --git a/database/platal/management/__init__.py b/database/platal/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/database/platal/management/commands/__init__.py b/database/platal/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/database/platal/static_data.py b/database/platal/management/commands/fake_email_accounts.py similarity index 75% rename from database/platal/static_data.py rename to database/platal/management/commands/fake_email_accounts.py index 14a0ad5..677a33f 100644 --- a/database/platal/static_data.py +++ b/database/platal/management/commands/fake_email_accounts.py @@ -18,9 +18,20 @@ #* Foundation, Inc., * #* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * #***************************************************************************/ -"""Define the data which must be initially in the database""" +"""Create fake email accounts in the database for testing purpose""" from __future__ import unicode_literals +from django.core.management.base import BaseCommand +from ... import factories, models -# XXX TODO Find a Django-way to do this -AccountType... + +class Command(BaseCommand): + help = "Create fake email accounts in the database" + + def handle(self, *args, **options): + # Remove every account from the database + models.Account.objects.all().delete() + + # Create several accounts + for i in range(20): + account = factories.AccountFactory.create() diff --git a/database/platal/management/commands/populate_db.py b/database/platal/management/commands/populate_db.py new file mode 100644 index 0000000..3b3ba0a --- /dev/null +++ b/database/platal/management/commands/populate_db.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +#*************************************************************************** +#* Copyright (C) 2015 Polytechnique.org * +#* http://opensource.polytechnique.org/ * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU General Public License as published by * +#* the Free Software Foundation; either version 2 of the License, or * +#* (at your option) any later version. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU General Public License for more details. * +#* * +#* You should have received a copy of the GNU General Public License * +#* along with this program; if not, write to the Free Software * +#* Foundation, Inc., * +#* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * +#***************************************************************************/ +"""Populate the database with initial data""" +from __future__ import unicode_literals + +from django.core.management.base import BaseCommand +from ... import models + + +class Command(BaseCommand): + help = "Populate the database with initial data" + + def populate_account_types(self): + account_types = ( + ('ax', 'groups,directory_ax,directory_hidden,edit_directory,user', "Personnel de l'AX"), + ('fx', 'groups,directory_ax,user', "Personnel de la FX"), + ('master', 'groups,mail,directory_private,forums,lists,payment,user', "Master de l'X"), + ('phd', 'groups,mail,directory_private,forums,lists,payment,user', "Docteur de l'X"), + ('pi', 'groups,forums,lists,user', "Elève du programme international"), + ('school', 'groups,directory_ax,user', "Personnel de l'X"), + ('virtual', '', None), + ('x', 'groups,mail,directory_private,forums,lists,payment,gapps,user', "Polytechnicien"), + ('xnet', 'groups', None), + ) + for t, perms, desc in account_types: + models.AccountType(type=t, perms=perms, description=desc).save() + + def handle(self, *args, **options): + self.populate_account_types() diff --git a/database/platal/models.py b/database/platal/models.py index efdad0b..c124932 100644 --- a/database/platal/models.py +++ b/database/platal/models.py @@ -116,7 +116,7 @@ class GeolocCountry(models.Model): class AccountType(models.Model): type = models.CharField(max_length=48, primary_key=True) perms = models.CharField(max_length=321) - description = models.TextField(blank=True) + description = models.TextField(blank=True, null=True) class Meta: db_table = 'account_types' @@ -131,7 +131,7 @@ class Account(models.Model): 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() + is_admin = models.BooleanField(default=False) state = models.CharField(max_length=24) password = models.CharField(max_length=120, blank=True) token = models.CharField(max_length=96, blank=True) diff --git a/database/requirements.txt b/database/requirements.txt index a49dcb0..24556d2 100644 --- a/database/requirements.txt +++ b/database/requirements.txt @@ -1,2 +1,2 @@ -Django>=1.7.0,<1.8.0 +Django>=1.8.0,<1.9.0 factory_boy>=2.3.1,<3.0.0 diff --git a/database/settings_sqlitedb.py b/database/settings_sqlitedb.py index 5c28e40..49e2419 100644 --- a/database/settings_sqlitedb.py +++ b/database/settings_sqlitedb.py @@ -27,12 +27,12 @@ CURRENT_DIR = os.path.abspath(os.path.dirname(__file__)) DEBUG = True INSTALLED_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', + # 'django.contrib.admin', + # 'django.contrib.auth', + # 'django.contrib.contenttypes', + # 'django.contrib.sessions', + # 'django.contrib.messages', + # 'django.contrib.staticfiles', 'platal', ) @@ -77,7 +77,7 @@ USE_I18N = True USE_L10N = True # If you set this to False, Django will not use timezone-aware datetimes. -USE_TZ = True +USE_TZ = False SECRET_KEY = 'Key for development purpose only ;)' -- 2.1.4