コード例 #1
0
def __get_create_kargs(request):

    kargs = {}

    score_result_fields = ScoreResult.get_fields()

    for field_name in score_result_fields:
        if field_name not in request.POST:
            kargs[field_name] = score_result_fields[field_name].default
        else:
            kargs[field_name] = request.POST[field_name]

    return kargs
コード例 #2
0
def search_page(request):

    def __get_annotate_args(score_result_field):
        if score_result_field.metric_type == "Average":
            return score_result_field.display_name, Avg('scoreresult__' + score_result_field.field_name)
        else:
            return score_result_field.display_name, Sum('scoreresult__' + score_result_field.field_name)

    def __get_filter_args(score_result_field, sign, value):

        django_value = ""
        if sign == '>=':
            django_value = "__gte"
        if sign == '<=':
            django_value = "__lte"

        return "%s%s" % (display_name, django_value), value

    context = {}

    # This would imply that they were on the search page, and made a request
    if len(request.GET) != 0:
        context['get'] = request.GET

        annotate_args = {}
        filter_args = {}
        good_fields = []

        valid_fields = []
        for score_result_field in ScoreResult.get_fields().values():
            field_name = score_result_field.field_name
            value_key = field_name + "_value"

            if field_name in request.GET and value_key in request.GET:
                value = request.GET[field_name]
                sign = request.GET[value_key]
                if len(value) != 0 and len(sign) != 0:

                    valid_fields.append(score_result_field)
                    display_name = score_result_field.display_name
                    annotate = __get_annotate_args(score_result_field)
                    filter_arg = __get_filter_args(score_result_field, sign, value)

                    annotate_args[annotate[0]] = annotate[1]
                    filter_args[filter_arg[0]] = filter_arg[1]
                    good_fields.append(score_result_field)
#
        search_results = Team.objects.all().annotate(**annotate_args).filter(**filter_args)
#
        results = []

        for result in search_results:
            team_result = []
            team_result.append(("Team Number", result.teamNumber))

            for field in good_fields:
                value = getattr(result, field.display_name)
                if field.metric_type == "Average":
                    value = "{:10.2f}".format(value)
                team_result.append((field.display_name, value))

            results.append(team_result)

        context['results'] = results

    return render(request, 'Scouting2011/search.html', context)