From 55aa7192485d93b2e38e0b73f936d9d7e03e49b4 Mon Sep 17 00:00:00 2001 From: David Morris Date: Mon, 3 Jun 2024 15:59:36 +0100 Subject: [PATCH] Adding companies and decorations to management screen --- cmp/forms.py | 13 ++++ cmp/urls.py | 13 +++- cmp/views.py | 96 +++++++++++++++++++-------- templates/cmp/detail-companies.html | 33 +++++++++ templates/cmp/edit-companies.html | 17 +++++ templates/cmp/edit-decorations.html | 17 +++++ templates/cmp/mgmt-index.html | 2 + templates/cmp/search-cemeteries.html | 2 + templates/cmp/search-companies.html | 35 ++++++++++ templates/cmp/search-decorations.html | 38 +++++++++++ 10 files changed, 236 insertions(+), 30 deletions(-) create mode 100644 templates/cmp/detail-companies.html create mode 100644 templates/cmp/edit-companies.html create mode 100644 templates/cmp/edit-decorations.html create mode 100644 templates/cmp/search-companies.html create mode 100644 templates/cmp/search-decorations.html diff --git a/cmp/forms.py b/cmp/forms.py index 3e576bb..53a4494 100644 --- a/cmp/forms.py +++ b/cmp/forms.py @@ -10,6 +10,8 @@ from .models import PowCamp from .models import Soldier from .models import SoldierDeath +from .models import Company +from .models import Decoration class CustomUserCreationForm(UserCreationForm): @@ -42,6 +44,17 @@ class Meta: fields = "__all__" +class editCompanyForm(forms.ModelForm): + class Meta: + model = Company + fields = "__all__" + +class editDecorationForm(forms.ModelForm): + class Meta: + model = Decoration + fields = "__all__" + + class editRankForm(forms.ModelForm): class Meta: model = Rank diff --git a/cmp/urls.py b/cmp/urls.py index c951337..a22257d 100644 --- a/cmp/urls.py +++ b/cmp/urls.py @@ -7,7 +7,6 @@ from .views import soldier_detail - urlpatterns = [ #path('soldier//', soldier_detail, name='soldier_detail'), @@ -37,6 +36,18 @@ path("mgmt/countries/edit/", views.edit_countries, name="edit-countries"), path("mgmt/countries/search/", views.search_countries, name='search-countries'), + # Companies + path("mgmt/companies", views.edit_companies, name="edit-companies"), + path("mgmt/companies//", views.detail_companies, name="companies"), + path("mgmt/companies/edit/", views.edit_companies, name="edit-companies"), + path("mgmt/companies/search/", views.search_companies, name='search-companies'), + + # Decorations + path("mgmt/decorations", views.edit_decorations, name="edit-decorations"), + path("mgmt/decorations//", views.detail_decorations, name="decorations"), + path("mgmt/decorations/edit/", views.edit_decorations, name="edit-decorations"), + path("mgmt/decorations/search/", views.search_decorations, name='search-decorations'), + # Cemeteries path("mgmt/cemeteries", views.edit_cemeteries, name="edit-cemeteries"), path("mgmt/cemeteries//", views.detail_cemeteries, name="cemeteries"), diff --git a/cmp/views.py b/cmp/views.py index 8c0d698..6024cb1 100644 --- a/cmp/views.py +++ b/cmp/views.py @@ -6,6 +6,12 @@ from .models import Country from cmp.forms import editCountryForm +from .models import Company +from cmp.forms import editCompanyForm + +from .models import Decoration +from cmp.forms import editDecorationForm + from .models import Rank from cmp.forms import editRankForm @@ -301,6 +307,30 @@ def edit_country(request, country_id): return render(request, "cmp/edit-countries.html", {"form": form}) +def edit_companies(request, company_id=None): + post = request.POST + form = editCompanyForm(post or None) + if company_id: + company = Company.objects.get(id=company_id) + form = editCompanyForm(post or None, instance=company) + if post and form.is_valid(): + form.save() + return HttpResponse("Company Added") + return render(request, "cmp/edit-companies.html", {"form": form}) + + +def edit_decorations(request, decoration_id=None): + post = request.POST + form = editDecorationForm(post or None) + if decoration_id: + decoration = Decoration.objects.get(id=decoration_id) + form = editDecorationForm(post or None, instance=decoration) + if post and form.is_valid(): + form.save() + return HttpResponse("Decoration Added") + return render(request, "cmp/edit-decorations.html", {"form": form}) + + def edit_countries(request, country_id=None): post = request.POST form = editCountryForm(post or None) @@ -380,6 +410,7 @@ def search_powcamps(request): powcamps = PowCamp.objects.all().order_by('name') return render(request, 'cmp/search-prisoner-of-war-camps.html', {'powcamps': powcamps}) + def search_soldiers(request): query = request.GET.get('q') page_number = request.GET.get('page') @@ -393,6 +424,30 @@ def search_soldiers(request): #return render(request, 'cmp/search-soldiers.html', {'soldiers': soldiers}) return render(request, 'cmp/search-soldiers.html', {'page_obj': page_obj}) + +def search_decorations(request): + query = request.GET.get('q') + page_number = request.GET.get('page') + if query: + decorations = Decoration.objects.filter(name__icontains=query).order_by('name') + else: + decorations = Decoration.objects.all().order_by('name') + paginator = Paginator(decorations, 25) + page_obj = paginator.get_page(page_number) + return render(request, 'cmp/search-decorations.html', {'page_obj': page_obj}) + + +def search_companies(request): + query = request.GET.get('q') + page_number = request.GET.get('page') + if query: + companies = Company.objects.filter(name__icontains=query).order_by('name') + else: + companies = Company.objects.all().order_by('name') + paginator = Paginator(companies, 15) + page_obj = paginator.get_page(page_number) + return render(request, 'cmp/search-companies.html', {'page_obj': page_obj}) + def search_countries(request): query = request.GET.get('q') @@ -428,6 +483,18 @@ def detail_countries(request, country_id): return render(request, "cmp/detail-countries.html", {"country": country}) +def detail_companies(request, company_id): + # get or return a 404 + company = get_object_or_404(Company, pk=company_id) + return render(request, "cmp/detail-companies.html", {"company": company}) + + +def detail_decorations(request, decoration_id): + # get or return a 404 + decoration = get_object_or_404(Decoration, pk=decoration_id) + return render(request, "cmp/detail-decorations.html", {"decoration": decoration}) + + def detail_soldiers(request, soldier_id): # get or return a 404 soldier = get_object_or_404(Soldier, pk=soldier_id) @@ -490,35 +557,6 @@ def soldier(request, soldier_id): context = { "soldier": soldier, "cemetery_map": cemetery_map } return render(request, "cmp/soldier.html", context) - -#def soldier_detail(request, soldier_id): -# """Gather together details about soldier""" -# s = Soldier.objects.get(id=soldier_id) -# soldier_record = { -# 's_surname': s.surname, -# #'s_initials': s.dot_initials(), -# 's_rank': s.rank, -# 's_armynumber': s.army_number, -# 's_notes': s.notes, -# } -# return soldier_record - - -#def index(request): -# if not request.POST: -# return render(request, 'cmp/index.html', {'soldiers': []}) -# """Search for soldier by surname or an army number""" -# print("DAVE") -# print(type(request)) -# print(request.method) -# # print the content of the POST request -# print(request.POST) -# print(request.POST.get('name')) -# surname = request.POST.get('name') -# -# soldiers = Soldier.objects.filter(surname__icontains=surname).order_by('surname') -# print(soldiers) -# return render(request, 'cmp/index.html', {'soldiers': soldiers}) def index(request): post = request.POST diff --git a/templates/cmp/detail-companies.html b/templates/cmp/detail-companies.html new file mode 100644 index 0000000..f57b171 --- /dev/null +++ b/templates/cmp/detail-companies.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Companies{% endblock %} +{% block content %} + +

{{ Company.name }} Details

+ +
+ + + + + + + + + + + + + + + + +
FlagNameAlpha2Alpha3Country #Edit
+ {{ company.name }} + + Edit + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/cmp/edit-companies.html b/templates/cmp/edit-companies.html new file mode 100644 index 0000000..dbf460a --- /dev/null +++ b/templates/cmp/edit-companies.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Decorations Form{% endblock %} +{% block content %} + +

Edit Decoration

+
+
+ {% csrf_token %} + {{ form.management_form }} + {{ form.errors }} + {{form | crispy}} + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/templates/cmp/edit-decorations.html b/templates/cmp/edit-decorations.html new file mode 100644 index 0000000..29ae2be --- /dev/null +++ b/templates/cmp/edit-decorations.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Companies Form{% endblock %} +{% block content %} + +

Edit Company

+
+
+ {% 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 67fe64a..9bfb540 100644 --- a/templates/cmp/mgmt-index.html +++ b/templates/cmp/mgmt-index.html @@ -9,6 +9,8 @@

Management Index

Working Search Pages

  • Soldiers
  • Countries
  • +
  • Companies
  • +
  • Decorations
  • Ranks
  • Cemeteries
  • Prisoner of War Camps
  • diff --git a/templates/cmp/search-cemeteries.html b/templates/cmp/search-cemeteries.html index 08d7a7c..03b8b7e 100644 --- a/templates/cmp/search-cemeteries.html +++ b/templates/cmp/search-cemeteries.html @@ -13,11 +13,13 @@

    Search Cemeteries

    +
    {% for cemetery in cemeteries %}

    {{ cemetery.name }}

    {% endfor %} +
    {% endblock %} \ No newline at end of file diff --git a/templates/cmp/search-companies.html b/templates/cmp/search-companies.html new file mode 100644 index 0000000..650d4ba --- /dev/null +++ b/templates/cmp/search-companies.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Companies{% endblock %} +{% block content %} + +

    Search Companies

    + +
    +
    + + +
    +
    + +
    +{% for company in page_obj %} +

    + + {{ company.name}} +

    +{% endfor %} +
    + +

    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}

    + + +{% if page_obj.has_previous %} + Previous +{% endif %} + +{% if page_obj.has_next %} + Next +{% endif %} + +{% endblock %} \ No newline at end of file diff --git a/templates/cmp/search-decorations.html b/templates/cmp/search-decorations.html new file mode 100644 index 0000000..47cf8a1 --- /dev/null +++ b/templates/cmp/search-decorations.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} +{% load static crispy_forms_tags %} +{% block title %}Decorations{% endblock %} +{% block content %} + +

    Search Decorations

    + +
    +
    + + +
    +
    + +
    +{% for decoration in page_obj %} +

    + + {{ decoration.name}} {{ decoration.country.name }} {{ decoration.country.flag }} +

    +{% endfor %} +
    + +

    Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}

    + +{% for decoration in page_obj %} + +{% endfor %} + +{% if page_obj.has_previous %} + Previous +{% endif %} + +{% if page_obj.has_next %} + Next +{% endif %} + +{% endblock %} \ No newline at end of file