odoo18/addons_extensions/hr_recruitment_extended/models/recruitment_attachments.py

76 lines
3.4 KiB
Python

from odoo import models, fields, api
from odoo.exceptions import ValidationError
import base64
import os
class RecruitmentAttachments(models.Model):
_name = 'recruitment.attachments'
_description = 'Recruitment Attachments'
name = fields.Char(string='Name', required=True)
attachment_type = fields.Selection([('personal','Personal Documents'),('education','Education Documents'),('previous_employer','Previous Employer'),('others','Others')],default="others",required=True)
is_default = fields.Boolean(string='Is Default')
employee_recruitment_attachments = fields.One2many('employee.recruitment.attachments','recruitment_attachment_id',string="Documents")
class EmployeeRecruitmentAttachments(models.Model):
_name = "employee.recruitment.attachments"
_rec_name = 'name'
name = fields.Char(string='Attachment Name', required=True)
employee_id = fields.Many2one('hr.employee')
applicant_id = fields.Many2one('hr.applicant')
candidate_id = fields.Many2one('hr.candidate')
recruitment_attachment_id = fields.Many2one('recruitment.attachments')
recruitment_attachment_type = fields.Selection([('personal','Personal Documents'),('education','Education Documents'),('previous_employer','Previous Employer'),('others','Others')],related='recruitment_attachment_id.attachment_type')
file = fields.Binary(string='File', required=True)
file_name = fields.Char(string='Filename')
review_status = fields.Selection([('draft','Under Review'),('pass','PASS'),('fail','FAIL')], default='draft')
review_comments = fields.Char()
@api.constrains('file', 'file_name')
def _check_file_validation(self):
allowed_extensions = ['pdf', 'doc', 'jpg', 'jpeg']
for rec in self:
if rec.file and rec.file_name:
extension = os.path.splitext(rec.file_name)[1].lower().replace('.', '')
if extension not in allowed_extensions:
raise ValidationError(
"Only PDF, DOC and JPG files are allowed."
)
file_size = len(base64.b64decode(rec.file))
if file_size > 5 * 1024 * 1024:
raise ValidationError(
"File size cannot exceed 5 MB."
)
def action_preview_file(self):
""" Returns a URL to preview the attachment in a popup """
for record in self:
if record.file:
attachment = self.env.ref("hr_recruitment_extended.employee_recruitment_attachments_preview")
attachment.sudo().datas = record.file
return {
'name': "File Preview",
'type': 'ir.actions.act_url',
'url': f'/web/content/{attachment.id}?download=false',
'target': 'current', # Opens in a new tab
}
@api.model
def create(self, vals):
# Auto-link applicant_id if context is passed correctly
if self.env.context.get('default_applicant_id'):
vals['applicant_id'] = self.env.context['default_applicant_id']
return super().create(vals)
class Employee(models.Model):
_inherit='hr.employee'
employee_attachment_ids = fields.One2many('employee.recruitment.attachments','employee_id',string="Attachments")