This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
import sys | ||
|
||
from django.conf import settings | ||
from django.core import validators | ||
|
||
|
||
MAX_LENGTH = getattr(settings, 'USERNAME_MAX_LENGTH', 100) | ||
|
||
|
||
def hack_validators(field): | ||
for i, v in enumerate(field.validators): | ||
if isinstance(v, validators.MaxLengthValidator): | ||
field.validators.pop(i) | ||
|
||
field.validators.insert(0, validators.MaxLengthValidator(MAX_LENGTH)) | ||
|
||
|
||
def hack_model(model_class, field_name): | ||
field = model_class._meta.get_field(field_name) | ||
field.max_length = MAX_LENGTH | ||
hack_validators(field) | ||
|
||
|
||
def hack_form(form_class, field_name): | ||
if hasattr(form_class, 'declared_fields') and form_class.declared_fields: | ||
fields = form_class.declared_fields | ||
elif hasattr(form_class, 'base_fields') and form_class.base_fields: | ||
fields = form_class.base_fields | ||
else: | ||
raise TypeError('Provided object: %s doesnt seem to be a valid Form or ' | ||
'ModelForm class.' % form_class) | ||
username = fields[field_name] | ||
username.help_text = "Obrigatório. %s caracteres ou menos. Somente letras, dígitos e @/./+/-/_." % MAX_LENGTH | ||
hack_validators(username) | ||
username.max_length = MAX_LENGTH | ||
username.widget.attrs['maxlength'] = MAX_LENGTH | ||
|
||
|
||
def hack_forms(): | ||
forms = [ | ||
"django.contrib.auth.forms.UserCreationForm", | ||
"django.contrib.auth.forms.UserChangeForm", | ||
"django.contrib.auth.forms.AuthenticationForm", | ||
] | ||
|
||
for f in forms: | ||
module_name, sep, class_name = f.rpartition(".") | ||
if not module_name in sys.modules: | ||
__import__(module_name) | ||
module = sys.modules[module_name] | ||
cls = getattr(module, class_name) | ||
hack_form(cls, "username") | ||
|
||
|
||
from django.contrib.auth import models | ||
hack_model(models.User, "username") | ||
hack_forms() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from auth_hacks.tests.model_hacks import * |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
from django import forms | ||
from django.db import models as django_models | ||
|
||
from auth_hacks import models | ||
|
||
|
||
class SampleModel(django_models.Model): | ||
name = django_models.CharField(max_length=30) | ||
|
||
class Meta: | ||
app_label = 'auth_hacks' | ||
|
||
|
||
class SampleModelForm(forms.ModelForm): | ||
class Meta: | ||
model = SampleModel | ||
|
||
|
||
class AuthHackModelTestCase(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
models.hack_model(SampleModel, "name") | ||
|
||
def test_deve_mudar_max_length_do_field_para_100(self): | ||
self.assertEquals(100, SampleModel._meta.get_field("name").max_length) | ||
|
||
def test_deve_mudar_validators_do_field(self): | ||
validator = SampleModel._meta.get_field("name").validators[0] | ||
self.assertEquals(100, validator.limit_value) | ||
|
||
|
||
class AuthHackFormTestCase(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
models.hack_form(SampleModelForm, "name") | ||
cls.field = SampleModelForm.base_fields["name"] | ||
|
||
def test_deve_mudar_max_length_de_field_no_formulario(self): | ||
self.assertEquals(100, self.field.max_length) | ||
|
||
def test_deve_mudar_attr_max_length_do_widget_do_formulario(self): | ||
self.assertEquals(100, self.field.widget.attrs['maxlength']) | ||
|
||
def test_deve_mudar_help_text_para_exibir_descricao_correta(self): | ||
self.assertEquals(u"Obrigatório. 100 caracteres ou menos. Somente letras, dígitos e @/./+/-/_.", self.field.help_text) | ||
|
||
def test_deve_mudar_validator_do_field(self): | ||
validator = self.field.validators[0] | ||
self.assertEquals(100, validator.limit_value) |
Binary file not shown.