示例#1
0
def save_flight_log(request, forms, max_pax, partial_range, 
                    before_fuel_location, list_fuel_location,
                    is_edit):
    dajax = Dajax()
    
    ''' Remove all errors message in popup '''
    dajax.remove(constant.class_error_mess)
    ''' Generate new form '''
    form = FlightLogDetail(deserialize_form(forms), partial_range = partial_range, max_pax = max_pax)
    if form.is_valid():
        ''' Get object form data '''
        result = form.save(commit = False)
        
        ''' If add new flight legs '''
        if is_edit == "false":
            dajax.script(constant.append_flight_log_form 
                         %save_add_case(request, result))
            """
                Announce after saved
            """
            dajax.script(constant.append_save_succ_mess %constant.add_succ_mess)
        
        ''' Edit flight legs '''
        if is_edit == "true":
            current_location = None
            location_name = None
            try:
                location = Location.objects.get(id_location = before_fuel_location)
                if location:
                    before_fuel_location = location.location_name
            except:
                pass
        

            if result.flight_data_fuel_station:
                current_location = result.flight_data_fuel_station.location_name
                
            '''
                Check location in fuel expenses is conflict with flight log session or not
                - If not when change
                - If has conflict so announce and return
            '''
            remove_older_location = False
            list_location_temp = []
            list_object = get_list_session_object(request)
            
            ''' Get older station location '''
            for obj in list_object:
                if obj.flight_data_fuel_station:
                    location_name = obj.flight_data_fuel_station.location_name
                    if location_name:
                        list_location_temp.append(str(location_name))
                if before_fuel_location == str(location_name) != "None" and not remove_older_location:
                    list_location_temp.remove(before_fuel_location)
                    remove_older_location = True
            ''' Input new station into list '''
            if current_location:   
                list_location_temp.append(current_location)
            ''' Verify whether conflict '''    
            for val in list_fuel_location:
                if str(val) != "None" and str(val) != "" and str(val) not in list_location_temp:
                    dajax.alert(constant.announce_mess_fuel_location_change)
                    pour_fuel_station_popup(dajax)
                    dajax.script(constant.prevent_default)
                    return dajax.json()
                
            ''' Change data of flight log in sessio '''    
            lst = request.session[constant.list_flight_log]
            lst[request.session["current_index"]] = serializers.serialize(constant.json, [result, ])
            request.session[constant.list_flight_log] = lst
            
            """
                Announce after edited
            """
            dajax.script(constant.append_save_succ_mess %constant.edited_succ_mess)
            
        ''' Modify data for hidden combobox and used for fuel expenses sessio '''
        dajax.remove("#hiddenff option")
        import operator
        sorted_list_fuel_station = sorted(fuel_station_list(request).items(), key=operator.itemgetter(1))
        for local_id, name in sorted_list_fuel_station:
            dajax.append('#hiddenff', 'innerHTML', "<option value="+str(local_id)+">"+str(name)+"</option>")
            
        '''
            Modify flight legs table
        '''
        dajax.remove(constant.id_rs_flight_log_form)
        render = render_to_string(constant.flight_log_table_page,
                                  {constant.forms:get_list_session_object(request)})
        dajax.script(constant.prepend_flight_log_table 
                     %render.replace('\n', ""))
        
    else:#Form not valid
        dajax.remove(constant.class_pop_main)
        render = render_to_string(constant.popup_page ,{constant.single_form:form})
        dajax.script(constant.prepend_table_popup %render.replace('\n', ""))
        dajax.script("if(!$('#id_load_schedule').is(':checked') & \
                ($('#id_flight_data_range_from').val() == '' || \
                $('#id_flight_data_range_to').val() == '')){\
                $('#range_to_error_id').css('display','block');}")
        
        ''' Pour data for fuel station in popup '''
        pour_fuel_station_popup(dajax)
        
        dajax.script(constant.prevent_default)
    
    ''' Announce message when edit flight legs '''
    list_log_section = []
    try:
        list_log_section = get_list_session_object(request)
    except:
        pass
    msg = pax_error(max_pax, list_log_section)
    
    dajax.script(constant.append_error_mess % msg)
    if msg:
        dajax.script(constant.remove_save_succ_mess)
        
    pour_fuel_station_popup(dajax)
    return dajax.json()
示例#2
0
def delete_flight_log(request, index, max_pax, list_fuel_location):
    dajax = Dajax()
    
    list_object = get_list_session_object(request)
    
    count = 0
    current_object = None
    for obj in list_object:
        if obj.order == int(index):
            current_object = obj
            
    for obj in list_object:
        if obj.flight_data_fuel_station and current_object.flight_data_fuel_station:
            if obj.flight_data_fuel_station == current_object.flight_data_fuel_station:
                if str(obj.flight_data_fuel_station.location_name) in list_fuel_location:
                    count += 1
    if count == 1:
        dajax.alert(constant.announce_mess_fuel_location_change);
        return dajax.json()
    
    '''
        Change all session here
    '''
    temp_list = []
    i = 1
    for obj in list_object:
        if obj.order != int(index):
            obj.order = i
            temp_list.append(serializers.serialize(constant.json, [obj, ]))
            i += 1 
    
    '''
        Add all flight leg into session
    '''
    request.session[constant.list_flight_log] = temp_list
    ''' Remove old flight legs table at home page '''
    dajax.remove(constant.id_rs_flight_log_form)
    ''' Render data into new flight legs table '''
    render = render_to_string(constant.flight_log_table_page,
                              {constant.forms:get_list_session_object(request)})
    ''' Insert new flight legs table into home  page '''
    dajax.script(constant.prepend_flight_log_table 
                 %render.replace('\n', ""))
    
    '''
        Announce message in home page if passenger of model is out of range
    '''
    list_log_section = []
    try:
        list_log_section = get_list_session_object(request)
    except:
        pass
    msg = pax_error(max_pax, list_log_section)
    dajax.script(constant.append_error_mess % msg)
    if msg:
        dajax.script(constant.remove_save_succ_mess)
    
    ''' Modify data for hidden combobox and used for fuel expenses sessio '''
    dajax.remove("#hiddenff option")
    import operator
    sorted_list_fuel_station = sorted(fuel_station_list(request).items(), key=operator.itemgetter(1))
    for local_id, name in sorted_list_fuel_station:
        dajax.append('#hiddenff', 'innerHTML', "<option value="+str(local_id)+">"+str(name)+"</option>")

     
    return dajax.json()