1 # -*- coding: utf-8; -*-
2 # Copyright (C) 2007 Gabriel Falcão <root@gabrielfalcao.com>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public
15 # License along with this program; if not, write to the
16 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
19 from django.http import HttpResponse, HttpResponseRedirect
20 from django.template import RequestContext
21 from django.shortcuts import render_to_response, get_object_or_404
22 from django.conf import settings
23 from django.core.exceptions import ObjectDoesNotExist
27 Esta classe recebe um dicionario como parametro e adiciona cada
28 chave do dicionario em seu atributo, contendo o valor de cada item
30 def __init__(self, dictionary):
31 if isinstance(dictionary, dict):
32 for key, value in dictionary.items():
33 setattr(self, key, value)
35 raise RuntimeError('MetaDict class only accepts a dictionary\
38 for value in self.__dict__.values():
41 def __getitem__(self,index):
43 return self.__dict__[index]
48 return ", ".join(self.all())
51 return self.__dict__.keys()
54 return self.__dict__[self.__dict__.keys()[0]]
57 return self.__dict__[self.__dict__.keys()[-1]]
59 def slice(self,index):
61 return self.__dict__[self.__dict__.keys()[index]]
65 def __contains__(self, item):
66 return item in self.__dict__.keys() or item in self.__dict__.values()
72 return len(self.__dict__.keys())
76 def __default__(self):
79 def __getattr__(self,*a):
80 return self.__default__
83 return self.__default__
85 def __nonzero__(self):
88 def get_object_or_list(obj,**kw):
91 except ObjectDoesNotExist:
94 def get_object_or_none(obj,**kw):
96 return obj.objects.get(**kw)
97 except ObjectDoesNotExist:
99 except AssertionError:
100 #hammer! hammer! hammer!
101 return empty()#obj.objects.filter(**kw)[0]
103 def limpa_conversas(visitante, corretor):
105 if isinstance(visitante, Visitante):
106 conversas_vc.append(get_object_or_none(Conversa,visitante__id=\
108 if isinstance(corretor, Corretor):
109 conversas_vc.append(get_object_or_none(Conversa,corretor__id=\
111 [c.delete() for c in conversas_vc if c]