922a623d5502bfea87c7cf7c0fae042ba066e441
[cascardo/eventmanager.git] / controllers.py
1 # -*- coding: utf-8; -*-
2 # Copyright (C) 2007 Gabriel Falcão <root@gabrielfalcao.com>
3 #
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.
8 #
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.
13 #
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.
18
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
24
25 class MetaDict:
26     """
27     Esta classe recebe um dicionario como parametro e adiciona cada
28     chave do dicionario em seu atributo, contendo o valor de cada item
29     """
30     def __init__(self, dictionary):
31         if isinstance(dictionary, dict):
32             for key, value in dictionary.items():
33                 setattr(self, key, value)
34         else:
35             raise RuntimeError('MetaDict class only accepts a dictionary\
36                                     as parameter')
37     def __iter__(self):
38         for value in self.__dict__.values():
39             yield value
40
41     def __getitem__(self,index):
42         try:
43             return self.__dict__[index]
44         except:
45             return False
46
47     def __repr__(self):
48         return ", ".join(self.all())
49
50     def all(self):
51         return self.__dict__.keys()
52
53     def first(self):
54         return self.__dict__[self.__dict__.keys()[0]]
55
56     def last(self):
57         return self.__dict__[self.__dict__.keys()[-1]]
58
59     def slice(self,index):
60         try:
61             return self.__dict__[self.__dict__.keys()[index]]
62         except:
63             return False
64
65     def __contains__(self, item):
66         return item in self.__dict__.keys() or item in self.__dict__.values()
67
68     def as_dict(self):
69         return self.__dict__
70
71     def __len__(self):
72         return len(self.__dict__.keys())
73
74 class empty:
75
76     def __default__(self):
77         return []
78
79     def __getattr__(self,*a):
80         return self.__default__
81
82     def __repr__(self):
83         return self.__default__
84     
85     def __nonzero__(self):
86         return 1
87
88 def get_object_or_list(obj,**kw):
89     try:
90         return obj(**kw)
91     except ObjectDoesNotExist:
92         return empty()
93
94 def get_object_or_none(obj,**kw):
95     try:
96         return obj.objects.get(**kw)
97     except ObjectDoesNotExist:
98         return None
99     except AssertionError:
100         #hammer! hammer! hammer!
101         return empty()#obj.objects.filter(**kw)[0]
102
103 def limpa_conversas(visitante, corretor):
104     conversas_vc = []
105     if isinstance(visitante, Visitante):
106         conversas_vc.append(get_object_or_none(Conversa,visitante__id=\
107             visitante.id))
108     if isinstance(corretor, Corretor):
109         conversas_vc.append(get_object_or_none(Conversa,corretor__id=\
110             corretor.id))
111     [c.delete() for c in conversas_vc if c]