File: validation.py

package info (click to toggle)
mysql-connector-python 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,716 kB
  • ctags: 5,129
  • sloc: python: 23,339; makefile: 28
file content (58 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# MySQL Connector/Python - MySQL driver written in Python.

import django
from django.db.backends import BaseDatabaseValidation

if django.VERSION < (1, 7):
    from django.db import models
else:
    from django.core import checks
    from django.db import connection


class DatabaseValidation(BaseDatabaseValidation):
    if django.VERSION < (1, 7):
        def validate_field(self, errors, opts, f):
            """
            MySQL has the following field length restriction:
            No character (varchar) fields can have a length exceeding 255
            characters if they have a unique index on them.
            """
            varchar_fields = (models.CharField,
                              models.CommaSeparatedIntegerField,
                              models.SlugField)
            if isinstance(f, varchar_fields) and f.max_length > 255 and f.unique:
                msg = ('"%(name)s": %(cls)s cannot have a "max_length" greater '
                       'than 255 when using "unique=True".')
                errors.add(opts, msg % {'name': f.name,
                                        'cls': f.__class__.__name__})

    else:
        def check_field(self, field, **kwargs):
            """
            MySQL has the following field length restriction:
            No character (varchar) fields can have a length exceeding 255
            characters if they have a unique index on them.
            """
            # Django 1.7
            errors = super(DatabaseValidation, self).check_field(field,
                                                                 **kwargs)

            # Ignore any related fields.
            if getattr(field, 'rel', None) is None:
                field_type = field.db_type(connection)

                if (field_type.startswith('varchar')  # Look for CharFields...
                        and field.unique  # ... that are unique
                        and (field.max_length is None or
                                     int(field.max_length) > 255)):
                    errors.append(
                        checks.Error(
                            ('MySQL does not allow unique CharFields to have a '
                             'max_length > 255.'),
                            hint=None,
                            obj=field,
                            id='mysql.E001',
                        )
                )
            return errors