1 # -*- coding: utf-8 -*-
4 from openerp import SUPERUSER_ID
5 from openerp import http
6 from openerp.tools.translate import _
7 from openerp.http import request
9 from openerp.addons.website.models.website import slug
11 class website_hr_recruitment(http.Controller):
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))
25 Country = env['res.country']
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)
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)
38 # Default search by user country
39 if not (country or department or office_id):
40 country_code = request.session['geoip'].get('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):
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)
51 jobs = (j for j in jobs if j.department_id and j.department_id.id == department.id)
53 jobs = (j for j in jobs if j.address_id and j.address_id.id == office_id)
56 return request.website.render("website_hr_recruitment.index", {
58 'countries': countries,
59 'departments': departments,
61 'country_id': country,
62 'department_id': department,
63 'office_id': office_id,
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'),
71 return request.redirect("/jobs/detail/%s?enable_editor=1" % slug(job))
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", {
80 @http.route('/jobs/apply/<model("hr.job"):job>', type='http', auth="public", website=True)
81 def jobs_apply(self, job):
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", {
93 @http.route('/jobs/thankyou', methods=['POST'], type='http', auth="public", website=True)
94 def jobs_thankyou(self, **post):
96 for field_name in ["partner_name", "phone", "email_from"]:
97 if not post.get(field_name):
98 error[field_name] = 'missing'
100 request.session['website_hr_recruitment_error'] = error
101 ufile = post.pop('ufile')
103 error['ufile'] = 'reset'
104 request.session['website_hr_recruitment_default'] = post
105 return request.redirect('/jobs/apply/%s' % post.get("job_id"))
107 # public user can't create applicants (duh)
108 env = request.env(user=SUPERUSER_ID)
110 'source_id' : env.ref('hr_recruitment.source_website_company').id,
111 'name': '%s\'s Application' % post.get('partner_name'),
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)
120 applicant_id = env['hr.applicant'].create(value).id
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,
130 env['ir.attachment'].create(attachment_value)
131 return request.render("website_hr_recruitment.thankyou", {})