Ejemplo n.º 1
0
    def split_pdf(self, indices=None, remainder=False):
        """
        called by the Document Viewer's Split PDF button.
        evaluates the input string and turns it into a list of lists to be processed by _split_pdf_groups

        :param indices: the formatted string of pdf split (e.g. 1,5-10, 8-22, 29-34) o_page_number_input
        :param remainder: bool, if true splits the non specified pages, one by one. form checkbox o_remainder_input
        :returns the list of the ID's of the newly created pdf attachments.
        """
        self.ensure_one()
        if 'pdf' not in self.mimetype:
            raise exceptions.ValidationError(
                _("ERROR: the file must be a PDF"))
        if indices:
            try:
                pages = [[int(x) for x in x.split('-')]
                         for x in indices.split(',')]
            except ValueError:
                raise exceptions.ValidationError(
                    _("ERROR: Invalid list of pages to split. Example: 1,5-9,10"
                      ))
            return self._split_pdf_groups(pdf_groups=[[min(x), max(x)]
                                                      for x in pages],
                                          remainder=remainder)
        return self._split_pdf_groups(remainder=remainder)
Ejemplo n.º 2
0
 def _check_validity_check_in_check_out(self):
     """ verifies if check_in is earlier than check_out. """
     for attendance in self:
         if attendance.check_in and attendance.check_out:
             if attendance.check_out < attendance.check_in:
                 raise exceptions.ValidationError(
                     _('"Check Out" time cannot be earlier than "Check In" time.'
                       ))
Ejemplo n.º 3
0
    def _check_validity(self):
        """ Verifies the validity of the attendance record compared to the others from the same employee.
            For the same employee we must have :
                * maximum 1 "open" attendance record (without check_out)
                * no overlapping time slices with previous employee records
        """
        for attendance in self:
            # we take the latest attendance before our check_in time and check it doesn't overlap with ours
            last_attendance_before_check_in = self.env['hr.attendance'].search([
                ('employee_id', '=', attendance.employee_id.id),
                ('check_in', '<=', attendance.check_in),
                ('id', '!=', attendance.id),
            ], order='check_in desc', limit=1)
            if last_attendance_before_check_in and last_attendance_before_check_in.check_out and last_attendance_before_check_in.check_out > attendance.check_in:
                raise exceptions.ValidationError(_("Cannot create new attendance record for %(empl_name)s, the employee was already checked in on %(datetime)s") % {
                    'empl_name': attendance.employee_id.name,
                    'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(attendance.check_in))),
                })

            if not attendance.check_out:
                # if our attendance is "open" (no check_out), we verify there is no other "open" attendance
                no_check_out_attendances = self.env['hr.attendance'].search([
                    ('employee_id', '=', attendance.employee_id.id),
                    ('check_out', '=', False),
                    ('id', '!=', attendance.id),
                ], order='check_in desc', limit=1)
                if no_check_out_attendances:
                    raise exceptions.ValidationError(_("Cannot create new attendance record for %(empl_name)s, the employee hasn't checked out since %(datetime)s") % {
                        'empl_name': attendance.employee_id.name,
                        'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(no_check_out_attendances.check_in))),
                    })
            else:
                # we verify that the latest attendance with check_in time before our check_out time
                # is the same as the one before our check_in time computed before, otherwise it overlaps
                last_attendance_before_check_out = self.env['hr.attendance'].search([
                    ('employee_id', '=', attendance.employee_id.id),
                    ('check_in', '<', attendance.check_out),
                    ('id', '!=', attendance.id),
                ], order='check_in desc', limit=1)
                if last_attendance_before_check_out and last_attendance_before_check_in != last_attendance_before_check_out:
                    raise exceptions.ValidationError(_("Cannot create new attendance record for %(empl_name)s, the employee was already checked in on %(datetime)s") % {
                        'empl_name': attendance.employee_id.name,
                        'datetime': fields.Datetime.to_string(fields.Datetime.context_timestamp(self, fields.Datetime.from_string(last_attendance_before_check_out.check_in))),
                    })
Ejemplo n.º 4
0
    def _split_pdf_groups(self, pdf_groups=None, remainder=False):
        """
        calls _make_pdf to create the a new attachment for each page section.
        :param pdf_groups: a list of lists representing the pages to split:  pages = [[1,1], [4,5], [7,7]]
        :returns the list of the ID's of the new PDF attachments.

        """
        self.ensure_one()
        with io.BytesIO(base64.b64decode(self.datas)) as stream:
            try:
                input_pdf = PdfFileReader(stream)
            except Exception:
                raise exceptions.ValidationError(_("ERROR: Invalid PDF file!"))
            max_page = input_pdf.getNumPages()
            remainder_set = set(range(0, max_page))
            new_pdf_ids = []
            if not pdf_groups:
                pdf_groups = []
            for pages in pdf_groups:
                pages[1] = min(max_page, pages[1])
                pages[0] = min(max_page, pages[0])
                if pages[0] == pages[1]:
                    name_ext = "%s" % (pages[0], )
                else:
                    name_ext = "%s-%s" % (pages[0], pages[1])
                output = PdfFileWriter()
                for i in range(pages[0] - 1, pages[1]):
                    output.addPage(input_pdf.getPage(i))
                new_pdf_id = self._make_pdf(output, name_ext)
                new_pdf_ids.append(new_pdf_id)
                remainder_set = remainder_set.difference(
                    set(range(pages[0] - 1, pages[1])))
            if remainder:
                for i in remainder_set:
                    output_page = PdfFileWriter()
                    name_ext = "%s" % (i + 1, )
                    output_page.addPage(input_pdf.getPage(i))
                    new_pdf_id = self._make_pdf(output_page, name_ext)
                    new_pdf_ids.append(new_pdf_id)
                self.write({'active': False})
            elif not len(remainder_set):
                self.write({'active': False})
            return new_pdf_ids
Ejemplo n.º 5
0
 def _verify_pin(self):
     for employee in self:
         if employee.pin and not employee.pin.isdigit():
             raise exceptions.ValidationError(
                 _("The PIN must be a sequence of digits."))
Ejemplo n.º 6
0
 def unlink(self):
     if any(self.get_external_id().values()) and not self._context.get(MODULE_UNINSTALL_FLAG):
         raise exceptions.ValidationError("You can not delete activity type that are used as master data.")
     return super(MailActivityType, self).unlink()