Last active
June 15, 2020 08:46
-
-
Save andriyor/5a85d418e825ffdbd97852772f51225d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import migrations | |
from dropbox import Dropbox | |
from dropbox.exceptions import ApiError | |
from dropbox.stone_validators import ValidationError | |
from realestate.images.models import Image | |
import environ | |
env = environ.Env( | |
DEBUG=(bool, False) | |
) | |
environ.Env.read_env() | |
client = Dropbox(env('DROPBOX_OAUTH2_TOKEN')) | |
def migrate_dropbox_path_to_url(apps, schema_editor): | |
for mm in Image.objects.all(): | |
print(mm.id) | |
try: | |
file_link_metadata = client.sharing_create_shared_link_with_settings(mm.photo.name) | |
downloadable_url = file_link_metadata.url.replace('dl=0', 'dl=1') | |
mm.photo = downloadable_url | |
mm.save() | |
# for already shared files | |
except ApiError as e: | |
try: | |
url = e.error.get_shared_link_already_exists().get_metadata().url | |
downloadable_url = url.replace('dl=0', 'dl=1') | |
mm.photo = downloadable_url | |
mm.save() | |
except Exception: | |
pass | |
except ValidationError: | |
pass | |
class Migration(migrations.Migration): | |
dependencies = [ | |
] | |
operations = [ | |
migrations.RunPython(migrate_dropbox_path_to_url) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment