/tsjepublisher/views/NoticiaViews.py
http://hackathon.codeplex.com · Python · 97 lines · 65 code · 18 blank · 14 comment · 15 complexity · 78f21e4d15dc99a73ff81b00b75615bb MD5 · raw file
- '''
- Created on Apr 7, 2012
- @author: jorge
- '''
- from pyramid.view import view_config
- from .UsuarioViews import isLoggedView
- import hashlib
- import transaction
- from . import ensureParams
- #from ..models.Rol import Rol
- from ..models import Usuario
- from ..models import Noticia
- import datetime
- from ..models import Tema
- from ..models import DBSession
- from sqlalchemy import desc
- from ..ws import notify
- @view_config(route_name='listNoticia', renderer = 'json')
- def listNoticiasView(request):
- '''
- Llamada a la vista, retorna el resultado del intento de logueo
- '''
- params = request.params
- session = request.session
- nots = DBSession.query(Noticia)
- nots = nots.filter(Noticia.idTema==int(params['idTema'])) if 'idTema' in params and int(params['idTema']) != -1 else nots
- nots = nots.order_by(desc(Noticia.fecha))
- return [p.toDict() for p in nots]##
- @view_config(route_name='delNoticia', renderer='json')
- def delNoticiaView(request):
- '''
- Elimina el proyecto seleccionado que no tenga aun fases
- '''
- params = request.params
- session = request.session
- if not ensureParams(params,['idNoticia']):
- return {'respuesta' : False , 'causa' : 'Parametros Insuficientes o Incorrectos.'}
- elif not isLoggedView(request)['logged']:
- return {'respuesta' : False , 'causa' : 'Debe iniciar sesion para realizar operaciones.'}
- try:
- noticia = DBSession.query(Noticia).filter(Tema.idNoticia==params['idNoticia'])
- if niticia.usuario.idUsuario != int(session['uid']):
- return {'respuesta' : False , 'causa' : 'No puede borrar una noticia que no le pertenece'}
- DBSession.delete(noticia.one())
- transaction.commit()
- except Exception, e:
- transaction.abort()
- return {'respuesta' : False , 'causa' : str(e)}
-
- return {'respuesta' : True , 'mensaje' : 'Los Datos se Borraron exitosamente'}
-
- @view_config(route_name='saveNoticia', renderer='json')
- def saveNoticiaView(request):
- params = request.params
- session = request.session
- if not ensureParams(params,['titulo', 'contenido', 'fecha', 'idTema']):
- return {'respuesta' : False , 'causa' : 'Parametros Insuficientes'}
- #elif not isLoggedView(request)['logged']:
- # return {'respuesta' : False , 'causa' : 'Debe iniciar sesion para realizar operaciones.'}
-
-
-
- toSave = {}
- acc = 'Agregaron'
- try:
- if 'idNoticia' in params:
- toSave = DBSession.query(Noticia).filter(Noticia.idNoticia==params['idNoticia']).one()
- acc = 'Modificaron'
- else:
- toSave = Noticia()
-
- toSave.titulo = params['titulo']
- toSave.contenido = params['contenido']
- toSave.fecha = datetime.date.today()
- toSave.idTema = params['idTema']
-
-
- if 'idNoticia' not in params:
- DBSession.add(toSave)
-
- transaction.commit()
-
- print params['idTema']
- notify(params['titulo'], int(params['idTema']))
-
- except Exception, e:
- transaction.abort()
- return {'respuesta' : False , 'causa' : str(e)}
-
- return {'respuesta' : True , 'mensaje' : 'Los Datos se ' + acc + ' exitosamente'}