使用iframe时,不会为Django中的新请求维护会话

时间:2018-12-04 09:25:27

标签: django python-3.x session chatbot

我正在创建一个聊天机器人。为此,我正在维护会话以创建聊天流程。并在运行服务器时正常工作。但是,当我使用iframe来提供聊天窗口文件的链接时,会话将无法维护。对于每个新请求,较旧的会话均被销毁。

这是我的聊天流程代码

def extract_data(request, input_data):
        from chat import validations
        if 'city' not in request.session:
            response_data = {'extra': {}, 'data': {}}
            response_data['extra']['statement'] = 'Select Car Model which you like to rent?'
            response_data['extra']['type'] = 'carmodel'
            response_data['data'] = Cars.city_check(request,response_data, input_data)

        elif 'veh_name' not in request.session:
            response_data = Cars.veh_name_check(request, input_data)

        elif 'days' not in request.session:
            response_data = validations.days_check(request, input_data)

        elif 'phone' not in request.session:
            response_data = validations.phone_check(request, input_data)

        elif 'email' not in request.session:
            response_data = validations.mail_check(request, input_data)

        elif 'name' not in request.session:
            response_data = validations.name_check(request, input_data, 'Car')

        return response_data




def city_check(request, response_data, input_data):
        import pdb;pdb.set_trace()
        if len(input_data.split()) == 6:
            request.session['city'] = input_data.split()[4]+' '+input_data.split()[5]
        else:request.session['city'] = input_data.split()[4]
        request.session['flag'] = 0
        city_list = dict(city=list(models.CityForCars.objects.filter(CityForCars=request.session['city']).values_list().values().order_by('id')))

        city = city_list['city'][0]['id']
        car_model_data = dict(car_model=list(models.CarModel.objects.filter(City_id=city).values_list().values().order_by('id')))

        response_data = car_model_data    
        return response_data

    @staticmethod
    def veh_name_check(request, input_data):
        response_data = {'extra': {}, 'data': {}}
        response_data['extra']['statement'] = 'For how many days are you looking to rent a vehicle? (Type or tap below)'
        response_data['data'] = ['1 Day', '2 Day', '3 Day', '4 Day', '5+ Days']
        response_data['extra']['type'] = 'duration'
        diff=' '
        request.session['veh_name'] = diff.join(input_data.split()[5:])
        return response_data

    @staticmethod
    def customer_details(request, data, client_ip):
        serializer = CustomerDetailPage(data=data)
        if serializer.is_valid():
            profile = serializer.save()
            print('data completely saved')
            print('client ipppppppppppppppppppppppppp')
            print(client_ip)
            common.send_mail(profile, client_ip)
            return JsonResponse(serializer.data, status=200)
        return HttpResponse(serializer.errors, status=400)

    @staticmethod
    def car_selected(request, input_text):
        request.session['car_selected'] = 1
        request.session['car_flag'] = 1
        print(request.session)

    @staticmethod
    def check_car_city(request, input_text):
        for input in input_text.split():
            if models.CityForCars.objects.filter(CityForCars=input.title()).values().exists():
                print(input)
                return input
            else:
                return None

    @staticmethod
    def vehicle_city(request, vehicle):
        if vehicle == 'cars':
            if request.session['count_flag'] == 1:
                request.session['end_count'] = 5
                request.session['start_count'] = 0
                request.session['count_flag'] = 0

            car_city = dict(city=list(models.CityForCars.objects.values_list().values().order_by('id')[
                                      request.session['start_count']:request.session['end_count']]))
            request.session['start_count'] = 5 + request.session['start_count']
            request.session['end_count'] = 5 + request.session['end_count']
            if len(car_city['city']) == 5:
                car_city['city'].append({'id': 10000, 'CityForCars': 'more...'})
            return car_city

    @staticmethod
    def car_models(request, city):
        car_model_data = dict(
            city=list(models.CarModel.objects.filter(City_id=city).values_list().values().order_by('id')))


def book_vehicle(request, input_data):
    serializer = BookVehicleSerializer(data=input_data)
    if serializer.is_valid():
        serializer.save()
        print('data completely saved')
        return JsonResponse(serializer.data, status=200)
    return HttpResponse(serializer.errors, status=400)


def fetch_cars_models(request):

    if request.method == 'POST':
        input_data = json.loads(request.read().decode('utf-8'))
        if models.CarModel.objects.filter(City_id=input_data['city_id']):
            car_city = dict(cars=list(models.CarModel.objects.filter(City_id=input_data['city_id']).values_list().values().order_by('id')))
            return JsonResponse(car_city, status=200)
        return HttpResponse('error', status=400)


def fetch_bikes_models(request):

    if request.method == 'POST':
        input_data = json.loads(request.read().decode('utf-8'))
        if models.BikeModel.objects.filter(City_id=input_data['city_id']):
            car_city = dict(bikes=list(models.BikeModel.objects.filter(City_id=input_data['city_id']).values_list().values().order_by('id')))
            return JsonResponse(car_city, status=200)
        return HttpResponse('error', status=400)

我创建了一个新文件夹,其中提供了项目的前端。并提供基本网址。但是当我使用iframe并提供该文件的链接时。它不维护会话。请帮助我解决此问题。

0 个答案:

没有答案