Advertisement
Guest User

Untitled

a guest
May 29th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class prueba_consultor_voto(models.Model):
  2. _name = 'prueba_consultor.voto'
  3.  
  4. # Fields
  5. active = fields.Boolean('Activo/Borrado', default=True)
  6. persona_id = fields.Many2one(
  7. 'res.users',
  8. 'Votante',
  9. default=lambda self: self._context.get('uid', None),
  10. )
  11. calificacion = fields.Float('Calificación')
  12. fecha = fields.Date(
  13. 'fecha',
  14. default = fields.Date.today()
  15. )
  16. idea_id = fields.Many2one(
  17. 'prueba_consultor.idea',
  18. 'Idea',
  19. default=lambda self: self._context.get('idea_id', None),
  20. )
  21.  
  22. @api.constrains('persona_id')
  23. def check_persona_id(self):
  24. if self.idea_id.autor_id == self.persona_id:
  25. raise Warning('El autor de la idea no puede votar por ella')
  26. return False
  27. ids = self.env['prueba_consultor.voto'].search([('persona_id', '=', self.persona_id.id)])
  28. if len(ids):
  29. raise Warning('El usuario ya realizó el voto')
  30. return False
  31. return True
  32.  
  33. @api.constrains('calificacion')
  34. def check_calificacion(self):
  35. if self.calificacion >=0 and self.calificacion <= 10:
  36. return True
  37. else:
  38. raise Warning('la calificación debe estar entre 0 y 10')
  39. return False
  40.  
  41. @api.model
  42. def create(self, vals):
  43. print vals
  44. record_idea = self.env['prueba_consultor.idea'].browse(vals['idea_id']);
  45. if record_idea.fecha_inicio and record_idea.fecha_fin:
  46. if vals['fecha'] >= record_idea.fecha_inicio and vals['fecha'] <= record_idea.fecha_fin:
  47. voto = super(prueba_consultor_voto, self).create(vals)
  48. return voto
  49. elif vals['fecha'] > record_idea.fecha_fin:
  50. raise Warning('Las votaciones estan cerradas')
  51. elif vals['fecha'] < record_idea.fecha_inicio:
  52. raise Warning('Las votaciones aun no se han abierto')
  53. else:
  54. raise Warning('Pendiente definir fecha de inicio de votaciones')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement