def clean(self):
        cleaned_data = super(CloneDatabaseForm, self).clean()
        if 'database_clone' in cleaned_data:

            origindatabase = Database.objects.get(
                pk=cleaned_data['origin_database_id'])

            # for infra in DatabaseInfra.objects.filter(environment=origindatabase.environment,plan=origindatabase.plan):
            #    if infra.databases.filter(name=cleaned_data['database_clone']):
            #        self._errors["database_clone"] = self.error_class([_("this name already exists in the selected environment")])

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

            dbs = origindatabase.team.databases_in_use_for(
                origindatabase.environment)

            database_alocation_limit = origindatabase.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 team: %s => %s" % (
                    database_alocation_limit, origindatabase.team, list(dbs)))
                raise forms.ValidationError([_("The database alocation limit of %s has been exceeded for the team:  %s => %s") % (
                    database_alocation_limit, origindatabase.team, list(dbs))])

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

            environment = cleaned_data.get('environment', None)
            database_name = cleaned_data.get('database_clone', None)

            if Database.objects.filter(name=database_name,
                                       environment=environment):
                raise forms.ValidationError(
                    _("There is already a database called {} on {}".format(database_name,
                                                                           environment)))

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

            if self._errors:
                return cleaned_data

        return cleaned_data
dataset/ETHPy150Open globocom/database-as-a-service/dbaas/logical/forms/database.py/CloneDatabaseForm.clean/original