Beispiel #1
0
def log(request):
    if not is_login(request):
        return response_redirect_login(request)
    link_return = "/"
    """" Initial for flight log table and fuel table """

    flight_table = []
    log_emp_list = []
    log_loc_list = []
    log_model = None
    log = None
    max_pax = None
    locations = None
    payload_available = None
    alert = None
    log_id = None
    check = None
    is_sub = None
    log_ob = Log.objects.order_by("log_number").all()
    if log_ob:
        log_count = log_ob.count()
        log_n = log_ob[log_count - 1].log_number + 1
    else:
        log_n = 1
    list_emp = Employee.objects.all().order_by("first_name")
    extra_value = 1
    extra_value_pilot = 0
    if "log_id" in request.GET:
        id = request.GET["log_id"]
        a = Log_Location.objects.filter(log_id=id).count() if id else 0
        if a >= 1:
            extra_value = 0
        b = LogEmployee.objects.filter(log=id).count() if id else 0
        if b >= 1:
            extra_value_pilot = 0

    FuelFormSet = modelformset_factory(
        model=Log_Location, form=FuelForm, can_delete=True, max_num=10, extra=extra_value
    )

    PilotFormSet = formset_factory(form=PilotForm, max_num=10, extra=extra_value_pilot)

    if request.method == "POST":
        formset = FuelFormSet(request.POST, request.FILES)
        formset_pilot = PilotFormSet(request.POST, request.FILES, prefix="pilots")

        form = LogForm(request.POST, customer=request.POST.get("customer"), model=request.POST.get("model"))

        log_num = request.POST.get("log_number")

        if form.is_valid() and formset.is_valid() and formset_pilot.is_valid():
            pk_id = request.POST.get("id_log")
            alert = save_data_form_grid(request, pk_id, form, formset, formset_pilot, log_num)
            check = True
            delete_session(request)
            try:
                submit = request.POST[constant.submit]

                if submit:
                    is_sub = True
                    link_return = "/search/"
            except:
                pass
            if not alert:
                try:
                    param = request.get_full_path()
                    next = param[5:]
                    if next == "1":
                        link_return = "/search/"
                    if next == "2":
                        link_return = "/"
                    if next == "3":
                        link_return = "/log/"
                except:
                    pass
                return HttpResponseRedirect(link_return)

    #             return HttpResponseRedirect(link_return)

    else:  # Get data from DB and reload data into sessions
        delete_session(request)
        try:
            """ Initial data for session flight log  """
            log_id = request.GET["log_id"]

            formset = FuelFormSet(queryset=Log_Location.objects.filter(log_id=log_id))
            cur_login_user = LogEmployee.objects.filter(log=log_id).values()
            formset_pilot = PilotFormSet(initial=cur_login_user, prefix="pilots")

            log_emp_list = LogEmployee.objects.filter(log=log_id)
            log_loc_list = Log_Location.objects.filter(log_id=log_id)
            log_object = Log.objects.get(id_log=log_id)
            flight_table = LogSection.objects.filter(log=log_object)
            list_flight_log = []
            for flight_log in flight_table:
                list_flight_log.append(serializers.serialize("json", [flight_log]))
            request.session[constant.list_flight_log] = list_flight_log

            """ Reload data for LogForm Section and AirCraft """
            log_id = request.GET["log_id"]
            log = Log.objects.get(id_log=log_id)
            log_n = log.log_number
            mod = 0

            if log.a1c.id_a1c:
                ac = log.a1c.id_a1c
                if A1C.objects.get(id_a1c=ac):
                    log_model = A1C.objects.get(id_a1c=ac).model
                    mod = log_model.id_model
                    max_pax = log_model.max_passenger
                    payload_available = log_model.max_gross_weight - log.opterational_weight

            form = LogForm(
                reload=True,
                customer=log_object.customer,
                model=log_object.a1c.model,
                instance=log,
                initial={"model": mod},
            )

        except Exception as e:
            form = LogForm(
                customer=request.POST.get("customer"),
                model=request.POST.get("model"),
                initial={"log_number": log_n, "user_id": request.session[constant.usernameParam]},
            )
            formset = FuelFormSet(queryset=Log_Location.objects.none())
            list = []
            sic = pic = night = day = vfr = ifr = nvg = co_nvg = 0
            pilot = LogEmployee()
            pilot.sic = sic
            pilot.day = day
            pilot.night = night
            pilot.nvg = nvg
            pilot.vfr = vfr
            pilot.ifr = ifr
            pilot.employee = Employee.objects.get(employee_number=request.session[constant.usernameParam])
            list.append(todict(pilot))
            formset_pilot = PilotFormSet(initial=list, prefix="pilots")
            pass

        list_flight_log = get_list_session_object(request)
        locations = []
        for flight_log in list_flight_log:
            if flight_log.flight_data_fuel_station:
                location = Location.objects.get(id_location=flight_log.flight_data_fuel_station.id_location)
                if location:
                    if location not in locations:
                        locations.append(location)

        import operator

        locations.sort(key=operator.attrgetter("location_name"))

    c = {
        "flight_table": flight_table,
        "form": form,
        "formset": formset,
        "formset_pilot": formset_pilot,
        "id": log_n,
        "list_emp": list_emp,
        "locations": locations,
        constant.forms: flight_table,
        "log_emp_list": log_emp_list,
        "log_model": log_model,
        "max_pax": max_pax,
        "payload_available": payload_available,
        "emp_number": request.session[constant.usernameParam],
        "log_loc_list": log_loc_list,
        "alert": alert,
        "log_id": log_id,
        "check": check,
        "is_sub": is_sub,
    }
    c.update(csrf(request))

    return render(request, "flights/index.html", c)
