Opened 5 years ago

Closed 2 years ago

#13473 closed New feature (invalid)

BRCPFField and BRCNPJField updates for localflavor.br

Reported by: chronos Owned by: chronos
Component: contrib.localflavor Version: master
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 chronos 5 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 Changed 5 years ago by chronos

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by chronos

  • Has patch set
  • Needs documentation set
  • Needs tests set

comment:3 Changed 5 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 5 years ago by chronos

  • 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 Changed 5 years ago by Guilherme Gondim (semente) <semente@…>

  • Cc semente+djangoproject@… added

comment:6 Changed 5 years ago by Guilherme Gondim (semente) <semente@…>

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

comment:7 Changed 5 years ago by Guilherme Gondim (semente) <semente@…>

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

sorry for the many changes

comment:8 Changed 5 years ago by chronos

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

Changed 5 years ago by chronos

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 Changed 4 years ago by jezdez

Please open a separate ticket for translation updates.

comment:10 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:11 Changed 4 years ago by semente

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

comment:12 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:13 Changed 2 years ago by aaugustin

  • Keywords localflavorsplit added
  • Resolution set to invalid
  • Status changed from new to closed

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