Пример #1
0
    def get_object(self, request, object_id, from_field=None):
        obj = super().get_object(request, object_id, from_field)
        if obj and not obj.receipt_fetched:
            obj.get_receipt_url()
        return obj

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in CUSTOM_WIDGETS:
            widgets = dict(subquota_description=SubquotaWidget,
                           receipt_url=ReceiptUrlWidget,
                           suspicions=SuspiciousWidget)
            kwargs['widget'] = widgets.get(db_field.name)
        return super().formfield_for_dbfield(db_field, **kwargs)

    def get_search_results(self, request, queryset, search_term):
        queryset, distinct = super(ReimbursementModelAdmin, self) \
            .get_search_results(request, queryset, None)

        if search_term:
            query = SearchQuery(search_term, config='portuguese')
            rank = SearchRank(F('search_vector'), query)
            queryset = queryset.annotate(rank=rank).filter(search_vector=query)

            if not queryset.was_ordered():
                queryset.order_by('-rank')

        return queryset, distinct


public_admin.register(Reimbursement, ReimbursementModelAdmin)
Пример #2
0
        context = {
            'year': request.GET.get('year'),
            'month': request.GET.get('month'),
            'chart_grouping': chart_grouping,
            'summary': tuple(queryset),
            'summary_total': dict(queryset.aggregate(**metrics)),
            'summary_over_time': tuple(
                self.serialize_summary_over_time(row, **over_time_args)
                for row in summary_over_time
            )
        }

        cache.set(key, context, 60 * 60 * 6)
        return context

    def changelist_view(self, request, extra=None):
        response = super().changelist_view(request, extra_context=extra)

        try:
            queryset = response.context_data['cl'].queryset
        except (AttributeError, KeyError):
            return response

        context = self.get_cached_context(request, queryset)
        response.context_data.update(context)
        return response


public_admin.register(Reimbursement, ReimbursementModelAdmin)
public_admin.register(ReimbursementSummary, ReimbursementSummaryModelAdmin)