Advertisement
Guest User

Parse Storage

a guest
Feb 26th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. from django.core.files.storage import Storage
  2. from django.conf import settings
  3. import json, httplib, os      
  4.  
  5. import logging                
  6.  
  7. logger = logging.getLogger('project.parse')
  8.  
  9. class ParseStorage(Storage):  
  10.   def __init__(self, option=None):
  11.     print "Initializing a ParseStorage"
  12.     logger.debug("Initializing a ParseStorage")
  13.  
  14.   def send(self, name, data):  
  15.     logger.debug("ParseStorage sending %s" % name)
  16.     connection = httplib.HTTPSConnection('api.parse.com', 443)
  17.     connection.connect()      
  18.     connection.request('POST', '/1/files/%s' % name, data, {
  19.       "X-Parse-Application-Id": settings.PARSE_APPLICATION_ID,
  20.       "X-Parse-REST-API-Key": settings.PARSE_API_KEY,
  21.       "Content-Type": "image/jpeg"    
  22.     })
  23.     result = json.loads(connection.getresponse().read())
  24.     logger.debug("Send Result: %s" % result)
  25.     return result              
  26.      
  27.   def fetch(self, name):      
  28.     connection = httplib.HTTPSConnection('file.parse.com', 443)
  29.     connection.connect()      
  30.     connection.request('GET', name, '', {})
  31.     result = connection.getresponse().read()
  32.      
  33.   def delete(self, name):      
  34.     logger.debug("ParseStorage deleting %s" % name)
  35.     connection = httplib.HTTPSConnection('api.parse.com', 443)
  36.     connection.connect()      
  37.     connection.request('DELETE', '/1/files/%s' % name, '', {
  38.       "X-Parse-Application-Id": settings.PARSE_APPLICATION_ID,
  39.       "X-Parse-Master-Key": settings.PARSE_MASTER_KEY,
  40.     })
  41.     result = json.loads(connection.getresponse().read())
  42.      
  43.   def url(self, name):        
  44.     logger.debug("Getting URL for %s" % name)
  45.     return 'http://files.parse.com/gibberish/%s' % os.path.basename(name)
  46.      
  47.   def path(self, name):        
  48.     logger.debug("Getting path for %s" % name)
  49.     raise NotImplementedError  
  50.  
  51.   def exists(self, name):      
  52.     # Parse ensures that you can always add a file named "name"
  53.     # TODO: This has to check that the whatever exists, or everything gets uploaded *again*
  54.     logger.debug("Checking existence of: %s" % name)
  55.     return False              
  56.  
  57.   def listdir(self):          
  58.     logger.debug("Listing contents of directory")
  59.     raise NotImplementedError  
  60.  
  61.   def size(self, name):        
  62.     logger.debug("Checking size of file: %s" % name)
  63.     raise NotImplementedError  
  64.  
  65.   def _save(self, name, path):
  66.     logger.debug("ParseStorage saving %s at %s" % (name, path))
  67.     parse_name = self.send(os.path.basename(name), path)['name']
  68.     logger.debug("ParseStorage saved as %s" % parse_name)
  69.     return parse_name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement