Skip to content

Commit

Permalink
Merge pull request #1238 from LibraryOfCongress/Project-Move-1227
Browse files Browse the repository at this point in the history
Remove the bot3 changes
  • Loading branch information
rstorey authored Aug 14, 2020
2 parents 295629d + 2e2a64d commit a8a29a5
Showing 1 changed file with 0 additions and 50 deletions.
50 changes: 0 additions & 50 deletions concordia/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import logging
from urllib.parse import urljoin

import boto3
import boto3.session
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
Expand Down Expand Up @@ -240,53 +237,6 @@ def get_urls(self):

return custom_urls + urls

def save_model(self, request, obj, form, change):

old_campaign = Project.objects.get(pk=obj.pk).campaign.slug
old_project = Project.objects.get(pk=obj.pk).slug

super().save_model(request, obj, form, change)
# call boto3 api to move project files from older campaign to newer campaign
try:

new_campaign = obj.campaign.slug
new_project = obj.slug
bucket_name = getattr(settings, "S3_BUCKET_NAME", None)
session = boto3.session.Session()
s3_resource = session.resource("s3")
top_level_bucket = s3_resource.Bucket(bucket_name)
filter_from = old_campaign + "/" + old_project
if old_campaign != new_campaign or old_project != new_project:

for obj in top_level_bucket.objects.filter(
Prefix=filter_from + "/"
).all():

s3_resource.Object(
bucket_name,
obj.key.replace(old_campaign, new_campaign).replace(
old_project, new_project
),
).copy_from(CopySource=bucket_name + "/" + obj.key)

res = []
bucket = s3_resource.Bucket(bucket_name)
for obj_version in bucket.object_versions.filter(
Prefix=filter_from + "/"
).all():
res.append(
{"Key": obj_version.object_key, "VersionId": obj_version.id}
)

if res != []:
bucket.delete_objects(Delete={"Objects": res})

except Exception:
logger.exception(
"Unhandled exception saving from %s to %s", new_campaign, new_project
)
raise

@method_decorator(permission_required("concordia.add_campaign"))
@method_decorator(permission_required("concordia.change_campaign"))
@method_decorator(permission_required("concordia.add_project"))
Expand Down

0 comments on commit a8a29a5

Please sign in to comment.