Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #683 #684

Open
wants to merge 1 commit into
base: django2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions xadmin/views/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, widgets, attrs=None):
super(WidgetTypeSelect, self).__init__(attrs)
self._widgets = widgets

def render(self, name, value, attrs=None):
def render(self, name, value, attrs=None, **kwargs):
if value is None:
value = ''
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
Expand All @@ -56,10 +56,10 @@ def render_option(self, selected_choice, widget, id):
'javascript:$(this).parent().parent().find(\'>li\').removeClass(\'active\');$(this).parent().addClass(\'active\');' +
'$(\'#%s_input\').attr(\'value\', \'%s\')' % (id, widget.widget_type) +
'"><h4><i class="%s"></i> %s</h4><p>%s</p></a></li>') % (
selected_html,
widget.widget_icon,
widget.widget_title or widget.widget_type,
widget.description)
selected_html,
widget.widget_icon,
widget.widget_title or widget.widget_type,
widget.description)

def render_options(self, selected_choice, id):
# Normalize to strings.
Expand All @@ -70,7 +70,6 @@ def render_options(self, selected_choice, id):


class UserWidgetAdmin(object):

model_icon = 'fa fa-dashboard'
list_display = ('widget_type', 'page_id', 'user')
list_filter = ['user', 'widget_type', 'page_id']
Expand All @@ -81,7 +80,7 @@ class UserWidgetAdmin(object):
wizard_form_list = (
(_(u"Widget Type"), ('page_id', 'widget_type')),
(_(u"Widget Params"), {'callback':
"get_widget_params_form", 'convert': "convert_widget_params"})
"get_widget_params_form", 'convert': "convert_widget_params"})
)

def formfield_for_dbfield(self, db_field, **kwargs):
Expand Down Expand Up @@ -163,6 +162,7 @@ def get(self, name):
def get_widgets(self, page_id):
return self._widgets.values()


widget_manager = WidgetManager()


Expand All @@ -175,7 +175,6 @@ def __init__(self, widget, errors):


class BaseWidget(forms.Form):

template = 'xadmin/widgets/base.html'
description = 'Base Widget, don\'t use it.'
widget_title = None
Expand Down Expand Up @@ -311,7 +310,6 @@ def valid_value(self, value):


class ModelBaseWidget(BaseWidget):

app_label = None
model_name = None
model_perm = 'change'
Expand Down Expand Up @@ -444,7 +442,7 @@ def context(self, context):
context['result_headers'] = [c for c in list_view.result_headers(
).cells if c.field_name in base_fields]
context['results'] = [[o for i, o in
enumerate(filter(lambda c:c.field_name in base_fields, r.cells))]
enumerate(filter(lambda c: c.field_name in base_fields, r.cells))]
for r in list_view.results()]
context['result_count'] = list_view.result_count
context['page_url'] = self.model_admin_url('changelist') + "?" + urlencode(self.list_params)
Expand Down Expand Up @@ -486,7 +484,6 @@ def media(self):


class Dashboard(CommAdminView):

widget_customiz = True
widgets = []
title = _(u"Dashboard")
Expand All @@ -512,6 +509,7 @@ class widget_with_perm(wid):
def context(self, context):
super(widget_with_perm, self).context(context)
context.update({'has_change_permission': self.request.user.has_perm('xadmin.change_userwidget')})

wid_instance = widget_with_perm(self, data or widget.get_value())
return wid_instance
except UserWidget.DoesNotExist:
Expand Down Expand Up @@ -551,7 +549,8 @@ def get_widgets(self):
widgets = []

if portal_pos:
user_widgets = dict([(uw.id, uw) for uw in UserWidget.objects.filter(user=self.user, page_id=self.get_page_id())])
user_widgets = dict(
[(uw.id, uw) for uw in UserWidget.objects.filter(user=self.user, page_id=self.get_page_id())])
for col in portal_pos.split('|'):
ws = []
for wid in col.split(','):
Expand Down Expand Up @@ -580,8 +579,10 @@ def get_context(self):
'portal_key': self.get_portal_key(),
'columns': [('col-sm-%d' % int(12 / len(self.widgets)), ws) for ws in self.widgets],
'has_add_widget_permission': self.has_model_perm(UserWidget, 'add') and self.widget_customiz,
'add_widget_url': self.get_admin_url('%s_%s_add' % (UserWidget._meta.app_label, UserWidget._meta.model_name)) +
"?user=%s&page_id=%s&_redirect=%s" % (self.user.id, self.get_page_id(), urlquote(self.request.get_full_path()))
'add_widget_url': self.get_admin_url(
'%s_%s_add' % (UserWidget._meta.app_label, UserWidget._meta.model_name)) +
"?user=%s&page_id=%s&_redirect=%s" % (
self.user.id, self.get_page_id(), urlquote(self.request.get_full_path()))
}
context = super(Dashboard, self).get_context()
context.update(new_context)
Expand All @@ -605,7 +606,8 @@ def post(self, request, *args, **kwargs):
user=self.user, page_id=self.get_page_id(), id=widget_id)
widget.delete()
try:
portal_pos = UserSettings.objects.get(user=self.user, key="dashboard:%s:pos" % self.get_page_id())
portal_pos = UserSettings.objects.get(user=self.user,
key="dashboard:%s:pos" % self.get_page_id())
pos = [[w for w in col.split(',') if w != str(
widget_id)] for col in portal_pos.value.split('|')]
portal_pos.value = '|'.join([','.join(col) for col in pos])
Expand All @@ -620,7 +622,7 @@ def post(self, request, *args, **kwargs):
@filter_hook
def get_media(self):
media = super(Dashboard, self).get_media() + \
self.vendor('xadmin.page.dashboard.js', 'xadmin.page.dashboard.css')
self.vendor('xadmin.page.dashboard.js', 'xadmin.page.dashboard.css')
if self.widget_customiz:
media = media + self.vendor('xadmin.plugin.portal.js')
for ws in self.widgets:
Expand All @@ -630,7 +632,6 @@ def get_media(self):


class ModelDashboard(Dashboard, ModelAdminView):

title = _(u"%s Dashboard")

def get_page_id(self):
Expand Down