Skip to content
Permalink
Browse files

Merge pull request #617 from GabbasovDinar/9.0-timelog

[REF] refactoring and fixing for project_timelog 9.0
  • Loading branch information...
yelizariev committed Aug 6, 2019
2 parents 80555e1 + 0699766 commit 04c1cc16110860a31fe1cfe8660ea1c0ef517f17
@@ -0,0 +1,75 @@
From d83720172376a7cabe097c4a5ed202dc9c90ca83 Mon Sep 17 00:00:00 2001
From: GabbasovDinar <gabbasov@it-projects.info>
Date: Thu, 21 Jun 2018 13:38:32 +0500
Subject: [PATCH] [IMP] support grouping by weeks starting with Sunday

---
openerp/models.py | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/openerp/models.py b/openerp/models.py
index ef1d925..d7a6dcc 100644
--- a/openerp/models.py
+++ b/openerp/models.py
@@ -1947,6 +1947,7 @@ class BaseModel(object):
# Cfr: http://babel.pocoo.org/docs/dates/#date-fields
'day': 'dd MMM yyyy', # yyyy = normal year
'week': "'W'w YYYY", # w YYYY = ISO week-year
+ 'week_sunday': "'W'w'(s)' YYYY", # w YYYY = ISO week-year
'month': 'MMMM yyyy',
'quarter': 'QQQ yyyy',
'year': 'yyyy',
@@ -1954,13 +1955,27 @@ class BaseModel(object):
time_intervals = {
'day': dateutil.relativedelta.relativedelta(days=1),
'week': datetime.timedelta(days=7),
+ 'week_sunday': datetime.timedelta(days=7),
'month': dateutil.relativedelta.relativedelta(months=1),
'quarter': dateutil.relativedelta.relativedelta(months=3),
'year': dateutil.relativedelta.relativedelta(years=1)
}
if tz_convert:
qualified_field = "timezone('%s', timezone('UTC',%s))" % (self._context.get('tz', 'UTC'), qualified_field)
- qualified_field = "date_trunc('%s', %s)" % (gb_function or 'month', qualified_field)
+
+ if gb_function == 'week_sunday':
+ # postgres's date_trunc supports only weeks starting with Monday
+ # So, make this hack to support weeks starting with Sunday
+
+ if field_type == 'date':
+ # interger must be used to don't convert DATE to TIMESTAMP
+ interval_day = "integer '1'"
+ else:
+ interval_day = "interval '1 day'"
+ qualified_field = "date_trunc('week', %s + %s)" % (qualified_field, interval_day)
+ else:
+ qualified_field = "date_trunc('%s', %s)" % (gb_function or 'month', qualified_field)
+
if field_type == 'boolean':
qualified_field = "coalesce(%s,false)" % qualified_field
return {
@@ -1995,6 +2010,10 @@ class BaseModel(object):
a given value. This is mostly relevant for date/datetime.
"""
if groupby['type'] in ('date', 'datetime') and value:
+ if ':week_sunday' in groupby['groupby']:
+ # postgres's date_trunc supports only weeks starting with Monday
+ # So, make this hack to support weeks starting with Sunday
+ value -= dateutil.relativedelta.relativedelta(days=1)
dt_format = DEFAULT_SERVER_DATETIME_FORMAT if groupby['type'] == 'datetime' else DEFAULT_SERVER_DATE_FORMAT
domain_dt_begin = value
domain_dt_end = value + groupby['interval']
@@ -2017,6 +2036,10 @@ class BaseModel(object):
for k,v in data.iteritems():
gb = groupby_dict.get(k)
if gb and gb['type'] in ('date', 'datetime') and v:
+ if ':week_sunday' in gb['groupby']:
+ # postgres's date_trunc supports only weeks starting with Monday
+ # So, make this hack to support weeks starting with Sunday
+ v -= dateutil.relativedelta.relativedelta(days=1)
data[k] = babel.dates.format_date(v, format=gb['display_format'], locale=context.get('lang', 'en_US'))

data['__domain'] = domain_group + domain
--
1.9.1

@@ -74,12 +74,22 @@ def init_timelog(self, **kwargs):
if timer_status:
day_timer = day_timer + current_time

config = request.env["ir.config_parameter"]

# 4. All time this week
week_timer = 0
today = datetime.datetime.today()
monday = today - datetime.timedelta(datetime.datetime.weekday(today))
sunday = today + datetime.timedelta(6 - datetime.datetime.weekday(today))
timelog_this_week = timelog_obj.search([("user_id", "=", user.id), ("start_datetime", ">=", monday.strftime('%Y-%m-%d 00:00:00')), ("start_datetime", "<=", sunday.strftime('%Y-%m-%d 23:59:59'))])

first_weekday = config.get_param("project_timelog.first_weekday")

if first_weekday == 'monday':
first_day_of_week = today - datetime.timedelta(datetime.datetime.weekday(today))
last_day_of_week = today + datetime.timedelta(6 - datetime.datetime.weekday(today))
elif first_weekday == 'sunday':
first_day_of_week = today - datetime.timedelta(1 + datetime.datetime.weekday(today))
last_day_of_week = today + datetime.timedelta(5 - datetime.datetime.weekday(today))

timelog_this_week = timelog_obj.search([("user_id", "=", user.id), ("start_datetime", ">=", first_day_of_week.strftime('%Y-%m-%d 00:00:00')), ("start_datetime", "<=", last_day_of_week.strftime('%Y-%m-%d 23:59:59'))])
if timelog_this_week:
week_work_ids = []
for r in timelog_this_week:
@@ -100,24 +110,21 @@ def init_timelog(self, **kwargs):
if timelogs_other_users:
another_users = []
for u in timelogs_other_users:
another_users.append({
'id': u.user_id.id,
'name': u.user_id.name
})
another_users.append(u.user_id.id)
another_users = list(set(another_users))
for u in another_users:
res = timelog_obj.search([("user_id", "=", u['id']), ("work_id.task_id", "=", task.id)])
for user_id in another_users:
res = timelog_obj.search([("user_id", "=", user_id), ("work_id.task_id", "=", task.id)])
sum_another_timelog = 0
for i in res:
sum_another_timelog = sum_another_timelog + i.corrected_duration
sum_another_timelog = 3600 * sum_another_timelog
sum_another_timelog = datetime.timedelta(seconds=round(sum_another_timelog, 0))
second_timer_info.append(u['name'] + ": " + str(sum_another_timelog) + "\n")
current_user = request.env['res.users'].browse(user_id)
second_timer_info.append(current_user.name + ": " + str(sum_another_timelog) + "\n")

for r in second_timer_info:
desctiption_timer = desctiption_timer + r

config = request.env["ir.config_parameter"]
convert_sec = 3600

timer_stopline = False
@@ -165,7 +172,9 @@ def init_timelog(self, **kwargs):
"task_name": task_name,

"timelog_id": last_timelog,
"end_datetime_status": end_datetime_status
"end_datetime_status": end_datetime_status,

"first_weekday": first_weekday,
}

@http.route('/timelog/connection', type='http', auth="public")
@@ -6,6 +6,11 @@ Installation
============

* `Install <https://odoo-development.readthedocs.io/en/latest/odoo/usage/install-module.html>`__ this module in a usual way
* To use the module, you need to be sure that your odoo instance support longpolling, i.e. Instant Messaging works. Read more about how to use the `longpolling <https://odoo-development.readthedocs.io/en/latest/admin/longpolling.html>`_
* You need to install a patch to use the new group (the beginning of the week is Sunday). The use of the patch is optional and needed only to use Sunday grouping (otherwise only Monday works). More `here. <https://github.com/odoo/odoo/pull/25086>`__ To apply the patch execute::

cd /path/to/odoo/source
patch -p1 < /path/to/0001-IMP-support-grouping-by-weeks-starting-with-Sunday.patch

Usage
=====
@@ -37,8 +42,3 @@ Forced completion of the task
Uninstallation
==============
* Open the``Project timelog`` module by going to Apps and click on the ``Uninstall`` button.

Note
====

* To use the module, you need to be sure that your odoo instance support longpolling, i.e. Instant Messaging works. Read more about how to use the `longpolling <https://odoo-development.readthedocs.io/en/latest/admin/longpolling.html>`_
@@ -14,6 +14,7 @@ class TimelogConfigSettings(models.TransientModel):

normal_time_week = fields.Float(string='Set normal time', help="""Setting time standards provided throughout the week""")
good_time_week = fields.Float(string='Set good time', help="""Set in excess of the time allowed for the week""")
first_weekday = fields.Selection([('monday', 'Monday'), ('sunday', 'Sunday')], string='Beginning of the Week', default='monday')

@api.multi
def set_custom_parameters(self):
@@ -26,6 +27,7 @@ def set_custom_parameters(self):
config_parameters.set_param(key="project_timelog.good_time_day", value=record.good_time_day)
config_parameters.set_param(key="project_timelog.normal_time_week", value=record.normal_time_week)
config_parameters.set_param(key="project_timelog.good_time_week", value=record.good_time_week)
config_parameters.set_param(key="project_timelog.first_weekday", value=record.first_weekday)

@api.multi
def get_default_custom_parameters(self, fields=None):
@@ -37,6 +39,7 @@ def get_default_custom_parameters(self, fields=None):
'good_time_day': icp.get_param('project_timelog.good_time_day'),
'normal_time_week': icp.get_param('project_timelog.normal_time_week'),
'good_time_week': icp.get_param('project_timelog.good_time_week'),
'first_weekday': icp.get_param('project_timelog.first_weekday'),
}

@api.model
@@ -48,3 +51,4 @@ def init_timer_parametrs(self):
icp.set_param(key="project_timelog.good_time_day", value=6)
icp.set_param(key="project_timelog.normal_time_week", value=30)
icp.set_param(key="project_timelog.good_time_week", value=40)
icp.set_param(key="project_timelog.first_weekday", value='monday')
@@ -450,10 +450,14 @@ odoo.define('project_timelog.timelog', function(require){
};
} else {
context = {
'search_default_week': 1,
'search_default_group_tasks': 1,
'search_default_group_subtasks': 1,
};
if (this.config.first_weekday === 'sunday') {
context.search_default_week_sunday = 1;
} else {
context.search_default_week = 1;
}
}
if (!action) {
action = {
@@ -167,14 +167,17 @@
<field name="end_datetime" />
<filter name="today" string="Today" domain="[('start_datetime', '&gt;=', datetime.datetime.combine(context_today(), datetime.time(0,0,0))), '|', ('end_datetime', '=', False), ('end_datetime', '&lt;=', datetime.datetime.combine(context_today(), datetime.time(23,59,59)))]"/>
<separator/>
<filter name="week" string="This week" domain="[('start_datetime', '&gt;=', ((context_today()+relativedelta(weeks=-1, days=1, weekday=0)).strftime('%%Y-%%m-%%d'))+' '+'00:00:00'), '|', ('end_datetime', '=', False),
<filter name="week" string="This week (monday)" domain="[('start_datetime', '&gt;=', ((context_today()+relativedelta(weeks=-1, days=1, weekday=0)).strftime('%%Y-%%m-%%d'))+' '+'00:00:00'), '|', ('end_datetime', '=', False),
('end_datetime', '&lt;=', ((context_today()+relativedelta(weeks=0, weekday=6)).strftime('%%Y-%%m-%%d'))+' '+'23:59:59')]"/>
<filter name="week_sunday" string="This week (sunday)" domain="[('start_datetime', '&gt;=', ((context_today()+relativedelta(weeks=-1, days=1, weekday=-1)).strftime('%%Y-%%m-%%d'))+' '+'00:00:00'), '|', ('end_datetime', '=', False),
('end_datetime', '&lt;=', ((context_today()+relativedelta(weeks=0, weekday=5)).strftime('%%Y-%%m-%%d'))+' '+'23:59:59')]"/>
<group string="Group By">
<filter name="group_projects" string="Projects" domain="[]" context="{'group_by':'project_name'}"/>
<filter name="group_tasks" string="Tasks" domain="[]" context="{'group_by':'task_name'}"/>
<filter name="group_subtasks" string="Subtasks" domain="[]" context="{'group_by':'work_id'}"/>
<filter name="group_months" string="Months" domain="[]" context="{'group_by':'start_datetime:month'}" help="Months"/>
<filter name="group_weeks" string="Weeks" domain="[]" context="{'group_by':'start_datetime:week'}" help="Week"/>
<filter name="group_weeks" string="Weeks Monday" domain="[]" context="{'group_by':'start_datetime:week'}" help="Week"/>
<filter name="group_weeks_sunday" string="Weeks Sunday" domain="[]" context="{'group_by':'start_datetime:week_sunday'}" help="Week Sunday"/>
<filter name="group_days" string="Days" domain="[]" context="{'group_by':'start_datetime:day'}" help="Day"/>
<filter name="group_users" string="Users" context="{'group_by':'user_id'}" groups="project.group_project_manager"/>
</group>
@@ -50,6 +50,10 @@
<label for="good_time_week"/>
<field name="good_time_week" class="oe_inline" widget="float_time"/>
</div>
<div>
<label for="first_weekday"/>
<field name="first_weekday" class="oe_inline" required="1"/>
</div>
</div>
</group>
</form>

0 comments on commit 04c1cc1

Please sign in to comment.
You can’t perform that action at this time.