From 371245abb5fd6a75763332cf37077a94ae3d68ab Mon Sep 17 00:00:00 2001 From: Thomas Noel Date: Mon, 17 Mar 2014 00:15:18 +0100 Subject: [PATCH] tenant creation form --- .../univnautes-idp/create-tenant.html | 15 ++++++ base/templates/univnautes-idp/homepage.html | 14 +++++- base/views.py | 28 ++++++++++- create-dirty-tenant.py | 46 +++++++++++++++++++ univnautes_idp/urls_public.py | 11 ++++- 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 base/templates/univnautes-idp/create-tenant.html create mode 100755 create-dirty-tenant.py diff --git a/base/templates/univnautes-idp/create-tenant.html b/base/templates/univnautes-idp/create-tenant.html new file mode 100644 index 0000000..cacacfd --- /dev/null +++ b/base/templates/univnautes-idp/create-tenant.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} +{% load i18n %} + +{% block content %} +

Nouveau tenant

+

+Indiquer ci-dessous le nom du nouveau tenant choisi.
+Utilisez uniquement des lettres minuscules, des chiffres et le trait d'union. +

+
+{% csrf_token %} +{{ form.as_p }} + +
+{% endblock %} diff --git a/base/templates/univnautes-idp/homepage.html b/base/templates/univnautes-idp/homepage.html index 612a2c8..4cbaa52 100644 --- a/base/templates/univnautes-idp/homepage.html +++ b/base/templates/univnautes-idp/homepage.html @@ -3,7 +3,19 @@ {% block content %}

Gestion des tenants

+ +

Demander un tenant

-Administration +Création d'un tenant +

+

Administration

+

+Interface d'administration complète (technique)

{% endblock %} diff --git a/base/views.py b/base/views.py index 60f00ef..8a49bea 100644 --- a/base/views.py +++ b/base/views.py @@ -1 +1,27 @@ -# Create your views here. +import subprocess +from django.views.generic.edit import FormView +from django.contrib import messages +from django import forms +from entrouvert.djommon.multitenant.models import Tenant + +CREATION_SCRIPT = '/home/thomas/univnautes-idp/create-dirty-tenant.py' + +class TenantCreateForm(forms.Form): + schema_name = forms.RegexField(regex='^[a-z0-9_-]+$', + required=True, + max_length=16) + +class TenantCreateView(FormView): + template_name = 'univnautes-idp/create-tenant.html' + form_class = TenantCreateForm + success_url = '/' + + def form_valid(self, form): + schema_name = form.cleaned_data['schema_name'] + r = subprocess.call([CREATION_SCRIPT, schema_name]) + if r: + messages.error(self.request, 'cannot create %s tenant (error %r)' % (schema_name, r)) + else: + messages.info(self.request, '%s tenant created' % schema_name) + return super(TenantCreateView, self).form_valid(form) + diff --git a/create-dirty-tenant.py b/create-dirty-tenant.py new file mode 100755 index 0000000..a6c1394 --- /dev/null +++ b/create-dirty-tenant.py @@ -0,0 +1,46 @@ +#!/bin/sh -e + +cd /home/thomas/univnautes-idp +. /home/thomas/venv/bin/activate +export UNIVNAUTES_IDP_SETTINGS_INI=/home/thomas/univnautes-idp/settings.ini.example + +TENANT=$(echo "$1" | tr -dc a-z0-9_-) +if [ x"$1" != x"$TENANT" ] +then + echo $1 not a valid tenant name + exit 1 +fi + +python << EOF +import os, sys +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "univnautes_idp.settings") +from entrouvert.djommon.multitenant.models import Tenant +if Tenant.objects.filter(schema_name="${TENANT}"): + print >> sys.stderr, "tenant ${TENANT} already exists" + sys.exit(1) +EOF + +echo "" +echo "creating tenant..." +echo "" +python manage.py create-tenant $TENANT.univnautes-idp.dev.entrouvert.org $TENANT + +echo "" +echo "creating 'admin' user in this tenant" +echo "" +python << EOF +import os, sys +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "univnautes_idp.settings") +from django.db import connection +from django.contrib.auth.models import User +connection.set_schema("${TENANT}") +u=User(username='admin') +u.set_password("${TENANT}") +u.is_staff=True +u.is_superuser=True +u.save() +print 'users:', User.objects.all() +EOF + +echo "" +echo "tenant ${TENANT} created" diff --git a/univnautes_idp/urls_public.py b/univnautes_idp/urls_public.py index e2e2024..d97d989 100644 --- a/univnautes_idp/urls_public.py +++ b/univnautes_idp/urls_public.py @@ -1,12 +1,19 @@ from django.conf.urls import patterns, url, include -from django.views.generic import TemplateView, RedirectView +from django.views.generic import ListView, CreateView, RedirectView from authentic2.urls import urlpatterns as authentic2_urlpatterns +from entrouvert.djommon.multitenant.models import Tenant +from base.views import TenantCreateView from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', - url(r'^$', TemplateView.as_view(template_name='univnautes-idp/homepage.html')), + url(r'^$', ListView.as_view( + model=Tenant, + template_name='univnautes-idp/homepage.html') + ), + #url(r'^create/$', CreateView.as_view(model=Tenant)), + url(r'^create/$', TenantCreateView.as_view()), url(r'^admin/', include(admin.site.urls)), url(r'^admin_tools/', include('admin_tools.urls')), url(r'^logout/$', RedirectView.as_view(url='/admin/logout'), name='auth_logout'),