Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. '''
  2. Created on 21/nov/2010
  3.  
  4. @author: placava
  5. '''
  6. from mod_python import psp
  7. from mod_python.Session import Session
  8. from piergiuseppe82.guestbook.model.Dedica import Dedica
  9. from piergiuseppe82.guestbook.model.dao.DedicaDao import DedicaDao
  10. import logging
  11.  
  12.  
  13. __DEFAULT_CONTENT_TYPE__ = "text/html"
  14.  
  15. __MESSAGES__ = 'messages'
  16. __NICKNAME__ = 'nickname'
  17. __DEDICA__ = 'dedica'
  18. __LISTA_DEDICHE__ = 'lista_dediche'
  19. __NICKNAME_SESSION_KEY__ = 'nickname'
  20.  
  21.  
  22. __NON_NICKNAME_MESSAGE__ = 'Torna alla home e inserisci prima il nickname.'
  23. __CAMPI_MANCANTI_MESSAGE__ = 'devi completare i campi.'
  24.  
  25. logging.basicConfig(level=logging.DEBUG
  26.         ,format='%(asctime)s [[%(levelname)s]] - %(module)s : %(lineno)d - %(message)s'
  27.         ,datefmt='%d %b %y %H:%M'
  28.         ,filename='/var/www/log/guestbook.log'
  29.         ,filemode='a')
  30.  
  31. def index(req):
  32.     session = Session(req)
  33.     nickname = None
  34.     logging.debug("Chiamato index")
  35.     req.content_type = __DEFAULT_CONTENT_TYPE__
  36.     if req.form.has_key(__NICKNAME_SESSION_KEY__):
  37.         session[__NICKNAME_SESSION_KEY__] = req.form[__NICKNAME_SESSION_KEY__]
  38.         nickname = req.form['nickname']
  39.     else:
  40.         try:
  41.             nickname = session[__NICKNAME_SESSION_KEY__]
  42.         except:
  43.             pass
  44.     session.save()
  45.     template = psp.PSP(req, filename='psp/index.psp')
  46.     template.run({__NICKNAME__:nickname})
  47.  
  48. def nuova_dedica(req):
  49.     logging.debug("Chiamato nuova_dedica")
  50.     session = Session(req)
  51.     req.content_type = __DEFAULT_CONTENT_TYPE__
  52.     template = psp.PSP(req, filename='psp/newDedica.psp')
  53.     messages = None
  54.     nickname = None
  55.     try:
  56.         nickname = session[__NICKNAME_SESSION_KEY__]
  57.     except:
  58.         pass
  59.     if not nickname:
  60.         template = psp.PSP(req, filename='psp/messages.psp')
  61.         messages = __NON_NICKNAME_MESSAGE__
  62.     template.run({__NICKNAME__:nickname,__MESSAGES__:messages})
  63.  
  64. def salva_dedica(req,nickname,email,dedica):
  65.     logging.debug("Chiamato salva_dedica")
  66.     messages = None
  67.     dediche = None
  68.     session = Session(req)
  69.     req.content_type = __DEFAULT_CONTENT_TYPE__
  70.     template = psp.PSP(req, filename='psp/listaDediche.psp')
  71.     if not(nickname and email and dedica):
  72.         template = psp.PSP(req, filename='psp/messages.psp')
  73.         messages = __CAMPI_MANCANTI_MESSAGE__
  74.     else:  
  75.         dedica = Dedica(nickname = nickname, email=email,messaggio=dedica)
  76.         dedicadao = DedicaDao()
  77.         dedicadao.insert(dedica);
  78.         dediche = dedicadao.findAll();
  79.     template.run({__LISTA_DEDICHE__:dediche,__NICKNAME__:session[__NICKNAME_SESSION_KEY__],__MESSAGES__:messages})
  80.  
  81. def nuovo_nick(req):
  82.     logging.debug("Chiamato nuovo_nick")
  83.     session = Session(req)
  84.     session.delete()
  85.     template = psp.PSP(req, filename='psp/index.psp')
  86.     req.content_type = __DEFAULT_CONTENT_TYPE__
  87.     template.run({__NICKNAME__:None})
  88.  
  89. def lista_dediche(req):
  90.     logging.debug("Chiamato lista_dedica")
  91.     dedicadao = DedicaDao()
  92.     dediche = dedicadao.findAll();
  93.     template = psp.PSP(req, filename='psp/listaDediche.psp')
  94.     req.content_type = __DEFAULT_CONTENT_TYPE__
  95.     template.run({__LISTA_DEDICHE__:dediche})
  96.  
  97. def elimina_dedica(req,id):
  98.     logging.debug("Chiamato elimina_dedica")
  99.     if id:
  100.         dedicadao = DedicaDao()
  101.         dedicadao.delete(int(id));
  102.     dediche = dedicadao.findAll()
  103.     template = psp.PSP(req, filename='psp/listaDediche.psp')
  104.     req.content_type = __DEFAULT_CONTENT_TYPE__
  105.     template.run({__LISTA_DEDICHE__:dediche})
  106.  
  107. def edit_dedica(req,id):
  108.     logging.debug("Chiamato edit_dedica")
  109.     dedicadao = DedicaDao()
  110.     req.content_type = __DEFAULT_CONTENT_TYPE__
  111.     if id:
  112.         dedicadao = DedicaDao()
  113.         dedica = dedicadao.findById(int(id));
  114.         template = psp.PSP(req, filename='psp/editDedica.psp')
  115.         template.run({__DEDICA__:dedica})
  116.     else:
  117.         dediche = dedicadao.findAll()
  118.         template = psp.PSP(req, filename='psp/listaDediche.psp')
  119.         req.content_type = __DEFAULT_CONTENT_TYPE__
  120.         template.run({__LISTA_DEDICHE__:dediche})
  121.  
  122. def aggiorna_dedica(req,id,nickname,email,dedica):
  123.     logging.debug("Chiamato salva_dedica")
  124.     messages = None
  125.     req.content_type = __DEFAULT_CONTENT_TYPE__
  126.     template = psp.PSP(req, filename='psp/listaDediche.psp')
  127.     if not(id and nickname and email and dedica):
  128.         template = psp.PSP(req, filename='psp/messages.psp')
  129.         messages = __CAMPI_MANCANTI_MESSAGE__  
  130.     dedica = Dedica(id = int(id),nickname = nickname, email=email,messaggio=dedica)
  131.     dedicadao = DedicaDao()
  132.     dedicadao.update(dedica);
  133.     dediche = dedicadao.findAll();
  134.     template.run({__LISTA_DEDICHE__:dediche,__MESSAGES__:messages})