Initial REST API version 0 alpha

This commit is contained in:
Roberto Rosario
2011-11-22 00:56:56 -04:00
parent ffcaec1e91
commit 4e8f2c370b
10 changed files with 139 additions and 4 deletions

View File

3
apps/rest_api/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,38 @@
from django.core.urlresolvers import reverse
from djangorestframework.resources import ModelResource
from documents.models import Document
from converter.exceptions import UnknownFileFormat, UnkownConvertError
class DocumentResourceSimple(ModelResource):
model = Document
fields = ('url', 'pk', 'document_type', 'uuid', 'date_added', 'description', 'tags', 'comments', 'expensive_methods', 'files')
def files(self, instance):
return [
{
'version': 1,
'mimetype': instance.file_mimetype,
'encoding': instance.file_mime_encoding,
'filename': instance.get_fullname(),
'date_updated': instance.date_updated,
'checksum': instance.checksum,
'size': instance.size,
'exists': instance.exists(),
'pages': [
{
'page_numer': page.page_number,
'page_label': page.page_label,
#'content':
}
for page in instance.documentpage_set.all()
]
}
]
def expensive_methods(self, instance):
return [
{'is_zoomable': reverse('documents-expensive-is_zoomable', args=[instance.pk])},
]

1
apps/rest_api/tests.py Normal file
View File

@@ -0,0 +1 @@

18
apps/rest_api/urls.py Normal file
View File

@@ -0,0 +1,18 @@
from django.conf.urls.defaults import patterns, url
from djangorestframework.views import ListModelView
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
from rest_api.views import APIBase, Version_0, ReadOnlyInstanceModelView, IsZoomable, Exists, Size
from rest_api.resources import DocumentResourceSimple
urlpatterns = patterns('',
url(r'^$', APIBase.as_view(), name='api-root'),
url(r'^v0/$', Version_0.as_view(), name='api-version-0'),
# Version 0 alpha API calls
url(r'^v0/document/(?P<pk>[0-9]+)/$', ReadOnlyInstanceModelView.as_view(resource=DocumentResourceSimple), name='documents-simple'),
url(r'^v0/document/(?P<pk>[0-9]+)/expensive/is_zoomable/$', IsZoomable.as_view(), name='documents-expensive-is_zoomable'),
url(r'^v0/document/(?P<pk>[0-9]+)/expensive/exists/$', IsZoomable.as_view(), name='documents-expensive-exists'),
url(r'^v0/document/(?P<pk>[0-9]+)/expensive/size/$', Size.as_view(), name='documents-expensive-size'),
)

69
apps/rest_api/views.py Normal file
View File

@@ -0,0 +1,69 @@
'''Views file for the rest_api app'''
import logging
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
from documents.models import Document
from converter.exceptions import UnknownFileFormat, UnkownConvertError
from djangorestframework.views import View, ModelView, ListModelView, InstanceModelView
from djangorestframework.mixins import InstanceMixin, ReadModelMixin
from djangorestframework.response import Response
from djangorestframework import status
logger = logging.getLogger(__name__)
class ReadOnlyInstanceModelView(InstanceModelView):
allowed_methods = ['GET']
class APIBase(View):
"""This is the REST API for Mayan EDMS (https://github.com/rosarior/mayan/).
All the API calls can be navigated either through the browser or from the command line...
bash: curl -X GET http://127.0.0.1:8000/api/ # (Use default renderer)
bash: curl -X GET http://127.0.0.1:8000/api/ -H 'Accept: text/plain' # (Use plaintext documentation renderer)
"""
def get(self, request):
return [
{'name': 'Version 0 Alpha', 'url': reverse('api-version-0')}
]
class Version_0(View):
def get(self, request):
return [
{'name': 'Resources', 'resources': ['documents/<pk>']}
]
class IsZoomable(View):
def get(self, request, pk):
logger.info('received is_zoomable call from: %s' % (request.META['REMOTE_ADDR']))
document = get_object_or_404(Document, pk=pk)
try:
document.get_image_cache_name(1) # TODO: page
return {'result': True}
except (UnknownFileFormat, UnkownConvertError):
return {'result': False}
class Exists(View):
def get(self, request, pk):
logger.info('received exists call from: %s' % (request.META['REMOTE_ADDR']))
document = get_object_or_404(Document, pk=pk)
return {'result': document.exists()}
class Size(View):
def get(self, request, pk):
logger.info('received size call from: %s' % (request.META['REMOTE_ADDR']))
document = get_object_or_404(Document, pk=pk)
return {'result': document.size}

