darklow / django-suit

Modern theme for Django admin interface
http://djangosuit.com/
Other
2.33k stars 705 forks source link

I a custom views is missed a value "request.current_app", hence not displaying a menu in the left sidebar #522

Open bevokactp opened 8 years ago

bevokactp commented 8 years ago

If this is a bug please specify versions you're using first.

Django version: Django==1.9.5 Django Suit version: django-suit==0.2.18 Python version: Python 3.4.3 (default, Oct 14 2015, 20:28:29)

Issue: I a custom views is missed a value "request.current_app", hence not displaying a menu in the left sidebar

For it, I created next decorator:

def add_current_app_to_request_in_admin_view(view):
    @functools.wraps(view)
    def wrapped_view(modelAdmin, request, **kwargs):

        # only for admin theme Django-Suit
        # it is give feature display menu in left sidebar
        request.current_app = 'admin'

        response = view(modelAdmin, request, **kwargs)
        return response
    return wrapped_view

Usage (from my current project):

class PollAdmin(admin.ModelAdmin):
    '''
    Admin View for Poll
    '''

    def get_urls(self):
        urls = super(PollAdmin, self).get_urls()
        # add urls for custom views

    @add_current_app_to_request_in_admin_view
    def view_statistics(self, request):
        """Admin view for display statistics about polls, choices, votes."""

For me it is working.

Much-esteemed creator django-suit, if you have a free time, please amend your project. Otherwise give me feedback, and I do make necessary changes alone (

but I am not so good know your project

)

darklow commented 8 years ago

Please try latest version. Possibly fixed (or related) in #528

bevokactp commented 8 years ago

I am installed latest version your labour

!pip list | egrep 'suit|Django'
Django (1.9.7)
django-suit (0.2.19)

import sys
sys.version
'3.4.3 (default, Oct 14 2015, 20:28:29) \n[GCC 4.8.4]'

But the problem still has in my project

My custom view without changes:

def view_make_report(self, request):
    """View for ability of creating an Pdf reports in the admin."""

    # if request`s method is GET,
    # then return simple view for customization creating of Pdf report
    if request.method == 'GET':
        context = dict(
            self.admin_site.each_context(request),
            title=_('Make a report about polls'),
            current_app=apps.get_app_config(Poll._meta.app_label),
            django_admin_media=self.media,
        )
        return TemplateResponse(request, "polls/admin/report.html", context)

    # if request`s method is POST,
    # then Pdf report as file
    elif request.method == 'POST':

        # get type output of report: pdf or excel
        output_report = request.POST.get('output_report', None)

        # get subjects of report
        subjects = [
            request.POST.get('polls', None),
            request.POST.get('choices', None),
            request.POST.get('votes', None),
            request.POST.get('voters', None),
            request.POST.get('results', None),
        ]

        subjects = [subject for subject in subjects if subject]
        if not subjects:
            return HttpResponseBadRequest('Not specified any theme for report.')

        # generate pdf-report by subject
        if output_report == 'report_pdf':
            report = PollPDFReport(request, subjects)

        # generate excel-report
        elif output_report == 'report_excel':
            report = ExcelReport(request, subjects)

        else:
            return HttpResponseBadRequest(_('Type of report is not supplied.'))

        response = report.make_report()
        return response

Result:

screenshot from 2016-08-19 22 24 44

def view_make_report(self, request):
        """View for ability of creating an Pdf reports in the admin."""

        # I ADDED THAT AND NOW ALL WORKING
        request.current_app = 'admin'

        # if request`s method is GET,
        # then return simple view for customization creating of Pdf report
        if request.method == 'GET':
            context = dict(
                self.admin_site.each_context(request),
                title=_('Make a report about polls'),
                current_app=apps.get_app_config(Poll._meta.app_label),
                django_admin_media=self.media,
            )
            return TemplateResponse(request, "polls/admin/report.html", context)

        # if request`s method is POST,
        # then Pdf report as file
        elif request.method == 'POST':

            # get type output of report: pdf or excel
            output_report = request.POST.get('output_report', None)

            # get subjects of report
            subjects = [
                request.POST.get('polls', None),
                request.POST.get('choices', None),
                request.POST.get('votes', None),
                request.POST.get('voters', None),
                request.POST.get('results', None),
            ]

            subjects = [subject for subject in subjects if subject]
            if not subjects:
                return HttpResponseBadRequest('Not specified any theme for report.')

            # generate pdf-report by subject
            if output_report == 'report_pdf':
                report = PollPDFReport(request, subjects)

            # generate excel-report
            elif output_report == 'report_excel':
                report = ExcelReport(request, subjects)

            else:
                return HttpResponseBadRequest(_('Type of report is not supplied.'))

            response = report.make_report()
            return response

Result: screenshot from 2016-08-19 22 26 25