Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix static files deletion #2526

Merged
merged 10 commits into from Aug 25, 2020
Merged

Conversation

quaxsze
Copy link
Collaborator

@quaxsze quaxsze commented Aug 18, 2020

No description provided.

@quaxsze
Copy link
Collaborator Author

quaxsze commented Aug 18, 2020

Custom migration script

from udata.app import create_app, standalone
from udata.models import Dataset, CommunityResource
from urllib.parse import urlparse


def main():
    app = create_app()
    standalone(app)
    with app.app_context():
        print('Processing resources and community resources.')

        datasets = Dataset.objects().no_cache().timeout(False)
        for dataset in datasets:
            save_res = False
            for resource in dataset.resources:
                if resource.url.startswith('https://www.data.gouv.fr/_uploads/'):
                    parsed = urlparse(resource.url)
                    url_file_path = parsed.path.replace('/_uploads/', '')
                    fs_name = parsed.path.replace('/_uploads/resources/', '')
                    resource.url = f'https://static.data.gouv.fr/{url_file_path}'
                    resource.fs_filename = fs_name
                    save_res = True
                elif resource.url.startswith('https://www.data.gouv.fr/s/'):
                    parsed = urlparse(resource.url)
                    url_file_path = parsed.path.replace('/s/', '')
                    fs_name = parsed.path.replace('/s/resources/', '')
                    resource.url = f'https://static.data.gouv.fr/{url_file_path}'
                    resource.fs_filename = fs_name
                    save_res = True
            if save_res:
                try:
                    dataset.save()
                except Exception as e:
                    print(e)
                    pass

        community_resources = CommunityResource.objects().no_cache().timeout(False)
        for community_resource in community_resources:
            save_res = False
            if community_resource.url.startswith('https://www.data.gouv.fr/_uploads/'):
                parsed = urlparse(community_resource.url)
                url_file_path = parsed.path.replace('/_uploads/', '')
                fs_name = parsed.path.replace('/_uploads/resources/', '')
                community_resource.url = f'https://static.data.gouv.fr/{url_file_path}'
                community_resource.fs_filename = fs_name
                save_res = True
            elif community_resource.url.startswith('https://www.data.gouv.fr/s/'):
                parsed = urlparse(community_resource.url)
                url_file_path = parsed.path.replace('/s/', '')
                fs_name = parsed.path.replace('/s/resources/', '')
                community_resource.url = f'https://static.data.gouv.fr/{url_file_path}'
                community_resource.fs_filename = fs_name
                save_res = True
            if save_res:
                try:
                    community_resource.save()
                except Exception as e:
                    print(e)
                    pass


if __name__ == "__main__":
    main()

@quaxsze quaxsze requested a review from abulte August 18, 2020 10:45
@quaxsze quaxsze merged commit c2c4fad into opendatateam:master Aug 25, 2020
@quaxsze quaxsze deleted the fixStaticDeletion branch August 25, 2020 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants