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

photo #145

Merged
merged 4 commits into from
Jan 1, 2024
Merged

photo #145

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from django.conf import settings
from . import views

# ...


urlpatterns = [

path("", views.index, name="index"),
Expand Down Expand Up @@ -40,3 +43,5 @@
#path("soldier-search/<str:surname>" , views.soldier_search , name="soldier-search" ),

]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
4 changes: 4 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@
}

STATIC_ROOT = BASE_DIR / "staticfiles"


MEDIA_ROOT = os.path.join(BASE_DIR, "cmp/media")
MEDIA_URL = "/media/"
66 changes: 66 additions & 0 deletions photo.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 8 additions & 1 deletion templates/cmp/soldier.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ <h4>Soldier Record</h4>
<tr><td>Company:</td><td>{{ soldier.soldierdeath.company }}</td></tr>
<tr><td>CWGC Link:</td><td><span style="font-size: 1.5em;"><a href="{{ soldier.soldierdeath.cwgc_url }}">{{ soldier.soldierdeath.cwgc_id }}</span></a></td></tr>
<tr><td>Cemetery:</td><td>{{ soldier.soldierdeath.cemetery }}, {{soldier.soldierdeath.cemetery.country }} <span style="font-size: 1.3em;">{{ soldier.soldierdeath.cemetery.country.flag }}</span>
<tr><td colspan="2"> {{ cemetery_map | safe }} </td></tr>
<tr><td colspan="2" style="text-align: center;"><img src="/media/{{ soldier.id}}/memorial/{{ soldier.id }}.jpg"</td></tr>
<tr><td colspan="2" style="text-align: center;">
<div style="width: 60%; height: auto; margin: auto;">
{{ cemetery_map | safe }} </td></tr>
</div>
</td>
</tr>

{% endif %}

</tbody>
Expand Down
Loading