-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtastyhacks.py
106 lines (83 loc) · 3.7 KB
/
tastyhacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Appropriated with license from https://github.com/newsapps/django-boundaryservice/blob/master/boundaryservice/tastyhacks.py
# The MIT License
#
#Copyright (c) 2011 Chicago Tribune, Christopher Groskopf, Ryan Nagle
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
from django.contrib.gis.db.models import GeometryField
from django.utils import simplejson
from tastypie.bundle import Bundle
from tastypie.fields import ApiField, CharField
from tastypie.resources import ModelResource
class GeometryApiField(ApiField):
"""
Custom ApiField for dealing with data from GeometryFields (by serializing them as GeoJSON).
"""
dehydrated_type = 'geometry'
help_text = 'Geometry data.'
def hydrate(self, bundle):
value = super(GeometryApiField, self).hydrate(bundle)
if value is None:
return value
return simplejson.dumps(value)
def dehydrate(self, obj):
return self.convert(super(GeometryApiField, self).dehydrate(obj))
def convert(self, value):
if value is None:
return None
if isinstance(value, dict):
return value
# Get ready-made geojson serialization and then convert it _back_ to a Python object
# so that Tastypie can serialize it as part of the bundle
return simplejson.loads(value.geojson)
class GeoResource(ModelResource):
"""
ModelResource subclass that handles geometry fields as GeoJSON.
"""
@classmethod
def api_field_from_django_field(cls, f, default=CharField):
"""
Overrides default field handling to support custom GeometryApiField.
"""
if isinstance(f, GeometryField):
return GeometryApiField
return super(GeoResource, cls).api_field_from_django_field(f, default)
import base64
from tastypie.fields import FileField
from django.core.files.uploadedfile import SimpleUploadedFile
class Base64FileField(FileField):
"""
A django-tastypie field for handling file-uploads through raw post data.
It uses base64 for en-/decoding the contents of the file.
Usage:
class MyResource(ModelResource):
file_field = Base64FileField("file_field")
class Meta:
queryset = ModelWithFileField.objects.all()
In the case of multipart for submission, it would also pass the filename.
By using a raw post data stream, we have to pass the filename within our
file_field structure:
file_field = {"name": "myfile.png", "file": "longbas64encodedstring"}
"""
def hydrate(self, obj):
value = super(Base64FileField, self).hydrate(obj)
if value:
value = SimpleUploadedFile(value["name"], base64.b64decode(value["file"]))
return value
return None