Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ pip-log.txt
sdist/
target/
var/
venv/
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sudo: false
language: python
cache: pip
python:
- "3.4"
- "2.7"
install:
- pip install -U pip
- pip install -r requirements-test.txt
script:
- py.test -ra -vvv --cov
after_success:
- bash <(curl -s https://codecov.io/bash)
11 changes: 8 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Django Sorting Field
====================
.. image:: https://travis-ci.org/andersinno/django-sorting-field.svg?branch=master
:target: https://travis-ci.org/andersinno/django-sorting-field
.. image:: https://codecov.io/gh/andersinno/django-sorting-field/branch/master/graph/badge.svg
:target: https://codecov.io/gh/andersinno/django-sorting-field

* This package implements a Django form field + widget for drag & drog sorting of items
* Sorting any item with a field called ``id`` is supported
Expand Down Expand Up @@ -77,9 +81,10 @@ Add the SortingFormField to the CMS Plugin and populate it

def __init__(self, *args, **kwargs):
super(CarouselPluginForm, self).__init__(*args, **kwargs)
self.fields["carousel_order"].populate(
items=self.instance.carousel.pictures.all(),
)
if self.instance.pk:
self.fields["carousel_order"].populate(
items=self.instance.carousel.pictures.all(),
)

class CMSCarouselPlugin(CMSPluginBase):
model = CarouselPlugin
Expand Down
10 changes: 5 additions & 5 deletions django_sorting_field/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
def clean_order_json(value):
value = "[]" if value is None else value

if not isinstance(value, six.string_types):
return value

try:
return json.loads(value)
except ValueError:
return []


def iterate_in_order(items, order):
# In case our order is still in json format
if isinstance(order, six.string_types):
order = clean_order_json(order)

order = clean_order_json(order)
items_by_id = {item.id: item for item in items}

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

# Return the rest
for identifier, item in items_by_id.iteritems():
for identifier, item in items_by_id.items():
yield item


Expand Down
19 changes: 19 additions & 0 deletions django_sorting_field_tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django_sorting_field.utils import sort_by_order


class DummyItem(object):

def __init__(self, item_id):
self.id = item_id


def test_sort_by_order_none():
items = [
DummyItem(0),
DummyItem(1),
DummyItem(2),
]
sorted_items = sort_by_order(items, None)
assert sorted_items[0].id == 0
assert sorted_items[1].id == 1
assert sorted_items[2].id == 2
3 changes: 3 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-e .
pytest
pytest-cov
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include_package_data = True
packages = find:
install_requires =
django
six

[bdist_wheel]
universal = 1
Expand Down