~school-dev-team/school-base-openerp-module/school-ampa-openerp-module

« back to all changes in this revision

Viewing changes to oerp_modules/training/wizard/create_session.py

  • Committer: pereerro
  • Date: 2013-11-30 20:34:17 UTC
  • Revision ID: pereerro-20131130203417-whwzik8hkmm6j89g
[REF] Begining of integration with school and training.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv
 
24
from osv import fields
 
25
import pooler
 
26
import time
 
27
import tools
 
28
from tools.translate import _
 
29
 
 
30
class training_session_create_wizard(osv.osv_memory):
 
31
    _name = 'training.session.create.wizard'
 
32
 
 
33
    _columns = {
 
34
        'date': fields.datetime('Date', required=True),
 
35
        'name': fields.char('Name', size=64, required=True),
 
36
    }
 
37
 
 
38
    def _default_get_name(self, cr, uid, context=None):
 
39
        if context is None:
 
40
            return ''
 
41
        active_id = context.get('active_id')
 
42
        if not active_id:
 
43
            return ''
 
44
        offer = self.pool.get('training.offer').browse(cr, uid, active_id, context=context)
 
45
        return offer.name
 
46
 
 
47
    _defaults = {
 
48
        'date': lambda *a: time.strftime('%Y-%m-%d 08:30:00'),
 
49
        'name': _default_get_name,
 
50
    }
 
51
 
 
52
    def create_session(self, cr, uid, ids, context=None):
 
53
        session_ids = []
 
54
        pool = pooler.get_pool(cr.dbname)
 
55
        proxy = pool.get('training.offer')
 
56
        offer = proxy.browse(cr, uid, context['active_id'], context=context)
 
57
        for form in self.read(cr, uid, ids, [], context=context):
 
58
            if not offer.can_be_planned:
 
59
                raise osv.except_osv(_('Warning'), _("You can not create a session with a non-validated offer") )
 
60
 
 
61
            proxy = pool.get('training.session')
 
62
            session_id = proxy.create(cr, uid,
 
63
                                      {
 
64
                                          'offer_id' : offer.id,
 
65
                                          'date' : form['date'],
 
66
                                          'name' : "%s (%s)" % (form['name'], time.strftime("%Y-%m-%d", time.strptime(form['date'], '%Y-%m-%d %H:%M:%S'))),
 
67
                                          'format_id' : offer.format_id.id,
 
68
                                      }
 
69
                                     )
 
70
            vals = proxy.on_change_offer(cr, uid, None, context['active_id'])
 
71
            if isinstance(vals, dict):
 
72
                # we have specified a custom name on session creation,
 
73
                # don't let on_change overwrite it now.
 
74
                try:
 
75
                    if 'name' in vals['value']:
 
76
                        del vals['value']['name']
 
77
                except KeyError:
 
78
                    pass
 
79
                proxy.write(cr, uid, [session_id], vals['value'], context=context)
 
80
            session_ids.append(session_id)
 
81
        number_of_sessions = len(session_ids)
 
82
        if not number_of_sessions:
 
83
            return {}
 
84
        elif number_of_sessions == 1:
 
85
            return {
 
86
                'res_id': int(session_ids[0]),
 
87
                'name': 'Sessions',
 
88
                'view_type': 'form',
 
89
                "view_mode": 'form,tree',
 
90
                'res_model': 'training.session',
 
91
                'view_id': pool.get('ir.ui.view').search(cr,uid,[('name','=','training.session.form')]),
 
92
                'type': 'ir.actions.act_window',
 
93
                'target': 'current',
 
94
            }
 
95
        else:
 
96
            return {
 
97
                'domain' : "[('id', 'in', [%s])]" % ','.join(map(str,session_ids)),
 
98
                'name' : 'Sessions',
 
99
                'view_type' : 'form',
 
100
                'view_mode' : 'tree,form',
 
101
                'res_model' : 'training.session',
 
102
                'view_id' : False,
 
103
                'type' : 'ir.actions.act_window',
 
104
                'target': 'current',
 
105
            }
 
106
 
 
107
training_session_create_wizard()