/tsjepublisher/views/NoticiaViews.py

http://hackathon.codeplex.com · Python · 97 lines · 65 code · 18 blank · 14 comment · 15 complexity · 78f21e4d15dc99a73ff81b00b75615bb MD5 · raw file

  1. '''
  2. Created on Apr 7, 2012
  3. @author: jorge
  4. '''
  5. from pyramid.view import view_config
  6. from .UsuarioViews import isLoggedView
  7. import hashlib
  8. import transaction
  9. from . import ensureParams
  10. #from ..models.Rol import Rol
  11. from ..models import Usuario
  12. from ..models import Noticia
  13. import datetime
  14. from ..models import Tema
  15. from ..models import DBSession
  16. from sqlalchemy import desc
  17. from ..ws import notify
  18. @view_config(route_name='listNoticia', renderer = 'json')
  19. def listNoticiasView(request):
  20. '''
  21. Llamada a la vista, retorna el resultado del intento de logueo
  22. '''
  23. params = request.params
  24. session = request.session
  25. nots = DBSession.query(Noticia)
  26. nots = nots.filter(Noticia.idTema==int(params['idTema'])) if 'idTema' in params and int(params['idTema']) != -1 else nots
  27. nots = nots.order_by(desc(Noticia.fecha))
  28. return [p.toDict() for p in nots]##
  29. @view_config(route_name='delNoticia', renderer='json')
  30. def delNoticiaView(request):
  31. '''
  32. Elimina el proyecto seleccionado que no tenga aun fases
  33. '''
  34. params = request.params
  35. session = request.session
  36. if not ensureParams(params,['idNoticia']):
  37. return {'respuesta' : False , 'causa' : 'Parametros Insuficientes o Incorrectos.'}
  38. elif not isLoggedView(request)['logged']:
  39. return {'respuesta' : False , 'causa' : 'Debe iniciar sesion para realizar operaciones.'}
  40. try:
  41. noticia = DBSession.query(Noticia).filter(Tema.idNoticia==params['idNoticia'])
  42. if niticia.usuario.idUsuario != int(session['uid']):
  43. return {'respuesta' : False , 'causa' : 'No puede borrar una noticia que no le pertenece'}
  44. DBSession.delete(noticia.one())
  45. transaction.commit()
  46. except Exception, e:
  47. transaction.abort()
  48. return {'respuesta' : False , 'causa' : str(e)}
  49. return {'respuesta' : True , 'mensaje' : 'Los Datos se Borraron exitosamente'}
  50. @view_config(route_name='saveNoticia', renderer='json')
  51. def saveNoticiaView(request):
  52. params = request.params
  53. session = request.session
  54. if not ensureParams(params,['titulo', 'contenido', 'fecha', 'idTema']):
  55. return {'respuesta' : False , 'causa' : 'Parametros Insuficientes'}
  56. #elif not isLoggedView(request)['logged']:
  57. # return {'respuesta' : False , 'causa' : 'Debe iniciar sesion para realizar operaciones.'}
  58. toSave = {}
  59. acc = 'Agregaron'
  60. try:
  61. if 'idNoticia' in params:
  62. toSave = DBSession.query(Noticia).filter(Noticia.idNoticia==params['idNoticia']).one()
  63. acc = 'Modificaron'
  64. else:
  65. toSave = Noticia()
  66. toSave.titulo = params['titulo']
  67. toSave.contenido = params['contenido']
  68. toSave.fecha = datetime.date.today()
  69. toSave.idTema = params['idTema']
  70. if 'idNoticia' not in params:
  71. DBSession.add(toSave)
  72. transaction.commit()
  73. print params['idTema']
  74. notify(params['titulo'], int(params['idTema']))
  75. except Exception, e:
  76. transaction.abort()
  77. return {'respuesta' : False , 'causa' : str(e)}
  78. return {'respuesta' : True , 'mensaje' : 'Los Datos se ' + acc + ' exitosamente'}