- Agora existem views para mostrar dados de detalhes de palestras e palestrantes
[cascardo/eventmanager.git] / controllers.py
diff --git a/controllers.py b/controllers.py
new file mode 100644 (file)
index 0000000..922a623
--- /dev/null
@@ -0,0 +1,111 @@
+# -*- coding: utf-8; -*-
+# Copyright (C) 2007 Gabriel Falcão <root@gabrielfalcao.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+# Boston, MA 02111-1307, USA.
+
+from django.http import HttpResponse, HttpResponseRedirect
+from django.template import RequestContext
+from django.shortcuts import render_to_response, get_object_or_404
+from django.conf import settings
+from django.core.exceptions import ObjectDoesNotExist
+
+class MetaDict:
+    """
+    Esta classe recebe um dicionario como parametro e adiciona cada
+    chave do dicionario em seu atributo, contendo o valor de cada item
+    """
+    def __init__(self, dictionary):
+        if isinstance(dictionary, dict):
+            for key, value in dictionary.items():
+                setattr(self, key, value)
+        else:
+            raise RuntimeError('MetaDict class only accepts a dictionary\
+                                    as parameter')
+    def __iter__(self):
+        for value in self.__dict__.values():
+            yield value
+
+    def __getitem__(self,index):
+        try:
+            return self.__dict__[index]
+        except:
+            return False
+
+    def __repr__(self):
+        return ", ".join(self.all())
+
+    def all(self):
+        return self.__dict__.keys()
+
+    def first(self):
+        return self.__dict__[self.__dict__.keys()[0]]
+
+    def last(self):
+        return self.__dict__[self.__dict__.keys()[-1]]
+
+    def slice(self,index):
+        try:
+            return self.__dict__[self.__dict__.keys()[index]]
+        except:
+            return False
+
+    def __contains__(self, item):
+        return item in self.__dict__.keys() or item in self.__dict__.values()
+
+    def as_dict(self):
+        return self.__dict__
+
+    def __len__(self):
+        return len(self.__dict__.keys())
+
+class empty:
+
+    def __default__(self):
+        return []
+
+    def __getattr__(self,*a):
+        return self.__default__
+
+    def __repr__(self):
+        return self.__default__
+    
+    def __nonzero__(self):
+        return 1
+
+def get_object_or_list(obj,**kw):
+    try:
+        return obj(**kw)
+    except ObjectDoesNotExist:
+        return empty()
+
+def get_object_or_none(obj,**kw):
+    try:
+        return obj.objects.get(**kw)
+    except ObjectDoesNotExist:
+        return None
+    except AssertionError:
+        #hammer! hammer! hammer!
+        return empty()#obj.objects.filter(**kw)[0]
+
+def limpa_conversas(visitante, corretor):
+    conversas_vc = []
+    if isinstance(visitante, Visitante):
+        conversas_vc.append(get_object_or_none(Conversa,visitante__id=\
+            visitante.id))
+    if isinstance(corretor, Corretor):
+        conversas_vc.append(get_object_or_none(Conversa,corretor__id=\
+            corretor.id))
+    [c.delete() for c in conversas_vc if c]