View File

@@ -19,3 +19,4 @@ Pillow==1.7.4
cssmin==0.1.4 cssmin==0.1.4
django-compressor==1.1 django-compressor==1.1
-e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile -e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile
djangorestframework==0.2.3

View File

@@ -16,3 +16,4 @@ Pillow==1.7.4
cssmin==0.1.4 cssmin==0.1.4
django-compressor==1.1 django-compressor==1.1
-e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile -e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile
djangorestframework==0.2.3

View File

@@ -81,7 +81,8 @@ STATIC_URL = '/%s-static/' % PROJECT_NAME
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash. # trailing slash.
# Examples: "http://foo.com/media/", "/media/". # Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = STATIC_URL + 'grappelli/' #ADMIN_MEDIA_PREFIX = STATIC_URL + 'grappelli/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
# Make this unique, and don't share it with anybody. # Make this unique, and don't share it with anybody.
SECRET_KEY = 'om^a(i8^6&h+umbd2%pt91cj!qu_@oztw117rgxmn(n2lp^*c!' SECRET_KEY = 'om^a(i8^6&h+umbd2%pt91cj!qu_@oztw117rgxmn(n2lp^*c!'
@@ -117,7 +118,7 @@ TEMPLATE_DIRS = (
) )
INSTALLED_APPS = ( INSTALLED_APPS = (
'grappelli', #'grappelli',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
@@ -164,6 +165,8 @@ INSTALLED_APPS = (
'history', 'history',
'main', 'main',
'compressor', 'compressor',
'djangorestframework',
'rest_api',
) )
TEMPLATE_CONTEXT_PROCESSORS = ( TEMPLATE_CONTEXT_PROCESSORS = (
@@ -286,7 +289,7 @@ CELERY_DISABLE_RATE_LIMITS = True
#--------- Web theme --------------- #--------- Web theme ---------------
WEB_THEME_ENABLE_SCROLL_JS = False WEB_THEME_ENABLE_SCROLL_JS = False
#--------- Grappelli ---------------- #--------- Grappelli ----------------
GRAPPELLI_ADMIN_TITLE = PROJECT_TITLE #GRAPPELLI_ADMIN_TITLE = PROJECT_TITLE
#--------- Django ------------------- #--------- Django -------------------
LOGIN_URL = '/login/' LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/' LOGIN_REDIRECT_URL = '/'

View File

@@ -15,7 +15,7 @@ urlpatterns = patterns('',
(r'^tags/', include('tags.urls')), (r'^tags/', include('tags.urls')),
(r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)), (r'^admin/', include(admin.site.urls)),
(r'^grappelli/', include('grappelli.urls')), #(r'^grappelli/', include('grappelli.urls')),
(r'^sentry/', include('sentry.urls')), (r'^sentry/', include('sentry.urls')),
(r'^comments/', include('document_comments.urls')), (r'^comments/', include('document_comments.urls')),
(r'^user_management/', include('user_management.urls')), (r'^user_management/', include('user_management.urls')),
@@ -28,6 +28,7 @@ urlpatterns = patterns('',
(r'^sources/', include('sources.urls')), (r'^sources/', include('sources.urls')),
(r'^project_setup/', include('project_setup.urls')), (r'^project_setup/', include('project_setup.urls')),
(r'^project_tools/', include('project_tools.urls')), (r'^project_tools/', include('project_tools.urls')),
(r'^api/', include('rest_api.urls')),
) )