Skip to content

Instantly share code, notes, and snippets.

@gterzian
Last active February 22, 2017 09:22
Show Gist options
  • Save gterzian/6649526 to your computer and use it in GitHub Desktop.
Save gterzian/6649526 to your computer and use it in GitHub Desktop.
Django model for working with the Dropbox API
from django.db import models
from django.conf import settings
import dropbox
class RequestToken(models.Model):
key = models.CharField(max_length=100)
secret = models.CharField(max_length=100)
user = models.ForeignKey('auth.User')
class AccessToken(models.Model):
key = models.CharField(max_length=100)
secret = models.CharField(max_length=100)
user = models.ForeignKey('auth.User')
class DropBox(models.Model):
#a dropbox can belong to either a userprofile or a shop
userprofile = models.OneToOneField('main.UserProfile', null=True, blank=True)
#it is always belongs to an access_token, which ultimately belongs to a user
access_token = models.OneToOneField(AccessToken)
def __unicode__(self):
return 'dropbox from %s' % self.userprofile.user.first_name
@staticmethod
def copy_item(source, destination, item):
source_session = dropbox.session.DropboxSession(settings.DROPBOX_KEY, settings.DROPBOX_SECRET, settings.DROPBOX_ACCESS_TYPE)
source_session.set_token(source.access_token.key, source.access_token.secret)
source_client = dropbox.client.DropboxClient(source_session)
destination_session = dropbox.session.DropboxSession(settings.DROPBOX_KEY, settings.DROPBOX_SECRET, settings.DROPBOX_ACCESS_TYPE)
destination_session.set_token(destination.access_token.key, destination.access_token.secret)
destination_client = dropbox.client.DropboxClient(destination_session)
f = source_client.get_file(item.path)
d = destination_client.put_file(item.path, f.read())
destination.refresh_items()
def add_file(self, file, path):
session = dropbox.session.DropboxSession(settings.DROPBOX_KEY, settings.DROPBOX_SECRET, settings.DROPBOX_ACCESS_TYPE)
session.set_token(self.access_token.key, self.access_token.secret)
client = dropbox.client.DropboxClient(session)
d = client.put_file(path, file.read())
self.refresh_items()
def refresh_items(self):
#sync our local 'dropbox' with the real one, it's just the paths of files that need to be in sync
self.dropbox_session = dropbox.session.DropboxSession(settings.DROPBOX_KEY, settings.DROPBOX_SECRET, settings.DROPBOX_ACCESS_TYPE)
self.dropbox_session.set_token(self.access_token.key, self.access_token.secret)
self.client = dropbox.client.DropboxClient(self.dropbox_session)
paths = []
for item in self.client.metadata('/')['contents']:
DropBoxItem.objects.get_or_create(dropbox=self, path=item['path'])
paths.append(item['path'])
for item in DropBoxItem.objects.filter(dropbox=self):
if item.path not in paths:
item.delete()
class DropBoxItem(models.Model):
dropbox = models.ForeignKey(DropBox)
path = models.CharField(max_length=200)
def __unicode__(self):
path = self.path.replace('/', '')
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment