gva/gnuviechadmin/userdbs/forms.py
Jan Dittberner 0e1a84826d 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
2015-01-26 12:06:03 +01:00

66 lines
2.1 KiB
Python

"""
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)