@ -1,18 +1,13 @@
# -*- coding: utf-8 -*-
from django . contrib . contenttypes . fields import ( GenericForeignKey ,
GenericRelation )
from django . contrib . contenttypes . models import ContentType
from django . core . validators import MaxValueValidator , MinValueValidator
from django . db import models
from django . utils . translation import gettext as _
from sigi . apps . utils import SearchField
class UnidadeFederativa ( models . Model ) :
""" Modelo que representa um estado brasileiro
"""
REGIAO_CHOICES = (
( ' CO ' , _ ( ' Centro-Oeste ' ) ) ,
( ' NE ' , _ ( ' Nordeste ' ) ) ,
@ -21,18 +16,19 @@ class UnidadeFederativa(models.Model):
( ' SL ' , _ ( ' Sul ' ) ) ,
)
codigo_ibge = models . PositiveIntegerField (
' código IBGE ' ,
_ ( ' código IBGE ' ) ,
primary_key = True ,
unique = True ,
help_text = _ ( ' Código do estado segundo IBGE. ' )
)
nome = models . CharField ( _ ( ' N ome UF' ) , max_length = 25 )
nome = models . CharField ( _ ( ' n ome UF' ) , max_length = 25 )
# Campo de busca em caixa baixa sem acento
search_text = SearchField ( field_names = [ ' nome ' ] )
sigla = models . CharField (
_ ( ' sigla ' ) ,
max_length = 2 ,
unique = True ,
help_text = _ ( " Exemplo " ) + " : <em>MG</em>. " ,
help_text = _ ( " Exemplo: <em>MG</em>. " ) ,
)
regiao = models . CharField ( _ ( ' região ' ) , max_length = 2 , choices = REGIAO_CHOICES )
populacao = models . PositiveIntegerField ( _ ( ' população ' ) )
@ -42,12 +38,12 @@ class UnidadeFederativa(models.Model):
verbose_name = _ ( ' Unidade Federativa ' )
verbose_name_plural = _ ( ' Unidades Federativas ' )
def __unicode __( self ) :
def __str __( self ) :
return self . nome
class Mesorregiao ( models . Model ) :
codigo_ibge = models . PositiveIntegerField (
_ ( ' C ódigo IBGE' ) ,
_ ( ' c ódigo IBGE' ) ,
primary_key = True ,
unique = True ,
help_text = _ ( ' Código da mesorregião segundo o IBGE ' )
@ -57,59 +53,57 @@ class Mesorregiao(models.Model):
on_delete = models . CASCADE ,
verbose_name = _ ( ' UF ' )
)
nome = models . CharField ( _ ( " N ome mesorregião" ) , max_length = 100 )
nome = models . CharField ( _ ( " n ome mesorregião" ) , max_length = 100 )
# Campo de busca em caixa baixa sem acento
search_text = SearchField ( field_names = [ ' nome ' ] )
class Meta :
ordering = ( ' uf ' , ' nome ' , )
verbose_name , verbose_name_plural = _ ( ' Mesorregião ' ) , _ ( ' Mesorregiões ' )
verbose_name = _ ( ' mesorregião ' ) ,
verbose_name_plural = _ ( ' mesorregiões ' )
def __unicode __( self ) :
def __str __( self ) :
return self . nome
class Microrregiao ( models . Model ) :
codigo_ibge = models . PositiveIntegerField (
_ ( ' C ódigo IBGE' ) ,
_ ( ' c ódigo IBGE' ) ,
primary_key = True ,
unique = True ,
help_text = _ ( ' Código da microrregião segundo o IBGE ' )
)
mesorregiao = models . ForeignKey (
Mesorregiao ,
on_delete = models . CASCADE
on_delete = models . CASCADE ,
verbose_name = _ ( ' mesorregião ' ) ,
)
nome = models . CharField ( _ ( " N ome microrregião" ) , max_length = 100 )
nome = models . CharField ( _ ( " n ome microrregião" ) , max_length = 100 )
# Campo de busca em caixa baixa sem acento
search_text = SearchField ( field_names = [ ' nome ' ] )
class Meta :
ordering = ( ' nome ' , )
verbose_name , verbose_name_plural = _ ( ' Microrregião ' ) , _ ( ' Microrregiões ' )
verbose_name = _ ( ' microrregião ' )
verbose_name_plural = _ ( ' microrregiões ' )
def __unicode __( self ) :
return " %s ( %s ) " % ( self . nome , self . mesorregiao . nome )
def __str __( self ) :
return f " { self . nome } ( { self . mesorregiao . nome } )"
class Municipio ( models . Model ) :
""" Modelo para representar as cidades brasileiras
"""
codigo_ibge = models . PositiveIntegerField (
_ ( ' código IBGE ' ) ,
primary_key = True ,
unique = True ,
help_text = _ ( ' Código do município segundo IBGE. ' )
)
microrregiao = models . ForeignKey (
Microrregiao ,
on_delete = models . PROTECT ,
verbose_name = _ ( ' M icrorregião' ) ,
verbose_name = _ ( ' m icrorregião' ) ,
blank = True ,
null = True
)
# codio designado pelo Tribunal Superior Eleitoral
# codigo designado pelo Tribunal Superior Eleitoral
codigo_tse = models . PositiveIntegerField (
_ ( ' código TSE ' ) ,
unique = True ,
@ -126,51 +120,65 @@ class Municipio(models.Model):
# verdadeiro se o município é capital do estado
is_capital = models . BooleanField ( _ ( ' capital ' ) , default = False )
populacao = models . PositiveIntegerField ( _ ( ' população ' ) )
populacao . list_filter_range = [ 10000 , 100000 , 1000000 ]
is_polo = models . BooleanField ( _ ( ' pólo ' ) , default = False )
data_criacao = models . DateField ( _ ( ' data de criação do município ' ) , null = True , blank = True )
data_criacao = models . DateField (
_ ( ' data de criação do município ' ) ,
null = True ,
blank = True
)
# posição geográfica do município
latitude = models . DecimalField (
_ ( ' latitude ' ) ,
max_digits = 10 ,
decimal_places = 8 ,
null = True ,
blank = True ,
help_text = _ ( ' Exemplo ' ) + ' : <em>-20,464</em>.'
help_text = _ ( ' Exemplo: <em>-20,464</em>. ' )
)
longitude = models . DecimalField (
_ ( ' longitude ' ) ,
max_digits = 11 ,
decimal_places = 8 ,
null = True ,
blank = True ,
help_text = _ ( ' Exemplo ' ) + ' : <em>-45,426</em>. '
help_text = _ ( ' Exemplo: <em>-45,426</em>. ' )
)
idh = models . DecimalField (
_ ( ' IDH ' ) ,
help_text = _ ( ' Índice de desenvolvimento Humano ' ) ,
max_digits = 4 ,
decimal_places = 3 ,
validators = [ MinValueValidator ( 0 ) , MaxValueValidator ( 1 ) ]
)
pib_total = models . DecimalField (
_ ( ' PIB total ' ) ,
max_digits = 18 ,
decimal_places = 3 ,
blank = True ,
null = True
)
pib_percapita = models . DecimalField (
_ ( ' PIB per capita ' ) ,
max_digits = 18 ,
decimal_places = 3 ,
blank = True ,
null = True
)
pib_ano = models . IntegerField (
_ ( ' Ano de apuração do PIB ' ) ,
blank = True ,
null = True
)
idh = models . DecimalField ( _ ( ' IDH ' ) , help_text = _ ( ' Índice de desenvolvimento Humano ' ) , max_digits = 4 , decimal_places = 3 ,
validators = [ MinValueValidator ( 0 ) , MaxValueValidator ( 1 ) ] )
idh . list_filter_range = [ 0.500 , 0.800 ]
pib_total = models . DecimalField ( _ ( ' PIB total ' ) , max_digits = 18 , decimal_places = 3 , blank = True , null = True )
pib_percapita = models . DecimalField ( _ ( ' PIB per capita ' ) , max_digits = 18 , decimal_places = 3 , blank = True , null = True )
pib_ano = models . IntegerField ( _ ( ' Ano de apuração do PIB ' ) , blank = True , null = True )
class Meta :
ordering = ( ' nome ' , ' codigo_ibge ' )
verbose_name = _ ( ' município ' )
verbose_name_plural = _ ( ' municípios ' )
def __unicode__ ( self ) :
return " %s - %s " % ( self . nome , self . uf )
def get_google_maps_url ( self ) :
return " http://maps.google.com.br/maps/mm?ie=UTF8&hl=pt-BR&t=h&ll= %s , %s &spn=1.61886,1.812744&z=9&source=embed " % \
( self . latitude , self . longitude )
def __str__ ( self ) :
return f " { self . nome } - { self . uf } "
class Telefone ( models . Model ) :
""" Modelo genérico para agrupar telefones dos modulos do sistema
"""
TELEFONE_CHOICES = (
( ' F ' , _ ( ' Fixo ' ) ) ,
( ' M ' , _ ( ' Móvel ' ) ) ,
@ -179,16 +187,23 @@ class Telefone(models.Model):
)
numero = models . CharField (
_ ( ' número ' ) ,
max_length = 64 , # TODO: diminuir tamanho de campo após migração de dados
help_text = _ ( ' Exemplo ' ) + ' : <em>(31)8851-9898</em>.' ,
max_length = 64 ,
help_text = _ ( ' Exemplo: <em>(31)8851-9898</em>. ' ) ,
)
tipo = models . CharField (
_ ( ' tipo ' ) ,
max_length = 1 ,
choices = TELEFONE_CHOICES ,
default = ' I '
)
nota = models . CharField ( max_length = 70 , null = True , blank = True )
ult_alteracao = models . DateTimeField ( _ ( ' Última alteração ' ) , null = True , blank = True , editable = False , auto_now = True )
ult_alteracao = models . DateTimeField (
_ ( ' última alteração ' ) ,
null = True ,
blank = True ,
editable = False ,
auto_now = True
)
# guarda o tipo do objeto (classe) vinculado a esse registro
content_type = models . ForeignKey ( ContentType , on_delete = models . CASCADE )
@ -203,22 +218,14 @@ class Telefone(models.Model):
ordering = ( ' numero ' , )
unique_together = ( ' numero ' , ' tipo ' )
def __unicode__ ( self ) :
return unicode ( self . numero )
def __str__ ( self ) :
return self . numero
class Contato ( models . Model ) :
""" Modelo generico para registrar contatos vinculados aos
modulos do sistema
"""
nome = models . CharField ( _ ( ' nome completo ' ) , max_length = 120 )
nome . alphabetic_filter = True
nota = models . CharField ( max_length = 70 , blank = True )
email = models . EmailField ( _ ( ' e-mail ' ) , blank = True )
telefones = GenericRelation ( Telefone )
municipio = models . ForeignKey (
Municipio ,
on_delete = models . SET_NULL ,
@ -226,7 +233,6 @@ class Contato(models.Model):
blank = True ,
null = True ,
)
# guarda o tipo do objeto (classe) vinculado a esse registro
content_type = models . ForeignKey ( ContentType , on_delete = models . CASCADE )
# identificador do registro na classe vinculado a esse registro
@ -241,10 +247,9 @@ class Contato(models.Model):
verbose_name = _ ( ' contato Interlegis ' )
verbose_name_plural = _ ( ' contatos Interlegis ' )
def __unicode __( self ) :
def __str __( self ) :
return self . nome
class Endereco ( models . Model ) :
TIPO_CHOICES = (
( ' aeroporto ' , _ ( ' Aeroporto ' ) ) ,
@ -294,26 +299,22 @@ class Endereco(models.Model):
( ' outro ' , _ ( ' Outro ' ) ) ,
)
# tipo do endereço obtido no site dos correios
tipo = models . CharField ( max_length = 15 , choices = TIPO_CHOICES )
tipo = models . CharField ( _ ( ' tipo ' ) , max_length = 15 , choices = TIPO_CHOICES )
logradouro = models . CharField (
_ ( ' logradouro ' ) ,
max_length = 100 ,
)
logradouro . alphabetic_filter = True
numero = models . CharField ( max_length = 15 , blank = True )
complemento = models . CharField ( max_length = 15 , blank = True )
# campo de texto livre
referencia = models . CharField ( max_length = 100 , blank = True )
bairro = models . CharField ( max_length = 100 , blank = True )
cep = models . CharField (
_ ( ' CEP ' ) ,
max_length = 9 ,
blank = True ,
null = True ,
help_text = _ ( " Formato " ) + " : <em>XXXXX-XXX</em>."
help_text = _ ( " Formato: <em>XXXXX-XXX</em>. " )
)
municipio = models . ForeignKey (
Municipio ,
on_delete = models . SET_NULL ,
@ -321,8 +322,6 @@ class Endereco(models.Model):
blank = True ,
null = True ,
)
municipio . uf_filter = True
# guarda o tipo do objeto (classe) vinculado a esse registro
content_type = models . ForeignKey ( ContentType , on_delete = models . CASCADE )
# identificador do registro na classe vinculado a esse registro
@ -337,6 +336,6 @@ class Endereco(models.Model):
verbose_name = _ ( ' endereço ' )
verbose_name_plural = _ ( ' endereços ' )
def __unicode __( self ) :
return self . tipo + ' ' + self . logradouro + ' , ' + self . numero \
+ ' ' + self . complemento + ' - ' + self . bairro
def __str __( self ) :
return ( f " { self . tipo } { self . logradouro } , { self . numero } "
f " { self . complemento } - { self . bairro } " )