-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
executable file
·304 lines (220 loc) · 9.92 KB
/
api.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import logging, os, mimetypes
import datetime as dt
from django.conf import settings
from django.db.models.loading import get_model
from django.db import IntegrityError
from django.utils.translation import ugettext as _
from django.template.defaultfilters import slugify
from glue import Epoxy, API_EXCEPTION_FORMERRORS, API_EXCEPTION_INTEGRITY, API_EXCEPTION_DOESNOTEXIST, API_EXCEPTION_OSERROR
from glue.models import Page, Pin, Tag
from glue.forms import AddPageForm, AddPinForm, EditPinForm, UploadPinForm
logger = logging.getLogger(__name__)
def home(request):
# logger.info("Welcome to GLUEBOX api")
return Epoxy( request ).json()
def index(request):
# logger.info("Welcome to GLUEBOX api")
return Epoxy( request ).json()
def manage_objects( request, model_name ):
# logger.info("Welcome to GLUEBOX api")
return Epoxy( request ).queryset( get_model( "glue", model_name ).objects.filter(), model_name=model_name ).json()
def manage_single_object( request, model_name, pk ):
# logger.info("Welcome to GLUEBOX api")
return Epoxy( request ).single( Page, {'pk':pk} ).json()
def test( request ):
response = Epoxy( request )
return response.json()
def pages(request):
# logger.info("Welcome to GLUEBOX api")
response = Epoxy( request )
if response.method =='POST':
form = AddPageForm( request.REQUEST )
if not form.is_valid():
return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json()
try:
p_en = Page( title=form.cleaned_data['title_en'], language='EN', slug=form.cleaned_data['slug'])
p_en.save()
p_fr = Page( title=form.cleaned_data['title_fr'], language='FR', slug=form.cleaned_data['slug'])
p_fr.save()
except IntegrityError, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_INTEGRITY).json()
response.add('object',[ p_en.json(), p_fr.json() ])
return response.queryset( Page.objects.filter() ).json()
def page( request, page_id ):
return Epoxy( request ).single( Page, {'id':page_id} ).json()
def page_by_slug( request, page_slug, page_language ):
return Epoxy( request ).single( Page, {'slug':page_slug,'language':page_language} ).json()
def pins( request ):
response = Epoxy( request )
if response.method =='POST':
form = AddPinForm( request.REQUEST )
if not form.is_valid():
return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json()
pages = {}
if len(form.cleaned_data['page_slug']) > 0:
# attacch new pin to a selected page (both languages)
response.add('page_slug',form.cleaned_data['page_slug'])
for language,l in settings.LANGUAGES:
try:
pages[ language ] = Page.objects.get( slug=form.cleaned_data['page_slug'], language=language )
except Page.DoesNotExist:
return response.throw_error( error=_("selected page does not exists"), code=API_EXCEPTION_FORMERRORS ).json()
response.add('pages', [ pages[p].json() for p in pages ] )
pins = {}
if len(form.cleaned_data['parent_pin_slug']) > 0:
# attacch new pin to a selected pin (pin children, usually displayed on the right side, both languages)
response.add('parent_pin_slug',form.cleaned_data['parent_pin_slug'])
for language,l in settings.LANGUAGES:
try:
pins[ language ] = Pin.objects.get( slug=form.cleaned_data['parent_pin_slug'], language=language )
except Pin.DoesNotExist, e:
return response.throw_error( error=_("selected pin does not exists. Exception: %s" % e), code=API_EXCEPTION_FORMERRORS).json()
response.add('pins', [ pins[p].json() for p in pins ] )
#return response.queryset( Pin.objects.filter() ).json()
ipins = {}
for language,l in settings.LANGUAGES:
try:
ipin = Pin( title=form.cleaned_data[ 'title_%s' % language ], language=language, slug=form.cleaned_data[ 'slug' ], permalink=form.cleaned_data['permalink'] )
ipin.save()
except IntegrityError, e:
return response.throw_error( error={'slug':"Exception %s" % e}, code=API_EXCEPTION_INTEGRITY).json()
if len(pages) > 0:
pages[ language ].pins.add( ipin )
pages[ language ].save()
if len(pins) > 0:
ipin.parent = pins[ language ]
ipin.save()
response.add('object',[ p.json() for p in ipins ])
return response.queryset( Pin.objects.filter() ).json()
def pin( request, pin_id ):
# @todo: check pin permissions
response = Epoxy( request )
if response.method == 'POST':
form = EditPinForm( request.REQUEST )
if form.is_valid():
try:
pin = Pin.objects.get( id=pin_id )
pin.title = form.cleaned_data['title']
pin.abstract = form.cleaned_data['abstract']
pin.content = form.cleaned_data['content']
pin.save()
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
else:
return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json()
elif response.method=='DELETE':
try:
Pin.objects.filter(slug=Pin.objects.get(id=pin_id).slug).delete()
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
return response.json()
return response.single( Pin, {'id':pin_id} ).json()
def pin_by_slug( request, pin_slug, pin_language ):
return Epoxy( request ).single( Pin, {'slug':pin_slug,'language':pin_language} ).json()
def pin_by_parmalink( request ):
return Epoxy( request ).single( Pin, {'slug':pin_slug,'language':pin_language} ).json()
def pin_alchemy( request, pin_id ):
response = Epoxy( request )
try:
pin = Pin.objects.get( id=pin_id )
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
# check alchemy availability
from glue import AlchemyAPI
ao = AlchemyAPI.AlchemyAPI()
try:
ao.setAPIKey( settings.ALCHEMY_API_KEY )
except AttributeError, e:
return response.throw_error( error="You didn't specify an AlchemyAPI key", code=API_EXCEPTION_DOESNOTEXIST).json()
# Has it alchemy tags already?
if len( pin.permalink) > 0 :
# if len( pin.permalink > 0) :
result = response.add( "URLGetTitle", ao.URLGetTitle( pin.permalink ) );
result = response.add( "URLGetRankedNamedEntities", ao.URLGetRankedNamedEntities( pin.permalink ) );
result = response.add( "URLGetRankedConcepts", ao.URLGetRankedConcepts( pin.permalink ) );
#except Exception, e:
# return response.throw_error( error="%s" % e, code=API_EXCEPTION_OSERROR ).json()
return response.json();
def pin_tags( request, pin_id ):
response = Epoxy( request )
# get pins
try:
pins = Pin.objects.get( id=pin_id )
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
if response.method == 'POST':
form = AddTagForm( request.REQUEST )
if not form.is_valid():
return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS ).json()
return response.queryset( Tag.objects.filter( pin__id=pin_id) ).json()
def publish_pin( request, pin_id ):
response = Epoxy( request )
new_status = request.POST.get("new_status")
try:
if new_status in dict(Pin.PIN_STATUS_CHOICES):
p=Pin.objects.get( id=pin_id )
p.status=new_status
p.save()
return response.json()
else:
return response.throw_error( error="status %s incorrect" % new_status).json()
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
def pin_clean( request, pin_id ):
import re
response = Epoxy( request )
try:
pin = Page.objects.get( id = pin_id )
except Pin.DoesNotExist, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST).json()
e = re.compile( r'<span *[^>]*>(.*)</span *>')
pin.content = e.sub( r"\1", pin.content )
pin.save()
response.add( 'object', pin, jsonify=True)
return response.json()
def pin_upload( request ):
response = Epoxy( request )
d = dt.datetime.now()
form = UploadPinForm( request.REQUEST )
if not form.is_valid():
return response.throw_error(error=form.errors, code=API_EXCEPTION_FORMERRORS)
if len(form.cleaned_data['page_slug']) > 0:
# attacch new pin to a selected page (both languages)
response.add('page_slug',form.cleaned_data['page_slug'])
try:
page_en = Page.objects.get( slug=form.cleaned_data['page_slug'],language='EN')
page_fr = Page.objects.get( slug=form.cleaned_data['page_slug'],language='FR')
except Page.DoesNotExist:
return response.throw_error( error=_("selected page does not exists"), code=API_EXCEPTION_FORMERRORS).json()
response.add('page', [ page_en.json(), page_fr.json() ] )
pin_path = response.add('path', "pins/%s-%s" % ( d.year, ( d.month if d.month >10 else "0%s" % d.month ) ) )
absolute_pin_path = os.path.join( settings.MEDIA_ROOT, pin_path )
try:
if not os.path.exists( absolute_pin_path ):
os.makedirs( absolute_pin_path ) # throw an OS ERROR if exists... OR if it is not writable!
except OSError, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_OSERROR ).json()
for f in request.FILES.getlist('files[]'):
if f.size == 0:
return response.throw_error( error="uploaded file is empty", code=API_EXCEPTION_EMPTY ).json()
filename = os.path.join( absolute_pin_path, f.name)
pinup = open( filename , 'w' )
for chunk in f.chunks():
pinup.write( chunk )
pinup.close()
# guess mimetype
pin_mimetype = mimetypes.guess_type( filename )[0]
filetitle, extension = os.path.splitext( f.name )
try:
p_en = Pin( title=filetitle, language='EN', slug=slugify( "-".join( filetitle.split("_") ) ), mimetype=pin_mimetype, local=os.path.join( pin_path, os.path.basename( filename ) ) )
p_fr = Pin( title=filetitle, language='FR', slug=slugify( "-".join( filetitle.split("_") ) ), mimetype=pin_mimetype, local=os.path.join( pin_path, os.path.basename( filename ) ) )
p_en.save()
p_fr.save()
except IntegrityError, e:
return response.throw_error( error="%s" % e, code=API_EXCEPTION_INTEGRITY ).json()
if page_en and page_fr:
page_en.pins.add( p_en )
page_fr.pins.add( p_fr )
page_en.save()
page_fr.save()
return response.json()