Permalink
Browse files

Fixed issue #29

  • Loading branch information...
1 parent 3e3764f commit af2d74758857aeddc6ac87c6ea0a18db92429047 @ikeikeikeike committed Aug 13, 2012
Showing with 172 additions and 23 deletions.
  1. +7 −0 .travis.yml
  2. +8 −2 HISTORY.rst
  3. +4 −0 README.md
  4. +4 −0 docs/source/index.rst
  5. +1 −0 docs/source/manager.rst
  6. +78 −15 queryset_client/client.py
  7. +70 −6 queryset_client/tests/base/tests/model.py
View
@@ -0,0 +1,7 @@
+language: python
+env:
+ - TOXENV=py26
+ - TOXENV=py27
+install:
+ - pip install --use-mirrors tox
+script: tox
View
@@ -1,14 +1,20 @@
History
--------
+0.6.x (2012-0x-xx)
+~~~~~~~~~~~~~~~~~~~
+* Support issue `#29 <https://github.com/ikeikeikeike/tastypie-queryset-client/issues/29>`_
+* Add clear method in the ManyToManyManager.
+* Add remove method in the ManyToManyManager.
+
0.6.3 (2012-07-15)
~~~~~~~~~~~~~~~~~~~
* Moved testsuite position.
0.6.2 (2012-07-15)
~~~~~~~~~~~~~~~~~~~
-* Fixed issue #31 (Pagination Performance)
-* Support strict field in Model.
+* Fixed issue `#31 <https://github.com/ikeikeikeike/tastypie-queryset-client/issues/30>`_ (Pagination Performance)
+* `Support strict field in Model. <https://github.com/ikeikeikeike/tastypie-queryset-client/issues/28>`_
0.5.2 (2012-07-10)
~~~~~~~~~~~~~~~~~~~
View
@@ -57,6 +57,10 @@ Delete
>>> assert True # throw AttributeError.
```
+Requirements
+=============
+
+Tastypie: [Over the 0.9.12-alpha](https://github.com/toastdriven/django-tastypie/commit/daca57ad5f62afcf1ffacaaf77bc3715a1b9a04f).
Setup
=====
View
@@ -6,6 +6,10 @@
Tastypie QuerySet Client's documentation
====================================================
+.. note::
+ Requirements::
+ Tastypie: `Over the 0.9.12-alpha <https://github.com/toastdriven/django-tastypie/commit/daca57ad5f62afcf1ffacaaf77bc3715a1b9a04f>`_
+
Contents:
.. toctree::
View
@@ -3,3 +3,4 @@ Manager
.. autoclass:: queryset_client.client.Manager
+.. autoclass:: queryset_client.client.ManyToManyManager
View
@@ -218,9 +218,19 @@ def _get_ids(self):
return [parse_id(obj["resource_uri"]) for obj in self._objects]
def _filter(self, *args, **kwargs):
- # TODO: ↓↓↓ ManyToManyで 一件も relationがない場合の処理, 現状元のQuerySetの結果が返される ↓↓↓↓
- # <QuerySet <class 'queryset_client.client.Response'> (0/0)>
- # TODO: id__in 上書きされる
+ """
+
+ .. note:: No request args(Empty request params).
+
+ - × e.p. id__in=[] -> http://no.com/?any=search
+ - ○ e.p. id__in=[] -> http://no.com/?any=search&id__in=
+
+
+ .. note:: overwrite, id__in
+
+ """
+
+ # TODO: Bugs? in the requests package. (The Issue #??).
query = dict(self._query.items() + kwargs.items())
clone = self._clone(self._get_responses(**query))
clone._query.update({"id__in": clone._get_ids()})
@@ -333,31 +343,83 @@ def exists(self, *args, **kwargs):
class ManyToManyManager(Manager):
- def __init__(self, query, **kwargs):
+ def __init__(self, query=None, instance=None, **kwargs):
super(ManyToManyManager, self).__init__(**kwargs)
- self._query = query
+ self._query = query or dict()
+ self._instance = instance
def get_query_set(self):
- return QuerySet(self.model, query=self._query)
-
- def add(self):
- # TODO: ManyToMany Manager parent_obj.add(related_object)
- pass
+ return QuerySet(self.model, query=self._query).filter()
def filter(self, *args, **kwargs):
if "id__in" in kwargs:
raise NotImplementedError("'id__in' does not allow ManyToManyManager.")
- return super(ManyToManyManager, self).filter(*args, **kwargs)
+ return QuerySet(self.model, query=self._query).filter(*args, **kwargs)
+
+ def add(self, *objs):
+ """
+
+ .. note::
+ After executing a add method, does not execute saving. You must be manual operation to the save method.
+
+ .. todo::
+ signal save
+
+ """
+ if objs:
+ resource_models = getattr(self._instance, self.model._model_name)
+ query_ids = self._query.get("id__in", [])
+ for obj in objs:
+ resource_uri = getattr(obj, "resource_uri")
+
+ resource_models.append(resource_uri)
+ query_ids.append(parse_id(resource_uri))
+ self._query.update({"id__in": list(set(query_ids))})
+ setattr(self._instance, self.model._model_name, list(set(resource_models)))
+
+ def remove(self, *objs):
+ """
+
+ .. note::
+ After executing a add method, does not execute saving. You must be manual operation to the save method.
+
+ .. todo::
+ signal save
+
+ """
+ if objs:
+ resource_models = getattr(self._instance, self.model._model_name)
+ query_ids = self._query.get("id__in", [])
+ for obj in objs:
+ resource_uri = getattr(obj, "resource_uri")
+
+ resource_models.remove(resource_uri)
+ query_ids.remove(parse_id(resource_uri))
+ self._query.update({"id__in": list(set(query_ids))})
+ setattr(self._instance, self.model._model_name, list(set(resource_models)))
+
+ def clear(self):
+ """
+
+ .. note::
+ After executing a add method, does not execute saving. You must be manual operation to the save method.
+
+ .. todo::
+ signal save
+
+ """
+ self._query.update({"id__in": list()})
+ setattr(self._instance, self.model._model_name, list())
-def parse_id(resouce_uri):
+def parse_id(resource_uri):
""" url parsing
:param resource_uri:
:rtype: str
:return: primary id
"""
- return resouce_uri.split("/")[::-1][1]
+ return resource_uri.split("/")[::-1][1]
class Response(object):
@@ -392,7 +454,8 @@ def __getattr__(self, attr):
model = self.model.clone(attr)
url = self._response[attr]
if related_type == "to_many":
- return self._to_many_class(model=model, query={"id__in": [parse_id(u) for u in url]})
+ return self._to_many_class(model=model,
+ query={"id__in": [parse_id(u) for u in url]}, instance=self.model)
elif related_type == "to_one":
return self._to_one_class(model=model, url=url)
@@ -426,6 +489,7 @@ def _response(self):
def save(self):
""" save saved response """
self.model.save()
+ self.__response = self.model._get_fields()
def delete(self):
""" remove saved response """
@@ -548,7 +612,6 @@ def _get_field(self, field):
raise FieldTypeError(
"'{0}' is '{1}' type. ( Input '{2}:{3}' )"
.format(field, field_type, value, type(value).__name__))
- # TODO: ManyToMany Manager parent_obj.add(related_object)
if field_type == "related":
value = getattr(value, "resource_uri", value)
if self._schema_store["fields"][field]["related_type"] == "to_many":
@@ -1,3 +1,4 @@
+# encoding: utf-8
from testcases import (
TestServerTestCase,
get_client
@@ -137,12 +138,12 @@ def test_object_save1(self):
inbox_message.inbox = inbox
inbox_message.save() # TODO: save success
- def test_save_rel1(self):
- """ relation """
- # subject = ""
- # body = ""
- # message = self.client.inbox_message(subject=subject, body=body)
- # message.save()
+# def test_save_rel1(self):
+# """ relation """
+# subject = ""
+# body = ""
+# message = self.client.inbox_message(subject=subject, body=body)
+# message.save()
def test_save_many1(self):
""" post """
@@ -158,6 +159,69 @@ def test_save_many2(self):
for inbox_message_many in self.client.inbox_message_many.objects.all():
inbox_message_many.save()
+ def test_save_many3(self):
+ """ add, remove, clear
+
+ * Check resource_uri (response, field)
+
+
+ """
+ call_command('loaddata', 'base_data.json')
+
+ inbox_messages = []
+ inbox_message_many = self.client.inbox_message_many.objects.get(id=2)
+ count_orig = inbox_message_many.inbox_message.count()
+
+ for i in range(0, 5):
+ message = self.client.message()
+ message.subject = id_generator()
+ message.body = id_generator()
+ message.save()
+
+ inbox = self.client.inbox()
+ inbox.did = id_generator()
+ inbox.save()
+
+ inbox_message = self.client.inbox_message()
+ inbox_message.message = message
+ inbox_message.inbox = inbox
+ inbox_message.save()
+
+ inbox_messages.append(inbox_message)
+ inbox_message_many.inbox_message.add(inbox_message)
+ inbox_message_many.save()
+ self.assertTrue(
+ inbox_message_many._response["inbox_message"] == inbox_message_many.model.inbox_message)
+
+ inbox_all = inbox_message_many.inbox_message.all()
+ for obj, obj_ in zip(inbox_all, inbox_message_many.inbox_message.filter()):
+ self.assertTrue(obj.id == obj_.id)
+
+ inbox_all_ = self.client.inbox_message.objects.filter(id__in=inbox_all._query["id__in"])
+ self.assertTrue(len(inbox_all) == len(inbox_all_))
+ for obj, obj_ in zip(inbox_all, inbox_all_):
+ self.assertTrue(obj.id == obj_.id)
+
+ ids = inbox_all._query["id__in"]
+ inbox_message_many = self.client.inbox_message_many.objects.get(id=2)
+ num = inbox_message_many.inbox_message.filter(id=ids[0]).count()
+ self.assertTrue(num == 1)
+
+ # remove
+ inbox_message_many.inbox_message.remove(*inbox_messages)
+ self.assertTrue(count_orig == inbox_message_many.inbox_message.count())
+ inbox_message_many.save()
+ self.assertTrue(count_orig == inbox_message_many.inbox_message.count())
+
+ # clear TODO: Issue #??
+# inbox_message_many.inbox_message.clear()
+# self.assertTrue(0 != inbox_message_many.inbox_message.count())
+# inbox_message_many.save()
+# self.assertTrue(0 == inbox_message_many.inbox_message.count())
+
+ inbox_message_many.save()
+
+
def test_delete1(self):
subject = "subject delete 1"
body = "body delete 1"

0 comments on commit af2d747

Please sign in to comment.