[FIX] website_hr_recruitment: add param to force all countries else it use GeoIP...
[odoo/odoo.git] / addons / website_hr_recruitment / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import base64
3
4 from openerp import SUPERUSER_ID
5 from openerp import http
6 from openerp.tools.translate import _
7 from openerp.http import request
8
9 from openerp.addons.website.models.website import slug
10
11 class website_hr_recruitment(http.Controller):
12     @http.route([
13         '/jobs',
14         '/jobs/country/<model("res.country"):country>',
15         '/jobs/department/<model("hr.department"):department>',
16         '/jobs/country/<model("res.country"):country>/department/<model("hr.department"):department>',
17         '/jobs/office/<int:office_id>',
18         '/jobs/country/<model("res.country"):country>/office/<int:office_id>',
19         '/jobs/department/<model("hr.department"):department>/office/<int:office_id>',
20         '/jobs/country/<model("res.country"):country>/department/<model("hr.department"):department>/office/<int:office_id>',
21     ], type='http', auth="public", website=True)
22     def jobs(self, country=None, department=None, office_id=None, **kwargs):
23         env = request.env(context=dict(request.env.context, show_address=True, no_tag_br=True))
24
25         Country = env['res.country']
26         Jobs = env['hr.job']
27
28         # List jobs available to current UID
29         job_ids = Jobs.search([], order="website_published desc,no_of_recruitment desc").ids
30         # Browse jobs as superuser, because address is restricted
31         jobs = Jobs.sudo().browse(job_ids)
32
33         # Deduce departments and offices of those jobs
34         departments = set(j.department_id for j in jobs if j.department_id)
35         offices = set(j.address_id for j in jobs if j.address_id)
36         countries = set(o.country_id for o in offices if o.country_id)
37
38         # Default search by user country
39         if not (country or department or office_id):
40             country_code = request.session['geoip'].get('country_code')
41             if country_code:
42                 countries_ = Country.search([('code', '=', country_code)])
43                 country = countries_[0] if countries_ else None
44                 if not any(j for j in jobs if j.address_id and j.address_id.country_id == country):
45                     country = False
46
47         # Filter the matching one
48         if country and not kwargs.get('all_countries'):
49             jobs = (j for j in jobs if j.address_id is None or j.address_id.country_id and j.address_id.country_id.id == country.id)
50         if department:
51             jobs = (j for j in jobs if j.department_id and j.department_id.id == department.id)
52         if office_id:
53             jobs = (j for j in jobs if j.address_id and j.address_id.id == office_id)
54
55         # Render page
56         return request.website.render("website_hr_recruitment.index", {
57             'jobs': jobs,
58             'countries': countries,
59             'departments': departments,
60             'offices': offices,
61             'country_id': country,
62             'department_id': department,
63             'office_id': office_id,
64         })
65
66     @http.route('/jobs/add', type='http', auth="user", website=True)
67     def jobs_add(self, **kwargs):
68         job = request.env['hr.job'].create({
69             'name': _('New Job Offer'),
70         })
71         return request.redirect("/jobs/detail/%s?enable_editor=1" % slug(job))
72
73     @http.route('/jobs/detail/<model("hr.job"):job>', type='http', auth="public", website=True)
74     def jobs_detail(self, job, **kwargs):
75         return request.render("website_hr_recruitment.detail", {
76             'job': job,
77             'main_object': job,
78         })
79
80     @http.route('/jobs/apply/<model("hr.job"):job>', type='http', auth="public", website=True)
81     def jobs_apply(self, job):
82         error = {}
83         default = {}
84         if 'website_hr_recruitment_error' in request.session:
85             error = request.session.pop('website_hr_recruitment_error')
86             default = request.session.pop('website_hr_recruitment_default')
87         return request.render("website_hr_recruitment.apply", {
88             'job': job,
89             'error': error,
90             'default': default,
91         })
92
93     @http.route('/jobs/thankyou', methods=['POST'], type='http', auth="public", website=True)
94     def jobs_thankyou(self, **post):
95         error = {}
96         for field_name in ["partner_name", "phone", "email_from"]:
97             if not post.get(field_name):
98                 error[field_name] = 'missing'
99         if error:
100             request.session['website_hr_recruitment_error'] = error
101             ufile = post.pop('ufile')
102             if ufile:
103                 error['ufile'] = 'reset'
104             request.session['website_hr_recruitment_default'] = post
105             return request.redirect('/jobs/apply/%s' % post.get("job_id"))
106
107         # public user can't create applicants (duh)
108         env = request.env(user=SUPERUSER_ID)
109         value = {
110             'source_id' : env.ref('hr_recruitment.source_website_company').id,
111             'name': '%s\'s Application' % post.get('partner_name'), 
112         }
113         for f in ['email_from', 'partner_name', 'description']:
114             value[f] = post.get(f)
115         for f in ['department_id', 'job_id']:
116             value[f] = int(post.get(f) or 0)
117         # Retro-compatibility for saas-3. "phone" field should be replace by "partner_phone" in the template in trunk.
118         value['partner_phone'] = post.pop('phone', False)
119
120         applicant_id = env['hr.applicant'].create(value).id
121         if post['ufile']:
122             attachment_value = {
123                 'name': post['ufile'].filename,
124                 'res_name': value['partner_name'],
125                 'res_model': 'hr.applicant',
126                 'res_id': applicant_id,
127                 'datas': base64.encodestring(post['ufile'].read()),
128                 'datas_fname': post['ufile'].filename,
129             }
130             env['ir.attachment'].create(attachment_value)
131         return request.render("website_hr_recruitment.thankyou", {})
132
133 # vim :et: