Skip to content

Commit 7362ba3

Browse files
authored
Merge pull request #4 from andersinno/fix-bugs
Fix bugs
2 parents 04aa9af + 00a920f commit 7362ba3

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ pip-log.txt
3434
sdist/
3535
target/
3636
var/
37+
venv/

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sudo: false
2+
language: python
3+
cache: pip
4+
python:
5+
- "3.4"
6+
- "2.7"
7+
install:
8+
- pip install -U pip
9+
- pip install -r requirements-test.txt
10+
script:
11+
- py.test -ra -vvv --cov
12+
after_success:
13+
- bash <(curl -s https://codecov.io/bash)

README.rst

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Django Sorting Field
22
====================
3+
.. image:: https://travis-ci.org/andersinno/django-sorting-field.svg?branch=master
4+
:target: https://travis-ci.org/andersinno/django-sorting-field
5+
.. image:: https://codecov.io/gh/andersinno/django-sorting-field/branch/master/graph/badge.svg
6+
:target: https://codecov.io/gh/andersinno/django-sorting-field
37

48
* This package implements a Django form field + widget for drag & drog sorting of items
59
* Sorting any item with a field called ``id`` is supported
@@ -77,9 +81,10 @@ Add the SortingFormField to the CMS Plugin and populate it
7781
7882
def __init__(self, *args, **kwargs):
7983
super(CarouselPluginForm, self).__init__(*args, **kwargs)
80-
self.fields["carousel_order"].populate(
81-
items=self.instance.carousel.pictures.all(),
82-
)
84+
if self.instance.pk:
85+
self.fields["carousel_order"].populate(
86+
items=self.instance.carousel.pictures.all(),
87+
)
8388
8489
class CMSCarouselPlugin(CMSPluginBase):
8590
model = CarouselPlugin

django_sorting_field/utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
def clean_order_json(value):
77
value = "[]" if value is None else value
88

9+
if not isinstance(value, six.string_types):
10+
return value
11+
912
try:
1013
return json.loads(value)
1114
except ValueError:
1215
return []
1316

1417

1518
def iterate_in_order(items, order):
16-
# In case our order is still in json format
17-
if isinstance(order, six.string_types):
18-
order = clean_order_json(order)
19-
19+
order = clean_order_json(order)
2020
items_by_id = {item.id: item for item in items}
2121

2222
# Return items that are ordered first
@@ -26,7 +26,7 @@ def iterate_in_order(items, order):
2626
yield items_by_id.pop(entry)
2727

2828
# Return the rest
29-
for identifier, item in items_by_id.iteritems():
29+
for identifier, item in items_by_id.items():
3030
yield item
3131

3232

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django_sorting_field.utils import sort_by_order
2+
3+
4+
class DummyItem(object):
5+
6+
def __init__(self, item_id):
7+
self.id = item_id
8+
9+
10+
def test_sort_by_order_none():
11+
items = [
12+
DummyItem(0),
13+
DummyItem(1),
14+
DummyItem(2),
15+
]
16+
sorted_items = sort_by_order(items, None)
17+
assert sorted_items[0].id == 0
18+
assert sorted_items[1].id == 1
19+
assert sorted_items[2].id == 2

requirements-test.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-e .
2+
pytest
3+
pytest-cov

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include_package_data = True
1212
packages = find:
1313
install_requires =
1414
django
15+
six
1516

1617
[bdist_wheel]
1718
universal = 1

0 commit comments

Comments
 (0)