diff --git a/cmp/urls.py b/cmp/urls.py index 1594e03..a7f6dd3 100644 --- a/cmp/urls.py +++ b/cmp/urls.py @@ -3,6 +3,9 @@ from django.conf import settings from . import views +# ... + + urlpatterns = [ path("", views.index, name="index"), @@ -40,3 +43,5 @@ #path("soldier-search/" , views.soldier_search , name="soldier-search" ), ] + +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file diff --git a/core/settings.py b/core/settings.py index f0e5207..a90ecc9 100644 --- a/core/settings.py +++ b/core/settings.py @@ -185,3 +185,7 @@ } STATIC_ROOT = BASE_DIR / "staticfiles" + + +MEDIA_ROOT = os.path.join(BASE_DIR, "cmp/media") +MEDIA_URL = "/media/" diff --git a/photo.py b/photo.py new file mode 100644 index 0000000..773be26 --- /dev/null +++ b/photo.py @@ -0,0 +1,66 @@ +import os +import django +import zipfile +import logzero +import pathlib + +from logzero import logger + + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') # replace 'myproject.settings' with your settings module +django.setup() + +from cmp.models import Soldier +from cmp.models import SoldierDeath + +# create a data class for soldiers + +class SoldierData: + def __init__(self, surname, initials, army_number, rank, notes): + self.id = id + self.army_number = army_number + self.photo_in_original = photo_in_original + + +def main(): + + origin_archive = '/Users/gm3dmo/src/old-cmp/grave-images-2023-12-31.zip' + + target_archive = "/Users/gm3dmo/src/old-cmp/memorial-image-by-soldier-id.zip" + # createa writeable zip archive called target_archive + + with zipfile.ZipFile(origin_archive, 'r') as zip_ref: + zip_contents = zip_ref.namelist() + counter = 0 + for item in zip_contents: + if item.endswith('.jpg'): + filename = os.path.basename(item) + filename = pathlib.Path(filename).stem + filename = filename.replace("_","/") + soldiers = Soldier.objects.filter(army_number=filename) + for soldier in soldiers: + logger.info(f"filename: {filename}") + logger.info(f"item: {item}") + logger.info(f"{counter} Soldier: {soldier.army_number} == {filename}") + soldier.photo_in_original = True + counter += 1 + new_filename = f"media/{soldier.id}/memorial/{soldier.id}.jpg" + directory_path = os.path.dirname(new_filename) + os.makedirs(directory_path, exist_ok=True) + file_bytes = zip_ref.read(item) + logger.info(f"len: {len(file_bytes)}") + with open(new_filename, 'wb') as f: + f.write(file_bytes) + + + + with zipfile.ZipFile(target_archive, 'w') as zip_ref: + for foldername, subfolders, filenames in os.walk('media'): + for filename in filenames: + zip_ref.write(os.path.join(foldername, filename)) + + + logger.info(f"Total number of soldiers: {counter}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/templates/cmp/soldier.html b/templates/cmp/soldier.html index 5a83a10..524ff1c 100644 --- a/templates/cmp/soldier.html +++ b/templates/cmp/soldier.html @@ -38,7 +38,14 @@

Soldier Record

Company:{{ soldier.soldierdeath.company }} CWGC Link:{{ soldier.soldierdeath.cwgc_id }} Cemetery:{{ soldier.soldierdeath.cemetery }}, {{soldier.soldierdeath.cemetery.country }} {{ soldier.soldierdeath.cemetery.country.flag }} - {{ cemetery_map | safe }} + + +
+ {{ cemetery_map | safe }} +
+ + + {% endif %}