Opened 15 years ago

Closed 12 years ago

#13473 closed New feature (invalid)

BRCPFField and BRCNPJField updates for localflavor.br

Reported by: Felipe 'chronos' Prenholato Owned by: Felipe 'chronos' Prenholato
Component: contrib.localflavor Version: dev
Severity: Normal Keywords: CPF, CNPJ, localflavorsplit
Cc: semente+djangoproject@…, django-l10n-portuguese@…, semente+django@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Django 1.1 (or 1.0) are added two fields in localflavor.br.forms for CPF and CNPJ, but this ones not have a similar o models and not normalize data to save in database (look #10895). I started some changes to have model fields, number generators (useful for tests), one 'type' and integration of model fields to form fields for CPF and CNPJ. I adding here a diff of my initial work and a link to my repo at github.

Attachments (1)

ticket13473.patch (80.2 KB ) - added by Felipe 'chronos' Prenholato 14 years ago.
Diff of work at http://github.com/chronossc/django/tree/ticket13473 against http://github.com/django/django at file upload date. This patch contains changes requested in #13495 about refactoring of localflavor tests.

Download all attachments as: .zip

Change History (14)

comment:1 by Felipe 'chronos' Prenholato, 15 years ago

About

I added attachment:ticket13473.patch containing changes against trunk to have CPF/CNPJ fields in models, that use field of forms, and CPF/CNPJ types and number generators.
CPF/CNPJ types and generators are located at django.contrib.localflavor.br.br_cpfcnpj
Models and Forms fields are located at models and forms modules.

Usage

models.py

# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.localflavor.br.models import BRCPFField,BRCNPJField

class CPFCNPJModel(models.Model):
    cnpj = BRCNPJField(null=True,blank=True)
    cpf = BRCPFField(null=True,blank=True)

    def __unicode__(self):
        return u"cpf: %s ,  cnpj: %s" % (self.cpf or u'',self.cnpj or u'')

forms.py

# -*- coding: utf-8 -*-
from django import forms
from models import CPFCNPJModel


class CPFCNPJForm(forms.ModelForm):
    class Meta:
        model = CPFCNPJMode

When you type in rendered form in template any know format for input works, here as example using python shell using as base one CPF/CNPJ generated by number generators:

>>> from webui.forms import CPFCNPJModel as model, CPFCNPJForm as form
>>> from django.contrib.localflavor.br.br_cpfcnpj import CPFGenerator,CNPJGenerator
>>>
>>> # generate numbers
>>> CPFGenerator()
'56146650560'
>>> CNPJGenerator()
'97196641000871'
>>>
>>> # init form in various number formats and check for errors
>>> f = form({'cpf':56146650560,'cnpj':97196641000871})
>>> f.is_valid()
True
>>> f = form({'cpf':'56146650560','cnpj':'97196641000871'})
>>> f.is_valid()
True
>>> f = form({'cpf':'561.466.505-60','cnpj':'97.196.641/0008-71'})
>>> f.is_valid()
True
>>>
>>> # Save form
>>> f.save()
<CPFCNPJModel: cpf: 561.466.505-60 ,  cnpj: 97.196.641/0008-71>
>>>
>>> # Show model properties
>>> model.objects.all()
[<CPFCNPJModel: cpf: 561.466.505-60 ,  cnpj: 97.196.641/0008-71>]
>>> m=model.objects.all()[0]
>>> 
>>> m.cpf,m.cnpj
(CPF('56146650560'), CNPJ('97196641000871'))
>>> str(m.cpf),str(m.cnpj)
('561.466.505-60', '97.196.641/0008-71')
>>> m.cpf.single,m.cnpj.single
(u'56146650560', u'97196641000871')

And here is how is stored in database:

$ m dbshell <<< "select * from webui_cpfcnpjmodel;"
 id |      cnpj      |     cpf     
----+----------------+-------------
  2 | 97196641000871 | 56146650560
(1 row)

Todo

I'm not created yet unit tests and some translations, etc, but I'll soon, if someone wanna to contribute, show errors, fixes, etc, plz take a look on for last changes.

comment:2 by Felipe 'chronos' Prenholato, 15 years ago

Has patch: set
Needs documentation: set
Needs tests: set

comment:3 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by Felipe 'chronos' Prenholato, 15 years ago

Needs tests: unset

Added tests, locale translations for pt-BR. This ticket depends of #13495 about localflavor tests refactoring to support various countries.

comment:5 by Guilherme Gondim (semente) <semente@…>, 15 years ago

Cc: semente+djangoproject@… added

comment:6 by Guilherme Gondim (semente) <semente@…>, 15 years ago

Cc: django-l10n-portugueses@… added; semente+djangoproject@… removed

comment:7 by Guilherme Gondim (semente) <semente@…>, 15 years ago

Cc: semente+djangoproject@… django-l10n-portuguese@… added; django-l10n-portugueses@… removed

sorry for the many changes

comment:8 by Felipe 'chronos' Prenholato, 14 years ago

added some more tests, fixed validations and default values of fields.

by Felipe 'chronos' Prenholato, 14 years ago

Attachment: ticket13473.patch added

Diff of work at http://github.com/chronossc/django/tree/ticket13473 against http://github.com/django/django at file upload date. This patch contains changes requested in #13495 about refactoring of localflavor tests.

comment:9 by Jannis Leidel, 14 years ago

Please open a separate ticket for translation updates.

comment:10 by Julien Phalip, 14 years ago

Severity: Normal
Type: New feature

comment:11 by Guilherme Gondim, 13 years ago

Cc: semente+django@… added
Easy pickings: unset
UI/UX: unset

comment:12 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:13 by Aymeric Augustin, 12 years ago

Keywords: localflavorsplit added
Resolution: invalid
Status: newclosed

django.contrib.localflavor is now deprecated — see https://docs.djangoproject.com/en/dev/ref/contrib/localflavor/

A repository was created for each localflavor at https://github.com/django/django-localflavor-? (Replace with the country code.)

If you're still interested in this ticket, could you create a pull request on that repository?

Sorry for not resolving this issue earlier, and thanks for your input!

Note: See TracTickets for help on using tickets.
Back to Top