@http.route(['/leaves/int:leave_id/approve'], type='http', auth="user", website=True)
def portal_leave_approve(self, leave_id=None, access_token=None, **kw):
leave_sudo = self._check_access_rights(leave_id, access_token)
if not leave_sudo.can_approve:
raise ValidationError("You're not allowed to perform this action.")
leave_sudo.with_user(2).action_approve()
return request.redirect('my/leaves')
# @http.route(['/my/leaves/new'], type='http', auth="user", website=True)
# def portal_leave_new(self, **kw):
# leave_model = request.env['hr.leave'].with_user(2)
# employee = request.env['hr.employee'].with_user(2).search([('user_partner_id', '=', request.env.user.partner_id.id)], limit=1)
# if not employee:
# raise ValidationError('Missing required employee for this user.')
# if request.httprequest.method == 'POST':
# # Handle file upload
# attachment_data = []
# for c_file in request.httprequest.files.getlist('supported_attachment_ids'):
# # Ensure the file is saved with a secure filename
# filename = secure_filename(c_file.filename)
# # Read the file data in binary mode
# file_content = base64.b64encode(c_file.read())
# # Prepare attachment data for creation
# attachment_data.append({
# 'name': filename,
# 'datas': file_content,
# 'res_model': 'hr.leave',
# 'res_id': 0, # Placeholder, to be replaced by the ID of the leave record
# 'type': 'binary',
# })
# if not kw.get('date_to'):
# if isinstance(kw['date_from'], str):
# kw['date_from'] = datetime.strptime(kw['date_from'], '%Y-%m-%d').date()
# kw['date_to'] = kw['date_from'] + timedelta(days=1)
# kw['date_to'] = kw['date_to'].strftime('%Y-%m-%d')
# kw['date_from'] = kw['date_from'].strftime('%Y-%m-%d')
# kw.update({
# 'holiday_type': 'employee',
# 'employee_id': employee.id,
# 'holiday_status_id': int(kw['holiday_status_id']),
# 'state': 'draft',
# 'request_unit_half': kw.get('request_unit_half', False),
# 'date_from': parse(kw['date_from']),
# 'request_date_from': parse(kw['date_from']),
# 'date_to': parse(kw['date_to']),
# 'request_date_to': parse(kw['date_to']),
# # 'supported_attachment_ids': kw['supported_attachment_ids'],
# })
# if request.httprequest.files.getlist('supported_attachment_ids'):
# attachments = request.env['ir.attachment'].with_user(2).create([{
# 'name': attachment.filename,
# 'datas': base64.b64encode(attachment.read()),
# 'res_model': 'hr.leave',
# 'res_id': leave.id,
# } for attachment in request.httprequest.files.getlist('supported_attachment_ids')])
# leave.supported_attachment_ids = [(6, 0, attachments.ids)]
# _logger.info(f'creating new leave for employee {employee.name} with data {kw}')
# leave = leave_model.with_context(leave_skip_date_check=True).create(kw)
# leave.with_user(2).action_confirm()
# # Link the attachments to the newly created leave
# for attachment in attachment_data:
# attachment['res_id'] = leave.id
# request.env['ir.attachment'].with_user(2).create(attachment)
# kw.clear()
# return request.redirect('my/leaves')
# @http.route(['/my/leaves/new'], type='http', auth="user", website=True)
# def portal_leave_new(self, **kw):
# leave_model = request.env['hr.leave'].with_user(2)
# employee = request.env['hr.employee'].with_user(2).search([('user_partner_id', '=', request.env.user.partner_id.id)], limit=1)
# if not employee:
# raise ValidationError('Missing required employee for this user.')
# if request.httprequest.files.getlist('supported_attachment_ids'):
# if request.httprequest.method == 'POST':
# # Handle file upload
# attachment_data = []
# for c_file in request.httprequest.files.getlist('supported_attachment_ids'):
# # Ensure the file is saved with a secure filename
# filename = secure_filename(c_file.filename)
# # Read the file data in binary mode
# file_content = base64.b64encode(c_file.read())
# # Prepare attachment data for creation
# attachment_data.append({
# 'name': filename,
# 'datas': file_content,
# 'res_model': 'hr.leave',
# 'res_id': 0, # Placeholder, to be replaced by the ID of the leave record
# 'type': 'binary',
# })
# if not kw.get('date_to'):
# if isinstance(kw['date_from'], str):
# kw['date_from'] = datetime.strptime(kw['date_from'], '%Y-%m-%d').date()
# kw['date_to'] = kw['date_from'] + timedelta(days=1)
# kw['date_to'] = kw['date_to'].strftime('%Y-%m-%d')
# kw['date_from'] = kw['date_from'].strftime('%Y-%m-%d')
# kw.update({
# 'holiday_type': 'employee',
# 'employee_id': employee.id,
# 'holiday_status_id': int(kw['holiday_status_id']),
# 'state': 'draft',
# 'request_unit_half': kw.get('request_unit_half', False),
# 'date_from': parse(kw['date_from']),
# 'request_date_from': parse(kw['date_from']),
# 'date_to': parse(kw['date_to']),
# 'request_date_to': parse(kw['date_to']),
# # 'supported_attachment_ids': kw['supported_attachment_ids'],
# })
# _logger.info(f'creating new leave for employee {employee.name} with data {kw}')
# leave = leave_model.with_context(leave_skip_date_check=True).create(kw)
# leave.with_user(2).action_confirm()
# # Move the block here, after 'leave' is assigned
# if request.httprequest.files.getlist('supported_attachment_ids'):
# attachments = request.env['ir.attachment'].with_user(2).create([{
# 'name': attachment.filename,
# 'datas': base64.b64encode(attachment.read()),
# 'res_model': 'hr.leave',
# 'res_id': leave.id,
# } for attachment in request.httprequest.files.getlist('supported_attachment_ids')])
# leave.supported_attachment_ids = [(6, 0, attachments.ids)]
# kw.clear()
# return request.redirect('/my/leaves')
@http.route(['/my/leaves/new'], type='http', auth="user", website=True)
def portal_leave_new(self, **kw):
leave_model = request.env['hr.leave'].with_user(2)
employee = request.env['hr.employee'].with_user(2).search([('user_partner_id', '=', request.env.user.partner_id.id)], limit=1)
if not employee:
raise ValidationError('Missing required employee for this user.')
# # Handle file upload
# attachment_data = []
# for c_file in request.httprequest.files.getlist('attachment_ids'):
# # Ensure the file is saved with a secure filename
# filename = secure_filename(c_file.filename)
# # Read the file data in binary mode
# file_content = base64.b64encode(c_file.read())
# # Prepare attachment data for creation
# attachment_data.append({
# 'name': filename,
# 'datas': file_content,
# 'res_model': 'hr.leave',
# 'res_id': 0, # Placeholder, to be replaced by the ID of the leave record
# 'type': 'binary',
# })
if not kw.get('date_to'):
if isinstance(kw['date_from'], str):
kw['date_from'] = datetime.strptime(kw['date_from'], '%Y-%m-%d').date()
kw['date_to'] = kw['date_from'] + timedelta(days=1)
kw['date_to'] = kw['date_to'].strftime('%Y-%m-%d')
kw['date_from'] = kw['date_from'].strftime('%Y-%m-%d')
kw.update({
'holiday_type': 'employee',
'employee_id': employee.id,
'holiday_status_id': int(kw['holiday_status_id']),
'state': 'draft',
'request_unit_half': kw.get('request_unit_half', False),
'date_from': parse(kw['date_from']),
'request_date_from': parse(kw['date_from']),
'date_to': parse(kw['date_to']),
'request_date_to': parse(kw['date_to']),
})
# # Handle file upload
# attachment = request.httprequest.files.get('supported_attachment_ids')
# attachment_id = None
# if attachment:
# filename = secure_filename(attachment.filename)
# file_content = base64.b64encode(attachment.read())
# attachment_data = {
# 'name': filename,
# 'datas': file_content,
# 'res_model': 'hr.leave',
# 'type': 'binary',
# }
# attachment_record = request.env['ir.attachment'].with_user(2).create(attachment_data)
# attachment_id = attachment_record.id
_logger.info(f'creating new leave for employee {employee.name} with data {kw}')
leave = leave_model.with_context(leave_skip_date_check=True).create(kw)
leave.with_user(2).action_confirm()
# Create the leave record
# leave = leave_model.create(leave_data)
# If there's an attachment, link it to the leave record
# if attachment_id:
# leave.sudo().write({'attachment_ids': [(4, attachment_id)]}) # Link the attachment to the leave record
# Additional leave actions (e.g., confirm leave) could be performed here
# leave.sudo().action_confirm() # Uncomment this line if you want to automatically confirm the
# Link the attachments to the newly created leave
# for attachment in attachment_data:
# attachment['res_id'] = leave.id
# request.env['ir.attachment'].with_user(2).create(attachment)
kw.clear()
return request.redirect('/my/leaves')