Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PATCH method, flake8, star imports, etc. #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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