Skip to content
Snippets Groups Projects
Commit 7f73d5ef authored by SESE JULIA's avatar SESE JULIA
Browse files

WIP: command for items's images import

parent 8f8e1a24
Branches main
No related merge requests found
from django.core.management import BaseCommand
from django.conf import settings
import os
import re
from s3_local_endpoint import s3_local_endpoint as s3
class Command(BaseCommand):
"""call me : python manage.py make_images --source
--source directory source
"""
def get_images_names(self, source):
"""get images names from source directoy
"""
images_names = os.listdir(source)
n=4
for img in images_names:
# get Unistra number
img_number = re.match(r'^((?:[^.]*.){%d}[^.]*).(.*)' % (n-1), img)
img_name = img_number.groups()[0] + ".jpg"
img_path = source + "/" + img
src = source + "/" + img_name
os.rename(img_path, src)
self.s3_upload(src)
def s3_upload(self, src):
"""
" Upload a file to distant store via S3 gateway
" :attr The local file to put on store
"""
s3_client = s3.s3_local_endpoint(
getattr(settings, 'AWS_ACCESS_KEY_ID', ''),
getattr(settings, 'AWS_SECRET_ACCESS_KEY', ''),
getattr(settings, 'AWS_S3_ENDPOINT_URL', ''),
getattr(settings, 'AWS_STORAGE_BUCKET_NAME', ''))
if(src not in s3_client.list_files()):
s3_client.upload_file(src)
def add_arguments(self, parser):
parser.add_argument(
'--source', type=str)
def handle(self, *args, **options):
source = options.get('source')
self.get_images_names(source=source)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment