diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | AUTHORS | 25 | ||||
| -rw-r--r-- | djangorestframework/mixins.py | 51 | ||||
| -rw-r--r-- | djangorestframework/parsers.py | 9 | ||||
| -rw-r--r-- | djangorestframework/runtests/settings.py | 1 | ||||
| -rw-r--r-- | djangorestframework/serializer.py | 12 | ||||
| -rw-r--r-- | djangorestframework/tests/mixins.py | 113 | ||||
| -rw-r--r-- | djangorestframework/tests/models.py | 28 | ||||
| -rw-r--r-- | djangorestframework/tests/modelviews.py | 86 | ||||
| -rw-r--r-- | djangorestframework/views.py | 4 |
10 files changed, 297 insertions, 33 deletions
@@ -21,3 +21,4 @@ MANIFEST .coverage .tox .DS_Store +.idea/*
\ No newline at end of file @@ -1,15 +1,18 @@ Tom Christie <tomchristie> - tom@tomchristie.com, @thisneonsoul - Author. -Paul Bagwell <pbgwl> - Suggestions & bugfixes. -Marko Tibold <markotibold> - Contributions & Providing the Jenkins CI Server. -Sébastien Piquemal <sebpiq> - Contributions. -Carmen Wick <cwick> - Bugfixes. -Alex Ehlke <aehlke> - Design Contributions. -Alen Mujezinovic <flashingpumpkin> - Contributions. -Carles Barrobés <txels> - HEAD support. -Michael Fötsch <mfoetsch> - File format support. -David Larlet <david> - OAuth support. -Andrew Straw <astraw> - Bugfixes. -<zeth> - Bugfixes. +Paul Bagwell <pbgwl> +Marko Tibold <markotibold> - Additional thanks for providing & managing the Jenkins CI Server. +Sébastien Piquemal <sebpiq> +Carmen Wick <cwick> +Alex Ehlke <aehlke> +Alen Mujezinovic <flashingpumpkin> +Carles Barrobés <txels> +Michael Fötsch <mfoetsch> +David Larlet <david> +Andrew Straw <astraw> +<zeth> +Fernando Zunino <fzunino> +Jens Alm <ulmus> +Craig Blaszczyk <jakul> THANKS TO: diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index b1ba0596..bb26ad96 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -5,7 +5,7 @@ classes that can be added to a `View`. from django.contrib.auth.models import AnonymousUser from django.db.models.query import QuerySet -from django.db.models.fields.related import RelatedField +from django.db.models.fields.related import ForeignKey from django.http import HttpResponse from django.http.multipartparser import LimitBytes @@ -161,17 +161,18 @@ class RequestMixin(object): return # At this point we're committed to parsing the request as form data. - self._data = data = self.request.POST + self._data = data = self.request.POST.copy() self._files = self.request.FILES # Method overloading - change the method and remove the param from the content. if self._METHOD_PARAM in data: - self._method = data[self._METHOD_PARAM].upper() + # NOTE: unlike `get`, `pop` on a `QueryDict` seems to return a list of values. + self._method = self._data.pop(self._METHOD_PARAM)[0].upper() # Content overloading - modify the content type, and re-parse. if self._CONTENT_PARAM in data and self._CONTENTTYPE_PARAM in data: - self._content_type = data[self._CONTENTTYPE_PARAM] - stream = StringIO(data[self._CONTENT_PARAM]) + self._content_type = self._data.pop(self._CONTENTTYPE_PARAM)[0] + stream = StringIO(self._data.pop(self._CONTENT_PARAM)[0]) (self._data, self._files) = self._parse(stream, self._content_type) @@ -508,21 +509,47 @@ class CreateModelMixin(object): """ Behavior to create a `model` instance on POST requests """ - def post(self, request, *args, **kwargs): + def post(self, request, *args, **kwargs): model = self.resource.model - # translated 'related_field' kwargs into 'related_field_id' - for related_name in [field.name for field in model._meta.fields if isinstance(field, RelatedField)]: - if kwargs.has_key(related_name): - kwargs[related_name + '_id'] = kwargs[related_name] - del kwargs[related_name] + # Copy the dict to keep self.CONTENT intact + content = dict(self.CONTENT) + m2m_data = {} + + for field in model._meta.fields: + if isinstance(field, ForeignKey) and kwargs.has_key(field.name): + # translate 'related_field' kwargs into 'related_field_id' + kwargs[field.name + '_id'] = kwargs[field.name] + del kwargs[field.name] + + for field in model._meta.many_to_many: + if content.has_key(field.name): + m2m_data[field.name] = ( + field.m2m_reverse_field_name(), content[field.name] + ) + del content[field.name] + + all_kw_args = dict(content.items() + kwargs.items()) - all_kw_args = dict(self.CONTENT.items() + kwargs.items()) if args: instance = model(pk=args[-1], **all_kw_args) else: instance = model(**all_kw_args) instance.save() + + for fieldname in m2m_data: + manager = getattr(instance, fieldname) + + if hasattr(manager, 'add'): + manager.add(*m2m_data[fieldname][1]) + else: + data = {} + data[manager.source_field_name] = instance + + for related_item in m2m_data[fieldname][1]: + data[m2m_data[fieldname][0]] = related_item + manager.through(**data).save() + headers = {} if hasattr(instance, 'get_absolute_url'): headers['Location'] = self.resource(self).url(instance) diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index 5f19c521..2ff64bd3 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -108,7 +108,8 @@ if yaml: except ValueError, exc: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'detail': 'YAML parse error - %s' % unicode(exc)}) - +else: + YAMLParser = None class PlainTextParser(BaseParser): """ @@ -167,3 +168,9 @@ class MultiPartParser(BaseParser): {'detail': 'multipart parse error - %s' % unicode(exc)}) return django_parser.parse() +DEFAULT_PARSERS = ( JSONParser, + FormParser, + MultiPartParser ) + +if YAMLParser: + DEFAULT_PARSERS += ( YAMLParser, ) diff --git a/djangorestframework/runtests/settings.py b/djangorestframework/runtests/settings.py index 9b3c2c92..a38ba8ed 100644 --- a/djangorestframework/runtests/settings.py +++ b/djangorestframework/runtests/settings.py @@ -95,6 +95,7 @@ INSTALLED_APPS = ( # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'djangorestframework', + 'djangorestframework.tests', ) # OAuth support is optional, so we only test oauth if it's installed. diff --git a/djangorestframework/serializer.py b/djangorestframework/serializer.py index 82aeb53f..22efa5ed 100644 --- a/djangorestframework/serializer.py +++ b/djangorestframework/serializer.py @@ -177,7 +177,7 @@ class Serializer(object): Keys serialize to their string value, unless they exist in the `rename` dict. """ - return getattr(self.rename, smart_str(key), smart_str(key)) + return self.rename.get(smart_str(key), smart_str(key)) def serialize_val(self, key, obj): @@ -229,16 +229,16 @@ class Serializer(object): # serialize each required field for fname in fields: if hasattr(self, smart_str(fname)): - # check for a method 'fname' on self first + # check first for a method 'fname' on self first meth = getattr(self, fname) if inspect.ismethod(meth) and len(inspect.getargspec(meth)[0]) == 2: obj = meth(instance) + elif hasattr(instance, '__contains__') and fname in instance: + # check for a key 'fname' on the instance + obj = instance[fname] elif hasattr(instance, smart_str(fname)): - # now check for an attribute 'fname' on the instance + # finally check for an attribute 'fname' on the instance obj = getattr(instance, fname) - elif fname in instance: - # finally check for a key 'fname' on the instance - obj = instance[fname] else: continue diff --git a/djangorestframework/tests/mixins.py b/djangorestframework/tests/mixins.py new file mode 100644 index 00000000..da7c4d86 --- /dev/null +++ b/djangorestframework/tests/mixins.py @@ -0,0 +1,113 @@ +"""Tests for the status module""" +from django.test import TestCase +from djangorestframework import status +from djangorestframework.compat import RequestFactory +from django.contrib.auth.models import Group, User +from djangorestframework.mixins import CreateModelMixin +from djangorestframework.resources import ModelResource +from djangorestframework.tests.models import CustomUser + + +class TestModelCreation(TestCase): + """Tests on CreateModelMixin""" + + def setUp(self): + self.req = RequestFactory() + + def test_creation(self): + self.assertEquals(0, Group.objects.count()) + + class GroupResource(ModelResource): + model = Group + + form_data = {'name': 'foo'} + request = self.req.post('/groups', data=form_data) + mixin = CreateModelMixin() + mixin.resource = GroupResource + mixin.CONTENT = form_data + + response = mixin.post(request) + self.assertEquals(1, Group.objects.count()) + self.assertEquals('foo', response.cleaned_content.name) + + + def test_creation_with_m2m_relation(self): + class UserResource(ModelResource): + model = User + + def url(self, instance): + return "/users/%i" % instance.id + + group = Group(name='foo') + group.save() + + form_data = {'username': 'bar', 'password': 'baz', 'groups': [group.id]} + request = self.req.post('/groups', data=form_data) + cleaned_data = dict(form_data) + cleaned_data['groups'] = [group] + mixin = CreateModelMixin() + mixin.resource = UserResource + mixin.CONTENT = cleaned_data + + response = mixin.post(request) + self.assertEquals(1, User.objects.count()) + self.assertEquals(1, response.cleaned_content.groups.count()) + self.assertEquals('foo', response.cleaned_content.groups.all()[0].name) + + def test_creation_with_m2m_relation_through(self): + """ + Tests creation where the m2m relation uses a through table + """ + class UserResource(ModelResource): + model = CustomUser + + def url(self, instance): + return "/customusers/%i" % instance.id + + form_data = {'username': 'bar0', 'groups': []} + request = self.req.post('/groups', data=form_data) + cleaned_data = dict(form_data) + cleaned_data['groups'] = [] + mixin = CreateModelMixin() + mixin.resource = UserResource + mixin.CONTENT = cleaned_data + + response = mixin.post(request) + self.assertEquals(1, CustomUser.objects.count()) + self.assertEquals(0, response.cleaned_content.groups.count()) + + group = Group(name='foo1') + group.save() + + form_data = {'username': 'bar1', 'groups': [group.id]} + request = self.req.post('/groups', data=form_data) + cleaned_data = dict(form_data) + cleaned_data['groups'] = [group] + mixin = CreateModelMixin() + mixin.resource = UserResource + mixin.CONTENT = cleaned_data + + response = mixin.post(request) + self.assertEquals(2, CustomUser.objects.count()) + self.assertEquals(1, response.cleaned_content.groups.count()) + self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name) + + + group2 = Group(name='foo2') + group2.save() + + form_data = {'username': 'bar2', 'groups': [group.id, group2.id]} + request = self.req.post('/groups', data=form_data) + cleaned_data = dict(form_data) + cleaned_data['groups'] = [group, group2] + mixin = CreateModelMixin() + mixin.resource = UserResource + mixin.CONTENT = cleaned_data + + response = mixin.post(request) + self.assertEquals(3, CustomUser.objects.count()) + self.assertEquals(2, response.cleaned_content.groups.count()) + self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name) + self.assertEquals('foo2', response.cleaned_content.groups.all()[1].name) + + diff --git a/djangorestframework/tests/models.py b/djangorestframework/tests/models.py new file mode 100644 index 00000000..61da1d45 --- /dev/null +++ b/djangorestframework/tests/models.py @@ -0,0 +1,28 @@ +from django.db import models
+from django.contrib.auth.models import Group
+
+class CustomUser(models.Model):
+ """
+ A custom user model, which uses a 'through' table for the foreign key
+ """
+ username = models.CharField(max_length=255, unique=True)
+ groups = models.ManyToManyField(
+ to=Group, blank=True, null=True, through='UserGroupMap'
+ )
+
+ @models.permalink
+ def get_absolute_url(self):
+ return ('custom_user', (), {
+ 'pk': self.id
+ })
+
+
+class UserGroupMap(models.Model):
+ user = models.ForeignKey(to=CustomUser)
+ group = models.ForeignKey(to=Group)
+
+ @models.permalink
+ def get_absolute_url(self):
+ return ('user_group_map', (), {
+ 'pk': self.id
+ })
\ No newline at end of file diff --git a/djangorestframework/tests/modelviews.py b/djangorestframework/tests/modelviews.py new file mode 100644 index 00000000..2fd1878a --- /dev/null +++ b/djangorestframework/tests/modelviews.py @@ -0,0 +1,86 @@ +from django.conf.urls.defaults import patterns, url +from django.test import TestCase +from django.forms import ModelForm +from django.contrib.auth.models import Group, User +from djangorestframework.resources import ModelResource +from djangorestframework.views import ListOrCreateModelView, InstanceModelView +from djangorestframework.tests.models import CustomUser + +class GroupResource(ModelResource): + model = Group + +class UserForm(ModelForm): + class Meta: + model = User + exclude = ('last_login', 'date_joined') + +class UserResource(ModelResource): + model = User + form = UserForm + +class CustomUserResource(ModelResource): + model = CustomUser + +urlpatterns = patterns('', + url(r'^users/$', ListOrCreateModelView.as_view(resource=UserResource), name='users'), + url(r'^users/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=UserResource)), + url(r'^customusers/$', ListOrCreateModelView.as_view(resource=CustomUserResource), name='customusers'), + url(r'^customusers/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=CustomUserResource)), + url(r'^groups/$', ListOrCreateModelView.as_view(resource=GroupResource), name='groups'), + url(r'^groups/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=GroupResource)), +) + + +class ModelViewTests(TestCase): + """Test the model views djangorestframework provides""" + urls = 'djangorestframework.tests.modelviews' + + def test_creation(self): + """Ensure that a model object can be created""" + self.assertEqual(0, Group.objects.count()) + + response = self.client.post('/groups/', {'name': 'foo'}) + + self.assertEqual(response.status_code, 201) + self.assertEqual(1, Group.objects.count()) + self.assertEqual('foo', Group.objects.all()[0].name) + + def test_creation_with_m2m_relation(self): + """Ensure that a model object with a m2m relation can be created""" + group = Group(name='foo') + group.save() + self.assertEqual(0, User.objects.count()) + + response = self.client.post('/users/', {'username': 'bar', 'password': 'baz', 'groups': [group.id]}) + + self.assertEqual(response.status_code, 201) + self.assertEqual(1, User.objects.count()) + + user = User.objects.all()[0] + self.assertEqual('bar', user.username) + self.assertEqual('baz', user.password) + self.assertEqual(1, user.groups.count()) + + group = user.groups.all()[0] + self.assertEqual('foo', group.name) + + def test_creation_with_m2m_relation_through(self): + """ + Ensure that a model object with a m2m relation can be created where that + relation uses a through table + """ + group = Group(name='foo') + group.save() + self.assertEqual(0, User.objects.count()) + + response = self.client.post('/customusers/', {'username': 'bar', 'groups': [group.id]}) + + self.assertEqual(response.status_code, 201) + self.assertEqual(1, CustomUser.objects.count()) + + user = CustomUser.objects.all()[0] + self.assertEqual('bar', user.username) + self.assertEqual(1, user.groups.count()) + + group = user.groups.all()[0] + self.assertEqual('foo', group.name) diff --git a/djangorestframework/views.py b/djangorestframework/views.py index eea3b97a..c25bb88f 100644 --- a/djangorestframework/views.py +++ b/djangorestframework/views.py @@ -44,9 +44,7 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView): """ List of parsers the resource can parse the request with. """ - parsers = ( parsers.JSONParser, - parsers.FormParser, - parsers.MultiPartParser ) + parsers = parsers.DEFAULT_PARSERS """ List of all authenticating methods to attempt. |
