Blame view

src/routine_manager/validators.py 2.61 KB
ddf59dd4   haribo   Remaniement :
1
from common.models import Sequence
ff448d43   Jeremy   Update
2
from django.conf import settings
bbf6e698   haribo   Date: 14/06/2016
3

3dbda6a0   haribo   Date: 17/06/2016
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def check_plan_validity(plan):
    """
        Checks if a plan is complete, and save its status in DB

        A plan is complete if His name, Filter, Duration and Nb_images are set
        :returns : a bool equal to True if the request was submitted but incomplete
    """

    if plan.name == None or plan.filter == None or plan.duration <= 0 or plan.nb_images <= 0:
        plan.complete = False
    else:
        plan.complete = True
    plan.save()
    return check_album_validity(plan.album)
bbf6e698   haribo   Date: 14/06/2016
18
19
20
21
22

def check_album_validity(alb):
    """
        Checks if an album is complete, and save its status in DB
        An album is complete if it has at least 1 plan, and all the plans are complete
eea995c9   haribo   Date: 16/06/2016
23
24
   
        :returns : a bool equal to True if the request was submitted but incomplete
bbf6e698   haribo   Date: 14/06/2016
25
26
27
28
29
30
31
    """

    if alb.plans.filter(complete=False).count() != 0 or alb.plans.count() == 0:
        alb.complete = False
    else:
        alb.complete = True
    alb.save()
eea995c9   haribo   Date: 16/06/2016
32
    return check_sequence_validity(alb.sequence)
bbf6e698   haribo   Date: 14/06/2016
33

3dbda6a0   haribo   Date: 17/06/2016
34

bbf6e698   haribo   Date: 14/06/2016
35
36
37
38
39
def check_sequence_validity(seq):
    """
        Computes the sequence duration
        Checks if a sequence is complete, and save its status in DB
        A sequence is complete if it has at least 1 album, and all the albums are complete
eea995c9   haribo   Date: 16/06/2016
40
41
   
        :returns : a bool equal to True if the request was submitted but incomplete
bbf6e698   haribo   Date: 14/06/2016
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    """

    max_duration = 0
    for album in seq.albums.all():
        duration = 0
        for plan in album.plans.all():
            duration = duration + plan.duration
        if max_duration < duration:
            max_duration = duration
    seq.duration = max_duration

    if seq.albums.filter(complete=False).count() != 0 or seq.albums.count() == 0:
        seq.status = Sequence.INCOMPLETE
    else:
ff448d43   Jeremy   Update
56
57
58
59
60
        #TODO CHANGE WHEN WE KNOW HOW TO DECIDE IF THE SEQUENCE IS OBSERVABLE
        if settings.SIMULATOR:
            seq.status = Sequence.TOBEPLANNED
        else:
            seq.status = Sequence.COMPLETE
bbf6e698   haribo   Date: 14/06/2016
61
    seq.save()
eea995c9   haribo   Date: 16/06/2016
62
    return check_request_validity(seq.request)
bbf6e698   haribo   Date: 14/06/2016
63
64
65
66
67

def check_request_validity(req):
    """
        Checks if a request is complete, and save its status in DB
        A request is complete if it has at least 1 sequence, and all the sequences are complete
eea995c9   haribo   Date: 16/06/2016
68
69
   
        :returns : a bool equal to True if the request was submitted but incomplete
bbf6e698   haribo   Date: 14/06/2016
70
71
72
73
74
    """

    if req.sequences.filter(status=Sequence.INCOMPLETE).count() != 0 or req.sequences.count() == 0:
        req.complete = False
        req.save()
eea995c9   haribo   Date: 16/06/2016
75
76
77
        if req.submitted == True:
            return True
        return False
bbf6e698   haribo   Date: 14/06/2016
78
79
    req.complete = True
    req.save()
eea995c9   haribo   Date: 16/06/2016
80
    return False