Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
y2kconnect committed Oct 18, 2016
1 parent 3fc8c90 commit 8017ed2
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 36 deletions.
35 changes: 26 additions & 9 deletions xadmin/plugins/bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ def get_context(self, context):

bookmarks = []

current_qs = '&'.join(['%s=%s' % (k, v) for k, v in sorted(
filter(lambda i: bool(i[1] and (i[0] in (COL_LIST_VAR, ORDER_VAR, SEARCH_VAR) or i[0].startswith(FILTER_PREFIX)
or i[0].startswith(RELATE_PREFIX))), self.request.GET.items()))])
current_qs = '&'.join([
'%s=%s' % (k, v)
for k, v in sorted(filter(
lambda i: bool(i[1] and (
i[0] in (COL_LIST_VAR, ORDER_VAR, SEARCH_VAR)
or i[0].startswith(FILTER_PREFIX)
or i[0].startswith(RELATE_PREFIX)
)),
self.request.GET.items()
))
])

model_info = (self.opts.app_label, self.opts.model_name)
has_selected = False
Expand All @@ -55,8 +63,10 @@ def get_context(self, context):
# local bookmarks
for bk in self.list_bookmarks:
title = bk['title']
params = dict(
[(FILTER_PREFIX + k, v) for (k, v) in bk['query'].items()])
params = dict([
(FILTER_PREFIX + k, v)
for (k, v) in bk['query'].items()
])
if 'order' in bk:
params[ORDER_VAR] = '.'.join(bk['order'])
if 'cols' in bk:
Expand All @@ -65,7 +75,10 @@ def get_context(self, context):
params[SEARCH_VAR] = bk['search']
def check_item(i):
return bool(i[1]) or i[1] == False
bk_qs = '&'.join(['%s=%s' % (k, v) for k, v in sorted(filter(check_item, params.items()))])
bk_qs = '&'.join([
'%s=%s' % (k, v)
for k, v in sorted(filter(check_item, params.items()))
])

