Skip to content

Commit

Permalink
Adding companies and decorations to management screen
Browse files Browse the repository at this point in the history
  • Loading branch information
gm3dmo committed Jun 3, 2024
1 parent aa52a96 commit 55aa719
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 30 deletions.
13 changes: 13 additions & 0 deletions cmp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion cmp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .views import soldier_detail



urlpatterns = [

#path('soldier/<int:soldier_id>/', soldier_detail, name='soldier_detail'),
Expand Down Expand Up @@ -37,6 +36,18 @@
path("mgmt/countries/edit/<int:country_id>", 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/<int:company_id>/", views.detail_companies, name="companies"),
path("mgmt/companies/edit/<int:company_id>", 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/<int:decoration_id>/", views.detail_decorations, name="decorations"),
path("mgmt/decorations/edit/<int:decoration_id>", 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/<int:cemetery_id>/", views.detail_cemeteries, name="cemeteries"),
Expand Down
96 changes: 67 additions & 29 deletions cmp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions templates/cmp/detail-companies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Companies{% endblock %}
{% block content %}

<h1>{{ Company.name }} Details</h1>

<div>
<table class="table table-borderless table-lg">
<thead>
<tr>
<th>Flag</th>
<th>Name</th>
<th>Alpha2</th>
<th>Alpha3</th>
<th>Country #</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td>
{{ company.name }}
</td>
<td>
<a class="btn btn-primary btn-sm" href="{% url 'edit-companies' company_id=company.id %}">Edit
</a>
</td>
</tr>
</table>
</div>

{% endblock %}
17 changes: 17 additions & 0 deletions templates/cmp/edit-companies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Decorations Form{% endblock %}
{% block content %}

<h1>Edit Decoration</h1>
<div>
<form method="POST">
{% csrf_token %}
{{ form.management_form }}
{{ form.errors }}
{{form | crispy}}
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>

{% endblock %}
17 changes: 17 additions & 0 deletions templates/cmp/edit-decorations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Companies Form{% endblock %}
{% block content %}

<h1>Edit Company</h1>
<div>
<form method="POST">
{% csrf_token %}
{{ form.management_form }}
{{ form.errors }}
{{form | crispy}}
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>

{% endblock %}
2 changes: 2 additions & 0 deletions templates/cmp/mgmt-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ <h1>Management Index</h1>
<h3>Working Search Pages</h3>
<li><a href="/mgmt/soldiers/search/">Soldiers</a></li>
<li><a href="/mgmt/countries/search/">Countries</a></li>
<li><a href="/mgmt/companies/search/">Companies</a></li>
<li><a href="/mgmt/decorations/search/">Decorations</a></li>
<li><a href="/mgmt/ranks/search/">Ranks</a></li>
<li><a href="/mgmt/cemeteries/search/">Cemeteries</a></li>
<li><a href="/mgmt/prisoner-of-war-camps/search/">Prisoner of War Camps</a></li>
Expand Down
2 changes: 2 additions & 0 deletions templates/cmp/search-cemeteries.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ <h1> Search Cemeteries</h1>
</div>


<div>
{% for cemetery in cemeteries %}
<p>
<a href="{% url 'edit-cemeteries' cemetery.id %}">{{ cemetery.name }}</a>
</p>
{% endfor %}
</div>


{% endblock %}
35 changes: 35 additions & 0 deletions templates/cmp/search-companies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Companies{% endblock %}
{% block content %}

<h1>Search Companies</h1>

<div name="search-input" class="search-input">
<form method="get">
<input type="text" name="q" value="{{ request.GET.q }}" placeholder="Search Companies">
<input type="submit" value="Search">
</form>
</div>

<div>
{% for company in page_obj %}
<p class="soldier-details">
<button class="glossy-button" onclick="location.href='{% url 'edit-companies' company.id %}'" type="button">Edit</button>
<span class="soldier-info">{{ company.name}} </span>
</p>
{% endfor %}
</div>

<p>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</p>


{% if page_obj.has_previous %}
<a href="?q={{ request.GET.q }}&page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}

{% if page_obj.has_next %}
<a href="?q={{ request.GET.q }}&page={{ page_obj.next_page_number }}">Next</a>
{% endif %}

{% endblock %}
38 changes: 38 additions & 0 deletions templates/cmp/search-decorations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "base.html" %}
{% load static crispy_forms_tags %}
{% block title %}Decorations{% endblock %}
{% block content %}

<h1>Search Decorations</h1>

<div name="search-input" class="search-input">
<form method="get">
<input type="text" name="q" value="{{ request.GET.q }}" placeholder="Search Decorations">
<input type="submit" value="Search">
</form>
</div>

<div>
{% for decoration in page_obj %}
<p class="soldier-details">
<button class="glossy-button" onclick="location.href='{% url 'edit-decorations' decoration.id %}'" type="button">Edit</button>
<span class="soldier-info">{{ decoration.name}} {{ decoration.country.name }} {{ decoration.country.flag }} </span>
</p>
{% endfor %}
</div>

<p>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</p>

{% for decoration in page_obj %}
<!-- Display the soldier -->
{% endfor %}

{% if page_obj.has_previous %}
<a href="?q={{ request.GET.q }}&page={{ page_obj.previous_page_number }}">Previous</a>
{% endif %}

{% if page_obj.has_next %}
<a href="?q={{ request.GET.q }}&page={{ page_obj.next_page_number }}">Next</a>
{% endif %}

{% endblock %}

0 comments on commit 55aa719

Please sign in to comment.