Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Added auth_hacks app
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Dec 29, 2011
1 parent 84f42f2 commit 49938b8
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 0 deletions.
Binary file added auth_hacks/.models.py.swp
Binary file not shown.
Empty file added auth_hacks/__init__.py
Empty file.
Binary file added auth_hacks/__init__.pyc
Binary file not shown.
58 changes: 58 additions & 0 deletions auth_hacks/models.py
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 added auth_hacks/models.pyc
Binary file not shown.
Binary file added auth_hacks/tests/.__init__.py.swp
Binary file not shown.
Binary file added auth_hacks/tests/.model_hacks.py.swp
Binary file not shown.
1 change: 1 addition & 0 deletions auth_hacks/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from auth_hacks.tests.model_hacks import *
Binary file added auth_hacks/tests/__init__.pyc
Binary file not shown.
54 changes: 54 additions & 0 deletions auth_hacks/tests/model_hacks.py
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 added auth_hacks/tests/model_hacks.pyc
Binary file not shown.

0 comments on commit 49938b8

Please sign in to comment.