Completed
Pull Request — master (#45)
by Paolo
06:21
created

validation.models.ValidationSummary.reset()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 1
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
53
    # TODO: should this be a ENUM object?
54
    type = models.CharField(max_length=6, blank=True, null=True)
55
56
    messages = ArrayField(
57
        models.TextField(max_length=255, blank=True),
58
        default=list
59
    )
60
61
    def get_unknown_count(self):
62
        """Returns number of samples or animals with unknown validation"""
63
64
        return self.all_count - self.validation_known_count
65
66
    def reset_all_count(self):
67
        """Set all_count column according to Animal/Sample objects"""
68
69
        if self.type == "animal":
70
            self.all_count = Animal.objects.filter(
71
                name__submission=self.submission).count()
72
73
        elif self.type == "sample":
74
            self.all_count = Sample.objects.filter(
75
                name__submission=self.submission).count()
76
77
        else:
78
            raise Exception("Unknown type '%s'" % (self.type))
79
80
        self.save()
81
82
    def reset(self):
83
        """Sets all counts to 0, except all_count field"""
84
        self.pass_count = 0
85
        self.warning_count = 0
86
        self.error_count = 0
87
        self.issues_count = 0
88
        self.validation_known_count = 0
89
        self.messages = list()
90
        self.save()
91