#13473: BRCPFField and BRCNPJField updates for localflavor.br
-------------------------------------------------+--------------------------
          Reporter:  chronos                     |         Owner:  chronos  
            Status:  new                         |     Milestone:  1.3      
         Component:  django.contrib.localflavor  |       Version:  SVN      
        Resolution:                              |      Keywords:  CPF, CNPJ
             Stage:  Unreviewed                  |     Has_patch:  0        
        Needs_docs:  0                           |   Needs_tests:  0        
Needs_better_patch:  0                           |  
-------------------------------------------------+--------------------------
Changes (by chronos):

  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 = 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'''
 {{{
 #!python
 # -*- 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'''
 {{{
 #!python
 # -*- 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:

 {{{
 #!python
 >>> 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.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13473#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to