def get(self, request, uuid, *args, **kwargs): server_run = self.get_object() if not server_run.is_stopped(): res = { 'Information': 'The tests against the provider-run is undergoing.' } response = HttpResponse(json.dumps(res)) response['Content-Type'] = 'application/json' return response postman_res = PostmanTestResult.objects.filter(server_run=server_run) response = [] for postman in postman_res: postman.json = postman.get_json_obj() postman_res_output = { 'time': postman.get_json_obj_info()['run']['timings']['started'], 'calls': [] } for call in postman.json: _call = { 'name': call['item']['name'], 'request': call['request']['method'], 'response': call.get('response', {}).get('code'), } if 'assertions' in call: for _assertion in call['assertions']: _assertion[ 'result'] = 'failed' if 'error' in _assertion else 'success' _call['assertions'] = call['assertions'] else: _call['assertions'] = [] if _call['response'] in ptm.get_error_codes( ) or not _call['response']: _call['status'] = 'Error' else: _call['status'] = 'Success' postman_res_output['calls'].append(_call) postman_res_output[ 'status'] = ResultChoices.success if postman.is_success( ) == 1 else ResultChoices.failed response.append(postman_res_output) return JsonResponse(response, safe=False)
def get(self, request, pk, *args, **kwargs): server_run = self.get_object() if not server_run.is_stopped(): res = { 'Information': 'The tests against the provider-run is undergoing.' } response = HttpResponse(json.dumps(res)) response['Content-Type'] = 'application/json' return response epr = ExpectedPostmanResult.objects.filter( postman_test__test_scenario=server_run.test_scenario) postman_res = PostmanTestResult.objects.filter(server_run=server_run) response = [] for postman in postman_res: epr = ExpectedPostmanResult.objects.filter( postman_test=postman.postman_test).order_by('order') postman.json = postman.get_json_obj() postman_res_output = { 'time': postman.get_json_obj_info()['run']['timings']['started'], 'calls': [] } for call, ep in zip_longest(postman.json, epr): _call = { 'name': call['item']['name'], 'request': call['request']['method'], 'response': call['response']['code'], } for _assertion in call['assertions']: _assertion[ 'result'] = 'failed' if 'error' in _assertion else 'success' _call['assertions'] = call['assertions'] if ep is None: _call['status'] = 'Expected response not specified' elif str(call['response']['code']) in ep.expected_response: _call['status'] = 'As expected' postman_res_output['calls'].append(_call) postman_res_output['status'] = postman.status response.append(postman_res_output) response = HttpResponse(json.dumps(response)) response['Content-Type'] = 'application/json' return response
def GET(self, request): form = SpotSearchForm(request.GET) has_valid_search_param = False if not form.is_valid(): return HttpResponse('[]') if len(request.GET) == 0: return HttpResponse('[]') query = Spot.objects.all() day_dict = {"Sunday": "su", "Monday": "m", "Tuesday": "t", "Wednesday": "w", "Thursday": "th", "Friday": "f", "Saturday": "sa", } # Exclude things that get special consideration here, otherwise add a filter for the keys for key in request.GET: if re.search('^oauth_', key): pass elif key == "expand_radius": pass elif key == "distance": pass elif key == "center_latitude": pass elif key == "center_longitude": pass elif key == "limit": pass elif key == "open_now": if request.GET["open_now"]: day_lookup = ["su", "m", "t", "w", "th", "f", "sa"] day_num = int(strftime("%w", localtime())) today = day_lookup[day_num] now = datetime.time(datetime.now()) query = query.filter(spotavailablehours__day__iexact=today, spotavailablehours__start_time__lt=now, spotavailablehours__end_time__gt=now) has_valid_search_param = True elif key == "open_until": if request.GET["open_until"] and request.GET["open_at"]: until_day, until_time = request.GET["open_until"].split(',') at_day, at_time = request.GET["open_at"].split(',') until_day = day_dict[until_day] at_day = day_dict[at_day] if until_day == at_day: query = query.filter(spotavailablehours__day__iexact=until_day, spotavailablehours__start_time__lte=at_time, spotavailablehours__end_time__gte=until_time) else: days_to_test = self.get_days_in_range(at_day, until_day) last_day = days_to_test.pop() days_to_test.reverse() first_day = days_to_test.pop() query = query.filter(spotavailablehours__day__iexact=first_day, spotavailablehours__start_time__lte=at_time, spotavailablehours__end_time__gte="23:59") query = query.filter(spotavailablehours__day__iexact=last_day, spotavailablehours__start_time__lte="00:00", spotavailablehours__end_time__gte=until_time) for day in days_to_test: query = query.filter(spotavailablehours__day__iexact=day, spotavailablehours__start_time__lte="00:00", spotavailablehours__end_time__gte="23:59") has_valid_search_param = True elif key == "open_at": if request.GET["open_at"]: try: request.GET["open_until"] except: day, time = request.GET['open_at'].split(',') day = day_dict[day] query = query.filter(spotavailablehours__day__iexact=day, spotavailablehours__start_time__lte=time, spotavailablehours__end_time__gt=time) has_valid_search_param = True elif key == "extended_info:reservable": query = query.filter(spotextendedinfo__key="reservable", spotextendedinfo__value__in=['true', 'reservations']) elif key == "extended_info:noise_level": noise_levels = request.GET.getlist("extended_info:noise_level") exclude_silent = True exclude_quiet = True exclude_moderate = True exclude_loud = True exclude_variable = True for level in noise_levels: if "silent" == level: exclude_silent = False if "quiet" == level: exclude_quiet = False exclude_variable = False if "moderate" == level: exclude_moderate = False exclude_variable = False if exclude_silent: query = query.exclude(spotextendedinfo__key="noise_level", spotextendedinfo__value__iexact="silent") if exclude_quiet: query = query.exclude(spotextendedinfo__key="noise_level", spotextendedinfo__value__iexact="quiet") if exclude_moderate: query = query.exclude(spotextendedinfo__key="noise_level", spotextendedinfo__value__iexact="moderate") if exclude_loud: query = query.exclude(spotextendedinfo__key="noise_level", spotextendedinfo__value__iexact="loud") if exclude_variable: query = query.exclude(spotextendedinfo__key="noise_level", spotextendedinfo__value__iexact="variable") elif key == "capacity": try: limit = int(request.GET["capacity"]) with_limit = Q(capacity__gte=limit) with_limit |= Q(capacity__isnull=True) query = query.filter(with_limit) has_valid_search_param = True except ValueError: # This we don't care about - if someone passes "", or "twenty", just ignore it pass except Exception as e: # Do something to complain?? pass elif key == "type": type_values = request.GET.getlist(key) q_obj = Q() type_qs = [Q(spottypes__name__exact=v) for v in type_values] for type_q in type_qs: q_obj |= type_q query = query.filter(q_obj).distinct() has_valid_search_param = True elif key == "building_name": building_names = request.GET.getlist(key) q_obj = Q() type_qs = [Q(building_name__exact=v) for v in building_names] for type_q in type_qs: q_obj |= type_q query = query.filter(q_obj).distinct() has_valid_search_param = True elif re.search('^extended_info:', key): kwargs = { 'spotextendedinfo__key': key[14:], 'spotextendedinfo__value__in': request.GET.getlist(key) } query = query.filter(**kwargs) has_valid_search_param = True elif key == "id": query = query.filter(id__in=request.GET.getlist(key)) has_valid_search_param = True else: try: kwargs = { '%s__icontains' % key: request.GET[key] } query = query.filter(**kwargs) has_valid_search_param = True except Exception as e: if not request.META['SERVER_NAME'] == 'testserver': print >> sys.stderr, "E: ", e limit = 20 if 'limit' in request.GET: if request.GET['limit'] == '0': limit = 0 else: limit = int(request.GET['limit']) if 'distance' in request.GET and 'center_longitude' in request.GET and 'center_latitude' in request.GET: try: g = Geod(ellps='clrk66') top = g.fwd(request.GET['center_longitude'], request.GET['center_latitude'], 0, request.GET['distance']) right = g.fwd(request.GET['center_longitude'], request.GET['center_latitude'], 90, request.GET['distance']) bottom = g.fwd(request.GET['center_longitude'], request.GET['center_latitude'], 180, request.GET['distance']) left = g.fwd(request.GET['center_longitude'], request.GET['center_latitude'], 270, request.GET['distance']) top_limit = "%.8f" % top[1] bottom_limit = "%.8f" % bottom[1] left_limit = "%.8f" % left[0] right_limit = "%.8f" % right[0] distance_query = query.filter(longitude__gte=left_limit) distance_query = distance_query.filter(longitude__lte=right_limit) distance_query = distance_query.filter(latitude__gte=bottom_limit) distance_query = distance_query.filter(latitude__lte=top_limit) has_valid_search_param = True if len(distance_query) > 0 or 'expand_radius' not in request.GET: query = distance_query else: # If we're querying everything, let's make sure we only return a limited number of spaces... limit = 10 except Exception as e: if not request.META['SERVER_NAME'] == 'testserver': print >> sys.stderr, "E: ", e #query = Spot.objects.all() elif 'distance' in request.GET or 'center_longitude' in request.GET or 'center_latitude' in request.GET: if 'distance' not in request.GET or 'center_longitude' not in request.GET or 'center_latitude' not in request.GET: # If distance, lat, or long are specified in the server request; all 3 must be present. return HttpResponseBadRequest("Bad Request") if not has_valid_search_param: return HttpResponse('[]') if limit > 0 and limit < len(query): sorted_list = list(query) try: sorted_list.sort(lambda x, y: cmp(self.distance(x, request.GET['center_longitude'], request.GET['center_latitude']), self.distance(y, request.GET['center_longitude'], request.GET['center_latitude']))) query = sorted_list[:limit] except KeyError: response = HttpResponse('{"error":"missing required parameters for this type of search"}') response.status_code = 400 return response response = [] for spot in set(query): response.append(spot.json_data_structure()) return HttpResponse(json.dumps(response))