Пример #1
0
def ptBaseHtml(request, project_id, template, params=None, obj=None):
    if request.method == 'POST':
        raise Http404

    params = params if params else {}

    dentries = request.path.split("/")
    verb = dentries[2] if len(dentries) > 3 else "comparison"

    default_params = {
        'projects':
        ProjectModel().ptGetAll(),
        'request':
        request,
        'verb':
        verb,
        'obj':
        obj,
        'project':
        ProjectModel.ptGetById(project_id),
        'api_ver':
        API_VER,
        'screens': [('Home', '/%d/home/' % project_id),
                    ('Regressions', '/%d/regression/' % project_id),
                    ('Comparisons', '/%d/comparison/' % project_id),
                    ('Jobs', '/%d/job/' % project_id),
                    ('Hosts', '/%d/hw_farm/' % project_id)]
    }
    params.update(default_params)
    return TemplateResponse(request, template, params)
Пример #2
0
def ptComparisonAllJson(request, api_ver, project_id):
    class ComparisonJson(BaseDatatableView):
        # The model we're going to show
        model = ComparisonModel

        # define the columns that will be returned
        columns = [
            '', 'id', 'updated', 'env_node', 'suite_ver', 'title', 'jobs',
            'tests_total', 'tests_completed', 'tests_failed'
        ]

        # define column names that will be used in sorting
        # order is important and should be same as order of columns
        # displayed by datatables. For non sortable columns use empty
        # value like ''
        order_columns = [
            '', 'id', 'updated', 'env_node', 'suite_ver', 'title', 'jobs',
            'tests_total', 'tests_completed', 'tests_failed'
        ]

        # set max limit of records returned, this is used to protect our site if someone tries to attack our site
        # and make it return huge amount of data
        max_display_length = 5000

        def render_column(self, row, column):
            # We want to render user as a custom column
            if column == 'tests_total':
                return '{0} {1}'.format(row.tests_total, row.tests_completed)
            else:
                return super(JobJson, self).render_column(row, column)

        def filter_queryset(self, qs):
            # use parameters passed in GET request to filter queryset

            # simple example:
            search = self.request.GET.get(u'search[value]', None)
            if search:
                qs = qs.filter(
                    Q(title__icontains=search)
                    | Q(suite_ver__icontains=search))

            if project_id != 0:
                qs = qs.filter(Q(project_id=project_id))

            qs = qs.filter(Q(deleted=False))

            return qs

        def prepare_results(self, qs):
            return ComparisonSimpleSerializer(qs, many=True).data

    if request.method == "POST":
        body_unicode = request.body.decode('utf-8')
        body = json.loads(body_unicode)

        try:
            ComparisonModel.ptValidateJson(body)
        except SuspiciousOperation as e:
            return HttpResponseBadRequest(e)

        cmp = ComparisonModel(project=ProjectModel.ptGetById(project_id))
        try:
            cmp.ptUpdate(body)
        except SuspiciousOperation as e:
            return HttpResponseBadRequest(e)

        return JsonResponse({'id': cmp.id}, safe=False)

    return ComparisonJson.as_view()(request)