Skip to content

Commit

Permalink
implements a PATCH method, get rid of star imports, pass flake8, upda…
Browse files Browse the repository at this point in the history
…te urlpatterns
  • Loading branch information
cleberzavadniak committed Apr 9, 2017
1 parent 47c1b00 commit 858eb89
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 66 deletions.
36 changes: 29 additions & 7 deletions restless/modelviews.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.forms.models import modelform_factory
from django.forms.models import modelform_factory, model_to_dict

from .views import Endpoint
from .http import HttpError, Http200, Http201
Expand All @@ -11,8 +11,9 @@
def _get_form(form, model):
from django import VERSION

if VERSION[:2] >= (1,8):
mf = lambda m: modelform_factory(m, fields='__all__')
if VERSION[:2] >= (1, 8):
def mf(model):
return modelform_factory(model, fields='__all__')
else:
mf = modelform_factory

Expand Down Expand Up @@ -95,7 +96,7 @@ def post(self, request, *args, **kwargs):
if form.is_valid():
obj = form.save()
return Http201(self.serialize(obj))

raise HttpError(400, 'Invalid Data', errors=form.errors)


Expand All @@ -120,7 +121,7 @@ class variable.
model = None
form = None
lookup_field = 'pk'
methods = ['GET', 'PUT', 'DELETE']
methods = ['GET', 'PUT', 'PATCH', 'DELETE']

def get_instance(self, request, *args, **kwargs):
"""Return a model instance represented by this endpoint.
Expand Down Expand Up @@ -169,6 +170,28 @@ def get(self, request, *args, **kwargs):

return self.serialize(self.get_instance(request, *args, **kwargs))

def patch(self, request, *args, **kwargs):
"""Update the object represented by this endpoint."""

if 'PATCH' not in self.methods:
raise HttpError(405, 'Method Not Allowed')

Form = _get_form(self.form, self.model)
instance = self.get_instance(request, *args, **kwargs)

form_data = model_to_dict(instance)
form_data.update(request.data)

form = Form(
form_data,
request.FILES,
instance=instance
)
if form.is_valid():
obj = form.save()
return Http200(self.serialize(obj))
raise HttpError(400, 'Invalid data', errors=form.errors)

def put(self, request, *args, **kwargs):
"""Update the object represented by this endpoint."""

Expand All @@ -177,8 +200,7 @@ def put(self, request, *args, **kwargs):

Form = _get_form(self.form, self.model)
instance = self.get_instance(request, *args, **kwargs)
form = Form(request.data or None, request.FILES,
instance=instance)
form = Form(request.data or None, request.FILES, instance=instance)
if form.is_valid():
obj = form.save()
return Http200(self.serialize(obj))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def run(self):

setup(
name='DjangoRestless',
version='0.0.10',
version='0.0.11',
author='Senko Rasic',
author_email='[email protected]',
description='A RESTful framework for Django',
Expand Down
2 changes: 1 addition & 1 deletion testproject/testapp/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django import forms

from .models import *
from .models import Author

__all__ = ['AuthorForm']

Expand Down
Loading

0 comments on commit 858eb89

Please sign in to comment.