Skip to content

Commit

Permalink
Merge pull request #4 from andersinno/fix-bugs
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
suutari-ai authored Oct 26, 2017
2 parents 04aa9af + 00a920f commit 7362ba3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
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

0 comments on commit 7362ba3

Please sign in to comment.