summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Noel <thomas@univnautes-idp.dev>2014-03-16 23:15:18 (GMT)
committerThomas Noel <thomas@univnautes-idp.dev>2014-03-16 23:15:18 (GMT)
commit371245abb5fd6a75763332cf37077a94ae3d68ab (patch)
treed5c2317b6fda5bfb3cb0f233990c51043364eb47
parentf56508132d31ce08d685b993e221c6187f0cd015 (diff)
downloadunivnautes-idp-371245abb5fd6a75763332cf37077a94ae3d68ab.zip
univnautes-idp-371245abb5fd6a75763332cf37077a94ae3d68ab.tar.gz
univnautes-idp-371245abb5fd6a75763332cf37077a94ae3d68ab.tar.bz2
tenant creation form
-rw-r--r--base/templates/univnautes-idp/create-tenant.html15
-rw-r--r--base/templates/univnautes-idp/homepage.html14
-rw-r--r--base/views.py28
-rwxr-xr-xcreate-dirty-tenant.py46
-rw-r--r--univnautes_idp/urls_public.py11
5 files changed, 110 insertions, 4 deletions
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 %}
+<h1>Nouveau tenant</h1>
+<p>
+Indiquer ci-dessous le nom du nouveau tenant choisi.<br />
+Utilisez uniquement des lettres minuscules, des chiffres et le trait d'union.
+</p>
+<form action="" method="post">
+{% csrf_token %}
+{{ form.as_p }}
+<input type="submit" value="Cr&eacute;er" />
+</form>
+{% 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 %}
<h1>Gestion des tenants</h1>
+<ul>
+{% for obj in object_list %}
+{% if obj.schema_name != "public" %}
+<li><a href="//{{ obj.domain_url }}">{{ obj.domain_url }}</a> &mdash; tenant &laquo;{{obj}}&raquo;</li>
+{% endif %}
+{% endfor %}
+</ul>
+<h1>Demander un tenant</h1>
<p>
-<a href="/admin">Administration</a>
+<a href="/create/">Cr&eacute;ation d'un tenant</a>
+</p>
+<h1>Administration</h1>
+<p>
+<a href="/admin/">Interface d'administration compl&egrave;te</a> (technique)
</p>
{% 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'),