url = list_base_url + '?' + bk_qs
selected = (current_qs == bk_qs)
Expand Down Expand Up @@ -208,9 +221,13 @@ 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))]
for r in list_view.results()]
context['results'] = [
[o for i, o in 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.bookmark.url

Expand Down
3 changes: 2 additions & 1 deletion xadmin/plugins/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def _format_csv_text(self, t):
if isinstance(t, bool):
return _('Yes') if t else _('No')
t = t.replace('"', '""').replace(',', '\,')
if isinstance(t, basestring):
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(t, cls_str):
t = '"%s"' % t
return t

Expand Down
4 changes: 3 additions & 1 deletion xadmin/plugins/inline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import inspect
import sys
from django import forms
from django.forms.formsets import all_valid, DELETION_FIELD_NAME
from django.forms.models import inlineformset_factory, BaseInlineFormSet, modelform_defines_fields
Expand Down Expand Up @@ -114,10 +115,11 @@ def get_attrs(self):

def replace_field_to_value(layout, av):
if layout:
cls_str = str if 2 < sys.version_info.major else basestring
for i, lo in enumerate(layout.fields):
if isinstance(lo, Field) or issubclass(lo.__class__, Field):
layout.fields[i] = ShowField(av, *lo.fields, **lo.attrs)
elif isinstance(lo, basestring):
elif isinstance(lo, cls_str):
layout.fields[i] = ShowField(av, lo)
elif hasattr(lo, 'get_field_names'):
replace_field_to_value(lo, av)
Expand Down
7 changes: 5 additions & 2 deletions xadmin/plugins/relate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=UTF-8
import sys
from itertools import chain

from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -206,7 +207,8 @@ def get_form_datas(self, datas):
return datas

def post_response(self, response):
if isinstance(response, basestring) and response != self.get_admin_url('index'):
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(response, cls_str) and response != self.get_admin_url('index'):
return self._get_url(response)
return response

Expand All @@ -222,7 +224,8 @@ def block_after_fieldsets(self, context, nodes):
class DeleteRelateDisplayPlugin(BaseRelateDisplayPlugin):

def post_response(self, response):
if isinstance(response, basestring) and response != self.get_admin_url('index'):
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(response, cls_str) and response != self.get_admin_url('index'):
return self._get_url(response)
return response

Expand Down
4 changes: 3 additions & 1 deletion xadmin/plugins/xversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from crispy_forms.utils import TEMPLATE_PACK
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -563,7 +564,8 @@ def total_form_count_hack(count):

if self.request.method == 'GET' and formset.helper and formset.helper.layout:
helper = formset.helper
helper.filter(basestring).wrap(InlineDiffField)
cls_str = str if 2 < sys.version_info.major else basestring
helper.filter(cls_str).wrap(InlineDiffField)
fake_admin_class = type(str('%s%sFakeAdmin' % (self.opts.app_label, self.opts.model_name)), (object, ), {'model': self.model})
for form in formset.forms:
instance = form.instance
Expand Down
6 changes: 4 additions & 2 deletions xadmin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def quote(s):
quoting is slightly different so that it doesn't get automatically
unquoted by the Web browser.
"""
if not isinstance(s, basestring):
cls_str = str if 2 < sys.version_info.major else basestring
if not isinstance(s, cls_str):
return s
res = list(s)
for i in range(len(res)):
Expand All @@ -143,7 +144,8 @@ def unquote(s):
"""
Undo the effects of quote(). Based heavily on urllib.unquote().
"""
if not isinstance(s, basestring):
cls_str = str if 2 < sys.version_info.major else basestring
if not isinstance(s, cls_str):
return s
mychr = chr
myatoi = int
Expand Down
7 changes: 2 additions & 5 deletions xadmin/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,10 @@ def wrap(func):
def method(self, context, nodes, *arg, **kwargs):
_dict = func(self, context, nodes, *arg, **kwargs)
from django.template.loader import get_template, select_template
if 2 < sys.version_info.major:
cls_string = str
else:
cls_string = basestring
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(file_name, Template):
t = file_name
elif not isinstance(file_name, cls_string) and is_iterable(file_name):
elif not isinstance(file_name, cls_str) and is_iterable(file_name):
t = select_template(file_name)
else:
t = get_template(file_name)
Expand Down
9 changes: 5 additions & 4 deletions xadmin/views/delete.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from django.core.exceptions import PermissionDenied
from django.db import transaction, router
from django.http import Http404, HttpResponseRedirect
Expand Down Expand Up @@ -51,10 +52,10 @@ def post(self, request, object_id):
self.delete_model()

response = self.post_response()
if isinstance(response, basestring):
return HttpResponseRedirect(response)
else:
return response
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(response, cls_str):
response = HttpResponseRedirect(response)
return response

@filter_hook
def delete_model(self):
Expand Down
11 changes: 8 additions & 3 deletions xadmin/views/detail.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import copy
import sys

from crispy_forms.utils import TEMPLATE_PACK
from django import forms
Expand Down Expand Up @@ -118,10 +119,14 @@ def val(self):

def replace_field_to_value(layout, cb):
for i, lo in enumerate(layout.fields):
if sys.version_info.major < 3:
cls_str = basestring
else:
cls_str = str
if isinstance(lo, Field) or issubclass(lo.__class__, Field):
layout.fields[i] = ShowField(
cb, *lo.fields, attrs=lo.attrs, wrapper_class=lo.wrapper_class)
elif isinstance(lo, basestring):
elif sys.version_info.major < 3 and isinstance(lo, cls_str):
layout.fields[i] = ShowField(cb, lo)
elif hasattr(lo, 'get_field_names'):
replace_field_to_value(lo, cb)
Expand Down Expand Up @@ -215,8 +220,8 @@ def get_form_helper(self):
layout = self.get_form_layout()
replace_field_to_value(layout, self.get_field_result)
helper.add_layout(layout)
helper.filter(
basestring, max_level=20).wrap(ShowField, admin_view=self)
cls_str = str if 2 < sys.version_info.major else basestring
helper.filter(cls_str, max_level=20).wrap(ShowField, admin_view=self)
return helper

@csrf_protect_m
Expand Down
9 changes: 7 additions & 2 deletions xadmin/views/edit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import copy
import sys

from crispy_forms.utils import TEMPLATE_PACK
from django import forms
Expand Down Expand Up @@ -191,7 +192,10 @@ def get_model_form(self, **kwargs):
@filter_hook
def get_form_layout(self):
layout = copy.deepcopy(self.form_layout)
fields = self.form_obj.fields.keys() + list(self.get_readonly_fields())
arr = self.form_obj.fields.keys()
if 2 < sys.version_info.major:
arr = [k for k in arr]
fields = arr + list(self.get_readonly_fields())

if layout is None:
layout = Layout(Container(Col('full',
Expand Down Expand Up @@ -288,7 +292,8 @@ def post(self, request, *args, **kwargs):
self.save_models()
self.save_related()
response = self.post_response()
if isinstance(response, basestring):
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(response, cls_str):
return HttpResponseRedirect(response)
else:
return response
Expand Down
4 changes: 3 additions & 1 deletion xadmin/views/form.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import copy
import sys

from django import forms
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -111,7 +112,8 @@ def post(self, request, *args, **kwargs):
if self.valid_forms():
self.save_forms()
response = self.post_response()
if isinstance(response, basestring):
cls_str = str if 2 < sys.version_info.major else basestring
if isinstance(response, cls_str):
return HttpResponseRedirect(response)
else:
return response
Expand Down
13 changes: 8 additions & 5 deletions xadmin/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,14 @@ def get_ordering(self):
or self._get_default_ordering())
if ORDER_VAR in self.params and self.params[ORDER_VAR]:
# Clear ordering and used params
ordering = [pfx + self.get_ordering_field(field_name) for n, pfx, field_name in
map(
lambda p: p.rpartition('-'),
self.params[ORDER_VAR].split('.'))
if self.get_ordering_field(field_name)]
ordering = [
pfx + self.get_ordering_field(field_name)
for n, pfx, field_name in map(
lambda p: p.rpartition('-'),
self.params[ORDER_VAR].split('.')
)
if self.get_ordering_field(field_name)
]

# Ensure that the primary key is systematically present in the list of
# ordering fields so we can guarantee a deterministic order across all
Expand Down

0 comments on commit 8017ed2

Please sign in to comment.