Skip to content

Instantly share code, notes, and snippets.

@name1984
Created October 28, 2014 19:44
Show Gist options
  • Save name1984/a7e78af7dba738449bc0 to your computer and use it in GitHub Desktop.
Save name1984/a7e78af7dba738449bc0 to your computer and use it in GitHub Desktop.
class PrisonPerson(orm.Model):
_name = 'prison.person'
_inherit = ['mail.thread']
_description = 'Personas Privadas de la Libertad'
def name_search(self, cr, uid, name='',
args=None, operator='ilike', context=None, limit=80):
ids = []
domain = ['|',
('name', 'ilike', name),
'|',
('last_name', 'ilike', name),
'|',
('prontuario', 'ilike', name),
('identificador', 'ilike', name)] + args
ids = self.search(cr, uid, domain, context=context, limit=limit)
return self.name_get(cr, uid, ids, context)
def name_get(self, cr, uid, ids, context=None):
res = []
for rec in self.read(cr, uid, ids, ['name', 'last_name']):
name = u"{0} {1}".format(rec['name'], rec['last_name'])
res.append((rec['id'], name))
return res
def compute_age(self, birth):
"""
Calculo de Edad
@birth: fecha de nacimiento %Y-%m-%d
"""
year, month, day = [int(x) for x in birth.split("-")]
born = datetime(year, month, day)
today = date.today()
# Days
if born.day > today.day:
days = today.day + 30 - born.day
born = born + relativedelta(months=1)
else:
days = today.day - born.day
# Month
if born.month > today.month:
mes = today.month + 12 - born.month
born = born + relativedelta(years=1)
else:
mes = today.month - born.month
# Years
years = today.year - born.year
age = u"{0} años {1} meses {2} días".format(years, mes, days)
return age
def _get_image(self, cr, uid, ids, name, args, context=None):
result = dict.fromkeys(ids, False)
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = tools.image_get_resized_images(obj.image)
return result
def _set_image(self, cr, uid, id, name, value, args, context=None):
return self.write(
cr,
uid,
[id],
{'image': tools.image_resize_image_big(value)},
context=context
)
def _has_image(self, cr, uid, ids, name, args, context=None):
result = {}
for obj in self.browse(cr, uid, ids, context=context):
result[obj.id] = obj.image != False
return result
def _compute_age(self, cr, uid, ids, name, args, context=None):
res = {}
for obj in self.browse(cr, uid, ids, context):
res[obj.id] = {
'time_in_prison': 0,
'age': 0
}
if obj.birth_date:
age = self.compute_age(obj.birth_date)
res[obj.id]['age'] = age
if obj.date_in:
time_prison = self.compute_age(obj.date_in)
res[obj.id]['time_in_prison'] = time_prison
return res
_columns = {
# Informacion Personal
'name': fields.char('Nombres', size=64, required=True),
'last_name': fields.char('Apellidos', size=64, required=True),
'tipo_identificador': fields.selection([
('cedula', 'Cedula'),
('pasaporte', 'Pasaporte')],
string='Tipo Identificador',
required=True
),
'identificador': fields.char('Identificador', size=16),
'prontuario': fields.char('Prontuario (si existe)', size=64),
'center_id': fields.many2one(
'prison.center',
string='CRS Actual',
required=True
),
'birth_date': fields.date('Fecha de Nacimiento'),
'age': fields.function(
_compute_age,
string="Edad",
type='char',
size=16,
multi='age'
),
'etnia_id': fields.many2one(
'res.etnia',
string='Etnia',
),
'sex': fields.selection([
('hombre', 'Hombre'),
('mujer', 'Mujer')],
string='Sexo',
required=True
),
'lang_id': fields.many2one(
'res.language',
string='Idioma'
),
'religion_id': fields.many2one(
'res.religion',
string='Religion'
),
'country_id': fields.many2one(
'res.country',
string='Pais',
),
'state_id': fields.many2one(
'res.country.state',
string='Provincia / Estado'
),
'city_id': fields.many2one(
'res.country.state.city',
string='Ciudad'
),
'canton_id': fields.many2one(
'res.canton',
string='Canton'
),
'parish_id': fields.many2one(
'res.parish',
string='Parroquia',
),
'civil_state': fields.many2one(
'res.civil.status',
string='Estado Civil',
),
'level_of_education': fields.many2one(
'res.education.level',
string='Nivel de Academico',
),
'senescyt': fields.char('Registro Senescyt', size=32),
# Informacion penitenciaria
'state': fields.selection(
[('preso', 'Privado de Libertad'),
('free', 'En Libertad')],
string='Estado',
required=True,
readonly=True
),
'verificado': fields.boolean('Indentidad Verificada', readonly=True),
'date_in': fields.date('Ultima Fecha Detencion'),
'time_in_prison': fields.function(
_compute_age,
string='Tiempo Recluido',
type='char', size=16,
multi='age'
),
'image': fields.binary("Foto",
help="This field holds the image used as photo for the employee, limited to 1024x1024px."),
'image_medium': fields.function(_get_image, fnct_inv=_set_image,
string="Medium-sized photo", type="binary", multi="_get_image",
store = {
'prison.person': (lambda self,
cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Medium-sized photo of the employee. It is automatically "\
"resized as a 128x128px image, with aspect ratio preserved. "\
"Use this field in form views or some kanban views."),
'image_small': fields.function(_get_image, fnct_inv=_set_image,
string="Small-sized photo", type="binary", multi="_get_image",
store = {
'prison.person': (lambda self,
cr, uid, ids, c={}: ids, ['image'], 10),
},
help="Small-sized photo of the employee. It is automatically "\
"resized as a 64x64px image, with aspect ratio preserved. "\
"Use this field anywhere a small image is required."),
'has_image': fields.function(_has_image, type="boolean"),
'data_registro_civil': fields.text('Datos de Registro Civil'),
'alias': fields.many2many('prison.person.alias',string='Alias'),
#Extras
'identity_ids': fields.one2many(
'prison.person.identity',
'person_id',
string='Indetificacion'
),
'family_ids': fields.one2many(
'prison.person.family',
'person_id',
string='Familiares'
),
'eval_ids': fields.one2many(
'prison.evaluation',
'ppl_id',
string='Evaluaciones'
)
}
_defaults = {
'state': 'preso',
'tipo_identificador': 'cedula',
'sex': 'hombre'
}
def onchange_date(self, cr, uid, ids, field, date):
"""
Calculo de fecha para vista
"""
if not date:
return {}
val = self.compute_age(date)
return {'value': {field: val}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment