Merge lp:~openerp-dev/openobject-addons/7.0-sort-events-mat into lp:openobject-addons/7.0

Proposed by Martin Trigaux (OpenERP)
Status: Merged
Merged at revision: 9747
Proposed branch: lp:~openerp-dev/openobject-addons/7.0-sort-events-mat
Merge into: lp:openobject-addons/7.0
Diff against target: 124 lines (+47/-14)
2 files modified
base_calendar/base_calendar.py (+46/-13)
base_calendar/crm_meeting.py (+1/-1)
To merge this branch: bzr merge lp:~openerp-dev/openobject-addons/7.0-sort-events-mat
Reviewer Review Type Date Requested Status
Christophe Simonis (OpenERP) Disapprove
Review via email: mp+200981@code.launchpad.net

Description of the change

Allow sorting on events, using patch from Erik Heeren

To post a comment you must log in.
Revision history for this message
Christophe Simonis (OpenERP) (kangol) wrote :

not for 7 as it change API.

review: Disapprove
Revision history for this message
Martin Trigaux (OpenERP) (mat-openerp) wrote :

Merged in 7.0 (after change according to Christophe's comments

revno: 9747 [merge]
revision-id: <email address hidden>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'base_calendar/base_calendar.py'
2--- base_calendar/base_calendar.py 2013-09-17 09:39:06 +0000
3+++ base_calendar/base_calendar.py 2014-01-09 13:09:43 +0000
4@@ -26,9 +26,11 @@
5 from openerp.osv import fields, osv
6 from openerp.service import web_services
7 from openerp.tools.translate import _
8+
9 import pytz
10 import re
11 import time
12+from operator import itemgetter
13 from openerp import tools, SUPERUSER_ID
14
15 months = {
16@@ -1170,28 +1172,44 @@
17 (_check_closing_date, 'Error ! End date cannot be set before start date.', ['date_deadline']),
18 ]
19
20+ # TODO for trunk: remove get_recurrent_ids
21 def get_recurrent_ids(self, cr, uid, select, domain, limit=100, context=None):
22+ """Wrapper for _get_recurrent_ids to get the 'order' parameter from the context"""
23+ if not context:
24+ context = {}
25+ order = context.get('order', self._order)
26+ return self._get_recurrent_ids(cr, uid, select, domain, limit=limit, order=order, context=context)
27+
28+ def _get_recurrent_ids(self, cr, uid, select, domain, limit=100, order=None, context=None):
29 """Gives virtual event ids for recurring events based on value of Recurrence Rule
30 This method gives ids of dates that comes between start date and end date of calendar views
31 @param self: The object pointer
32 @param cr: the current row, from the database cursor,
33 @param uid: the current user's ID for security checks,
34- @param limit: The Number of Results to Return """
35+ @param limit: The Number of Results to Return
36+ @param order: The fields (comma separated, format "FIELD {DESC|ASC}") on which the events should be sorted"""
37 if not context:
38 context = {}
39
40 result = []
41- for data in super(calendar_event, self).read(cr, uid, select, ['rrule', 'recurrency', 'exdate', 'exrule', 'date'], context=context):
42+ result_data = []
43+ fields = ['rrule', 'recurrency', 'exdate', 'exrule', 'date']
44+ if order:
45+ order_fields = [field.split()[0] for field in order.split(',')]
46+ else:
47+ # fallback on self._order defined on the model
48+ order_fields = [field.split()[0] for field in self._order.split(',')]
49+ fields = list(set(fields + order_fields))
50+
51+ for data in super(calendar_event, self).read(cr, uid, select, fields, context=context):
52 if not data['recurrency'] or not data['rrule']:
53+ result_data.append(data)
54 result.append(data['id'])
55 continue
56 event_date = datetime.strptime(data['date'], "%Y-%m-%d %H:%M:%S")
57
58 # TOCHECK: the start date should be replaced by event date; the event date will be changed by that of calendar code
59
60- if not data['rrule']:
61- continue
62-
63 exdate = data['exdate'] and data['exdate'].split(',') or []
64 rrule_str = data['rrule']
65 new_rrule_str = []
66@@ -1249,12 +1267,24 @@
67 if [True for item in new_pile if not item]:
68 continue
69 idval = real_id2base_calendar_id(data['id'], r_date.strftime("%Y-%m-%d %H:%M:%S"))
70+ r_data = dict(data, id=idval, date=r_date.strftime("%Y-%m-%d %H:%M:%S"))
71 result.append(idval)
72-
73- if isinstance(select, (str, int, long)):
74- return ids and ids[0] or False
75- else:
76- ids = list(set(result))
77+ result_data.append(r_data)
78+ ids = list(set(result))
79+
80+ if order_fields:
81+
82+ def comparer(left, right):
83+ for fn, mult in comparers:
84+ result = cmp(fn(left), fn(right))
85+ if result:
86+ return mult * result
87+ return 0
88+
89+ sort_params = [key.split()[0] if key[-4:].lower() != 'desc' else '-%s' % key.split()[0] for key in (order or self._order).split(',')]
90+ comparers = [ ((itemgetter(col[1:]), -1) if col[0] == '-' else (itemgetter(col), 1)) for col in sort_params]
91+ ids = [r['id'] for r in sorted(result_data, cmp=comparer)]
92+
93 return ids
94
95 def compute_rule_string(self, data):
96@@ -1378,10 +1408,13 @@
97 new_id = get_real_ids(arg[2])
98 new_arg = (arg[0], arg[1], new_id)
99 new_args.append(new_arg)
100- #offset, limit and count must be treated separately as we may need to deal with virtual ids
101- res = super(calendar_event, self).search(cr, uid, new_args, offset=0, limit=0, order=order, context=context, count=False)
102 if context.get('virtual_id', True):
103- res = self.get_recurrent_ids(cr, uid, res, args, limit, context=context)
104+ #offset, limit, order and count must be treated separately as we may need to deal with virtual ids
105+ res = super(calendar_event, self).search(cr, uid, new_args, offset=0, limit=0, order=None, context=context, count=False)
106+ res = self._get_recurrent_ids(cr, uid, res, args, limit, order=order, context=context)
107+ else:
108+ res = super(calendar_event, self).search(cr, uid, new_args, offset=offset, limit=limit, order=order, context=context, count=count)
109+
110 if count:
111 return len(res)
112 elif limit:
113
114=== modified file 'base_calendar/crm_meeting.py'
115--- base_calendar/crm_meeting.py 2012-12-20 12:49:27 +0000
116+++ base_calendar/crm_meeting.py 2014-01-09 13:09:43 +0000
117@@ -135,7 +135,7 @@
118
119 def _find_allowed_model_wise(self, cr, uid, doc_model, doc_dict, context=None):
120 if doc_model == 'crm.meeting':
121- for virtual_id in self.pool.get(doc_model).get_recurrent_ids(cr, uid, doc_dict.keys(), [], context=context):
122+ for virtual_id in self.pool.get(doc_model)._get_recurrent_ids(cr, uid, doc_dict.keys(), [], context=context):
123 doc_dict.setdefault(virtual_id, doc_dict[get_real_ids(virtual_id)])
124 return super(mail_message, self)._find_allowed_model_wise(cr, uid, doc_model, doc_dict, context=context)
125