Skip to content

Commit

Permalink
added basic api
Browse files Browse the repository at this point in the history
  • Loading branch information
randomknowledge committed Mar 9, 2013
1 parent 307080f commit f78c2dd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
60 changes: 60 additions & 0 deletions django_webvideo/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# coding=utf-8
from tastypie.cache import SimpleCache
from tastypie.exceptions import Unauthorized
from tastypie.resources import ModelResource
from tastypie import fields
from tastypie.authentication import SessionAuthentication
from tastypie.authorization import DjangoAuthorization
from tastypie.serializers import Serializer
from tastypie.throttle import CacheThrottle
from django_webvideo.models import WebVideo, ConvertedVideo


class WebVideoAuthorization(DjangoAuthorization):
pass


class MultipartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.META.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultipartResource, self).deserialize(request, data, format)

def put_detail(self, request, **kwargs):
if not hasattr(request, '_body'):
request._body = ''
return super(MultipartResource, self).put_detail(request, **kwargs)


class ConvertedVideoResource(ModelResource):
class Meta:
queryset = ConvertedVideo.objects.all()
resource_name = 'converted'
excludes = ['status', ]


class WebVideoResource(MultipartResource, ModelResource):
video = fields.FileField(attribute="video")
versions = fields.OneToManyField(ConvertedVideoResource, 'converted', full=True, null=True, blank=True)

class Meta:
queryset = WebVideo.objects.all()
resource_name = 'video'
fields = ['id', 'versions', 'duration', 'video', ]
allowed_methods = ['get', 'post', ]

filtering = {
'duration': ['exact', 'gt', 'gte', 'lt', 'lte', 'range'],
}

authentication = SessionAuthentication()
authorization = WebVideoAuthorization()
cache = SimpleCache(timeout=300)
throttle = CacheThrottle(throttle_at=50, timeframe=60)
serializer = Serializer(formats=['json',])
1 change: 1 addition & 0 deletions django_webvideo/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'django.contrib.staticfiles',
'south',

'tastypie',
'easy_thumbnails',
'django_webvideo',
]
Expand Down
10 changes: 9 additions & 1 deletion django_webvideo/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# coding=utf-8
from django.conf import settings
from django.conf.urls import patterns, url, include
from tastypie.api import Api
from django_webvideo.api import WebVideoResource

v1_api = Api(api_name='v1')
v1_api.register(WebVideoResource())


urlpatterns = patterns(
'',
url(r'^test/?$', 'django_webvideo.views.test', name='test')
url(r'^test/?$', 'django_webvideo.views.test', name='test'),
url(r'^upload_test/?$', 'django_webvideo.views.upload_test', name='upload_test'),
(r'^api/', include(v1_api.urls)),
)

if settings.DEBUG:
Expand Down
7 changes: 7 additions & 0 deletions django_webvideo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ def test(request):
{
'video': video
}
)


def upload_test(request):
return render(
request,
'django_webvideo/upload_test.html',
)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Django==1.5
South==0.7.6
PIL==1.1.7
easy-thumbnails==1.2
rq==0.3.7
rq==0.3.7
django-tastypie==0.9.12
1 change: 1 addition & 0 deletions requirements_prod.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-r requirements.txt
gunicorn
raven
django-redis-cache

0 comments on commit f78c2dd

Please sign in to comment.