| 58 | |
| 59 | class UsernameField(forms.CharField): |
| 60 | def to_python(self, value): |
| 61 | value = super().to_python(value) |
| 62 | if self.max_length is not None and len(value) > self.max_length: |
| 63 | # Normalization can increase the string length (e.g. |
| 64 | # "ff" -> "ff", "½" -> "1⁄2") but cannot reduce it, so there is no |
| 65 | # point in normalizing invalid data. Moreover, Unicode |
| 66 | # normalization is very slow on Windows and can be a DoS attack |
| 67 | # vector. |
| 68 | return value |
| 69 | return unicodedata.normalize("NFKC", value) |
| 70 | |
| 71 | def widget_attrs(self, widget): |
| 72 | return { |