Launchpad automatic translations update.
[odoo/odoo.git] / addons / base_calendar / wizard / base_calendar_set_exrule.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from base_calendar import base_calendar
23 from osv import fields, osv
24 from tools.translate import _
25 import tools
26 import re
27
28
29 months = {
30     1: "January", 2: "February", 3: "March", 4: "April", \
31     5: "May", 6: "June", 7: "July", 8: "August", 9: "September", \
32     10: "October", 11: "November", 12: "December"
33 }
34
35 class base_calendar_set_exrule(osv.osv_memory):
36     """
37     Set Exrule.
38     """
39
40     _name = "base.calendar.set.exrule"
41     _description = "Set Exrule"
42
43     _columns = {
44       'freq': fields.selection([('None', 'No Repeat'), \
45                                 ('secondly', 'Secondly'), \
46                                 ('minutely', 'Minutely'), \
47                                 ('hourly', 'Hourly'), \
48                                 ('daily', 'Daily'), \
49                                 ('weekly', 'Weekly'), \
50                                 ('monthly', 'Monthly'), \
51                                 ('yearly', 'Yearly')], 'Frequency',required=True),
52         'interval': fields.integer('Interval'),
53         'count': fields.integer('Count'),
54         'mo': fields.boolean('Mon'),
55         'tu': fields.boolean('Tue'),
56         'we': fields.boolean('Wed'),
57         'th': fields.boolean('Thu'),
58         'fr': fields.boolean('Fri'),
59         'sa': fields.boolean('Sat'),
60         'su': fields.boolean('Sun'),
61         'select1': fields.selection([('date', 'Date of month'), \
62                                     ('day', 'Day of month')], 'Option'),
63         'day': fields.integer('Date of month'),
64         'week_list': fields.selection([('MO', 'Monday'), ('TU', 'Tuesday'), \
65                                    ('WE', 'Wednesday'), ('TH', 'Thursday'), \
66                                    ('FR', 'Friday'), ('SA', 'Saturday'), \
67                                    ('SU', 'Sunday')], 'Weekday'),
68         'byday': fields.selection([('1', 'First'), ('2', 'Second'), \
69                                    ('3', 'Third'), ('4', 'Fourth'), \
70                                    ('5', 'Fifth'), ('-1', 'Last')], 'By day'),
71         'month_list': fields.selection(months.items(),'Month'),
72         'end_date': fields.date('Repeat Until'),
73
74     }
75
76     def view_init(self, cr, uid, fields, context=None):
77         """
78         This function checks for precondition before wizard executes
79         @param self: The object pointer
80         @param cr: the current row, from the database cursor,
81         @param uid: the current user’s ID for security checks,
82         @param fields: List of fields for default value
83         @param context: A standard dictionary for contextual values
84         """
85         if context is None: 
86             context = {}
87         event_obj = self.pool.get(context.get('active_model'))
88         for event in event_obj.browse(cr, uid, context.get('active_ids', []), context=context):
89             if not event.rrule:
90                 raise osv.except_osv(_("Warning !"), _("Please Apply Recurrency before applying Exception Rule."))
91         return False
92
93     def compute_exrule_string(self, cr, uid, ids, context=None):
94         """
95         Compute rule string.
96         @param self: the object pointer
97         @param cr: the current row, from the database cursor,
98         @param uid: the current user’s ID for security checks,
99         @param datas: dictionary of freq and interval value.
100         @return: string value which compute FREQILY;INTERVAL
101         """
102
103         weekdays = ['mo', 'tu', 'we', 'th', 'fr', 'sa', 'su']
104         weekstring = ''
105         monthstring = ''
106         yearstring = ''
107         if context is None: 
108             context = {}
109         ex_id = base_calendar.base_calendar_id2real_id(context.get('active_id', False))
110         model = context.get('model', False)
111         model_obj = self.pool.get(model)
112         for datas in self.read(cr, uid, ids, context=context):
113             freq = datas.get('freq')
114             if freq == 'None':
115                 model_obj.write(cr, uid, ex_id,{'exrule': ''})
116                 return{}
117
118             interval_srting = datas.get('interval') and (';INTERVAL=' + str(datas.get('interval'))) or ''
119
120             if freq == 'weekly':
121
122                 byday = map(lambda x: x.upper(), filter(lambda x: datas.get(x) and x in weekdays, datas))
123                 if byday:
124                     weekstring = ';BYDAY=' + ','.join(byday)
125
126             elif freq == 'monthly':
127                 if datas.get('select1')=='date' and (datas.get('day') < 1 or datas.get('day') > 31):
128                     raise osv.except_osv(_('Error!'), ("Please select proper Day of month"))
129                 if datas.get('select1')=='day':
130                     monthstring = ';BYDAY=' + datas.get('byday') + datas.get('week_list')
131                 elif datas.get('select1')=='date':
132                     monthstring = ';BYMONTHDAY=' + str(datas.get('day'))
133
134             elif freq == 'yearly':
135                 if datas.get('select1')=='date' and (datas.get('day') < 1 or datas.get('day') > 31):
136                     raise osv.except_osv(_('Error!'), ("Please select proper Day of month"))
137                 bymonth = ';BYMONTH=' + str(datas.get('month_list'))
138                 if datas.get('select1')=='day':
139                     bystring = ';BYDAY=' + datas.get('byday') + datas.get('week_list')
140                 elif datas.get('select1')=='date':
141                     bystring = ';BYMONTHDAY=' + str(datas.get('day'))
142                 yearstring = bymonth + bystring
143
144             if datas.get('end_date'):
145                 datas['end_date'] = ''.join((re.compile('\d')).findall(datas.get('end_date'))) + '235959Z'
146             enddate = (datas.get('count') and (';COUNT=' + str(datas.get('count'))) or '') +\
147                                  ((datas.get('end_date') and (';UNTIL=' + datas.get('end_date'))) or '')
148
149             exrule_string = 'FREQ=' + freq.upper() + weekstring + interval_srting \
150                                 + enddate + monthstring + yearstring
151
152             model_obj.write(cr, uid, ex_id,{'exrule': exrule_string})
153             return {'type': 'ir.actions.act_window_close'}
154
155         _defaults = {
156          'freq': lambda *x: 'None',
157          'select1': lambda *x: 'date',
158          'interval': lambda *x: 1,
159     }
160
161 base_calendar_set_exrule()
162
163 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: