xadmin icon indicating copy to clipboard operation
xadmin copied to clipboard

Creating a custom menu from a custom view

Open ni8mr opened this issue 9 years ago • 0 comments

I have defined a custom view like this -

`class AttendanceReportAdminView(CommAdminView): form_class = AttendanceReportForm app_config = MainAppConfig

def get_context(self, *args, **kwargs):
    ctx = super(AttendanceReportAdminView, self).get_context(*args, **kwargs)
    form = AttendanceReportForm()
    ctx["form"] = form

    return ctx

def get(self, request, *args, **kwargs):
    return render(request, "attendance/attendance_form.html", self.get_context())

def post(self, request, *args, **kwargs):
    date_from = datetime.datetime.strptime(request.POST.get("date_from"), "%m/%d/%Y")
    date_to = datetime.datetime.strptime(request.POST.get("date_to"), "%m/%d/%Y")
    report = request.POST.get("report")
    class_name = None
    student = None
    try:
        class_name = ClassInfo.objects.get(id=request.POST.get("class_name"))
    except: pass
    try:
        student = Student.objects.get(id=request.POST.get("student"))
    except: pass

    if report == "SR":
        output = student_report(date_from, date_to, student)
        tpl = "student"
    elif report == "CR":
        output = class_report(date_from, date_to, class_name)
        tpl = "class"

    return render(request,
                  "attendance/{}_report.html".format(tpl),
                  output)

xadmin.site.register_view(r"^attendance-report/$", AttendanceReportAdminView, name="attendance-report-view")`

apps.py-

`from xadmin_extras.apps import AdminAppMixin

class MainAppConfig(AdminAppMixin): """name and verbose_name are going to be used for django AppConfig too

"""
name = 'reports'
verbose_name = 'Reports'
icon = 'fa fa-chart'

def init_menu(self):
    """Add custom menu entries to the menu displayed for this app

    Return a list of dicts, each dict will be a entry for the submenu of
    the app:
        {'url': '/admin/.../', 'icon': 'bolt', 'title': 'Custom'}
    also 'perm' and 'order' keys can be added.

    """
    return [{
        'url': r"^attendance-report/$", 'icon': 'bolt',
        'title': 'Send notifications', 'order': '', 'perm': None}]

APP_CONFIG = MainAppConfig()`

I have used django-xadmin-extras plugin.

ni8mr avatar Oct 15 '17 21:10 ni8mr