From ebc6ac8d372f4d615fda15087fe1f4416bad1476 Mon Sep 17 00:00:00 2001 From: Miguel Marcos Date: Tue, 22 Aug 2017 15:26:50 +0200 Subject: [PATCH] started docs on DataListView setup the test for DataListView --- docs/reference.md | 59 +++++++++++++++++++++- tests/test_generics/test_data_list_view.py | 38 ++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/test_generics/test_data_list_view.py diff --git a/docs/reference.md b/docs/reference.md index 49fdbc09..60946197 100755 --- a/docs/reference.md +++ b/docs/reference.md @@ -203,6 +203,7 @@ template and is able to do filtering, sorting, pagination and linking. **Extends** * `arctic.generics.View` +* `arctic.mixins.ListMixin` * `django.views.generic.ListView` **Properties** @@ -272,7 +273,61 @@ list of links with the format `('name', 'url')`, not connected to the table data ### `tool_links_icon` -default is fa-wrench. an icon displayed for the dropdown of multiple tool links or, if only one tool link set, it would be use as default icon. +default is fa-wrench. an icon displayed for the dropdown of multiple tool +links or, if only one tool link set, it would be use as default icon. + + +## DataListView + +`class arctic.generics.DataListView` + +This view is similar in features to ListView but displays tabular data from an +API source instead of a django model, it includes a default template and is +able to do filtering, sorting, pagination and linking. +The biggest difference here is that DataListView requires a RemoteDataSet +instance instead of a model. + +A `RemoteDataSet` is a `QuerySet`-like object that acts as a gateway between +the `DataListView` and an API at the very least it needs to implement the +following: + +- `url_template` property with optional `{filters}`, `{fields}`, `{order}` or + `{paginate}` format paramenters, which in turn can be customized with their + own templates. + +- `get()` method which accepts two parameters, by default `start` and `stop` or, +if using the `offset_limit` decorator then `offset` and `limit`. This method is +responsible to connect to the API with the `url_template` and fetch the data +needed to populate the list. + +Example: + + from arctic.utils import RemoteDataSet, offset_limit + + class CountriesDataSet(RemoteDataSet): + url_template = 'http://example.com/countries-api/?{filters}{fields}{order}{paginate}' + order_template = '&order_by={}' + + @offset_limit + def get(self, offset, limit): + r = urllib.request.urlopen((self.get_url(offset, limit))) + data = r.read().decode("utf-8") + return json.loads(data) + + class CountryListView(DataListView): + dataset = CountriesDataSet() + ... + +**Extends** + +* `arctic.generics.TemplateView` +* `arctic.mixins.ListMixin` + +**Properties** + +### `fields` + + ## FormView @@ -396,7 +451,7 @@ will check if it returns `True` or `False`. Note that on multiple permissions, only one permission is needed to validate a user's role. -## LayoutMixin +## FormMixin ### `layout` diff --git a/tests/test_generics/test_data_list_view.py b/tests/test_generics/test_data_list_view.py new file mode 100644 index 00000000..907d115c --- /dev/null +++ b/tests/test_generics/test_data_list_view.py @@ -0,0 +1,38 @@ +import pytest + +from django.core.urlresolvers import reverse + + +@pytest.mark.django_db +class TestDataListView(object): + """ + Test ListView using ArticleListView from example application + """ + def _request(self, admin_client): + response = admin_client.get(reverse('countries-list')) + assert response.status_code == 200 + return response + + def _assert_list_items_len(self, response, length): + assert 'list_items' in response.context_data + assert len(response.context_data['list_items']) == length + + # def test_paginated_items(self, admin_client): + # """ + # Paginated List view + # """ + # response = self._request(admin_client) + # self._assert_list_items_len(response, 250) + + # assert response.context_data['page_obj'].paginator.num_pages == 20 + + # def test_simple_search_form(self, admin_client): + # # empty search, return all + # response = admin_client.get(reverse('countries-list'), + # {'search': ''}) + # assert len(response.context_data['list_items']) == 150 + + # # search filled, filter content + # response = admin_client.get(reverse('countries-list'), + # {'search': 'af'}) + # assert len(response.context_data['list_items']) == 3