From 8fdf9250157cde2341ec9c86ead44b2ed1354aa2 Mon Sep 17 00:00:00 2001 From: Michael Elovskikh Date: Fri, 15 Feb 2013 14:41:12 +0600 Subject: [PATCH 1/7] Added tabs between object form and generic content form on PUT/PATCH form Some extra behaviour to `BrowsableAPIRenderer` to handle PATCH form. Added PATCH button on generic content PUT form. Tabs between object form and generic content form on PUT/PATCH form wich are both allways visible now. Fix #570 Refs #591 --- rest_framework/renderers.py | 10 ++- .../static/rest_framework/js/default.js | 2 + .../templates/rest_framework/base.html | 63 ++++++++++++------- .../templates/rest_framework/form.html | 13 ++++ 4 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 rest_framework/templates/rest_framework/form.html diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index a65254042..736384d64 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -345,12 +345,11 @@ class BrowsableAPIRenderer(BaseRenderer): if not self.show_form_for_method(view, method, request, obj): return - if method == 'DELETE' or method == 'OPTIONS': + if method in ('DELETE', 'OPTIONS'): return True # Don't actually need to return a form if not getattr(view, 'get_serializer', None) or not parsers.FormParser in view.parser_classes: - media_types = [parser.media_type for parser in view.parser_classes] - return self.get_generic_content_form(media_types) + return serializer = view.get_serializer(instance=obj) fields = self.serializer_to_form_fields(serializer) @@ -422,14 +421,17 @@ class BrowsableAPIRenderer(BaseRenderer): view = renderer_context['view'] request = renderer_context['request'] response = renderer_context['response'] + media_types = [parser.media_type for parser in view.parser_classes] renderer = self.get_default_renderer(view) content = self.get_content(renderer, data, accepted_media_type, renderer_context) put_form = self.get_form(view, 'PUT', request) post_form = self.get_form(view, 'POST', request) + patch_form = self.get_form(view, 'PATCH', request) delete_form = self.get_form(view, 'DELETE', request) options_form = self.get_form(view, 'OPTIONS', request) + generic_content_form = self.get_generic_content_form(media_types) name = self.get_name(view) description = self.get_description(view) @@ -449,8 +451,10 @@ class BrowsableAPIRenderer(BaseRenderer): 'available_formats': [renderer.format for renderer in view.renderer_classes], 'put_form': put_form, 'post_form': post_form, + 'patch_form': patch_form, 'delete_form': delete_form, 'options_form': options_form, + 'generic_content_form': generic_content_form, 'api_settings': api_settings }) diff --git a/rest_framework/static/rest_framework/js/default.js b/rest_framework/static/rest_framework/js/default.js index ecaccc0f0..bc5b02928 100644 --- a/rest_framework/static/rest_framework/js/default.js +++ b/rest_framework/static/rest_framework/js/default.js @@ -3,3 +3,5 @@ prettyPrint(); $('.js-tooltip').tooltip({ delay: 1000 }); + +$('#form-switcher a:first').tab('show'); \ No newline at end of file diff --git a/rest_framework/templates/rest_framework/base.html b/rest_framework/templates/rest_framework/base.html index 8d807574b..87e5dc04e 100644 --- a/rest_framework/templates/rest_framework/base.html +++ b/rest_framework/templates/rest_framework/base.html @@ -147,32 +147,49 @@ {% endif %} - {% if put_form %} + {% if 'PUT' in allowed_methods or 'PATCH' in allowed_methods %}
-
-
- - {% csrf_token %} - {{ put_form.non_field_errors }} - {% for field in put_form %} -
- {{ field.label_tag|add_class:"control-label" }} -
- {{ field }} - {{ field.help_text }} - -
-
- {% endfor %} -
- -
- -
-
+ +
+ {% if put_form %} +
+ {% with form=put_form %} +
+
+ {% include "rest_framework/form.html" %} +
+ +
+
+
+ {% endwith %} +
+ {% endif %} +
+ {% with form=generic_content_form %} +
+
+ {% include "rest_framework/form.html" %} +
+ {% if 'PUT' in allowed_methods %} + + {% endif %} + {% if 'PATCH' in allowed_methods %} + + {% endif %} +
+
+
+ {% endwith %} +
+
{% endif %} - {% endif %} diff --git a/rest_framework/templates/rest_framework/form.html b/rest_framework/templates/rest_framework/form.html new file mode 100644 index 000000000..dc7acc708 --- /dev/null +++ b/rest_framework/templates/rest_framework/form.html @@ -0,0 +1,13 @@ +{% load rest_framework %} +{% csrf_token %} +{{ form.non_field_errors }} +{% for field in form %} +
+ {{ field.label_tag|add_class:"control-label" }} +
+ {{ field }} + {{ field.help_text }} + +
+
+{% endfor %} From d3f6536365cefa01f93cfadcc5e6a737d5c5fa80 Mon Sep 17 00:00:00 2001 From: Michael Elovskikh Date: Fri, 15 Feb 2013 15:33:36 +0600 Subject: [PATCH 2/7] Added tests for PATCH form in the Browsable API --- rest_framework/tests/renderers.py | 4 ++++ rest_framework/tests/utils.py | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rest_framework/tests/renderers.py b/rest_framework/tests/renderers.py index e3f45ce60..90ef12212 100644 --- a/rest_framework/tests/renderers.py +++ b/rest_framework/tests/renderers.py @@ -112,6 +112,9 @@ class POSTDeniedView(APIView): def put(self, request): return Response() + def patch(self, request): + return Response() + class DocumentingRendererTests(TestCase): def test_only_permitted_forms_are_displayed(self): @@ -120,6 +123,7 @@ class DocumentingRendererTests(TestCase): response = view(request).render() self.assertNotContains(response, '>POST<') self.assertContains(response, '>PUT<') + self.assertContains(response, '>PATCH<') class RendererEndToEndTests(TestCase): diff --git a/rest_framework/tests/utils.py b/rest_framework/tests/utils.py index 224c4f9d3..8c87917d9 100644 --- a/rest_framework/tests/utils.py +++ b/rest_framework/tests/utils.py @@ -1,10 +1,10 @@ from __future__ import unicode_literals -from django.test.client import RequestFactory, FakePayload +from django.test.client import FakePayload, Client as _Client, RequestFactory as _RequestFactory from django.test.client import MULTIPART_CONTENT from rest_framework.compat import urlparse -class RequestFactory(RequestFactory): +class RequestFactory(_RequestFactory): def __init__(self, **defaults): super(RequestFactory, self).__init__(**defaults) @@ -26,3 +26,15 @@ class RequestFactory(RequestFactory): } r.update(extra) return self.request(**r) + + +class Client(_Client, RequestFactory): + def patch(self, path, data={}, content_type=MULTIPART_CONTENT, + follow=False, **extra): + """ + Send a resource to the server using PATCH. + """ + response = super(Client, self).patch(path, data=data, content_type=content_type, **extra) + if follow: + response = self._handle_redirects(response, **extra) + return response From 3195f72784a2d55d10f3d7a58acdfee694e89e4b Mon Sep 17 00:00:00 2001 From: Michael Elovskikh Date: Fri, 15 Feb 2013 16:39:24 +0600 Subject: [PATCH 3/7] POST form using new form.html template --- .../templates/rest_framework/base.html | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/rest_framework/templates/rest_framework/base.html b/rest_framework/templates/rest_framework/base.html index 87e5dc04e..fb541e944 100644 --- a/rest_framework/templates/rest_framework/base.html +++ b/rest_framework/templates/rest_framework/base.html @@ -125,25 +125,16 @@ {% if post_form %}
-
-
- {% csrf_token %} - {{ post_form.non_field_errors }} - {% for field in post_form %} -
- {{ field.label_tag|add_class:"control-label" }} -
- {{ field }} - {{ field.help_text }} - -
+ {% with form=post_form %} + +
+ {% include "rest_framework/form.html" %} +
+
- {% endfor %} -
- -
-
- +
+ + {% endwith %}
{% endif %} From 533e47235210d735dbe68d96fb55460eca19be9b Mon Sep 17 00:00:00 2001 From: Michael Elovskikh Date: Fri, 15 Feb 2013 18:25:36 +0600 Subject: [PATCH 4/7] Added tabs between object form and generic content form on POST form --- .../templates/rest_framework/base.html | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/rest_framework/templates/rest_framework/base.html b/rest_framework/templates/rest_framework/base.html index fb541e944..9d47a2edd 100644 --- a/rest_framework/templates/rest_framework/base.html +++ b/rest_framework/templates/rest_framework/base.html @@ -125,16 +125,40 @@ {% if post_form %}
- {% with form=post_form %} -
-
- {% include "rest_framework/form.html" %} -
- -
-
-
- {% endwith %} + +
+ {% if post_form %} +
+ {% with form=post_form %} +
+
+ {% include "rest_framework/form.html" %} +
+ +
+
+
+ {% endwith %} +
+ {% endif %} +
+ {% with form=generic_content_form %} +
+
+ {% include "rest_framework/form.html" %} +
+ +
+
+
+ {% endwith %} +
+
{% endif %} From 2fb6fa2dd3b336cc442e707dbb80a4d5616582a6 Mon Sep 17 00:00:00 2001 From: Michael Elovskikh Date: Wed, 20 Feb 2013 17:15:12 +0600 Subject: [PATCH 5/7] Minimal forms appearance improvements --- rest_framework/static/rest_framework/css/default.css | 11 +++++++++++ rest_framework/static/rest_framework/js/default.js | 2 +- rest_framework/templates/rest_framework/base.html | 12 ++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/rest_framework/static/rest_framework/css/default.css b/rest_framework/static/rest_framework/css/default.css index b2e41b994..731075271 100644 --- a/rest_framework/static/rest_framework/css/default.css +++ b/rest_framework/static/rest_framework/css/default.css @@ -150,6 +150,17 @@ html, body { margin: 0 auto -60px; } +.form-switcher { + margin-bottom: 0; +} + +.tab-content { + padding-top: 25px; + background: #fff; + border: 1px solid #ddd; + border-top: none; + border-radius: 0 0 4px 4px; +} #footer, #push { height: 60px; /* .push must be the same height as .footer */ diff --git a/rest_framework/static/rest_framework/js/default.js b/rest_framework/static/rest_framework/js/default.js index bc5b02928..484a3bdf1 100644 --- a/rest_framework/static/rest_framework/js/default.js +++ b/rest_framework/static/rest_framework/js/default.js @@ -4,4 +4,4 @@ $('.js-tooltip').tooltip({ delay: 1000 }); -$('#form-switcher a:first').tab('show'); \ No newline at end of file +$('.form-switcher a:first').tab('show'); \ No newline at end of file diff --git a/rest_framework/templates/rest_framework/base.html b/rest_framework/templates/rest_framework/base.html index 9d47a2edd..2fe7b6536 100644 --- a/rest_framework/templates/rest_framework/base.html +++ b/rest_framework/templates/rest_framework/base.html @@ -125,11 +125,11 @@ {% if post_form %}
-