| 1 |  |  | #!/usr/bin/env python3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | Created on Mon Jan 28 11:09:02 2019 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | @author: Paolo Cozzi <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from django.db import models | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from django.contrib.postgres.fields import ArrayField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from image_app.models import Name, Animal, Sample, Submission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ValidationResult(models.Model): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     name = models.OneToOneField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         Name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         on_delete=models.CASCADE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     status = models.CharField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             max_length=255, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             blank=False, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             null=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     messages = ArrayField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         models.TextField(max_length=255, blank=True), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         default=list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     def __str__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return "%s:%s" % (self.name, self.status) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class ValidationSummary(models.Model): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     submission = models.ForeignKey( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Submission, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         on_delete=models.CASCADE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     all_count = models.PositiveIntegerField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         default=0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         help_text="number of all samples or animals in a submission") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     validation_known_count = models.PositiveIntegerField(default=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     issues_count = models.PositiveIntegerField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         default=0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         help_text="number of samples or animals with issues in validation") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     pass_count = models.PositiveIntegerField(default=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     warning_count = models.PositiveIntegerField(default=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     error_count = models.PositiveIntegerField(default=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     json_count = models.PositiveIntegerField(default=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     # TODO: should this be a ENUM object? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     type = models.CharField(max_length=6, blank=True, null=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     messages = ArrayField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         models.TextField(max_length=255, blank=True), | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |         default=list | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     ) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     def get_unknown_count(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         """Returns number of samples or animals with unknown validation""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return self.all_count - self.validation_known_count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     def reset_all_count(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         """Set all_count column according to Animal/Sample objects""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if self.type == "animal": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             self.all_count = Animal.objects.filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 name__submission=self.submission).count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         elif self.type == "sample": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             self.all_count = Sample.objects.filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 name__submission=self.submission).count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             raise Exception("Unknown type %s" % (self.type)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |         self.save() | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |