Advertisement
Guest User

class prueba_consultor_voto

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