Beispiel #2
0
def load_copilot_sub(request, co_pilot, id_log, dajax):
    '''Create formset'''
    PilotFormSet = formset_factory( form = PilotForm, 
                                       max_num=10, extra = 0)
    '''Load data for table'''
    list = []
    '''Get list flight log in session and calculate sum value'''
    lst_flight_log = get_list_session_object(request)
    sic = pic = night = day = vfr = ifr = nvg = co_nvg = 0
    
    for flight in lst_flight_log:
        sic = sic + flight.flight_time
        pic = pic + flight.flight_time
        day = day + flight.day
        night = night + flight.night
        if flight.pilot_nvg:
            nvg = nvg + flight.pilot_nvg
        if flight.co_pilot_nvg:
            co_nvg = co_nvg + flight.co_pilot_nvg
        if flight.partial_nfr != -1 and type(flight.partial_nfr) != unicode:
            vfr = vfr + flight.partial_nfr
            ifr = ifr + flight.flight_time - flight.partial_nfr
        else:
            if flight.all_nfr:
                vfr = vfr + flight.flight_time
                ifr = ifr + 0
            if flight.all_ifr:
                ifr = ifr + flight.flight_time
                vfr = vfr + 0
    
    pilot = LogEmployee()
    pilot.pic = pic
    pilot.day = day
    pilot.night = night
    pilot.nvg = nvg
    pilot.vfr = vfr
    pilot.ifr = ifr
    try:
        log_temp = Log.objects.filter(id_log=id_log)
        if log_temp:
            pilot.log = log_temp[0]
    except:
        pass
    emp_temp = Employee.objects.filter(employee_number = request.session[constant.usernameParam])
    if emp_temp:
        pilot.employee = emp_temp[0]
    list.append(todict(pilot))

    if co_pilot != "Choose One" and request.session[constant.usernameParam] != co_pilot:
        co_pilot_object = LogEmployee()
        co_pilot_object.sic = sic
        co_pilot_object.day = day
        co_pilot_object.night = night
        co_pilot_object.nvg = co_nvg
        co_pilot_object.vfr = vfr
        co_pilot_object.ifr = ifr
        try:
            log_temp = Log.objects.filter(id_log=id_log)
            if log_temp:
                co_pilot_object.log = log_temp[0]
        except:
            pass
        emp_temp = Employee.objects.filter(employee_number = co_pilot)
        if emp_temp:
            co_pilot_object.employee = emp_temp[0]
        list.append(todict(co_pilot_object))
    
    '''Remove older table'''
    dajax.remove("#table_pilot")
    dajax.remove("#pi_content")
    
    '''Init data for formset'''
    formset_pilot = PilotFormSet(initial = list, prefix='pilots')
    
    '''Render form'''
    render = render_to_string(constant.pilot_section ,{"formset_pilot":formset_pilot})
    render1 = render_to_string(constant.pilot_form ,{"formset_pilot":formset_pilot})
    
    '''Append form'''
    dajax.script(constant.append_pilot_section %render.replace('\n', ""))
    dajax.script(constant.append_pilot_form %render1.replace('\n', ""))