-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.py
79 lines (59 loc) · 2.39 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from django.core.files import File
from .models import *
# local_folder = sys.args[1]
# if len(sys.args) == 1:
# local_folder = "~/Documents/06_Development/03_GRIDH/Utils_EtruscanTombs/SG_tombs_data_dump/Test_data_dump/"
def get_or_none(classmodel: models.Model, **kwargs):
try:
return classmodel.objects.get(**kwargs)
except classmodel.DoesNotExist:
return None
except ValueError:
return None
except classmodel.MultipleObjectsReturned:
return None
def fetch_tomb_id_from_name(filename):
tomb_name, _ = filename.split("_")
tomb = Place.objects.get(name=tomb_name)
return tomb.id
def upload_image(filename):
try:
# if format isn't recognized, return nothing
tomb_name, author, creation_date, image_type, identifier = filename.split("_")
except:
try:
tomb_name, author, creation_date, image_type, identifier, hdr = filename.split("_")
except:
return
author_firstname, author_lastname = author.split("-")
tomb_name = str(int(tomb_name))
tomb = get_or_none(Place, **{"name": tomb_name})
author = get_or_none(Author, **{"firstname": author_firstname, "lastname": author_lastname})
image_type = get_or_none(TypeOfImage, **{"text": image_type})
print(f"Managing file {filename}")
fetch_existing_image = Image.objects.filter(file__icontains=filename)
if len(fetch_existing_image) == 0:
print(f"Uploading file {filename}")
image = Image(
author = author,
tomb = tomb,
file = os.path.join("etruscantombs/original", filename),
date = creation_date
) # title = f"Documentation {identifier}",
image.save()
image.type_of_image.add(image_type)
else:
print(f"Changing file {filename}")
fetch_existing_image.update(tomb=tomb)
def batch_upload(folder):
files = filter(lambda f: os.path.isfile(os.path.join(folder, f)), os.listdir(folder))
for imagepath in sorted(files):
file_name_proper, extension = imagepath.split(".")
try:
tomb_name = int(file_name_proper[:3])
is_tomb_file = isinstance(tomb_name, int)
except:
is_tomb_file = False
if extension == "jpg" and is_tomb_file:
upload_image(imagepath)