marcgibbons/django-rest-swagger — pull request #15 by lukas-hetzenecker (2013-09-08 13:14:20+00:00)
Title: [Request for comments] Miscellaneous Features

**DO NOT MERGE YET!**

I needed to extend django-rest-swagger for the API used in a project of mine.

These modifications were made:

---
#### Support of image uploading

I added [file] as a keyword to the docstring, which tells swagger that a multipart file should be uploaded.

```python
class ProfilePictureView(APIView):
"""
Multipart -- Image [required] [file]
"""
parser_classes = (MultiPartParser,)

def put(self, request, *args, **kwargs):
files = request.FILES
print "files", files
return Response({})
```
---
#### Different request and response serializers

It is now possible to use different serializers for the request and the response.
I introduced *request_serializer_class* and *response_serializer_class* attributes. If they are not found, it still falls back to *serializer_class*

```python
class PlacesPrefetchRequest(APIView):
"""
Request Body -- Places as JSON [required] [body]
"""

request_serializer_class = serializers.SimplePlacesSerializer
response_serializer_class = serializers.PlacesSerializer

def post(self, request, *args, **kwargs):
[...]
```
---
#### Namespaces from URL partition

Namespaces did not work correctly with APIViews (they needed Viewsets).
The only alternative was to group the resources by the URL.
This was made using a new setting: *namespace_part_of_url*

```python
SWAGGER_SETTINGS = {
'namespace_part_of_url' : 2,
}
```

This means that the second part of the URL is responsible for the namespace.
For example: http://example.com/api/v1/profile/pictures belongs to the namespace 'profile'

---
### body, required and overwrite keyword
The keyword *[body]* marks the parameter type as body. This generates a textarea that is send as the message body.
The keyword *[required]* marks the query string argument as required.
The keyword *[overwrite]* overwrites all other parameter types - only the one in the docstring are used.

---
### Description for serializer fields
The help_text of the serializer fields is now used for their description (previously this was always empty).

---

Please tell me if you are interested in merging any of these changes. I'll then generate single merge requests for each feature.