Guest User

Untitled

a guest
Jan 24th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.70 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from pymongo.son_manipulator import SONManipulator
  4. from collections import *
  5.  
  6. class MongoPersistent(object):
  7.     _type = ''
  8.     attributes = ['_type',
  9.                   'attributes',
  10.                   ]
  11.     primary_key = ''
  12.     collection = ''
  13.    
  14.     def __init__(self,
  15.                 decode=None,
  16.                 write=False,
  17.                 **kwargs):
  18.         """
  19.        Initialize the object, and check if it must be decoded or saved at the mongodb's collection.
  20.        """
  21.  
  22.         #Simple setting variables
  23.         for key in kwargs.keys():
  24.             setattr(self,key,kwargs.get(key))
  25.        
  26.         #Check if the object must be decoded
  27.         #If decode != None is True, run the decode method.
  28.         if decode != None:
  29.             self.decode(decode)
  30.        
  31.         #If write is True, save the object at the mongodb's collection.
  32.         if write == True:
  33.             if self.collection.find({self.primary_key:getattr(self,self.primary_key)}).count() > 0:
  34.                 print self._type,' ja existe na base: ', getattr(self,self.primary_key)
  35.             else:
  36.                 print 'Salvando %s: %s' % (self._type,getattr(self,self.primary_key))
  37.                 self.save()
  38.  
  39.         if write == True:
  40.             self.CustomInit(kwargs)
  41.  
  42.     def CustomInit(self,kwargs):
  43.         """
  44.        This method should be overwritten
  45.        """
  46.         self.data_criacao_objeto = datetime.now()
  47.         if 'params_obj' in kwargs.keys():
  48.             self.salvaDadosWS(kwargs.get('params_obj'))
  49.                
  50.     def decode(self,vars):
  51.         """Faz o decode do objeto, pegando o dicionario gravado no mongodb e colocando os valores nos devidos attributos."""                
  52.         for key in vars.keys():
  53.             setattr(self,key,vars[key])
  54.  
  55.     def encode(self):
  56.         encoded = {}
  57.         for field in self.attributes:
  58.             encoded[field] = getattr(self,field)
  59.         return encoded
  60.        
  61.     def save(self):
  62.         self.collection.remove({self.primary_key:getattr(self,self.primary_key)})
  63.         self.collection.insert({self.primary_key:self})
  64.  
  65.     def remove(self):
  66.         self.collection.remove({self.primary_key:getattr(self,self.primary_key)})
  67.        
  68.     def salvaDadosWS(self,objeto_ws):
  69.         """
  70.        Recebe objeto do WS e salva os dados.
  71.        """
  72.         for param in dir(objeto_ws):
  73.             if param in self.attributes:
  74.                 setattr(self,param,getattr(objeto_ws,param))
  75.                
  76.         self.CustomSalvaDados(objeto_ws)
  77.         self.save()
  78.  
  79.     def CustomSalvaDados(self,objeto_ws):
  80.         pass
  81.  
  82. class Imovel(MongoPersistent):          
  83.     _type = 'Imovel'
  84.     primary_key = 'Id'
  85.     attributes = ['_type',
  86.                   'Id',
  87.                   'Id_antigo',
  88.                   'Logradouro',
  89.                   'Complemento',
  90.                   'Cep',
  91.                   'Descricao',
  92.                   'DescricaoCompleta',
  93.                   'ResumoEndereco',
  94.                   'Quarto',
  95.                   'Suite',
  96.                   'AluguelGarantidows',
  97.                   'descontoIRRFws',
  98.                   'Banheiro',
  99.                   'Lavabo',
  100.                   'Elevador',
  101.                   'Garagem',
  102.                   'Dce',
  103.                   'Armarios',
  104.                   'Vazadows',
  105.                   'Posicao',
  106.                   'dataInclusao',
  107.                   'dataAlteracao',
  108.                   'Responsavel',
  109.                   'Creci',
  110.                   'Telefone1',
  111.                   'Telefone2',
  112.                   'Plantaws',
  113.                   'Agiows',
  114.                   'exibirPortalws',
  115.                   'Exibirsitews',
  116.                   'Exibirobraws',
  117.                   'Destaquews',
  118.                   'Ativows',
  119.                   'areaTerrenostr',
  120.                   'Areaverdestr',
  121.                   'AreaCasaStr',
  122.                   'areaConstruidoStr',
  123.                   'Areatotalstr',
  124.                   'Valorcondominiostr',
  125.                   'Valorquitadostr',
  126.                   'Valoragiostr',
  127.                   'Valorstr',
  128.                   'Valoradicionalstr',
  129.                   'Valorquitadostr',
  130.                   'Valoragiostr',
  131.                   'Valorstr',
  132.                   'Valoradicionalstr',
  133.                   'Numeroapartemanto',
  134.                   'Empreendimento',
  135.                   'NumeroPasta',
  136.                   'numeroContrato',
  137.                   'dataOpcaoVenda',
  138.                   'dataVencimento',
  139.                   'Vista',
  140.                   'Fachada',
  141.                   'Andar',
  142.                   'Andares',
  143.                   'NumeroIPTU',
  144.                   'numeroIPTUBox',
  145.                   'NumeroChave',
  146.                   'SituacaoChave',
  147.                   'Caracteristica',
  148.                   'PosicaoSol',
  149.                   'ValorAluguelStr',
  150.                   'Prioridade',
  151.                   'videoUrl',
  152.                   'videoPath',
  153.                   'videoPath2',
  154.                   'videoPath3',
  155.                   'Referencia',
  156.                   'data_criacao_objeto',
  157.                   'DataOpcaoVenda',
  158.                   'ValorAluguelStr',
  159.                   'Cidade_Id',
  160.                   'Uf_Id',
  161.                   'Regiao_Id',
  162.                   'Bairro_Id',
  163.                   'Situacao_Id',
  164.                   'TipoImovel_Id',
  165.                   'Aluguel',
  166.                   'Venda',
  167.                   ]
  168.     Id = ''
  169.     Id_antigo = None
  170.     Logradouro = None
  171.     Complemento = None
  172.     Cep = None
  173.     Descricao = None
  174.     DescricaoCompleta = None
  175.     ResumoEndereco = None
  176.     Quarto = None
  177.     Suite = None
  178.     AluguelGarantidows = None
  179.     descontoIRRFws = None
  180.     Banheiro = None
  181.     Lavabo = None
  182.     Elevador = None
  183.     Garagem = None
  184.     Dce = None
  185.     Armarios = None
  186.     Vazadows = None
  187.     Posicao = None
  188.     dataInclusao = None
  189.     dataAlteracao = None
  190.     Responsavel = None
  191.     Creci = None
  192.     Telefone1 = None
  193.     Telefone2 = None
  194.     Plantaws = None
  195.     Agiows = None
  196.     exibirPortalws = None
  197.     Exibirsitews = None
  198.     Exibirobraws = None
  199.     Destaquews = None
  200.     Ativows = None
  201.     areaTerrenostr = None
  202.     Areaverdestr = None
  203.     AreaCasaStr  = None
  204.     areaConstruidoStr = None
  205.     Areatotalstr = None
  206.     Valorcondominiostr = None
  207.     Valorquitadostr = None
  208.     Valoragiostr = None
  209.     Valorstr = None
  210.     Valoradicionalstr = None
  211.     Valorquitadostr = None
  212.     Valoragiostr = None
  213.     Valorstr = None
  214.     Valoradicionalstr = None
  215.     Numeroapartemanto = None
  216.     Empreendimento = None
  217.     NumeroPasta = None
  218.     numeroContrato = None
  219.     dataOpcaoVenda = None
  220.     dataVencimento = None
  221.     Vista = None
  222.     Fachada = None
  223.     Andar = None
  224.     Andares = None
  225.     NumeroIPTU = None
  226.     numeroIPTUBox = None
  227.     NumeroChave = None
  228.     SituacaoChave = None
  229.     Caracteristica = None
  230.     PosicaoSol  = None
  231.     ValorAluguelStr  = None
  232.     Prioridade = None
  233.     videoUrl = None
  234.     videoPath = None
  235.     videoPath2 = None
  236.     videoPath3 = None
  237.     Referencia = None
  238.     data_criacao_objeto = None
  239.     DataOpcaoVenda = None
  240.     DataVencimento = None
  241.     ValorAluguelStr = None
  242.     Cidade_Id = None
  243.     Uf_Id = None
  244.     Regiao_Id = None
  245.     Bairro_Id = None
  246.     Situacao_Id = None
  247.     TipoImovel_Id = None
  248.     Aluguel = False
  249.     Venda = False
  250.    
  251.     def CustomSalvaDados(self,objeto_ws):
  252.         from collections import *
  253.         self.Cidade_Id = objeto_ws.Cidade.Id
  254.         #Pesquisando se existe a cidade, senao existir cria.
  255.         CidadeCollection(self.collection.database).get(objeto_ws.Cidade.Id,params_obj=objeto_ws.Cidade)
  256.        
  257.         self.Uf_Id = objeto_ws.Uf.Uf
  258.         #Pesquisando se existe a UF, senao existir cria.
  259.         UfCollection(self.collection.database).get(objeto_ws.Uf.Uf,params_obj=objeto_ws.Uf)
  260.        
  261.         self.Regiao_Id = objeto_ws.Regiao.Id
  262.         #Pesquisando se existe a Regiao, senao existir cria.
  263.         RegiaoCollection(self.collection.database).get(objeto_ws.Regiao.Id,params_obj=objeto_ws.Regiao)
  264.        
  265.         self.Bairro_Id = objeto_ws.Bairro.Id
  266.         #Pesquisando se existe o Bairro, senao existir cria.
  267.         BairroCollection(self.collection.database).get(objeto_ws.Bairro.Id,params_obj=objeto_ws.Bairro)
  268.        
  269.         self.Situacao_Id = objeto_ws.Situacao.Id
  270.         #Pesquisando se existe a Situa��o, senao existir cria.
  271.         SituacaoCollection(self.collection.database).get(objeto_ws.Situacao.Id,params_obj=objeto_ws.Situacao)
  272.        
  273.         self.TipoImovel_Id = objeto_ws.Tipo.Id
  274.         #Pesquisando se existe a Situacao, senao existir cria.
  275.         TipoImovelCollection(self.collection.database).get(objeto_ws.Tipo.Id,params_obj=objeto_ws.Tipo)
  276.        
  277.         self.FotoImovel_Id = objeto_ws.Foto.Id
  278.         #Pesquisando se existe a Foto, senao existir cria.
  279.         FotoImovelCollection(self.collection.database).get(objeto_ws.Foto.Id,params_obj=objeto_ws.Foto)
  280.         self.save()
  281.        
  282.     def getCidade(self):
  283.         cidade = CidadeCollection(self.collection.database).get(self.Cidade_Id)
  284.         return cidade
  285.    
  286.     def getUf(self):
  287.         uf = UfCollection(self.collection.database).get(self.Uf_Id)
  288.         return uf
  289.    
  290.     def getRegiao(self):
  291.         regiao = RegiaoCollection(self.collection.database).get(self.Regiao_Id)
  292.         return regiao
  293.    
  294.     def getBairro(self):
  295.         bairro = BairroCollection(self.collection.database).get(self.Bairro_Id)
  296.    
  297.     def getSituacao(self):
  298.         situacao = SituacaoCollection(self.collection.database).get(self.Situacao_Id)
  299.    
  300.     def getTipoImovel(self):
  301.         tipo_imovel = TipoImovelCollection(self.collection.database).get(self.TipoImovel_Id)
  302.    
  303.     def getFotoImovel(self):
  304.         foto_imovel = FotoImovelCollection(self.collection.database).get(self.FotoImovel_Id)
  305.  
  306. class Cidade(MongoPersistent):          
  307.     _type = 'Cidade'
  308.     primary_key = 'Id'
  309.     attributes = ['_type',
  310.                   'Id',
  311.                   'data_criacao_objeto',
  312.                   'Nome',
  313.                   'GMapZoom',
  314.                   'Latitude',
  315.                   'Longitude',
  316.                   ]
  317.    
  318.     Id = ''
  319.     data_criacao_objeto = None
  320.     Nome = None
  321.     GMapZoom = None
  322.     Latitude = None
  323.     Longitude = None
  324.  
  325. class Uf(MongoPersistent):
  326.     _type = 'Uf'
  327.     primary_key = 'Uf'
  328.     attributes = ['_type',
  329.                   'Uf',
  330.                   'Nome',
  331.                   'IdCapital',
  332.                   'GMapZoom',
  333.                   'Latitude',
  334.                   'Longitude',
  335.                   ]
  336.     Uf = ''
  337.     Nome = None
  338.     IdCapital = None
  339.     GMapZoom = None
  340.     Latitude = None
  341.     Longitude = None
  342.  
  343. class Bairro(MongoPersistent):
  344.     _type = 'Uf'
  345.     primary_key = 'Id'
  346.     attributes = ['_type',
  347.                   'Id',
  348.                   'Id_Regiao'
  349.                   'Nome',
  350.                   'GMapZoom',
  351.                   'Latitude',
  352.                   'Longitude',
  353.                   'Id_RegiaoNome',
  354.                   ]
  355.     Id = ''
  356.     Id = None
  357.     Id_Regiao = None
  358.     Nome = None
  359.     GMapZoom = None
  360.     Latitude = None
  361.     Longitude = None
  362.     Id_RegiaoNome = None
  363.  
  364. class Situacao(MongoPersistent):
  365.     _type = 'Situacao'
  366.     primary_key = 'Id'
  367.     attributes = ['_type',
  368.                   'Id',
  369.                   'Ativo'
  370.                   'AtivoPortal',
  371.                   'AtivoSite',
  372.                   'Existe',
  373.                   'AtivoAtivoPortal',
  374.                   ]
  375.     Id = ''
  376.     Id = None
  377.     Ativo = None
  378.     AtivoPortal = None
  379.     AtivoSite = None
  380.     Existe = None
  381.     AtivoAtivoPortal = None
  382.  
  383. class Regiao(MongoPersistent):
  384.     _type = 'Regiao'
  385.     primary_key = 'Id'
  386.     attributes = ['_type',
  387.                   'Id',
  388.                   'Regiao_Id',
  389.                   'IdCidade',
  390.                   'Cidade_Id',
  391.                   'Nome',
  392.                   'GMapZoom',
  393.                   'Latitude',
  394.                   'Longitude',
  395.                   ]
  396.     Id = None
  397.     Regiao_Id = None
  398.     IdCidade = None
  399.     Cidade_Id = None
  400.     Nome = None
  401.     GMapZoom = None
  402.     Latitude = None
  403.     Longitude = None
  404.  
  405. class TipoImovel(MongoPersistent):
  406.     _type = 'Tipo'
  407.     primary_key = 'Id'
  408.     attributes = ['_type',
  409.                   'Id',
  410.                   'Descricao',
  411.                   ]
  412.     Id = ''
  413.     Descricao = None
  414.  
  415. class FotoImovel(MongoPersistent):
  416.     _type = 'Tipo'
  417.     primary_key = 'Id'
  418.     attributes = ['_type',
  419.                   'Id',
  420.                   'FotoImovel_Id',
  421.                   'Fisico',
  422.                   'Path',
  423.                   'NomeArquivo',
  424.                   'DataInclusao',
  425.                   'DataAlteracao',
  426.                   'AtivoWs',
  427.                   'AtualizadoWs',
  428.                   'Descricao',
  429.                   ]
  430.     Id = ''
  431.     FotoImovel_Id = None
  432.     Fisico = None
  433.     Path = None
  434.     NomeArquivo = None
  435.     DataInclusao = None
  436.     DataAlteracao = None
  437.     AtivoWs = None
  438.     AtualizadoWs = None
  439.     Descricao = None
  440.    
  441. class Transform(SONManipulator):
  442.     types = ['Imovel',
  443.              'Cidade',
  444.              'Uf',
  445.              'Regiao',
  446.              'Bairro',
  447.              'Situacao',
  448.              'TipoImovel',
  449.              'FotoImovel',
  450.              ]
  451.  
  452.     def transform_incoming(self, son, collection):
  453.         for key in son.keys():
  454.             for class_type in self.types:
  455.                 if isinstance(son[key],eval(class_type)):
  456.                     son = son[key].encode()
  457.         return son
  458.  
  459.     def transform_outgoing(self, son, collection):
  460.         if son.get('_type',None) in self.types:
  461.             son = eval(son.get('_type'))(decode=son)
  462.             son.collection = collection
  463.         return son
Add Comment
Please, Sign In to add comment