svetlyak40wt/django-fields — Issue #17: EncryptedCharField doesn't validate length
Author: dustinlacewell
Created: 2012-02-24 07:30:57+00:00
State: open

Instead of raising a validationerror at the appropriate time, a ValueError is raised at save time:

```bash
Traceback:
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/contrib/admin/options.py" in wrapper
226. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/contrib/admin/sites.py" in inner
186. return view(request, *args, **kwargs)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/transaction.py" in _commit_on_success
240. res = func(*args, **kw)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/contrib/admin/options.py" in change_view
833. self.save_formset(request, form, formset, change=True)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/contrib/admin/options.py" in save_formset
563. formset.save()
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/forms/models.py" in save
522. return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/forms/models.py" in save_existing_objects
640. saved_instances.append(self.save_existing(form, obj, commit=commit))
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/forms/models.py" in save_existing
510. return form.save(commit=commit)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/forms/models.py" in save
407. fail_message, commit, exclude=self._meta.exclude)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/forms/models.py" in save_instance
78. instance.save()
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/models/base.py" in save
410. self.save_base(force_insert=force_insert, force_update=force_update)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/models/base.py" in save_base
474. rows = manager.filter(pk=pk_val)._update(values)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/models/query.py" in _update
442. query.add_update_fields(values)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/models/sql/subqueries.py" in add_update_fields
245. val = field.get_db_prep_save(val)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
192. return self.get_db_prep_value(value)
File "/iscape/sites/ahcdeploy/lib/python2.5/site-packages/django_fields/fields.py" in get_db_prep_value
106. str(len(value)) + " > " + str(self.unencrypted_length))

Exception Type: ValueError at /admin/cne/registration/1/
Exception Value: Field value longer than max allowed: 89 > 64
```

```python

class EncryptedCharField(BaseEncryptedField):
__metaclass__ = models.SubfieldBase

def get_internal_type(self):
return "CharField"

def formfield(self, **kwargs):
defaults = {'max_length': self.max_length}
defaults.update(kwargs)
return super(EncryptedCharField, self).formfield(**defaults)

def get_db_prep_value(self, value, connection=None, prepared=False):
if value is not None and not self._is_encrypted(value):
if len(value) > self.unencrypted_length:
raise ValueError("Field value longer than max allowed: " +
str(len(value)) + " > " + str(self.unencrypted_length))
return super(EncryptedCharField, self).get_db_prep_value(
value,
connection=connection,
prepared=prepared,
)
```

https://github.com/svetlyak40wt/django-fields/blob/master/src/django_fields/fields.py#L106