implement setup of new user databases

- implement userdbs.forms.AddUserDatabaseForm
- implement userdbs.views.AddUserDatabase
- add new URL pattern 'add_userdatabase' in userdbs.urls
- add templates userdbs/base.html and userdbs/userdatabase_create.html
- add generated code documentation for new modules
This commit is contained in:
Jan Dittberner 2015-01-26 12:06:03 +01:00
parent 2447f558e4
commit 0e1a84826d
6 changed files with 200 additions and 0 deletions

View file

@ -17,6 +17,13 @@
.. automodule:: userdbs.apps .. automodule:: userdbs.apps
:py:mod:`forms <userdbs.forms>`
-------------------------------
.. automodule:: userdbs.forms
:members:
:py:mod:`models <userdbs.models>` :py:mod:`models <userdbs.models>`
--------------------------------- ---------------------------------
@ -34,3 +41,17 @@
.. automodule:: userdbs.templatetags.userdb .. automodule:: userdbs.templatetags.userdb
:members: :members:
:py:mod:`urls <userdbs.urls>`
-----------------------------
.. automodule:: userdbs.urls
:members:
:py:mod:`views <userdbs.views>`
-------------------------------
.. automodule:: userdbs.views
:members:

View file

@ -0,0 +1 @@
{% extends "base.html" %}

View file

@ -0,0 +1,31 @@
{% extends "userdbs/base.html" %}
{% load i18n crispy_forms_tags %}
{% block title %}{{ block.super }} - {% spaceless %}
{% if user == customer %}
{% blocktrans %}Add new Database{% endblocktrans %}
{% else %}
{% blocktrans with full_name=customer.get_full_name %}Add new Database for Customer {{ full_name }}{% endblocktrans %}
{% endif %}
{% endspaceless %}{% endblock title %}
{% block page_title %}{% spaceless %}
{% if user == customer %}
{% blocktrans %}Add new Database{% endblocktrans %}
{% else %}
{% blocktrans with full_name=customer.get_full_name %}Add new Database for Customer {{ full_name }}{% endblocktrans %}
{% endif %}
{% endspaceless %}{% endblock page_title %}
{% block content %}
<p>{% blocktrans %}Please enter a password for a new database user for your database.{% endblocktrans %}</p>
{% crispy form %}
{% endblock content %}
{% block extra_js %}
<script type="text/javascript">
$(document).ready(function() {
$('input[type=password]').val('').first().focus();
});
</script>
{% endblock %}

View file

@ -0,0 +1,66 @@
"""
This module defines form classes for user database editing.
"""
from __future__ import absolute_import, unicode_literals
from django import forms
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
Submit,
)
from .models import (
DB_TYPES,
UserDatabase,
)
from gvawebcore.forms import PasswordModelFormMixin
class AddUserDatabaseForm(forms.ModelForm, PasswordModelFormMixin):
"""
This form is used to create new user database instances.
"""
db_type = forms.TypedChoiceField(
label=_('Database type'),
choices=DB_TYPES,
widget=forms.RadioSelect,
coerce=int,
)
class Meta:
model = UserDatabase
fields = []
def __init__(self, *args, **kwargs):
self.hosting_package = kwargs.pop('hostingpackage')
self.available_dbtypes = kwargs.pop('dbtypes')
super(AddUserDatabaseForm, self).__init__(*args, **kwargs)
self.fields['db_type'].choices = self.available_dbtypes
if len(self.available_dbtypes) == 1:
self.fields['db_type'].initial = self.available_dbtypes[0][0]
self.fields['db_type'].widget = forms.HiddenInput()
self.helper = FormHelper()
self.helper.form_action = reverse(
'add_userdatabase', kwargs={'package': self.hosting_package.id})
self.helper.add_input(Submit('submit', _('Create database')))
def save(self, commit=True):
"""
Setup a new database with a new database user with the specified
password.
:param boolean commit: whether to save the created database
:return: database instance
:rtype: :py:class:`userdbs.models.UserDatabase`
"""
data = self.cleaned_data
self.instance = UserDatabase.objects.create_userdatabase_with_user(
data['db_type'], self.hosting_package.osuser,
password=data['password1'], commit=commit)
return super(AddUserDatabaseForm, self).save(commit)

View file

@ -0,0 +1,17 @@
"""
This module defines the URL patterns for user database views.
"""
from __future__ import absolute_import, unicode_literals
from django.conf.urls import patterns, url
from .views import (
AddUserDatabase,
)
urlpatterns = patterns(
'',
url(r'^(?P<package>\d+)/create$',
AddUserDatabase.as_view(), name='add_userdatabase'),
)

View file

@ -0,0 +1,64 @@
"""
This module defines views for user database handling.
"""
from __future__ import absolute_import, unicode_literals
from django.shortcuts import redirect
from django.utils.translation import ugettext as _
from django.views.generic.edit import (
CreateView,
)
from django.contrib import messages
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from gvawebcore.views import HostingPackageAndCustomerMixin
from .forms import (
AddUserDatabaseForm,
)
from .models import (
DB_TYPES,
UserDatabase,
)
class AddUserDatabase(
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, CreateView
):
"""
This view is used to setup new user databases.
"""
model = UserDatabase
context_object_name = 'database'
template_name_suffix = '_create'
form_class = AddUserDatabaseForm
def _get_dbtypes(self, hostingpackage):
retval = []
db_options = hostingpackage.get_databases()
for opt in db_options:
dbs_of_type = UserDatabase.objects.filter(
db_user__osuser=hostingpackage.osuser,
db_user__db_type=opt['db_type']).count()
if dbs_of_type < opt['number']:
retval.append((opt['db_type'], DB_TYPES[opt['db_type']]))
return retval
def get_form_kwargs(self):
kwargs = super(AddUserDatabase, self).get_form_kwargs()
kwargs['hostingpackage'] = self.get_hosting_package()
kwargs['dbtypes'] = self._get_dbtypes(kwargs['hostingpackage'])
return kwargs
def form_valid(self, form):
userdatabase = form.save()
messages.success(
self.request,
_('Successfully create new {type} database {dbname} for user '
'{dbuser}').format(
type=userdatabase.db_user.db_type,
dbname=userdatabase.db_name, db_user=userdatabase.db_user)
)
return redirect(self.get_hosting_package())