|
55 | 55 | from bika.lims.content.bikaschema import BikaSchema
|
56 | 56 | from bika.lims.content.bikaschema import BikaFolderSchema
|
57 | 57 | from bika.lims import bikaMessageFactory as _
|
58 |
| -from bika.lims import deprecated |
59 | 58 |
|
60 | 59 | schema = BikaFolderSchema.copy() + BikaSchema.copy() + Schema((
|
61 | 60 |
|
@@ -514,7 +513,7 @@ def getCertifications(self):
|
514 | 513 | """ Returns the certifications of the instrument. Both internal
|
515 | 514 | and external certifitions
|
516 | 515 | """
|
517 |
| - return [c for c in self.objectValues('InstrumentCertification') if c] |
| 516 | + return self.objectValues('InstrumentCertification') |
518 | 517 |
|
519 | 518 | def getValidCertifications(self):
|
520 | 519 | """ Returns the certifications fully valid
|
@@ -616,128 +615,111 @@ def isOutOfDate(self):
|
616 | 615 | """ Returns if the current instrument is out-of-date regards to
|
617 | 616 | its certifications
|
618 | 617 | """
|
619 |
| - cert = self.getLatestValidCertification() |
620 |
| - today = date.today() |
621 |
| - if cert and cert.getValidTo(): |
622 |
| - validto = cert.getValidTo().asdatetime().date(); |
623 |
| - if validto > today: |
624 |
| - return False |
| 618 | + certification = self.getLatestValidCertification() |
| 619 | + if certification: |
| 620 | + return not certification.isValid() |
625 | 621 | return True
|
626 | 622 |
|
627 | 623 | def isValidationInProgress(self):
|
628 | 624 | """ Returns if the current instrument is under validation progress
|
629 | 625 | """
|
630 | 626 | validation = self.getLatestValidValidation()
|
631 |
| - today = date.today() |
632 |
| - if validation and validation.getDownTo(): |
633 |
| - validfrom = validation.getDownFrom().asdatetime().date() |
634 |
| - validto = validation.getDownTo().asdatetime().date() |
635 |
| - if validfrom <= today <= validto: |
636 |
| - return True |
| 627 | + if validation: |
| 628 | + return validation.isValidationInProgress() |
637 | 629 | return False
|
638 | 630 |
|
639 | 631 | def isCalibrationInProgress(self):
|
640 | 632 | """ Returns if the current instrument is under calibration progress
|
641 | 633 | """
|
642 |
| - for calibration in self.getCalibrations(): |
643 |
| - if calibration.isCalibrationInProgress(): |
644 |
| - return True |
| 634 | + calibration = self.getLatestValidCalibration() |
| 635 | + if calibration is not None: |
| 636 | + return calibration.isCalibrationInProgress() |
645 | 637 | return False
|
646 | 638 |
|
647 | 639 | def getCertificateExpireDate(self):
|
648 | 640 | """ Returns the current instrument's data expiration certificate
|
649 | 641 | """
|
650 |
| - cert = self.getLatestValidCertification() |
651 |
| - if cert == None: |
652 |
| - return '' |
653 |
| - date = cert.getValidTo() |
654 |
| - return date |
| 642 | + certification = self.getLatestValidCertification() |
| 643 | + if certification: |
| 644 | + return certification.getValidTo() |
| 645 | + return None |
655 | 646 |
|
656 | 647 | def getWeeksToExpire(self):
|
657 |
| - """ Returns the amount of weeks untils it's certification expire |
| 648 | + """ Returns the amount of weeks and days untils it's certification expire |
658 | 649 | """
|
659 |
| - cert = self.getLatestValidCertification() |
660 |
| - if cert == None: |
661 |
| - return '' |
662 |
| - date = cert.getValidTo().asdatetime().date(); |
663 |
| - return date - date.today() |
| 650 | + certification = self.getLatestValidCertification() |
| 651 | + if certification: |
| 652 | + return certification.getWeeksAndDaysToExpire() |
| 653 | + return 0, 0 |
664 | 654 |
|
665 | 655 | def getLatestValidCertification(self):
|
666 |
| - """ Returns the latest valid certification. If no latest valid |
667 |
| - certification found, returns None |
| 656 | + """Returns the certification with the most remaining days until expiration. |
| 657 | + If no certification was found, it returns None. |
668 | 658 | """
|
669 |
| - saved_cert = None |
670 |
| - lastfrom = None |
671 |
| - lastto = None |
672 |
| - for cert in self.getCertifications(): |
673 |
| - if not cert.isValid(): |
674 |
| - continue |
675 |
| - validfrom = cert.getValidFrom() if cert else None |
676 |
| - validto = cert.getValidTo() if validfrom else None |
677 |
| - if not validfrom or not validto: |
678 |
| - continue |
679 |
| - validfrom = validfrom.asdatetime().date() |
680 |
| - validto = validto.asdatetime().date() |
681 |
| - if not saved_cert \ |
682 |
| - or validto > lastto \ |
683 |
| - or (validto == lastto and validfrom > lastfrom): |
684 |
| - saved_cert = cert |
685 |
| - lastfrom = validfrom |
686 |
| - lastto = validto |
687 |
| - return saved_cert |
| 659 | + |
| 660 | + # 1. get all certifications |
| 661 | + certifications = self.getCertifications() |
| 662 | + |
| 663 | + # 2. filter out certifications, which are invalid |
| 664 | + valid_certifications = filter(lambda x: x.isValid(), certifications) |
| 665 | + |
| 666 | + # 3. sort by the remaining days to expire, e.g. [10, 7, 6, 1] |
| 667 | + def sort_func(x, y): |
| 668 | + return cmp(x.getDaysToExpire(), y.getDaysToExpire()) |
| 669 | + sorted_certifications = sorted(valid_certifications, cmp=sort_func, reverse=True) |
| 670 | + |
| 671 | + # 4. return the certification with the most remaining days |
| 672 | + if len(sorted_certifications) > 0: |
| 673 | + return sorted_certifications[0] |
| 674 | + return None |
688 | 675 |
|
689 | 676 | def getLatestValidValidation(self):
|
690 |
| - """ Returns the latest *done* validation. If no latest *done* |
691 |
| - validation found, returns None |
| 677 | + """Returns the validation with the most remaining days in validation. |
| 678 | + If no validation was found, it returns None. |
692 | 679 | """
|
693 |
| - validation = None |
694 |
| - lastfrom = None |
695 |
| - lastto = None |
696 |
| - for v in self.getValidations(): |
697 |
| - if v.isValidationInProgress() or v.isFutureValidation(): |
698 |
| - continue |
699 |
| - validfrom = v.getDownFrom() if v else None |
700 |
| - validto = v.getDownTo() if validfrom else None |
701 |
| - if not validfrom or not validto: |
702 |
| - continue |
703 |
| - validfrom = validfrom.asdatetime().date() |
704 |
| - validto = validto.asdatetime().date() |
705 |
| - if not validation \ |
706 |
| - or validto > lastto \ |
707 |
| - or (validto == lastto and validfrom > lastfrom): |
708 |
| - validation = v |
709 |
| - lastfrom = validfrom |
710 |
| - lastto = validto |
711 |
| - return validation |
| 680 | + # 1. get all validations |
| 681 | + validations = self.getValidations() |
| 682 | + |
| 683 | + # 2. filter out validations, which are not in progress |
| 684 | + active_validations = filter(lambda x: x.isValidationInProgress(), validations) |
| 685 | + |
| 686 | + # 3. sort by the remaining days in validation, e.g. [10, 7, 6, 1] |
| 687 | + def sort_func(x, y): |
| 688 | + return cmp(x.getRemainingDaysInValidation(), y.getRemainingDaysInValidation()) |
| 689 | + sorted_validations = sorted(active_validations, cmp=sort_func, reverse=True) |
| 690 | + |
| 691 | + # 4. return the validation with the most remaining days |
| 692 | + if len(sorted_validations) > 0: |
| 693 | + return sorted_validations[0] |
| 694 | + return None |
712 | 695 |
|
713 | 696 | def getLatestValidCalibration(self):
|
714 |
| - """ Returns the latest *done* calibration. If no latest *done* |
715 |
| - calibration found, returns None |
| 697 | + """Returns the calibration with the most remaining days in calibration. |
| 698 | + If no calibration was found, it returns None. |
716 | 699 | """
|
717 |
| - calibration = None |
718 |
| - lastto = None |
719 |
| - for cal in self.getCalibrations(): |
720 |
| - if cal.isCalibrationInProgress() or cal.isFutureCalibration(): |
721 |
| - continue |
722 |
| - validfrom = cal.getDownFrom() if cal else None |
723 |
| - validto = cal.getDownTo() if validfrom else None |
724 |
| - if not validfrom or not validto: |
725 |
| - continue |
726 |
| - validto = validto.asdatetime().date() |
727 |
| - if calibration is None or validto > lastto: |
728 |
| - calibration = cal |
729 |
| - lastto = validto |
730 |
| - return calibration |
| 700 | + # 1. get all calibrations |
| 701 | + calibrations = self.getCalibrations() |
| 702 | + |
| 703 | + # 2. filter out calibrations, which are not in progress |
| 704 | + active_calibrations = filter(lambda x: x.isCalibrationInProgress(), calibrations) |
| 705 | + |
| 706 | + # 3. sort by the remaining days in calibration, e.g. [10, 7, 6, 1] |
| 707 | + def sort_func(x, y): |
| 708 | + return cmp(x.getRemainingDaysInCalibration(), y.getRemainingDaysInCalibration()) |
| 709 | + sorted_calibrations = sorted(active_calibrations, cmp=sort_func, reverse=True) |
| 710 | + |
| 711 | + # 4. return the calibration with the most remaining days |
| 712 | + if len(sorted_calibrations) > 0: |
| 713 | + return sorted_calibrations[0] |
| 714 | + return None |
731 | 715 |
|
732 | 716 | def getValidations(self):
|
733 |
| - """ |
734 |
| - Return all the validations objects related with the instrument |
| 717 | + """ Return all the validations objects related with the instrument |
735 | 718 | """
|
736 | 719 | return self.objectValues('InstrumentValidation')
|
737 | 720 |
|
738 | 721 | def getDocuments(self):
|
739 |
| - """ |
740 |
| - Return all the multifile objects related with the instrument |
| 722 | + """ Return all the multifile objects related with the instrument |
741 | 723 | """
|
742 | 724 | return self.objectValues('Multifile')
|
743 | 725 |
|
@@ -839,6 +821,8 @@ def addReferences(self, reference, service_uids):
|
839 | 821 | wf.doActionFor(ref_analysis, 'assign')
|
840 | 822 | addedanalyses.append(ref_analysis)
|
841 | 823 |
|
| 824 | + self.setAnalyses(self.getAnalyses() + addedanalyses) |
| 825 | + |
842 | 826 | # Initialize LatestReferenceAnalyses cache
|
843 | 827 | self.cleanReferenceAnalysesCache()
|
844 | 828 |
|
|
0 commit comments