Ejemplo n.º 1
0
    def add_widget(self, obj, item):
        model = item['model']
        title = item['verbose_name']
        func_name = item['function']
        dashboard = item['dashboard']
        list_display = item.get('list_display')
        link = item['link']

        l = []
        if dashboard:
            if type(dashboard) == dict:
                for position, group_names in list(dashboard.items()):
                    group_names = type(
                        group_names) == tuple and group_names or (
                            group_names, )
                    l.append((position, group_names))
            else:
                l.append((dashboard, item['can_view']))

            for position, group_names in l:
                if permissions.check_group_or_permission(
                        self.request, group_names, ignore_superuser=True):
                    func = getattr(obj, func_name)
                    f_return = execute_and_format(self.request, func)

                    if type(f_return) in (int, Decimal):
                        verbose_name = get_metadata(model,
                                                    'verbose_name_plural')
                        icon = get_metadata(model, 'icon')
                        panel = NumberPanel(self.request, verbose_name,
                                            f_return, title, icon)
                        html = str(panel)

                    elif hasattr(f_return, 'model'):
                        compact = position in ('left', 'right')
                        app_label = get_metadata(model, 'app_label')
                        model_name = model.__name__.lower()
                        verbose_name_plural = get_metadata(
                            model, 'verbose_name_plural')
                        if link:
                            title = '{} {}'.format(verbose_name_plural, title)
                        url = '/list/{}/{}/{}/'.format(app_label, model_name,
                                                       func_name)
                        paginator = Paginator(self.request,
                                              f_return,
                                              title,
                                              readonly=compact,
                                              list_display=list_display,
                                              list_filter=(),
                                              search_fields=(),
                                              list_subsets=[func_name],
                                              url=link and url or None)
                        if compact and not paginator.template:
                            paginator.column_names = paginator.column_names[
                                0:1]
                        html = str(paginator)
                    else:
                        html = str(f_return)
                    self.add(html, position)
Ejemplo n.º 2
0
    def __init__(self, request):

        super(DashboardPanel, self).__init__('dashboard', request)
        self.top = []
        self.center = []
        self.left = []
        self.right = []
        self.bottom = []

        from djangoplus.cache import loader
        for model in loader.subsets:
            icon = get_metadata(model, 'icon', 'fa-bell-o')
            title = get_metadata(model, 'verbose_name_plural')
            app_label = get_metadata(model, 'app_label')
            model_name = model.__name__.lower()
            for item in loader.subsets[model]:
                description = item['title']
                can_view = item['can_view']
                notify = item['notify']
                if notify and permissions.check_group_or_permission(
                        request, can_view):
                    attr_name = item['function'].__func__.__name__
                    qs = model.objects.all(request.user)
                    qs = getattr(qs, attr_name)()
                    count = qs.count()
                    if count:
                        url = '/list/{}/{}/{}/'.format(app_label, model_name,
                                                       attr_name)
                        notification_panel = NotificationPanel(
                            request, title, count, url, description, icon)
                        self.right.append(notification_panel)

        icon_panel = ShortcutPanel(request)
        card_panel = CardPanel(request)

        self.top.append(icon_panel)
        self.center.append(card_panel)

        for item in loader.subset_widgets:

            model = item['model']
            title = item['title']
            function = item['function']
            dashboard = item['dashboard']
            formatter = item['formatter']
            list_display = item.get('list_display')
            link = item['link']

            l = []
            if type(dashboard) == dict:
                for position, group_names in list(dashboard.items()):
                    group_names = type(
                        group_names) == tuple and group_names or (
                            group_names, )
                    l.append((position, group_names))
            else:
                l.append((dashboard, item['can_view']))

            for position, group_names in l:
                if permissions.check_group_or_permission(
                        request, group_names, ignore_superuser=True):
                    qs = model.objects.all(request.user)
                    f_return = getattr(qs, function)()
                    html = ''

                    if type(f_return) in (int, Decimal):
                        verbose_name = get_metadata(model,
                                                    'verbose_name_plural')
                        icon = get_metadata(model, 'icon')
                        panel = NumberPanel(request, verbose_name, f_return,
                                            title, icon)
                        html = str(panel)

                    if type(f_return
                            ).__name__ == 'QueryStatistics' and not formatter:
                        formatter = 'statistics'

                    if formatter:
                        func = loader.formatters[formatter]
                        if len(func.__code__.co_varnames) == 1:
                            html = str(func(f_return))
                        else:
                            html = str(
                                func(f_return,
                                     request=self.request,
                                     verbose_name=title))
                    elif hasattr(f_return, 'model'):
                        compact = position in ('left', 'right')
                        app_label = get_metadata(model, 'app_label')
                        model_name = model.__name__.lower()
                        verbose_name_plural = get_metadata(
                            model, 'verbose_name_plural')
                        if link:
                            title = '{} {}'.format(verbose_name_plural, title)
                        url = '/list/{}/{}/{}/'.format(app_label, model_name,
                                                       function)
                        paginator = Paginator(self.request,
                                              f_return,
                                              title,
                                              readonly=compact,
                                              list_display=list_display,
                                              list_filter=(),
                                              search_fields=(),
                                              list_subsets=[function],
                                              url=link and url or None)
                        if compact:
                            paginator.column_names = paginator.column_names[
                                0:1]
                        html = str(paginator)

                    if position == 'top':
                        self.top.append(html)
                    elif position == 'center':
                        self.center.append(html)
                    elif position == 'left':
                        self.left.append(html)
                    elif position == 'right':
                        self.right.append(html)
                    elif position == 'bottom':
                        self.bottom.append(html)

        for item in loader.widgets:
            if permissions.check_group_or_permission(request,
                                                     item['can_view'],
                                                     ignore_superuser=True):
                function = item['function']
                position = item['position']
                f_return = function(request)
                html = render_to_string(
                    ['{}.html'.format(function.__name__), 'dashboard.html'],
                    f_return, request)
                if position == 'top':
                    self.top.append(html)
                elif position == 'center':
                    self.center.append(html)
                elif position == 'left':
                    self.left.append(html)
                elif position == 'right':
                    self.right.append(html)
                elif position == 'bottom':
                    self.bottom.append(html)