Skip to content

Commit

Permalink
Merge pull request #214 from globocom/fix_database_change_view_form_c…
Browse files Browse the repository at this point in the history
…lean

Fix database change view form clean
  • Loading branch information
otherpirate authored Sep 1, 2016
2 parents 5c59896 + 2ba8e21 commit d654c6f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 47 deletions.
4 changes: 2 additions & 2 deletions dbaas/logical/admin/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,13 @@ def generate_random_string(length, stringset=string.ascii_letters + string.digit
environment, lognit_environment, uri)
try:
database_logs = json.loads(database_logs)
except Exception, e:
except Exception as e:
pass
else:
for database_log in database_logs:
try:
items = database_log['items']
except KeyError, e:
except KeyError as e:
pass
else:
parsed_logs = "\n".join(
Expand Down
104 changes: 59 additions & 45 deletions dbaas/logical/forms/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,54 +170,82 @@ def __init__(self, *args, **kwargs):
else:
self.fields['is_in_quarantine'].widget = forms.HiddenInput()

def clean(self):
cleaned_data = super(DatabaseForm, self).clean()
def _validate_description(self, cleaned_data):
if 'description' in cleaned_data:
if not cleaned_data.get('description', None):
self._errors["description"] = self.error_class(
[_("Description: This field is required.")])

# if there is an instance, that means that we are in a edit page and therefore
# it should return the default cleaned_data
if self.instance and self.instance.id:
return cleaned_data
def _validate_contacts(self, cleaned_data):
if 'contacts' in cleaned_data:
if not cleaned_data.get('contacts', None):
self._errors["contacts"] = self.error_class(
[_("Contacts: This field is required.")])

# TODO: change model field to blank=False
if 'team' in cleaned_data:
team = cleaned_data['team']
LOG.debug("team: %s" % team)
def _validate_project(self, cleaned_data):
if 'project' in cleaned_data:
if not cleaned_data.get('project', None):
self._errors["project"] = self.error_class(
[_("Project: This field is required.")])

if not team:
def _validate_team(self, cleaned_data):
if 'team' in cleaned_data:
if not cleaned_data['team']:
LOG.warning("No team specified in database form")
self._errors["team"] = self.error_class(
[_("Team: This field is required.")])

if not self.is_valid():
raise forms.ValidationError(self.errors)
def _validate_team_resources(self, cleaned_data):
team = cleaned_data['team']

if team:
dbs = team.databases_in_use_for(cleaned_data['environment'])
database_alocation_limit = team.database_alocation_limit
LOG.debug("dbs: %s | type: %s" % (dbs, type(dbs)))

if (database_alocation_limit != 0 and len(dbs) >= database_alocation_limit):
LOG.warning("The database alocation limit of %s has been exceeded for the selected team %s => %s" % (
database_alocation_limit, team, list(dbs)))
self._errors["team"] = self.error_class(
[_("The database alocation limit of %s has been exceeded for the selected team: %s") % (database_alocation_limit, list(dbs))])

def _validate_name(self, cleaned_data):
if len(cleaned_data['name']) > 40:
self._errors["name"] = self.error_class(
[_("Database name too long")])

plan = cleaned_data['plan']
driver = DriverFactory.get_driver_class(plan.engines[0].name)
if cleaned_data['name'] in driver.RESERVED_DATABASES_NAME:
raise forms.ValidationError(
_("%s is a reserved database name" % cleaned_data['name']))

def clean(self):
cleaned_data = super(DatabaseForm, self).clean()

# if there is an instance, that means that we are in a edit page and therefore
# it should return the default cleaned_data
if self.instance and self.instance.id:
self._validate_project(cleaned_data)
self._validate_description(cleaned_data)
self._validate_contacts(cleaned_data)
self._validate_team(cleaned_data)
return cleaned_data

if not self.is_valid():
raise forms.ValidationError(self.errors)

if 'plan' in cleaned_data:
plan = cleaned_data.get('plan', None)
if not plan:
self._errors["plan"] = self.error_class(
[_("Plan: This field is required.")])

if 'project' in cleaned_data:
project = cleaned_data.get('project', None)
if not project:
self._errors["project"] = self.error_class(
[_("Project: This field is required.")])

if 'description' in cleaned_data:
description = cleaned_data.get('description', None)
if not description:
self._errors["description"] = self.error_class(
[_("Description: This field is required.")])

if 'contacts' in cleaned_data:
contacts = cleaned_data.get('contacts', None)
if not contacts:
self._errors["contacts"] = self.error_class(
[_("This field is required.")])
self._validate_name(cleaned_data)
self._validate_project(cleaned_data)
self._validate_description(cleaned_data)
self._validate_contacts(cleaned_data)
self._validate_team(cleaned_data)

if 'environment' in cleaned_data:
environment = cleaned_data.get('environment', None)
Expand All @@ -232,21 +260,7 @@ def clean(self):
[_("this name already exists in the selected environment")])
del cleaned_data["name"]

# validate if the team has available resources
dbs = team.databases_in_use_for(environment)
database_alocation_limit = team.database_alocation_limit
LOG.debug("dbs: %s | type: %s" % (dbs, type(dbs)))
if (database_alocation_limit != 0 and len(dbs) >= database_alocation_limit):
LOG.warning("The database alocation limit of %s has been exceeded for the selected team %s => %s" % (
database_alocation_limit, team, list(dbs)))
self._errors["team"] = self.error_class(
[_("The database alocation limit of %s has been exceeded for the selected team: %s") % (database_alocation_limit, list(dbs))])

driver = DriverFactory.get_driver_class(plan.engines[0].name)
if 'name' in cleaned_data and cleaned_data['name'] in driver.RESERVED_DATABASES_NAME:
raise forms.ValidationError(
_("%s is a reserved database name" % cleaned_data['name']))

self._validate_team_resources(cleaned_data)
if database_name_evironment_constraint(database_name, environment.name):
raise forms.ValidationError(
_('%s already exists in production!') % database_name
Expand Down

0 comments on commit d654c6f

Please sign in to comment.