Skip to content

Instantly share code, notes, and snippets.

@willcalderbank
Created September 22, 2014 15:06
Show Gist options
  • Save willcalderbank/9cf186bbb7e4c8f69221 to your computer and use it in GitHub Desktop.
Save willcalderbank/9cf186bbb7e4c8f69221 to your computer and use it in GitHub Desktop.
S3Property
class S3Property(db.StringProperty):
"""
On a put the value in the property is moved from the temp bucket to the
stated permanent one.
get_value_for_datastore is called as the model instance is placed in the
datastore, at this point we interrupt to process and move the asset before
setting the assets new url in the datastore. get_value_for_datastore doesnt
effect the model
"""
def __init__(self, bucket, *args, **kwargs):
super(S3Property, self).__init__(*args, **kwargs)
self.bucket = bucket
# This will hold the new url (the value stored in the db) for the asset
self.new_url = None
def __get__(self, model_instance, model_class):
if self.new_url is not None:
return self.new_url
return super(S3Property, self).__get__(model_instance, model_class)
def __set__(self, model_instance, value):
# We clear the new url as we are setting a new one
self.new_url = None
# Raise an error if its not an s3 url
if not (
's3-external-3.amazonaws.com/' in value
):
raise db.BadValueError(
'Property %s must be a url of a resource in a s3 bucket, not %s'
% (self.name, value)
)
super(S3Property, self).__set__(model_instance, value)
def get_value_for_datastore(self, model_instance):
# Get the current url (that would be the one that is placed in this
# field before saving the instance)
url = super(S3Property, self).get_value_for_datastore(model_instance)
# If self.bucket isnt in the url then the resource isnt in that bucket
# and has to be moved
if not (
'%s.s3-external-3.amazonaws.com/' % self.bucket in url
):
# Work out the location of the resource
url = url.split('/')
domain = url[2]
bucket_name = domain.split('.')[0]
file_name = '/'.join(url[3:])
# # Move the asset
self.new_url = s3.move_asset(
bucket_name,
urllib.unquote(file_name),
self.bucket
)
return self.new_url
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment