def name_get(self): result = [] for shift_template in self: start_time = time(hour=int(shift_template.start_time), minute=round( math.modf(shift_template.start_time)[0] / (1 / 60.0))) duration = timedelta(hours=int(shift_template.duration), minutes=round( math.modf(shift_template.duration)[0] / (1 / 60.0))) end_time = datetime.combine(date.today(), start_time) + duration name = '%s - %s %s %s' % ( format_time(shift_template.env, start_time, time_format='short').replace(':00 ', ' '), format_time(shift_template.env, end_time.time(), time_format='short').replace( ':00 ', ' '), _('(%s days span)') % (duration.days + 1) if duration.days > 0 else '', shift_template.role_id.name if shift_template.role_id.name is not False else '') result.append([shift_template.id, name]) return result
def format_time(env, time, tz=False, time_format='medium', lang_code=False): try: return tools.format_time(env, time, tz=tz, time_format=time_format, lang_code=lang_code) except babel.core.UnknownLocaleError: return time
def _compute_last_activity(self): employees = self.filtered(lambda e: e.user_id) presences = self.env['bus.presence'].search([('user_id', 'in', employees.mapped('user_id.id'))]) for presence in presences: for employee in presence.user_id.employee_ids.filtered(lambda e: e in self): tz = employee.tz last_activity_datetime = presence.last_presence.replace(tzinfo=UTC).astimezone(timezone(tz)).replace(tzinfo=None) employee.last_activity = last_activity_datetime.date() if employee.last_activity == fields.Date.today(): employee.last_activity_time = format_time(self.env, last_activity_datetime, time_format='short') else: employee.last_activity_date = False
def name_get(self): group_by = self.env.context.get('group_by', []) field_list = [ fname for fname in self._name_get_fields() if fname not in group_by ][:2] # limit to 2 labels result = [] for slot in self: # label part, depending on context `groupby` name = ' - '.join([ self._fields[fname].convert_to_display_name(slot[fname], slot) for fname in field_list if slot[fname] ]) # date / time part destination_tz = pytz.timezone(self.env.user.tz or 'UTC') start_datetime = pytz.utc.localize( slot.start_datetime).astimezone(destination_tz).replace( tzinfo=None) end_datetime = pytz.utc.localize( slot.end_datetime).astimezone(destination_tz).replace( tzinfo=None) if slot.end_datetime - slot.start_datetime <= timedelta( hours=24): # shift on a single day name = '%s - %s %s' % (format_time( self.env, start_datetime.time(), time_format='short'), format_time(self.env, end_datetime.time(), time_format='short'), name) else: name = '%s - %s %s' % (start_datetime.date(), end_datetime.date(), name) # add unicode bubble to tell there is a note if slot.name: name = u'%s \U0001F4AC' % name result.append([slot.id, name]) return result
def _compute_last_activity(self): presences = self.env['bus.presence'].search_read([('user_id', 'in', self.mapped('user_id').ids)], ['user_id', 'last_presence']) # transform the result to a dict with this format {user.id: last_presence} presences = {p['user_id'][0]: p['last_presence'] for p in presences} for employee in self: tz = employee.tz last_presence = presences.get(employee.user_id.id, False) if last_presence: last_activity_datetime = last_presence.replace(tzinfo=UTC).astimezone(timezone(tz)).replace(tzinfo=None) employee.last_activity = last_activity_datetime.date() if employee.last_activity == fields.Date.today(): employee.last_activity_time = format_time(self.env, last_activity_datetime, time_format='short') else: employee.last_activity_time = False else: employee.last_activity = False employee.last_activity_time = False