Blame view

pyros_django/routine_manager/validators.py 2.61 KB
cc7b66b3   Etienne Pallier   renamed src/ to p...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from common.models import Sequence
from django.conf import settings

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)

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
   
        :returns : a bool equal to True if the request was submitted but incomplete
    """

    if alb.plans.filter(complete=False).count() != 0 or alb.plans.count() == 0:
        alb.complete = False
    else:
        alb.complete = True
    alb.save()
    return check_sequence_validity(alb.sequence)


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
   
        :returns : a bool equal to True if the request was submitted but incomplete
    """

    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:
        #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
    seq.save()
    return check_request_validity(seq.request)

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
   
        :returns : a bool equal to True if the request was submitted but incomplete
    """

    if req.sequences.filter(status=Sequence.INCOMPLETE).count() != 0 or req.sequences.count() == 0:
        req.complete = False
        req.save()
        if req.submitted == True:
            return True
        return False
    req.complete = True
    req.save()
    return False