def get_timesheets(): groupby_mapping = self._get_groupby_mapping() field = groupby_mapping.get(groupby, None) orderby = '%s, %s' % (field, order) if field else order timesheets = Timesheet_sudo.search(domain, order=orderby, limit=_items_per_page, offset=pager['offset']) if field: if groupby == 'date': time_data = Timesheet_sudo.read_group(domain, ['date', 'unit_amount:sum'], ['date:day']) mapped_time = dict([(datetime.strptime(m['date:day'], '%d %b %Y').date(), m['unit_amount']) for m in time_data]) grouped_timesheets = [(Timesheet_sudo.concat(*g), mapped_time[k]) for k, g in groupbyelem(timesheets, itemgetter('date'))] else: time_data = time_data = Timesheet_sudo.read_group(domain, [field, 'unit_amount:sum'], [field]) mapped_time = dict([(m[field][0] if m[field] else False, m['unit_amount']) for m in time_data]) grouped_timesheets = [(Timesheet_sudo.concat(*g), mapped_time[k.id]) for k, g in groupbyelem(timesheets, itemgetter(field))] return timesheets, grouped_timesheets grouped_timesheets = [( timesheets, sum(Timesheet_sudo.search(domain).mapped('unit_amount')) )] if timesheets else [] return timesheets, grouped_timesheets
def portal_my_tasks(self, page=1, date_begin=None, date_end=None, sortby=None, filterby=None, search=None, search_in='content', **kw): groupby = kw.get('groupby', 'project') #TODO master fix this values = self._prepare_portal_layout_values() searchbar_sortings = { 'date': { 'label': _('Newest'), 'order': 'create_date desc' }, 'name': { 'label': _('Title'), 'order': 'name' }, 'stage': { 'label': _('Stage'), 'order': 'stage_id' }, 'update': { 'label': _('Last Stage Update'), 'order': 'date_last_stage_update desc' }, } searchbar_filters = { 'all': { 'label': _('All'), 'domain': [] }, } searchbar_inputs = { 'content': { 'input': 'content', 'label': _('Search <span class="nolabel"> (in Content)</span>') }, 'message': { 'input': 'message', 'label': _('Search in Messages') }, 'customer': { 'input': 'customer', 'label': _('Search in Customer') }, 'stage': { 'input': 'stage', 'label': _('Search in Stages') }, 'all': { 'input': 'all', 'label': _('Search in All') }, } searchbar_groupby = { 'none': { 'input': 'none', 'label': _('None') }, 'project': { 'input': 'project', 'label': _('Project') }, } # extends filterby criteria with project (criteria name is the project id) # Note: portal users can't view projects they don't follow partner = request.env.user.partner_id domain_projects = [ '&', ('privacy_visibility', '=', 'portal'), '|', ('message_partner_ids', 'child_of', [partner.commercial_partner_id.id]), ('task_ids.message_partner_ids', 'child_of', [partner.commercial_partner_id.id]) ] projects = request.env['project.project'].sudo().search( domain_projects) domain = [('project_id', 'in', projects.ids)] for proj in projects: searchbar_filters.update({ str(proj.id): { 'label': proj.name, 'domain': [('project_id', '=', proj.id)] } }) # default sort by value if not sortby: sortby = 'date' order = searchbar_sortings[sortby]['order'] # default filter by value if not filterby: filterby = 'all' domain += searchbar_filters[filterby]['domain'] # archive groups - Default Group By 'create_date' archive_groups = self._get_archive_groups('project.task', domain) if date_begin and date_end: domain += [('create_date', '>', date_begin), ('create_date', '<=', date_end)] # search if search and search_in: search_domain = [] if search_in in ('content', 'all'): search_domain = OR([ search_domain, [ '|', ('name', 'ilike', search), ('description', 'ilike', search) ] ]) if search_in in ('customer', 'all'): search_domain = OR( [search_domain, [('partner_id', 'ilike', search)]]) if search_in in ('message', 'all'): search_domain = OR( [search_domain, [('message_ids.body', 'ilike', search)]]) if search_in in ('stage', 'all'): search_domain = OR( [search_domain, [('stage_id', 'ilike', search)]]) domain += search_domain # task count task_count = request.env['project.task'].search_count(domain) # pager pager = portal_pager(url="/my/tasks", url_args={ 'date_begin': date_begin, 'date_end': date_end, 'sortby': sortby, 'filterby': filterby, 'search_in': search_in, 'search': search }, total=task_count, page=page, step=self._items_per_page) # content according to pager and archive selected if groupby == 'project': order = "project_id, %s" % order # force sort on project first to group by project in view tasks = request.env['project.task'].search(domain, order=order, limit=self._items_per_page, offset=(page - 1) * self._items_per_page) request.session['my_tasks_history'] = tasks.ids[:100] if groupby == 'project': grouped_tasks = [ request.env['project.task'].concat(*g) for k, g in groupbyelem(tasks, itemgetter('project_id')) ] else: grouped_tasks = [tasks] values.update({ 'date': date_begin, 'date_end': date_end, 'projects': projects, 'tasks': tasks, 'grouped_tasks': grouped_tasks, 'page_name': 'task', 'archive_groups': archive_groups, 'default_url': '/my/tasks', 'pager': pager, 'searchbar_sortings': searchbar_sortings, 'searchbar_groupby': searchbar_groupby, 'searchbar_inputs': searchbar_inputs, 'search_in': search_in, 'sortby': sortby, 'groupby': groupby, 'searchbar_filters': OrderedDict(sorted(searchbar_filters.items())), 'filterby': filterby, }) return request.render("project.portal_my_tasks", values)
def portal_my_tasks(self, page=1, date_begin=None, date_end=None, sortby=None, filterby=None, search=None, search_in='content', groupby=None, **kw): values = self._prepare_portal_layout_values() searchbar_sortings = { 'date': {'label': _('Newest'), 'order': 'create_date desc'}, 'name': {'label': _('Title'), 'order': 'name'}, 'stage': {'label': _('Stage'), 'order': 'stage_id, project_id'}, 'project': {'label': _('Project'), 'order': 'project_id, stage_id'}, 'update': {'label': _('Last Stage Update'), 'order': 'date_last_stage_update desc'}, } searchbar_filters = { 'all': {'label': _('All'), 'domain': []}, } searchbar_inputs = { 'content': {'input': 'content', 'label': _('Search <span class="nolabel"> (in Content)</span>')}, 'message': {'input': 'message', 'label': _('Search in Messages')}, 'customer': {'input': 'customer', 'label': _('Search in Customer')}, 'stage': {'input': 'stage', 'label': _('Search in Stages')}, 'project': {'input': 'project', 'label': _('Search in Project')}, 'all': {'input': 'all', 'label': _('Search in All')}, } searchbar_groupby = { 'none': {'input': 'none', 'label': _('None')}, 'project': {'input': 'project', 'label': _('Project')}, 'stage': {'input': 'stage', 'label': _('Stage')}, } # extends filterby criteria with project the customer has access to projects = request.env['project.project'].search([]) for project in projects: searchbar_filters.update({ str(project.id): {'label': project.name, 'domain': [('project_id', '=', project.id)]} }) # extends filterby criteria with project (criteria name is the project id) # Note: portal users can't view projects they don't follow project_groups = request.env['project.task'].read_group([('project_id', 'not in', projects.ids)], ['project_id'], ['project_id']) for group in project_groups: proj_id = group['project_id'][0] if group['project_id'] else False proj_name = group['project_id'][1] if group['project_id'] else _('Others') searchbar_filters.update({ str(proj_id): {'label': proj_name, 'domain': [('project_id', '=', proj_id)]} }) # default sort by value if not sortby: sortby = 'date' order = searchbar_sortings[sortby]['order'] # default filter by value if not filterby: filterby = 'all' domain = searchbar_filters.get(filterby, searchbar_filters.get('all'))['domain'] # default group by value if not groupby: groupby = 'project' if date_begin and date_end: domain += [('create_date', '>', date_begin), ('create_date', '<=', date_end)] # search if search and search_in: search_domain = [] if search_in in ('content', 'all'): search_domain = OR([search_domain, ['|', ('name', 'ilike', search), ('description', 'ilike', search)]]) if search_in in ('customer', 'all'): search_domain = OR([search_domain, [('partner_id', 'ilike', search)]]) if search_in in ('message', 'all'): search_domain = OR([search_domain, [('message_ids.body', 'ilike', search)]]) if search_in in ('stage', 'all'): search_domain = OR([search_domain, [('stage_id', 'ilike', search)]]) if search_in in ('project', 'all'): search_domain = OR([search_domain, [('project_id', 'ilike', search)]]) domain += search_domain # task count task_count = request.env['project.task'].search_count(domain) # pager pager = portal_pager( url="/my/tasks", url_args={'date_begin': date_begin, 'date_end': date_end, 'sortby': sortby, 'filterby': filterby, 'groupby': groupby, 'search_in': search_in, 'search': search}, total=task_count, page=page, step=self._items_per_page ) # content according to pager and archive selected if groupby == 'project': order = "project_id, %s" % order # force sort on project first to group by project in view elif groupby == 'stage': order = "stage_id, %s" % order # force sort on stage first to group by stage in view tasks = request.env['project.task'].search(domain, order=order, limit=self._items_per_page, offset=pager['offset']) request.session['my_tasks_history'] = tasks.ids[:100] if groupby == 'project': grouped_tasks = [request.env['project.task'].concat(*g) for k, g in groupbyelem(tasks, itemgetter('project_id'))] elif groupby == 'stage': grouped_tasks = [request.env['project.task'].concat(*g) for k, g in groupbyelem(tasks, itemgetter('stage_id'))] else: grouped_tasks = [tasks] values.update({ 'date': date_begin, 'date_end': date_end, 'grouped_tasks': grouped_tasks, 'page_name': 'task', 'default_url': '/my/tasks', 'pager': pager, 'searchbar_sortings': searchbar_sortings, 'searchbar_groupby': searchbar_groupby, 'searchbar_inputs': searchbar_inputs, 'search_in': search_in, 'search': search, 'sortby': sortby, 'groupby': groupby, 'searchbar_filters': OrderedDict(sorted(searchbar_filters.items())), 'filterby': filterby, }) return request.render("project.portal_my_tasks", values)