- Author:
- ivarne
- Posted:
- March 14, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
This is a simple module for use with django to make a per user dropbox access simple
Requirements: standard django authentication django sessions enabeled * dropbox python api
easy_install dropbox
To use this dropbox module you have to add the following configuration to your settings.py file
DROPBOX_SETTINGS = {
'app_key' : "insert key",
'app_secret' : "insert secret",
'type' : "app_folder",
}
and of course to include it in INSTALLED_APPS
INSTALLED_APPS = (
...,
'django_dropbox',
)
to make a table to store personal access tokens for your users run
python manage.py syncdb
In your views you can import the dropbox_user_required decorator to mark views that should recive the named parameter dropbox_client ` from django_dropbox.decorator import dropbox_user_required
@dropbox_user_required def myViewFunk(request, ..., dropbox_client): file = ... dropbox_client.put_file(file) `
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 | """
django view methods to interact with the dropbox api
"""
from dropbox import client, session
from dropbox.rest import ErrorResponse
from oauth.oauth import OAuthToken
from django.http import HttpResponseRedirect
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from models import DropboxExtra
DROPBOX_REQUEST_SESSION_KEY = 'dropbox_request_token3' # random temp storage name
def _saveUserToken(user, token):
try:
d = user.django_dropbox
except:
d = DropboxExtra()
d.user = user
d.dropbox_token = token
d.save()
def _dropboxConnect(request,sess):
request_token = sess.obtain_request_token()
request.session[DROPBOX_REQUEST_SESSION_KEY] = request_token
url = sess.build_authorize_url(request_token,request.build_absolute_uri())
return HttpResponseRedirect(url)
def dropbox_user_required(funk):
@login_required
def _dropbox_wrap(request, *args, **kwargs):
_keys = settings.DROPBOX_SETTINGS
sess = session.DropboxSession(_keys['app_key'], _keys['app_secret'],_keys['type'])
try:
if request.session.has_key(DROPBOX_REQUEST_SESSION_KEY):
sess.token = sess.obtain_access_token(request.session.pop(DROPBOX_REQUEST_SESSION_KEY))
_saveUserToken( request.user, sess.token )
else:
token = request.user.django_dropbox.dropbox_token
sess.token = OAuthToken.from_string(token)
c = client.DropboxClient(sess)
except ObjectDoesNotExist:
return _dropboxConnect(request, sess)
try:
return funk(request, *args, dropbox_client=c, **kwargs)
except ErrorResponse, e:
if e.status == 401:
_dropboxConnect(request, sess)# re authentication needed
else:
raise e # let django log the exception that the usier did not handle
return _dropbox_wrap
## and to store personal dropbox access tocens you need
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
class DropboxExtra(models.Model):
user = models.OneToOneField(User,related_name="django_dropbox")
dropbox_token = models.CharField(max_length=256)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.