diff --git a/cmp/forms.py b/cmp/forms.py index 53a4494..e8424e4 100644 --- a/cmp/forms.py +++ b/cmp/forms.py @@ -12,6 +12,7 @@ from .models import SoldierDeath from .models import Company from .models import Decoration +from .models import Acknowledgement class CustomUserCreationForm(UserCreationForm): @@ -43,6 +44,11 @@ class Meta: model = Country fields = "__all__" +class editAcknowledgementForm(forms.ModelForm): + class Meta: + model = Acknowledgement + fields = "__all__" + class editCompanyForm(forms.ModelForm): class Meta: diff --git a/cmp/models.py b/cmp/models.py index e290437..4d0cdda 100644 --- a/cmp/models.py +++ b/cmp/models.py @@ -227,6 +227,14 @@ def __str__(self): return self.pow_camp.name +class Acknowledgement(models.Model): + surname = models.CharField(max_length=50, blank=True) + name = models.CharField(max_length=50, blank=True) + notes = models.TextField(max_length=50000, blank=True) + def __str__(self) -> str: + return self.surname + + class SoldierDecoration(models.Model): soldier = models.ForeignKey( Soldier, null=False, blank=False, on_delete=models.CASCADE diff --git a/cmp/urls.py b/cmp/urls.py index 7a74514..9f6ce8c 100644 --- a/cmp/urls.py +++ b/cmp/urls.py @@ -64,9 +64,14 @@ path("mgmt/ranks/edit/", views.edit_ranks, name="edit-ranks"), path('mgmt/ranks/search/', views.search_ranks, name='search-ranks'), + # Acknowledgements + #path("mgmt/acknowledgements", views.edit_acknowledgements, name="edit-acknowledgement"), + path("mgmt/acknowledgements//", views.detail_acknowledgements, name="acknowledgements"), + path("mgmt/acknowledgements/edit/", views.edit_acknowledgements, name="edit-acknowledgements"), + path('mgmt/acknowledgements/search/', views.search_acknowledgements, name='search-acknowledgements'), # Soldiers - path('soldiers/', views.soldiers, name='soldiersrn'), + path('soldiers/', views.soldiers, name='soldiers'), # Soldier management path("mgmt/soldiers", views.edit_soldiers, name="soldiersa"), diff --git a/cmp/views.py b/cmp/views.py index bee74c0..10d4b23 100644 --- a/cmp/views.py +++ b/cmp/views.py @@ -4,6 +4,9 @@ from django.contrib.auth.decorators import login_required +from .models import Acknowledgement +from cmp.forms import editAcknowledgementForm + from .models import Country from cmp.forms import editCountryForm @@ -386,6 +389,18 @@ def edit_soldiers(request, soldier_id): return render(request, "cmp/edit-soldiers.html", {"form": form, 'death_form': death_form}) +def search_acknowledgements(request): + query = request.GET.get('q') + page_number = request.GET.get('page') + if query: + acknowledgements = Acknowledgement.objects.filter(name__icontains=query).order_by('surname') + else: + acknowledgements = Acknowledgement.objects.all().order_by('surname') + paginator = Paginator(acknowledgements, settings.PAGE_SIZE) + page_obj = paginator.get_page(page_number) + return render(request, 'cmp/search-acknowledgements.html', {'page_obj': page_obj}) + + def search_ranks(request): query = request.GET.get('q') page_number = request.GET.get('page') @@ -471,6 +486,12 @@ def search_countries(request): return render(request, 'cmp/search-countries.html', {'page_obj': page_obj}) +def detail_acknowledgements(request, acknowledgement_id): + # get or return a 404 + rank = get_object_or_404(Rank, pk=rank_id) + return render(request, "cmp/detail-ranks.html", {"rank": rank}) + + def detail_ranks(request, rank_id): # get or return a 404 rank = get_object_or_404(Rank, pk=rank_id) @@ -523,6 +544,18 @@ def edit_ranks(request, rank_id): form.save() return HttpResponse("Rank Added") return render(request, "cmp/edit-ranks.html", {"form": form}) + + +def edit_acknowledgements(request, acknowledgement_id): + post = request.POST + form = editAcknowledgementForm(post or None) + if acknowledgement_id: + acknowledgement = Acknowledgement.objects.get(id=acknowledgement_id) + form = editAcknowledgementForm(post or None, instance=acknowledgement) + if post and form.is_valid(): + form.save() + return HttpResponse("Acknowledgement Added") + return render(request, "cmp/edit-acknowledgements.html", {"form": form}) def edit_cemeteries(request, cemetery_id): diff --git a/insertall.sh b/insertall.sh index 0a4b71f..a9c8417 100755 --- a/insertall.sh +++ b/insertall.sh @@ -10,3 +10,5 @@ python manage.py runscript insert-all-soldiers python manage.py runscript insert-all-soldier-imprisonments python manage.py runscript insert-all-soldier-deaths python manage.py runscript insert-all-soldier-decorations +python manage.py runscript insert-all-acknowledgements + diff --git a/photo.py b/photo.py index 773be26..a5c79d4 100644 --- a/photo.py +++ b/photo.py @@ -24,7 +24,7 @@ def __init__(self, surname, initials, army_number, rank, notes): def main(): - origin_archive = '/Users/gm3dmo/src/old-cmp/grave-images-2023-12-31.zip' + origin_archive = '/Users/gm3dmo/src/old-cmp/grave-images-2024-07-29.zip' target_archive = "/Users/gm3dmo/src/old-cmp/memorial-image-by-soldier-id.zip" # createa writeable zip archive called target_archive @@ -44,7 +44,7 @@ def main(): 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" + new_filename = f"{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) @@ -63,4 +63,4 @@ def main(): logger.info(f"Total number of soldiers: {counter}") if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/scripts/delete-all-acknowledgements.py b/scripts/delete-all-acknowledgements.py new file mode 100644 index 0000000..34433e4 --- /dev/null +++ b/scripts/delete-all-acknowledgements.py @@ -0,0 +1,5 @@ +from cmp.models import Acknowledgement + +def run(): + Acknowledgements = Acknowledgement.objects.all() + Acknowledgements.delete() diff --git a/scripts/insert-all-acknowledgements.py b/scripts/insert-all-acknowledgements.py new file mode 100644 index 0000000..676b9f0 --- /dev/null +++ b/scripts/insert-all-acknowledgements.py @@ -0,0 +1,38 @@ + +def run(): + + import sys + import urllib3 + import csv + import time + from cmp.models import Acknowledgement + + print() + title = sys.argv[2] + + start_fetch_time = time.time() + ref_data_url = "https://raw.githubusercontent.com/gm3dmo/old-cmp/main/data/acknowledgement.csv" + http = urllib3.PoolManager() + r = http.request('GET', ref_data_url) + end_fetch_time = time.time() + # load the response into a csv dictionary reader + reader = csv.DictReader(r.data.decode('utf-8').splitlines()) + + start_insert_time = time.time() + for row in reader: + #print(row['name']) + try: + Acknowledgement.objects.create( + id=row['id'], + surname=row['surname'], + name=row['name'], + notes=row['notes'] + ) + except Exception as e: + print(f"""💥row: ({row}) """) + raise e + + end_insert_time = time.time() + time_to_fetch = end_fetch_time - start_fetch_time + time_to_insert = end_insert_time - start_insert_time + print(f"""\033[4;33m{title}\033[0m Fetch table response code: {r.status} time (seconds) to fetch: {time_to_fetch:.2f} time to insert {time_to_insert:.2f}""") diff --git a/templates/cmp/acknowledgements.html b/templates/cmp/acknowledgements.html new file mode 100644 index 0000000..5f25299 --- /dev/null +++ b/templates/cmp/acknowledgements.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Acknowledgements{% endblock %} +{% block content %} + +
+

Acknowledgements

+ + + + + + + + + + + {% for person in acknowledgements |dictsort:"surname" %} + + + + + {% endfor %} + +

Name

Tasks

{{ person.surname }} + {{ person.notes }} + + +
+{% endblock %} +
\ No newline at end of file diff --git a/templates/cmp/edit-acknowledgements.html b/templates/cmp/edit-acknowledgements.html new file mode 100644 index 0000000..5764dc9 --- /dev/null +++ b/templates/cmp/edit-acknowledgements.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Acknowledgement Form{% endblock %} +{% block content %} + +

Edit Acknowledgement

+
+
+ {% csrf_token %} + {{ form.management_form }} + {{ form.errors }} + {{form | crispy}} + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/cmp/mgmt-index.html b/templates/cmp/mgmt-index.html index 763d50f..5b03c30 100644 --- a/templates/cmp/mgmt-index.html +++ b/templates/cmp/mgmt-index.html @@ -13,6 +13,7 @@

Management Index

  • Decorations
  • Ranks
  • Prisoner of War Camps
  • +
  • Acknowledgements
  • {% endblock %}
    \ No newline at end of file diff --git a/templates/cmp/search-acknowledgements.html b/templates/cmp/search-acknowledgements.html new file mode 100644 index 0000000..155b113 --- /dev/null +++ b/templates/cmp/search-acknowledgements.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Acknowledgements{% endblock %} +{% block content %} + +
    +

    Search Acknowledgements

    + +
    +
    + + +
    +
    + +
    + + + + + + + + {% for acknowledgement in page_obj%} + + + + + + + {% endfor %} +
    SurnameNameNotes
    {{ acknowledgement.surname}}{{ acknowledgement.name}}{{ acknowledgement.notes}}
    +
    + +{% include 'cmp/pagination-footer.html' %} + +{% endblock %} +
    \ No newline at end of file