validators.py 2.61 KB
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