From 13f302ea9b63b0e02a7ab86ff3eedbebbd0004b2 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 11 Aug 2022 15:43:02 +1000 Subject: [PATCH 01/35] Update for session 8 content --- src/community_db/admin.py | 12 +++++- src/community_db/models.py | 2 +- src/community_db/templates/base.html | 9 +++++ .../templates/community_db/person_list.html | 13 ++++++ .../community_db/person_list_in_base.html | 11 +++++ src/community_db/views.py | 40 ++++++++++++++++++- src/pacificconnect/urls.py | 5 +++ 7 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/community_db/templates/base.html create mode 100644 src/community_db/templates/community_db/person_list.html create mode 100644 src/community_db/templates/community_db/person_list_in_base.html diff --git a/src/community_db/admin.py b/src/community_db/admin.py index 8c38f3f..e7a3b1f 100644 --- a/src/community_db/admin.py +++ b/src/community_db/admin.py @@ -1,3 +1,13 @@ from django.contrib import admin -# Register your models here. +from .models import Person + + +class PersonAdmin(admin.ModelAdmin): + list_display = ( + "first_name", + "last_name", + ) + + +admin.site.register(Person, PersonAdmin) diff --git a/src/community_db/models.py b/src/community_db/models.py index f0853ab..9106665 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -3,6 +3,6 @@ class Person(models.Model): first_name = models.CharField(max_length=100) - last_name = models.CharField(max_length=100, blank=True) + last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, blank=True) mobile_number = models.CharField(max_length=20, blank=True) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html new file mode 100644 index 0000000..56ac5bc --- /dev/null +++ b/src/community_db/templates/base.html @@ -0,0 +1,9 @@ + + + +

Welcome to the Pacific Connect Community Database

+ On this site, you can find details of members of the Pacific Connect Community Database + {% block content %}{% endblock %} + + + \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list.html b/src/community_db/templates/community_db/person_list.html new file mode 100644 index 0000000..92c0a9a --- /dev/null +++ b/src/community_db/templates/community_db/person_list.html @@ -0,0 +1,13 @@ + + + + This is my list of folks + + + + \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html new file mode 100644 index 0000000..bca85c5 --- /dev/null +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} + This is my list of folks + +{% endblock %} \ No newline at end of file diff --git a/src/community_db/views.py b/src/community_db/views.py index 91ea44a..1b0d3dd 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,41 @@ +from django.http import HttpResponse from django.shortcuts import render +from django.template import loader +from django.views.generic.list import ListView -# Create your views here. +from community_db.models import Person + + +def list_persons(request): + html = "This is my list of folks" + return HttpResponse(html) + + +# def list_persons_with_template(request): +# persons = Person.objects.all() +# template = loader.get_template("community_db/person_list.html") +# context = {"object_list": persons} +# return HttpResponse(template.render(context, request)) + + +def list_persons_with_template(request): + persons = Person.objects.all() + template = loader.get_template("community_db/person_list_in_base.html") + context = {"object_list": persons} + return HttpResponse(template.render(context, request)) + + +# def list_persons_with_template(request): +# persons = Person.objects.all() +# context = {"object_list": persons} +# return render(request, "community_db/person_list.html", context) + + +class PersonListView(ListView): + model = Person + template_name = "community_db/person_list_in_base.html" diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 5a454ad..c06409c 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -16,6 +16,11 @@ from django.contrib import admin from django.urls import path +from community_db import views + urlpatterns = [ path("admin/", admin.site.urls), + path("myview", views.list_persons), + path("myview-with-template/", views.list_persons_with_template), + path("person-list/", views.PersonListView.as_view()), ] From db72024d5464859f565aa0882fb34eac1b48286b Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:18:34 +1000 Subject: [PATCH 02/35] Content for session 9. Tidy up URLs and Views. --- .../community_db/person_detail_in_base.html | 14 +++++++ .../community_db/person_list_in_base.html | 7 +++- src/community_db/views.py | 40 +++++++------------ src/pacificconnect/urls.py | 15 +++++-- 4 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/community_db/templates/community_db/person_detail_in_base.html diff --git a/src/community_db/templates/community_db/person_detail_in_base.html b/src/community_db/templates/community_db/person_detail_in_base.html new file mode 100644 index 0000000..31102ff --- /dev/null +++ b/src/community_db/templates/community_db/person_detail_in_base.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block content %} +
+ Back to list +
+ This is the detail of a person: + +{% endblock %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index bca85c5..d9d4ede 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -4,8 +4,11 @@ This is my list of folks {% endblock %} \ No newline at end of file diff --git a/src/community_db/views.py b/src/community_db/views.py index 1b0d3dd..d6ea8e0 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,41 +1,29 @@ -from django.http import HttpResponse from django.shortcuts import render -from django.template import loader -from django.views.generic.list import ListView +from django.views.generic import DetailView, ListView -from community_db.models import Person +from .models import Person - -def list_persons(request): - html = "This is my list of folks" - return HttpResponse(html) - - -# def list_persons_with_template(request): -# persons = Person.objects.all() -# template = loader.get_template("community_db/person_list.html") -# context = {"object_list": persons} -# return HttpResponse(template.render(context, request)) +# FUNCTION BASED VIEWS def list_persons_with_template(request): persons = Person.objects.all() - template = loader.get_template("community_db/person_list_in_base.html") context = {"object_list": persons} - return HttpResponse(template.render(context, request)) + return render(request, "community_db/person_list_in_base.html", context) -# def list_persons_with_template(request): -# persons = Person.objects.all() -# context = {"object_list": persons} -# return render(request, "community_db/person_list.html", context) +def detail_person_with_template(request, pk): + person = Person.objects.get(id=pk) + context = {"object": person} + return render(request, "community_db/person_detail_in_base.html", context) +# CLASS BASED VIEWS class PersonListView(ListView): model = Person template_name = "community_db/person_list_in_base.html" + + +class PersonDetailView(DetailView): + model = Person + template_name = "community_db/person_detail_in_base.html" diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index c06409c..d602477 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -20,7 +20,16 @@ urlpatterns = [ path("admin/", admin.site.urls), - path("myview", views.list_persons), - path("myview-with-template/", views.list_persons_with_template), - path("person-list/", views.PersonListView.as_view()), + path("fbv/people/", views.list_persons_with_template, name="fbv-person-list"), + path( + "fbv/people//", + views.detail_person_with_template, + name="fbv-person-detail", + ), + path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"), + path( + "cbv/people//", + views.PersonDetailView.as_view(), + name="cbv-person-detail", + ), ] From 813a8090a613aaeb947bc75c7565a0c296ace88a Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:54:17 +1000 Subject: [PATCH 03/35] Session 10 - add search --- src/community_db/templates/base.html | 4 ++++ src/community_db/views.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 56ac5bc..0aeafd8 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -3,6 +3,10 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database +
+
+ Search: +
{% block content %}{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index d6ea8e0..4a318ec 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,4 @@ +from django.db import models from django.shortcuts import render from django.views.generic import DetailView, ListView @@ -6,8 +7,13 @@ # FUNCTION BASED VIEWS +# Searching only the first_name field def list_persons_with_template(request): + search_text = request.GET.get("search") + persons = Person.objects.all() + if search_text: + persons = persons.filter(first_name__icontains=search_text) context = {"object_list": persons} return render(request, "community_db/person_list_in_base.html", context) From 509bb13b9c1b051eb98a425f1ed56f0727f5f101 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:55:46 +1000 Subject: [PATCH 04/35] Session 10 - multi-field search --- src/community_db/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index 4a318ec..908cd6d 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -6,14 +6,16 @@ # FUNCTION BASED VIEWS - -# Searching only the first_name field +# Searching the first name and last name fields def list_persons_with_template(request): search_text = request.GET.get("search") persons = Person.objects.all() if search_text: - persons = persons.filter(first_name__icontains=search_text) + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + persons = persons.filter(search_filters) context = {"object_list": persons} return render(request, "community_db/person_list_in_base.html", context) From 80a5ffbfc1be655dfa258d57e6bc94ed43feef46 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 12:05:28 +1000 Subject: [PATCH 05/35] Session 10 - Add search to generic view --- src/community_db/views.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/community_db/views.py b/src/community_db/views.py index 908cd6d..fa34fac 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -31,6 +31,25 @@ class PersonListView(ListView): model = Person template_name = "community_db/person_list_in_base.html" + def get_queryset(self): + # Get the default queryset - i.e. set of rows - that we want + # to filter + queryset = super().get_queryset() + + # Get the search field value - but from self.request in the + # class based view + search_text = self.request.GET.get("search") + + # Filter if we need to + if search_text: + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + queryset = queryset.filter(search_filters) + + # Return the queryset now that we have filtered it (if we need to) + return queryset + class PersonDetailView(DetailView): model = Person From 91af5567d76edf8f572bbe285a0b2193b0da4f87 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:58:21 +1000 Subject: [PATCH 06/35] Session 10 - text in search box --- src/community_db/templates/base.html | 2 +- src/community_db/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 0aeafd8..f2ed587 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,7 +5,7 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + Search:
{% block content %}{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index fa34fac..79d176f 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -6,7 +6,7 @@ # FUNCTION BASED VIEWS -# Searching the first name and last name fields +# Searching the first name and last name fields with text in the search box def list_persons_with_template(request): search_text = request.GET.get("search") @@ -16,7 +16,7 @@ def list_persons_with_template(request): last_name__icontains=search_text ) persons = persons.filter(search_filters) - context = {"object_list": persons} + context = {"object_list": persons, "search_text": search_text} return render(request, "community_db/person_list_in_base.html", context) From a3cb85e9be4a8767f64a033681d839bbe9b8a574 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 14:01:40 +1000 Subject: [PATCH 07/35] Session 10 - you searched for text --- src/community_db/templates/base.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index f2ed587..f0d8871 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,8 +5,12 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + Search:
+ {% if search_text %} + Search results for: {{ search_text }} + {% endif %} +
{% block content %}{% endblock %} From 4a8044623f2fe155bbcf94bf6da46d390d3d3274 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 12:10:05 +1000 Subject: [PATCH 08/35] Session 10 - you searched for text in CBV --- src/community_db/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/community_db/views.py b/src/community_db/views.py index 79d176f..56ebb50 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -50,6 +50,17 @@ def get_queryset(self): # Return the queryset now that we have filtered it (if we need to) return queryset + def get_context_data(self, **kwargs): + # Get the default context that would be generated + context = super().get_context_data(**kwargs) + + # Get the search text and add it to the context + search_text = self.request.GET.get("search") + context["search_text"] = search_text + + # Return our new context + return context + class PersonDetailView(DetailView): model = Person From 67997f4f05da954a2ce1a80fb9fdf25f87e587e7 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 18 Aug 2022 11:59:15 +1000 Subject: [PATCH 09/35] Session 10 - Django form --- src/community_db/forms.py | 5 +++++ src/community_db/templates/base.html | 3 ++- src/community_db/views.py | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 src/community_db/forms.py diff --git a/src/community_db/forms.py b/src/community_db/forms.py new file mode 100644 index 0000000..726140d --- /dev/null +++ b/src/community_db/forms.py @@ -0,0 +1,5 @@ +from django import forms + + +class QuickSearchForm(forms.Form): + search = forms.CharField(max_length=100, required=False) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index f0d8871..666ebf8 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,7 +5,8 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- Search: + {{ form.as_p }} +
{% if search_text %} Search results for: {{ search_text }} diff --git a/src/community_db/views.py b/src/community_db/views.py index 56ebb50..e46dccc 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -2,21 +2,25 @@ from django.shortcuts import render from django.views.generic import DetailView, ListView +from .forms import QuickSearchForm from .models import Person # FUNCTION BASED VIEWS -# Searching the first name and last name fields with text in the search box +# Searching the first name and last name fields using a Django form def list_persons_with_template(request): - search_text = request.GET.get("search") + form = QuickSearchForm(request.GET) persons = Person.objects.all() - if search_text: - search_filters = models.Q(first_name__icontains=search_text) | models.Q( - last_name__icontains=search_text - ) - persons = persons.filter(search_filters) - context = {"object_list": persons, "search_text": search_text} + search_text = "" + if form.is_valid(): + search_text = form.cleaned_data["search"] + if search_text: + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + persons = persons.filter(search_filters) + context = {"object_list": persons, "search_text": search_text, "form": form} return render(request, "community_db/person_list_in_base.html", context) From 81a9b9dcc62366ea00f46cffe766415bd33b8337 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 22 Aug 2022 13:09:41 +1000 Subject: [PATCH 10/35] Show message if no results --- src/community_db/templates/base.html | 3 ++- .../community_db/person_list_in_base.html | 26 +++++++++++-------- src/community_db/views.py | 20 ++++++-------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 666ebf8..96c5da4 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -5,7 +5,8 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
- {{ form.as_p }} + +
{% if search_text %} diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index d9d4ede..d003e30 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -1,14 +1,18 @@ {% extends "base.html" %} {% block content %} - This is my list of folks - -{% endblock %} \ No newline at end of file + {% if object_list %} + This is my list of folks + + {% else %} + No profiles match that search + {% endif %} +{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index e46dccc..56ebb50 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -2,25 +2,21 @@ from django.shortcuts import render from django.views.generic import DetailView, ListView -from .forms import QuickSearchForm from .models import Person # FUNCTION BASED VIEWS -# Searching the first name and last name fields using a Django form +# Searching the first name and last name fields with text in the search box def list_persons_with_template(request): - form = QuickSearchForm(request.GET) + search_text = request.GET.get("search") persons = Person.objects.all() - search_text = "" - if form.is_valid(): - search_text = form.cleaned_data["search"] - if search_text: - search_filters = models.Q(first_name__icontains=search_text) | models.Q( - last_name__icontains=search_text - ) - persons = persons.filter(search_filters) - context = {"object_list": persons, "search_text": search_text, "form": form} + if search_text: + search_filters = models.Q(first_name__icontains=search_text) | models.Q( + last_name__icontains=search_text + ) + persons = persons.filter(search_filters) + context = {"object_list": persons, "search_text": search_text} return render(request, "community_db/person_list_in_base.html", context) From a6e9a788f0da795b50871997d9a4e3ab34856927 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 22 Aug 2022 13:09:51 +1000 Subject: [PATCH 11/35] Handle search from any page --- src/community_db/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html index 96c5da4..182305f 100644 --- a/src/community_db/templates/base.html +++ b/src/community_db/templates/base.html @@ -4,7 +4,7 @@

Welcome to the Pacific Connect Community Database

On this site, you can find details of members of the Pacific Connect Community Database
-
+ From 3e0dd8047ff2bee27c569bc5b2e35cc3d86e4437 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 22 Aug 2022 13:45:54 +1000 Subject: [PATCH 12/35] Add get_object_or_404 --- src/community_db/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index 56ebb50..f63d5dd 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,5 +1,5 @@ from django.db import models -from django.shortcuts import render +from django.shortcuts import get_object_or_404, render from django.views.generic import DetailView, ListView from .models import Person @@ -21,7 +21,7 @@ def list_persons_with_template(request): def detail_person_with_template(request, pk): - person = Person.objects.get(id=pk) + person = get_object_or_404(Person, id=pk) context = {"object": person} return render(request, "community_db/person_detail_in_base.html", context) From 528f0960ec5a14a3cb1dc3986b16acfdb6431a83 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 22 Aug 2022 14:15:31 +1000 Subject: [PATCH 13/35] Add basic form --- src/community_db/forms.py | 7 +++++ .../community_db/person_detail_in_base.html | 1 + .../community_db/person_form_in_base.html | 10 +++++++ src/community_db/views.py | 27 +++++++++++++++++++ src/pacificconnect/urls.py | 5 ++++ 5 files changed, 50 insertions(+) create mode 100644 src/community_db/templates/community_db/person_form_in_base.html diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 726140d..cc1df70 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -3,3 +3,10 @@ class QuickSearchForm(forms.Form): search = forms.CharField(max_length=100, required=False) + + +class PersonForm(forms.Form): + first_name = forms.CharField(max_length=100) + last_name = forms.CharField(max_length=100) + country = forms.CharField(max_length=100, required=False) + mobile_number = forms.CharField(max_length=20, required=False) diff --git a/src/community_db/templates/community_db/person_detail_in_base.html b/src/community_db/templates/community_db/person_detail_in_base.html index 31102ff..a5d318c 100644 --- a/src/community_db/templates/community_db/person_detail_in_base.html +++ b/src/community_db/templates/community_db/person_detail_in_base.html @@ -11,4 +11,5 @@
  • Country: {{ object.country }}
  • Phone number: {{ object.mobile_number }}
  • + edit {% endblock %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_form_in_base.html b/src/community_db/templates/community_db/person_form_in_base.html new file mode 100644 index 0000000..b291a08 --- /dev/null +++ b/src/community_db/templates/community_db/person_form_in_base.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +
    + + {% csrf_token %} + {{ form.as_p }} + + +{% endblock %} \ No newline at end of file diff --git a/src/community_db/views.py b/src/community_db/views.py index f63d5dd..bd4f2ca 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,7 +1,10 @@ from django.db import models +from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render +from django.urls import reverse from django.views.generic import DetailView, ListView +from .forms import PersonForm from .models import Person # FUNCTION BASED VIEWS @@ -26,6 +29,30 @@ def detail_person_with_template(request, pk): return render(request, "community_db/person_detail_in_base.html", context) +def edit_person_with_template(request, pk): + person = get_object_or_404(Person, id=pk) + if request.POST: + form = PersonForm(request.POST) + if form.is_valid(): + person.first_name = form.cleaned_data["first_name"] + person.last_name = form.cleaned_data["last_name"] + person.country = form.cleaned_data["country"] + person.mobile_number = form.cleaned_data["mobile_number"] + person.save() + return HttpResponseRedirect(reverse("fbv-person-detail", args=[person.id])) + else: + form = PersonForm( + { + "first_name": person.first_name, + "last_name": person.last_name, + "country": person.country, + "mobile_number": person.mobile_number, + } + ) + context = {"object": person, "form": form} + return render(request, "community_db/person_form_in_base.html", context) + + # CLASS BASED VIEWS class PersonListView(ListView): model = Person diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index d602477..ad0c45c 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -26,6 +26,11 @@ views.detail_person_with_template, name="fbv-person-detail", ), + path( + "fbv/people//edit/", + views.edit_person_with_template, + name="fbv-person-edit", + ), path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"), path( "cbv/people//", From c77a1808e4fa6d8f2c74c7edd770242c57e2e6fd Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 11:50:29 +1000 Subject: [PATCH 14/35] Add list of choices with country code and country name --- src/community_db/models.py | 254 ++++++++++++++++++++++++++++++++++++- 1 file changed, 253 insertions(+), 1 deletion(-) diff --git a/src/community_db/models.py b/src/community_db/models.py index 9106665..0c762bf 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -2,7 +2,259 @@ class Person(models.Model): + COUNTRIES = [ + ("AF", "Afghanistan"), + ("AL", "Albania"), + ("DZ", "Algeria"), + ("AS", "American Samoa"), + ("AD", "Andorra"), + ("AO", "Angola"), + ("AI", "Anguilla"), + ("AQ", "Antarctica"), + ("AG", "Antigua and Barbuda"), + ("AR", "Argentina"), + ("AM", "Armenia"), + ("AW", "Aruba"), + ("AU", "Australia"), + ("AT", "Austria"), + ("AZ", "Azerbaijan"), + ("BS", "Bahamas (the)"), + ("BH", "Bahrain"), + ("BD", "Bangladesh"), + ("BB", "Barbados"), + ("BY", "Belarus"), + ("BE", "Belgium"), + ("BZ", "Belize"), + ("BJ", "Benin"), + ("BM", "Bermuda"), + ("BT", "Bhutan"), + ("BO", "Bolivia (Plurinational State of)"), + ("BQ", "Bonaire, Sint Eustatius and Saba"), + ("BA", "Bosnia and Herzegovina"), + ("BW", "Botswana"), + ("BV", "Bouvet Island"), + ("BR", "Brazil"), + ("IO", "British Indian Ocean Territory (the)"), + ("BN", "Brunei Darussalam"), + ("BG", "Bulgaria"), + ("BF", "Burkina Faso"), + ("BI", "Burundi"), + ("CV", "Cabo Verde"), + ("KH", "Cambodia"), + ("CM", "Cameroon"), + ("CA", "Canada"), + ("KY", "Cayman Islands (the)"), + ("CF", "Central African Republic (the)"), + ("TD", "Chad"), + ("CL", "Chile"), + ("CN", "China"), + ("CX", "Christmas Island"), + ("CC", "Cocos (Keeling) Islands (the)"), + ("CO", "Colombia"), + ("KM", "Comoros (the)"), + ("CD", "Congo (the Democratic Republic of the)"), + ("CG", "Congo (the)"), + ("CK", "Cook Islands (the)"), + ("CR", "Costa Rica"), + ("HR", "Croatia"), + ("CU", "Cuba"), + ("CW", "Curaçao"), + ("CY", "Cyprus"), + ("CZ", "Czechia"), + ("CI", "Côte d'Ivoire"), + ("DK", "Denmark"), + ("DJ", "Djibouti"), + ("DM", "Dominica"), + ("DO", "Dominican Republic (the)"), + ("EC", "Ecuador"), + ("EG", "Egypt"), + ("SV", "El Salvador"), + ("GQ", "Equatorial Guinea"), + ("ER", "Eritrea"), + ("EE", "Estonia"), + ("SZ", "Eswatini"), + ("ET", "Ethiopia"), + ("FK", "Falkland Islands (the) [Malvinas]"), + ("FO", "Faroe Islands (the)"), + ("FJ", "Fiji"), + ("FI", "Finland"), + ("FR", "France"), + ("GF", "French Guiana"), + ("PF", "French Polynesia"), + ("TF", "French Southern Territories (the)"), + ("GA", "Gabon"), + ("GM", "Gambia (the)"), + ("GE", "Georgia"), + ("DE", "Germany"), + ("GH", "Ghana"), + ("GI", "Gibraltar"), + ("GR", "Greece"), + ("GL", "Greenland"), + ("GD", "Grenada"), + ("GP", "Guadeloupe"), + ("GU", "Guam"), + ("GT", "Guatemala"), + ("GG", "Guernsey"), + ("GN", "Guinea"), + ("GW", "Guinea-Bissau"), + ("GY", "Guyana"), + ("HT", "Haiti"), + ("HM", "Heard Island and McDonald Islands"), + ("VA", "Holy See (the)"), + ("HN", "Honduras"), + ("HK", "Hong Kong"), + ("HU", "Hungary"), + ("IS", "Iceland"), + ("IN", "India"), + ("ID", "Indonesia"), + ("IR", "Iran (Islamic Republic of)"), + ("IQ", "Iraq"), + ("IE", "Ireland"), + ("IM", "Isle of Man"), + ("IL", "Israel"), + ("IT", "Italy"), + ("JM", "Jamaica"), + ("JP", "Japan"), + ("JE", "Jersey"), + ("JO", "Jordan"), + ("KZ", "Kazakhstan"), + ("KE", "Kenya"), + ("KI", "Kiribati"), + ("KP", "Korea (the Democratic People's Republic of)"), + ("KR", "Korea (the Republic of)"), + ("KW", "Kuwait"), + ("KG", "Kyrgyzstan"), + ("LA", "Lao People's Democratic Republic (the)"), + ("LV", "Latvia"), + ("LB", "Lebanon"), + ("LS", "Lesotho"), + ("LR", "Liberia"), + ("LY", "Libya"), + ("LI", "Liechtenstein"), + ("LT", "Lithuania"), + ("LU", "Luxembourg"), + ("MO", "Macao"), + ("MG", "Madagascar"), + ("MW", "Malawi"), + ("MY", "Malaysia"), + ("MV", "Maldives"), + ("ML", "Mali"), + ("MT", "Malta"), + ("MH", "Marshall Islands (the)"), + ("MQ", "Martinique"), + ("MR", "Mauritania"), + ("MU", "Mauritius"), + ("YT", "Mayotte"), + ("MX", "Mexico"), + ("FM", "Micronesia (Federated States of)"), + ("MD", "Moldova (the Republic of)"), + ("MC", "Monaco"), + ("MN", "Mongolia"), + ("ME", "Montenegro"), + ("MS", "Montserrat"), + ("MA", "Morocco"), + ("MZ", "Mozambique"), + ("MM", "Myanmar"), + ("NA", "Namibia"), + ("NR", "Nauru"), + ("NP", "Nepal"), + ("NL", "Netherlands (the)"), + ("NC", "New Caledonia"), + ("NZ", "New Zealand"), + ("NI", "Nicaragua"), + ("NE", "Niger (the)"), + ("NG", "Nigeria"), + ("NU", "Niue"), + ("NF", "Norfolk Island"), + ("MP", "Northern Mariana Islands (the)"), + ("NO", "Norway"), + ("OM", "Oman"), + ("PK", "Pakistan"), + ("PW", "Palau"), + ("PS", "Palestine, State of"), + ("PA", "Panama"), + ("PG", "Papua New Guinea"), + ("PY", "Paraguay"), + ("PE", "Peru"), + ("PH", "Philippines (the)"), + ("PN", "Pitcairn"), + ("PL", "Poland"), + ("PT", "Portugal"), + ("PR", "Puerto Rico"), + ("QA", "Qatar"), + ("MK", "Republic of North Macedonia"), + ("RO", "Romania"), + ("RU", "Russian Federation (the)"), + ("RW", "Rwanda"), + ("RE", "Réunion"), + ("BL", "Saint Barthélemy"), + ("SH", "Saint Helena, Ascension and Tristan da Cunha"), + ("KN", "Saint Kitts and Nevis"), + ("LC", "Saint Lucia"), + ("MF", "Saint Martin (French part)"), + ("PM", "Saint Pierre and Miquelon"), + ("VC", "Saint Vincent and the Grenadines"), + ("WS", "Samoa"), + ("SM", "San Marino"), + ("ST", "Sao Tome and Principe"), + ("SA", "Saudi Arabia"), + ("SN", "Senegal"), + ("RS", "Serbia"), + ("SC", "Seychelles"), + ("SL", "Sierra Leone"), + ("SG", "Singapore"), + ("SX", "Sint Maarten (Dutch part)"), + ("SK", "Slovakia"), + ("SI", "Slovenia"), + ("SB", "Solomon Islands"), + ("SO", "Somalia"), + ("ZA", "South Africa"), + ("GS", "South Georgia and the South Sandwich Islands"), + ("SS", "South Sudan"), + ("ES", "Spain"), + ("LK", "Sri Lanka"), + ("SD", "Sudan (the)"), + ("SR", "Suriname"), + ("SJ", "Svalbard and Jan Mayen"), + ("SE", "Sweden"), + ("CH", "Switzerland"), + ("SY", "Syrian Arab Republic"), + ("TW", "Taiwan (Province of China)"), + ("TJ", "Tajikistan"), + ("TZ", "Tanzania, United Republic of"), + ("TH", "Thailand"), + ("TL", "Timor-Leste"), + ("TG", "Togo"), + ("TK", "Tokelau"), + ("TO", "Tonga"), + ("TT", "Trinidad and Tobago"), + ("TN", "Tunisia"), + ("TR", "Turkey"), + ("TM", "Turkmenistan"), + ("TC", "Turks and Caicos Islands (the)"), + ("TV", "Tuvalu"), + ("UG", "Uganda"), + ("UA", "Ukraine"), + ("AE", "United Arab Emirates (the)"), + ("GB", "United Kingdom of Great Britain and Northern Ireland (the)"), + ("UM", "United States Minor Outlying Islands (the)"), + ("US", "United States of America (the)"), + ("UY", "Uruguay"), + ("UZ", "Uzbekistan"), + ("VU", "Vanuatu"), + ("VE", "Venezuela (Bolivarian Republic of)"), + ("VN", "Viet Nam"), + ("VG", "Virgin Islands (British)"), + ("VI", "Virgin Islands (U.S.)"), + ("WF", "Wallis and Futuna"), + ("EH", "Western Sahara"), + ("YE", "Yemen"), + ("ZM", "Zambia"), + ("ZW", "Zimbabwe"), + ("AX", "Åland Islands"), + ] + first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) - country = models.CharField(max_length=100, blank=True) + country = models.CharField(max_length=100, choices=COUNTRIES, blank=True) mobile_number = models.CharField(max_length=20, blank=True) From fd2cbcd7a9975418591d20cc49af3cdc03620644 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 11:51:58 +1000 Subject: [PATCH 15/35] Add list of choices with just country name --- src/community_db/models.py | 525 +++++++++++++++++++------------------ 1 file changed, 276 insertions(+), 249 deletions(-) diff --git a/src/community_db/models.py b/src/community_db/models.py index 0c762bf..c891b87 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -3,255 +3,282 @@ class Person(models.Model): COUNTRIES = [ - ("AF", "Afghanistan"), - ("AL", "Albania"), - ("DZ", "Algeria"), - ("AS", "American Samoa"), - ("AD", "Andorra"), - ("AO", "Angola"), - ("AI", "Anguilla"), - ("AQ", "Antarctica"), - ("AG", "Antigua and Barbuda"), - ("AR", "Argentina"), - ("AM", "Armenia"), - ("AW", "Aruba"), - ("AU", "Australia"), - ("AT", "Austria"), - ("AZ", "Azerbaijan"), - ("BS", "Bahamas (the)"), - ("BH", "Bahrain"), - ("BD", "Bangladesh"), - ("BB", "Barbados"), - ("BY", "Belarus"), - ("BE", "Belgium"), - ("BZ", "Belize"), - ("BJ", "Benin"), - ("BM", "Bermuda"), - ("BT", "Bhutan"), - ("BO", "Bolivia (Plurinational State of)"), - ("BQ", "Bonaire, Sint Eustatius and Saba"), - ("BA", "Bosnia and Herzegovina"), - ("BW", "Botswana"), - ("BV", "Bouvet Island"), - ("BR", "Brazil"), - ("IO", "British Indian Ocean Territory (the)"), - ("BN", "Brunei Darussalam"), - ("BG", "Bulgaria"), - ("BF", "Burkina Faso"), - ("BI", "Burundi"), - ("CV", "Cabo Verde"), - ("KH", "Cambodia"), - ("CM", "Cameroon"), - ("CA", "Canada"), - ("KY", "Cayman Islands (the)"), - ("CF", "Central African Republic (the)"), - ("TD", "Chad"), - ("CL", "Chile"), - ("CN", "China"), - ("CX", "Christmas Island"), - ("CC", "Cocos (Keeling) Islands (the)"), - ("CO", "Colombia"), - ("KM", "Comoros (the)"), - ("CD", "Congo (the Democratic Republic of the)"), - ("CG", "Congo (the)"), - ("CK", "Cook Islands (the)"), - ("CR", "Costa Rica"), - ("HR", "Croatia"), - ("CU", "Cuba"), - ("CW", "Curaçao"), - ("CY", "Cyprus"), - ("CZ", "Czechia"), - ("CI", "Côte d'Ivoire"), - ("DK", "Denmark"), - ("DJ", "Djibouti"), - ("DM", "Dominica"), - ("DO", "Dominican Republic (the)"), - ("EC", "Ecuador"), - ("EG", "Egypt"), - ("SV", "El Salvador"), - ("GQ", "Equatorial Guinea"), - ("ER", "Eritrea"), - ("EE", "Estonia"), - ("SZ", "Eswatini"), - ("ET", "Ethiopia"), - ("FK", "Falkland Islands (the) [Malvinas]"), - ("FO", "Faroe Islands (the)"), - ("FJ", "Fiji"), - ("FI", "Finland"), - ("FR", "France"), - ("GF", "French Guiana"), - ("PF", "French Polynesia"), - ("TF", "French Southern Territories (the)"), - ("GA", "Gabon"), - ("GM", "Gambia (the)"), - ("GE", "Georgia"), - ("DE", "Germany"), - ("GH", "Ghana"), - ("GI", "Gibraltar"), - ("GR", "Greece"), - ("GL", "Greenland"), - ("GD", "Grenada"), - ("GP", "Guadeloupe"), - ("GU", "Guam"), - ("GT", "Guatemala"), - ("GG", "Guernsey"), - ("GN", "Guinea"), - ("GW", "Guinea-Bissau"), - ("GY", "Guyana"), - ("HT", "Haiti"), - ("HM", "Heard Island and McDonald Islands"), - ("VA", "Holy See (the)"), - ("HN", "Honduras"), - ("HK", "Hong Kong"), - ("HU", "Hungary"), - ("IS", "Iceland"), - ("IN", "India"), - ("ID", "Indonesia"), - ("IR", "Iran (Islamic Republic of)"), - ("IQ", "Iraq"), - ("IE", "Ireland"), - ("IM", "Isle of Man"), - ("IL", "Israel"), - ("IT", "Italy"), - ("JM", "Jamaica"), - ("JP", "Japan"), - ("JE", "Jersey"), - ("JO", "Jordan"), - ("KZ", "Kazakhstan"), - ("KE", "Kenya"), - ("KI", "Kiribati"), - ("KP", "Korea (the Democratic People's Republic of)"), - ("KR", "Korea (the Republic of)"), - ("KW", "Kuwait"), - ("KG", "Kyrgyzstan"), - ("LA", "Lao People's Democratic Republic (the)"), - ("LV", "Latvia"), - ("LB", "Lebanon"), - ("LS", "Lesotho"), - ("LR", "Liberia"), - ("LY", "Libya"), - ("LI", "Liechtenstein"), - ("LT", "Lithuania"), - ("LU", "Luxembourg"), - ("MO", "Macao"), - ("MG", "Madagascar"), - ("MW", "Malawi"), - ("MY", "Malaysia"), - ("MV", "Maldives"), - ("ML", "Mali"), - ("MT", "Malta"), - ("MH", "Marshall Islands (the)"), - ("MQ", "Martinique"), - ("MR", "Mauritania"), - ("MU", "Mauritius"), - ("YT", "Mayotte"), - ("MX", "Mexico"), - ("FM", "Micronesia (Federated States of)"), - ("MD", "Moldova (the Republic of)"), - ("MC", "Monaco"), - ("MN", "Mongolia"), - ("ME", "Montenegro"), - ("MS", "Montserrat"), - ("MA", "Morocco"), - ("MZ", "Mozambique"), - ("MM", "Myanmar"), - ("NA", "Namibia"), - ("NR", "Nauru"), - ("NP", "Nepal"), - ("NL", "Netherlands (the)"), - ("NC", "New Caledonia"), - ("NZ", "New Zealand"), - ("NI", "Nicaragua"), - ("NE", "Niger (the)"), - ("NG", "Nigeria"), - ("NU", "Niue"), - ("NF", "Norfolk Island"), - ("MP", "Northern Mariana Islands (the)"), - ("NO", "Norway"), - ("OM", "Oman"), - ("PK", "Pakistan"), - ("PW", "Palau"), - ("PS", "Palestine, State of"), - ("PA", "Panama"), - ("PG", "Papua New Guinea"), - ("PY", "Paraguay"), - ("PE", "Peru"), - ("PH", "Philippines (the)"), - ("PN", "Pitcairn"), - ("PL", "Poland"), - ("PT", "Portugal"), - ("PR", "Puerto Rico"), - ("QA", "Qatar"), - ("MK", "Republic of North Macedonia"), - ("RO", "Romania"), - ("RU", "Russian Federation (the)"), - ("RW", "Rwanda"), - ("RE", "Réunion"), - ("BL", "Saint Barthélemy"), - ("SH", "Saint Helena, Ascension and Tristan da Cunha"), - ("KN", "Saint Kitts and Nevis"), - ("LC", "Saint Lucia"), - ("MF", "Saint Martin (French part)"), - ("PM", "Saint Pierre and Miquelon"), - ("VC", "Saint Vincent and the Grenadines"), - ("WS", "Samoa"), - ("SM", "San Marino"), - ("ST", "Sao Tome and Principe"), - ("SA", "Saudi Arabia"), - ("SN", "Senegal"), - ("RS", "Serbia"), - ("SC", "Seychelles"), - ("SL", "Sierra Leone"), - ("SG", "Singapore"), - ("SX", "Sint Maarten (Dutch part)"), - ("SK", "Slovakia"), - ("SI", "Slovenia"), - ("SB", "Solomon Islands"), - ("SO", "Somalia"), - ("ZA", "South Africa"), - ("GS", "South Georgia and the South Sandwich Islands"), - ("SS", "South Sudan"), - ("ES", "Spain"), - ("LK", "Sri Lanka"), - ("SD", "Sudan (the)"), - ("SR", "Suriname"), - ("SJ", "Svalbard and Jan Mayen"), - ("SE", "Sweden"), - ("CH", "Switzerland"), - ("SY", "Syrian Arab Republic"), - ("TW", "Taiwan (Province of China)"), - ("TJ", "Tajikistan"), - ("TZ", "Tanzania, United Republic of"), - ("TH", "Thailand"), - ("TL", "Timor-Leste"), - ("TG", "Togo"), - ("TK", "Tokelau"), - ("TO", "Tonga"), - ("TT", "Trinidad and Tobago"), - ("TN", "Tunisia"), - ("TR", "Turkey"), - ("TM", "Turkmenistan"), - ("TC", "Turks and Caicos Islands (the)"), - ("TV", "Tuvalu"), - ("UG", "Uganda"), - ("UA", "Ukraine"), - ("AE", "United Arab Emirates (the)"), - ("GB", "United Kingdom of Great Britain and Northern Ireland (the)"), - ("UM", "United States Minor Outlying Islands (the)"), - ("US", "United States of America (the)"), - ("UY", "Uruguay"), - ("UZ", "Uzbekistan"), - ("VU", "Vanuatu"), - ("VE", "Venezuela (Bolivarian Republic of)"), - ("VN", "Viet Nam"), - ("VG", "Virgin Islands (British)"), - ("VI", "Virgin Islands (U.S.)"), - ("WF", "Wallis and Futuna"), - ("EH", "Western Sahara"), - ("YE", "Yemen"), - ("ZM", "Zambia"), - ("ZW", "Zimbabwe"), - ("AX", "Åland Islands"), + ("Afghanistan", "Afghanistan"), + ("Albania", "Albania"), + ("Algeria", "Algeria"), + ("American Samoa", "American Samoa"), + ("Andorra", "Andorra"), + ("Angola", "Angola"), + ("Anguilla", "Anguilla"), + ("Antarctica", "Antarctica"), + ("Antigua and Barbuda", "Antigua and Barbuda"), + ("Argentina", "Argentina"), + ("Armenia", "Armenia"), + ("Aruba", "Aruba"), + ("Australia", "Australia"), + ("Austria", "Austria"), + ("Azerbaijan", "Azerbaijan"), + ("Bahamas (the)", "Bahamas (the)"), + ("Bahrain", "Bahrain"), + ("Bangladesh", "Bangladesh"), + ("Barbados", "Barbados"), + ("Belarus", "Belarus"), + ("Belgium", "Belgium"), + ("Belize", "Belize"), + ("Benin", "Benin"), + ("Bermuda", "Bermuda"), + ("Bhutan", "Bhutan"), + ("Bolivia (Plurinational State of)", "Bolivia (Plurinational State of)"), + ("Bonaire, Sint Eustatius and Saba", "Bonaire, Sint Eustatius and Saba"), + ("Bosnia and Herzegovina", "Bosnia and Herzegovina"), + ("Botswana", "Botswana"), + ("Bouvet Island", "Bouvet Island"), + ("Brazil", "Brazil"), + ( + "British Indian Ocean Territory (the)", + "British Indian Ocean Territory (the)", + ), + ("Brunei Darussalam", "Brunei Darussalam"), + ("Bulgaria", "Bulgaria"), + ("Burkina Faso", "Burkina Faso"), + ("Burundi", "Burundi"), + ("Cabo Verde", "Cabo Verde"), + ("Cambodia", "Cambodia"), + ("Cameroon", "Cameroon"), + ("Canada", "Canada"), + ("Cayman Islands (the)", "Cayman Islands (the)"), + ("Central African Republic (the)", "Central African Republic (the)"), + ("Chad", "Chad"), + ("Chile", "Chile"), + ("China", "China"), + ("Christmas Island", "Christmas Island"), + ("Cocos (Keeling) Islands (the)", "Cocos (Keeling) Islands (the)"), + ("Colombia", "Colombia"), + ("Comoros (the)", "Comoros (the)"), + ( + "Congo (the Democratic Republic of the)", + "Congo (the Democratic Republic of the)", + ), + ("Congo (the)", "Congo (the)"), + ("Cook Islands (the)", "Cook Islands (the)"), + ("Costa Rica", "Costa Rica"), + ("Croatia", "Croatia"), + ("Cuba", "Cuba"), + ("Curaçao", "Curaçao"), + ("Cyprus", "Cyprus"), + ("Czechia", "Czechia"), + ("Côte d'Ivoire", "Côte d'Ivoire"), + ("Denmark", "Denmark"), + ("Djibouti", "Djibouti"), + ("Dominica", "Dominica"), + ("Dominican Republic (the)", "Dominican Republic (the)"), + ("Ecuador", "Ecuador"), + ("Egypt", "Egypt"), + ("El Salvador", "El Salvador"), + ("Equatorial Guinea", "Equatorial Guinea"), + ("Eritrea", "Eritrea"), + ("Estonia", "Estonia"), + ("Eswatini", "Eswatini"), + ("Ethiopia", "Ethiopia"), + ("Falkland Islands (the) [Malvinas]", "Falkland Islands (the) [Malvinas]"), + ("Faroe Islands (the)", "Faroe Islands (the)"), + ("Fiji", "Fiji"), + ("Finland", "Finland"), + ("France", "France"), + ("French Guiana", "French Guiana"), + ("French Polynesia", "French Polynesia"), + ("French Southern Territories (the)", "French Southern Territories (the)"), + ("Gabon", "Gabon"), + ("Gambia (the)", "Gambia (the)"), + ("Georgia", "Georgia"), + ("Germany", "Germany"), + ("Ghana", "Ghana"), + ("Gibraltar", "Gibraltar"), + ("Greece", "Greece"), + ("Greenland", "Greenland"), + ("Grenada", "Grenada"), + ("Guadeloupe", "Guadeloupe"), + ("Guam", "Guam"), + ("Guatemala", "Guatemala"), + ("Guernsey", "Guernsey"), + ("Guinea", "Guinea"), + ("Guinea-Bissau", "Guinea-Bissau"), + ("Guyana", "Guyana"), + ("Haiti", "Haiti"), + ("Heard Island and McDonald Islands", "Heard Island and McDonald Islands"), + ("Holy See (the)", "Holy See (the)"), + ("Honduras", "Honduras"), + ("Hong Kong", "Hong Kong"), + ("Hungary", "Hungary"), + ("Iceland", "Iceland"), + ("India", "India"), + ("Indonesia", "Indonesia"), + ("Iran (Islamic Republic of)", "Iran (Islamic Republic of)"), + ("Iraq", "Iraq"), + ("Ireland", "Ireland"), + ("Isle of Man", "Isle of Man"), + ("Israel", "Israel"), + ("Italy", "Italy"), + ("Jamaica", "Jamaica"), + ("Japan", "Japan"), + ("Jersey", "Jersey"), + ("Jordan", "Jordan"), + ("Kazakhstan", "Kazakhstan"), + ("Kenya", "Kenya"), + ("Kiribati", "Kiribati"), + ( + "Korea (the Democratic People's Republic of)", + "Korea (the Democratic People's Republic of)", + ), + ("Korea (the Republic of)", "Korea (the Republic of)"), + ("Kuwait", "Kuwait"), + ("Kyrgyzstan", "Kyrgyzstan"), + ( + "Lao People's Democratic Republic (the)", + "Lao People's Democratic Republic (the)", + ), + ("Latvia", "Latvia"), + ("Lebanon", "Lebanon"), + ("Lesotho", "Lesotho"), + ("Liberia", "Liberia"), + ("Libya", "Libya"), + ("Liechtenstein", "Liechtenstein"), + ("Lithuania", "Lithuania"), + ("Luxembourg", "Luxembourg"), + ("Macao", "Macao"), + ("Madagascar", "Madagascar"), + ("Malawi", "Malawi"), + ("Malaysia", "Malaysia"), + ("Maldives", "Maldives"), + ("Mali", "Mali"), + ("Malta", "Malta"), + ("Marshall Islands (the)", "Marshall Islands (the)"), + ("Martinique", "Martinique"), + ("Mauritania", "Mauritania"), + ("Mauritius", "Mauritius"), + ("Mayotte", "Mayotte"), + ("Mexico", "Mexico"), + ("Micronesia (Federated States of)", "Micronesia (Federated States of)"), + ("Moldova (the Republic of)", "Moldova (the Republic of)"), + ("Monaco", "Monaco"), + ("Mongolia", "Mongolia"), + ("Montenegro", "Montenegro"), + ("Montserrat", "Montserrat"), + ("Morocco", "Morocco"), + ("Mozambique", "Mozambique"), + ("Myanmar", "Myanmar"), + ("Namibia", "Namibia"), + ("Nauru", "Nauru"), + ("Nepal", "Nepal"), + ("Netherlands (the)", "Netherlands (the)"), + ("New Caledonia", "New Caledonia"), + ("New Zealand", "New Zealand"), + ("Nicaragua", "Nicaragua"), + ("Niger (the)", "Niger (the)"), + ("Nigeria", "Nigeria"), + ("Niue", "Niue"), + ("Norfolk Island", "Norfolk Island"), + ("Northern Mariana Islands (the)", "Northern Mariana Islands (the)"), + ("Norway", "Norway"), + ("Oman", "Oman"), + ("Pakistan", "Pakistan"), + ("Palau", "Palau"), + ("Palestine, State of", "Palestine, State of"), + ("Panama", "Panama"), + ("Papua New Guinea", "Papua New Guinea"), + ("Paraguay", "Paraguay"), + ("Peru", "Peru"), + ("Philippines (the)", "Philippines (the)"), + ("Pitcairn", "Pitcairn"), + ("Poland", "Poland"), + ("Portugal", "Portugal"), + ("Puerto Rico", "Puerto Rico"), + ("Qatar", "Qatar"), + ("Republic of North Macedonia", "Republic of North Macedonia"), + ("Romania", "Romania"), + ("Russian Federation (the)", "Russian Federation (the)"), + ("Rwanda", "Rwanda"), + ("Réunion", "Réunion"), + ("Saint Barthélemy", "Saint Barthélemy"), + ( + "Saint Helena, Ascension and Tristan da Cunha", + "Saint Helena, Ascension and Tristan da Cunha", + ), + ("Saint Kitts and Nevis", "Saint Kitts and Nevis"), + ("Saint Lucia", "Saint Lucia"), + ("Saint Martin (French part)", "Saint Martin (French part)"), + ("Saint Pierre and Miquelon", "Saint Pierre and Miquelon"), + ("Saint Vincent and the Grenadines", "Saint Vincent and the Grenadines"), + ("Samoa", "Samoa"), + ("San Marino", "San Marino"), + ("Sao Tome and Principe", "Sao Tome and Principe"), + ("Saudi Arabia", "Saudi Arabia"), + ("Senegal", "Senegal"), + ("Serbia", "Serbia"), + ("Seychelles", "Seychelles"), + ("Sierra Leone", "Sierra Leone"), + ("Singapore", "Singapore"), + ("Sint Maarten (Dutch part)", "Sint Maarten (Dutch part)"), + ("Slovakia", "Slovakia"), + ("Slovenia", "Slovenia"), + ("Solomon Islands", "Solomon Islands"), + ("Somalia", "Somalia"), + ("South Africa", "South Africa"), + ( + "South Georgia and the South Sandwich Islands", + "South Georgia and the South Sandwich Islands", + ), + ("South Sudan", "South Sudan"), + ("Spain", "Spain"), + ("Sri Lanka", "Sri Lanka"), + ("Sudan (the)", "Sudan (the)"), + ("Suriname", "Suriname"), + ("Svalbard and Jan Mayen", "Svalbard and Jan Mayen"), + ("Sweden", "Sweden"), + ("Switzerland", "Switzerland"), + ("Syrian Arab Republic", "Syrian Arab Republic"), + ("Taiwan (Province of China)", "Taiwan (Province of China)"), + ("Tajikistan", "Tajikistan"), + ("Tanzania, United Republic of", "Tanzania, United Republic of"), + ("Thailand", "Thailand"), + ("Timor-Leste", "Timor-Leste"), + ("Togo", "Togo"), + ("Tokelau", "Tokelau"), + ("Tonga", "Tonga"), + ("Trinidad and Tobago", "Trinidad and Tobago"), + ("Tunisia", "Tunisia"), + ("Turkey", "Turkey"), + ("Turkmenistan", "Turkmenistan"), + ("Turks and Caicos Islands (the)", "Turks and Caicos Islands (the)"), + ("Tuvalu", "Tuvalu"), + ("Uganda", "Uganda"), + ("Ukraine", "Ukraine"), + ("United Arab Emirates (the)", "United Arab Emirates (the)"), + ( + "United Kingdom of Great Britain and Northern Ireland (the)", + "United Kingdom of Great Britain and Northern Ireland (the)", + ), + ( + "United States Minor Outlying Islands (the)", + "United States Minor Outlying Islands (the)", + ), + ("United States of America (the)", "United States of America (the)"), + ("Uruguay", "Uruguay"), + ("Uzbekistan", "Uzbekistan"), + ("Vanuatu", "Vanuatu"), + ( + "Venezuela (Bolivarian Republic of)", + "Venezuela (Bolivarian Republic of)", + ), + ("Viet Nam", "Viet Nam"), + ("Virgin Islands (British)", "Virgin Islands (British)"), + ("Virgin Islands (U.S.)", "Virgin Islands (U.S.)"), + ("Wallis and Futuna", "Wallis and Futuna"), + ("Western Sahara", "Western Sahara"), + ("Yemen", "Yemen"), + ("Zambia", "Zambia"), + ("Zimbabwe", "Zimbabwe"), + ("Åland Islands", "Åland Islands"), ] first_name = models.CharField(max_length=100) From 8f29b252d49121e0d8d786bd21bc6f531a2c8c30 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 12:02:12 +1000 Subject: [PATCH 16/35] Use 'TextChoices' for countries --- ...r_person_country_alter_person_last_name.py | 337 +++++++++++ src/community_db/models.py | 521 +++++++++--------- 2 files changed, 597 insertions(+), 261 deletions(-) create mode 100644 src/community_db/migrations/0002_alter_person_country_alter_person_last_name.py diff --git a/src/community_db/migrations/0002_alter_person_country_alter_person_last_name.py b/src/community_db/migrations/0002_alter_person_country_alter_person_last_name.py new file mode 100644 index 0000000..8f6cde8 --- /dev/null +++ b/src/community_db/migrations/0002_alter_person_country_alter_person_last_name.py @@ -0,0 +1,337 @@ +# Generated by Django 4.1 on 2022-08-25 02:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("community_db", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="person", + name="country", + field=models.CharField( + blank=True, + choices=[ + ("Afghanistan", "Afghanistan"), + ("Albania", "Albania"), + ("Algeria", "Algeria"), + ("American Samoa", "American Samoa"), + ("Andorra", "Andorra"), + ("Angola", "Angola"), + ("Anguilla", "Anguilla"), + ("Antarctica", "Antarctica"), + ("Antigua and Barbuda", "Antigua and Barbuda"), + ("Argentina", "Argentina"), + ("Armenia", "Armenia"), + ("Aruba", "Aruba"), + ("Australia", "Australia"), + ("Austria", "Austria"), + ("Azerbaijan", "Azerbaijan"), + ("Bahamas (the)", "Bahamas (the)"), + ("Bahrain", "Bahrain"), + ("Bangladesh", "Bangladesh"), + ("Barbados", "Barbados"), + ("Belarus", "Belarus"), + ("Belgium", "Belgium"), + ("Belize", "Belize"), + ("Benin", "Benin"), + ("Bermuda", "Bermuda"), + ("Bhutan", "Bhutan"), + ( + "Bolivia (Plurinational State of)", + "Bolivia (Plurinational State of)", + ), + ( + "Bonaire, Sint Eustatius and Saba", + "Bonaire, Sint Eustatius and Saba", + ), + ("Bosnia and Herzegovina", "Bosnia and Herzegovina"), + ("Botswana", "Botswana"), + ("Bouvet Island", "Bouvet Island"), + ("Brazil", "Brazil"), + ( + "British Indian Ocean Territory (the)", + "British Indian Ocean Territory (the)", + ), + ("Brunei Darussalam", "Brunei Darussalam"), + ("Bulgaria", "Bulgaria"), + ("Burkina Faso", "Burkina Faso"), + ("Burundi", "Burundi"), + ("Cabo Verde", "Cabo Verde"), + ("Cambodia", "Cambodia"), + ("Cameroon", "Cameroon"), + ("Canada", "Canada"), + ("Cayman Islands (the)", "Cayman Islands (the)"), + ( + "Central African Republic (the)", + "Central African Republic (the)", + ), + ("Chad", "Chad"), + ("Chile", "Chile"), + ("China", "China"), + ("Christmas Island", "Christmas Island"), + ("Cocos (Keeling) Islands (the)", "Cocos (Keeling) Islands (the)"), + ("Colombia", "Colombia"), + ("Comoros (the)", "Comoros (the)"), + ( + "Congo (the Democratic Republic of the)", + "Congo (the Democratic Republic of the)", + ), + ("Congo (the)", "Congo (the)"), + ("Cook Islands (the)", "Cook Islands (the)"), + ("Costa Rica", "Costa Rica"), + ("Croatia", "Croatia"), + ("Cuba", "Cuba"), + ("Curaçao", "Curaçao"), + ("Cyprus", "Cyprus"), + ("Czechia", "Czechia"), + ("Côte d'Ivoire", "Côte d'Ivoire"), + ("Denmark", "Denmark"), + ("Djibouti", "Djibouti"), + ("Dominica", "Dominica"), + ("Dominican Republic (the)", "Dominican Republic (the)"), + ("Ecuador", "Ecuador"), + ("Egypt", "Egypt"), + ("El Salvador", "El Salvador"), + ("Equatorial Guinea", "Equatorial Guinea"), + ("Eritrea", "Eritrea"), + ("Estonia", "Estonia"), + ("Eswatini", "Eswatini"), + ("Ethiopia", "Ethiopia"), + ( + "Falkland Islands (the) [Malvinas]", + "Falkland Islands (the) [Malvinas]", + ), + ("Faroe Islands (the)", "Faroe Islands (the)"), + ("Fiji", "Fiji"), + ("Finland", "Finland"), + ("France", "France"), + ("French Guiana", "French Guiana"), + ("French Polynesia", "French Polynesia"), + ( + "French Southern Territories (the)", + "French Southern Territories (the)", + ), + ("Gabon", "Gabon"), + ("Gambia (the)", "Gambia (the)"), + ("Georgia", "Georgia"), + ("Germany", "Germany"), + ("Ghana", "Ghana"), + ("Gibraltar", "Gibraltar"), + ("Greece", "Greece"), + ("Greenland", "Greenland"), + ("Grenada", "Grenada"), + ("Guadeloupe", "Guadeloupe"), + ("Guam", "Guam"), + ("Guatemala", "Guatemala"), + ("Guernsey", "Guernsey"), + ("Guinea", "Guinea"), + ("Guinea-Bissau", "Guinea-Bissau"), + ("Guyana", "Guyana"), + ("Haiti", "Haiti"), + ( + "Heard Island and McDonald Islands", + "Heard Island and McDonald Islands", + ), + ("Holy See (the)", "Holy See (the)"), + ("Honduras", "Honduras"), + ("Hong Kong", "Hong Kong"), + ("Hungary", "Hungary"), + ("Iceland", "Iceland"), + ("India", "India"), + ("Indonesia", "Indonesia"), + ("Iran (Islamic Republic of)", "Iran (Islamic Republic of)"), + ("Iraq", "Iraq"), + ("Ireland", "Ireland"), + ("Isle of Man", "Isle of Man"), + ("Israel", "Israel"), + ("Italy", "Italy"), + ("Jamaica", "Jamaica"), + ("Japan", "Japan"), + ("Jersey", "Jersey"), + ("Jordan", "Jordan"), + ("Kazakhstan", "Kazakhstan"), + ("Kenya", "Kenya"), + ("Kiribati", "Kiribati"), + ( + "Korea (the Democratic People's Republic of)", + "Korea (the Democratic People's Republic of)", + ), + ("Korea (the Republic of)", "Korea (the Republic of)"), + ("Kuwait", "Kuwait"), + ("Kyrgyzstan", "Kyrgyzstan"), + ( + "Lao People's Democratic Republic (the)", + "Lao People's Democratic Republic (the)", + ), + ("Latvia", "Latvia"), + ("Lebanon", "Lebanon"), + ("Lesotho", "Lesotho"), + ("Liberia", "Liberia"), + ("Libya", "Libya"), + ("Liechtenstein", "Liechtenstein"), + ("Lithuania", "Lithuania"), + ("Luxembourg", "Luxembourg"), + ("Macao", "Macao"), + ("Madagascar", "Madagascar"), + ("Malawi", "Malawi"), + ("Malaysia", "Malaysia"), + ("Maldives", "Maldives"), + ("Mali", "Mali"), + ("Malta", "Malta"), + ("Marshall Islands (the)", "Marshall Islands (the)"), + ("Martinique", "Martinique"), + ("Mauritania", "Mauritania"), + ("Mauritius", "Mauritius"), + ("Mayotte", "Mayotte"), + ("Mexico", "Mexico"), + ( + "Micronesia (Federated States of)", + "Micronesia (Federated States of)", + ), + ("Moldova (the Republic of)", "Moldova (the Republic of)"), + ("Monaco", "Monaco"), + ("Mongolia", "Mongolia"), + ("Montenegro", "Montenegro"), + ("Montserrat", "Montserrat"), + ("Morocco", "Morocco"), + ("Mozambique", "Mozambique"), + ("Myanmar", "Myanmar"), + ("Namibia", "Namibia"), + ("Nauru", "Nauru"), + ("Nepal", "Nepal"), + ("Netherlands (the)", "Netherlands (the)"), + ("New Caledonia", "New Caledonia"), + ("New Zealand", "New Zealand"), + ("Nicaragua", "Nicaragua"), + ("Niger (the)", "Niger (the)"), + ("Nigeria", "Nigeria"), + ("Niue", "Niue"), + ("Norfolk Island", "Norfolk Island"), + ( + "Northern Mariana Islands (the)", + "Northern Mariana Islands (the)", + ), + ("Norway", "Norway"), + ("Oman", "Oman"), + ("Pakistan", "Pakistan"), + ("Palau", "Palau"), + ("Palestine, State of", "Palestine, State of"), + ("Panama", "Panama"), + ("Papua New Guinea", "Papua New Guinea"), + ("Paraguay", "Paraguay"), + ("Peru", "Peru"), + ("Philippines (the)", "Philippines (the)"), + ("Pitcairn", "Pitcairn"), + ("Poland", "Poland"), + ("Portugal", "Portugal"), + ("Puerto Rico", "Puerto Rico"), + ("Qatar", "Qatar"), + ("Republic of North Macedonia", "Republic of North Macedonia"), + ("Romania", "Romania"), + ("Russian Federation (the)", "Russian Federation (the)"), + ("Rwanda", "Rwanda"), + ("Réunion", "Réunion"), + ("Saint Barthélemy", "Saint Barthélemy"), + ( + "Saint Helena, Ascension and Tristan da Cunha", + "Saint Helena, Ascension and Tristan da Cunha", + ), + ("Saint Kitts and Nevis", "Saint Kitts and Nevis"), + ("Saint Lucia", "Saint Lucia"), + ("Saint Martin (French part)", "Saint Martin (French part)"), + ("Saint Pierre and Miquelon", "Saint Pierre and Miquelon"), + ( + "Saint Vincent and the Grenadines", + "Saint Vincent and the Grenadines", + ), + ("Samoa", "Samoa"), + ("San Marino", "San Marino"), + ("Sao Tome and Principe", "Sao Tome and Principe"), + ("Saudi Arabia", "Saudi Arabia"), + ("Senegal", "Senegal"), + ("Serbia", "Serbia"), + ("Seychelles", "Seychelles"), + ("Sierra Leone", "Sierra Leone"), + ("Singapore", "Singapore"), + ("Sint Maarten (Dutch part)", "Sint Maarten (Dutch part)"), + ("Slovakia", "Slovakia"), + ("Slovenia", "Slovenia"), + ("Solomon Islands", "Solomon Islands"), + ("Somalia", "Somalia"), + ("South Africa", "South Africa"), + ( + "South Georgia and the South Sandwich Islands", + "South Georgia and the South Sandwich Islands", + ), + ("South Sudan", "South Sudan"), + ("Spain", "Spain"), + ("Sri Lanka", "Sri Lanka"), + ("Sudan (the)", "Sudan (the)"), + ("Suriname", "Suriname"), + ("Svalbard and Jan Mayen", "Svalbard and Jan Mayen"), + ("Sweden", "Sweden"), + ("Switzerland", "Switzerland"), + ("Syrian Arab Republic", "Syrian Arab Republic"), + ("Taiwan (Province of China)", "Taiwan (Province of China)"), + ("Tajikistan", "Tajikistan"), + ("Tanzania, United Republic of", "Tanzania, United Republic of"), + ("Thailand", "Thailand"), + ("Timor-Leste", "Timor-Leste"), + ("Togo", "Togo"), + ("Tokelau", "Tokelau"), + ("Tonga", "Tonga"), + ("Trinidad and Tobago", "Trinidad and Tobago"), + ("Tunisia", "Tunisia"), + ("Turkey", "Turkey"), + ("Turkmenistan", "Turkmenistan"), + ( + "Turks and Caicos Islands (the)", + "Turks and Caicos Islands (the)", + ), + ("Tuvalu", "Tuvalu"), + ("Uganda", "Uganda"), + ("Ukraine", "Ukraine"), + ("United Arab Emirates (the)", "United Arab Emirates (the)"), + ( + "United Kingdom of Great Britain and Northern Ireland (the)", + "United Kingdom of Great Britain and Northern Ireland (the)", + ), + ( + "United States Minor Outlying Islands (the)", + "United States Minor Outlying Islands (the)", + ), + ( + "United States of America (the)", + "United States of America (the)", + ), + ("Uruguay", "Uruguay"), + ("Uzbekistan", "Uzbekistan"), + ("Vanuatu", "Vanuatu"), + ( + "Venezuela (Bolivarian Republic of)", + "Venezuela (Bolivarian Republic of)", + ), + ("Viet Nam", "Viet Nam"), + ("Virgin Islands (British)", "Virgin Islands (British)"), + ("Virgin Islands (U.S.)", "Virgin Islands (U.S.)"), + ("Wallis and Futuna", "Wallis and Futuna"), + ("Western Sahara", "Western Sahara"), + ("Yemen", "Yemen"), + ("Zambia", "Zambia"), + ("Zimbabwe", "Zimbabwe"), + ("Åland Islands", "Åland Islands"), + ], + max_length=100, + ), + ), + migrations.AlterField( + model_name="person", + name="last_name", + field=models.CharField(max_length=100), + ), + ] diff --git a/src/community_db/models.py b/src/community_db/models.py index c891b87..05ffc40 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -2,286 +2,285 @@ class Person(models.Model): - COUNTRIES = [ - ("Afghanistan", "Afghanistan"), - ("Albania", "Albania"), - ("Algeria", "Algeria"), - ("American Samoa", "American Samoa"), - ("Andorra", "Andorra"), - ("Angola", "Angola"), - ("Anguilla", "Anguilla"), - ("Antarctica", "Antarctica"), - ("Antigua and Barbuda", "Antigua and Barbuda"), - ("Argentina", "Argentina"), - ("Armenia", "Armenia"), - ("Aruba", "Aruba"), - ("Australia", "Australia"), - ("Austria", "Austria"), - ("Azerbaijan", "Azerbaijan"), - ("Bahamas (the)", "Bahamas (the)"), - ("Bahrain", "Bahrain"), - ("Bangladesh", "Bangladesh"), - ("Barbados", "Barbados"), - ("Belarus", "Belarus"), - ("Belgium", "Belgium"), - ("Belize", "Belize"), - ("Benin", "Benin"), - ("Bermuda", "Bermuda"), - ("Bhutan", "Bhutan"), - ("Bolivia (Plurinational State of)", "Bolivia (Plurinational State of)"), - ("Bonaire, Sint Eustatius and Saba", "Bonaire, Sint Eustatius and Saba"), - ("Bosnia and Herzegovina", "Bosnia and Herzegovina"), - ("Botswana", "Botswana"), - ("Bouvet Island", "Bouvet Island"), - ("Brazil", "Brazil"), - ( + class Countries(models.TextChoices): + AF = ("Afghanistan", "Afghanistan") + AL = ("Albania", "Albania") + DZ = ("Algeria", "Algeria") + AS = ("American Samoa", "American Samoa") + AD = ("Andorra", "Andorra") + AO = ("Angola", "Angola") + AI = ("Anguilla", "Anguilla") + AQ = ("Antarctica", "Antarctica") + AG = ("Antigua and Barbuda", "Antigua and Barbuda") + AR = ("Argentina", "Argentina") + AM = ("Armenia", "Armenia") + AW = ("Aruba", "Aruba") + AU = ("Australia", "Australia") + AT = ("Austria", "Austria") + AZ = ("Azerbaijan", "Azerbaijan") + BS = ("Bahamas (the)", "Bahamas (the)") + BH = ("Bahrain", "Bahrain") + BD = ("Bangladesh", "Bangladesh") + BB = ("Barbados", "Barbados") + BY = ("Belarus", "Belarus") + BE = ("Belgium", "Belgium") + BZ = ("Belize", "Belize") + BJ = ("Benin", "Benin") + BM = ("Bermuda", "Bermuda") + BT = ("Bhutan", "Bhutan") + BO = ("Bolivia (Plurinational State of)", "Bolivia (Plurinational State of)") + BQ = ("Bonaire, Sint Eustatius and Saba", "Bonaire, Sint Eustatius and Saba") + BA = ("Bosnia and Herzegovina", "Bosnia and Herzegovina") + BW = ("Botswana", "Botswana") + BV = ("Bouvet Island", "Bouvet Island") + BR = ("Brazil", "Brazil") + IO = ( "British Indian Ocean Territory (the)", "British Indian Ocean Territory (the)", - ), - ("Brunei Darussalam", "Brunei Darussalam"), - ("Bulgaria", "Bulgaria"), - ("Burkina Faso", "Burkina Faso"), - ("Burundi", "Burundi"), - ("Cabo Verde", "Cabo Verde"), - ("Cambodia", "Cambodia"), - ("Cameroon", "Cameroon"), - ("Canada", "Canada"), - ("Cayman Islands (the)", "Cayman Islands (the)"), - ("Central African Republic (the)", "Central African Republic (the)"), - ("Chad", "Chad"), - ("Chile", "Chile"), - ("China", "China"), - ("Christmas Island", "Christmas Island"), - ("Cocos (Keeling) Islands (the)", "Cocos (Keeling) Islands (the)"), - ("Colombia", "Colombia"), - ("Comoros (the)", "Comoros (the)"), - ( + ) + BN = ("Brunei Darussalam", "Brunei Darussalam") + BG = ("Bulgaria", "Bulgaria") + BF = ("Burkina Faso", "Burkina Faso") + BI = ("Burundi", "Burundi") + CV = ("Cabo Verde", "Cabo Verde") + KH = ("Cambodia", "Cambodia") + CM = ("Cameroon", "Cameroon") + CA = ("Canada", "Canada") + KY = ("Cayman Islands (the)", "Cayman Islands (the)") + CF = ("Central African Republic (the)", "Central African Republic (the)") + TD = ("Chad", "Chad") + CL = ("Chile", "Chile") + CN = ("China", "China") + CX = ("Christmas Island", "Christmas Island") + CC = ("Cocos (Keeling) Islands (the)", "Cocos (Keeling) Islands (the)") + CO = ("Colombia", "Colombia") + KM = ("Comoros (the)", "Comoros (the)") + CD = ( "Congo (the Democratic Republic of the)", "Congo (the Democratic Republic of the)", - ), - ("Congo (the)", "Congo (the)"), - ("Cook Islands (the)", "Cook Islands (the)"), - ("Costa Rica", "Costa Rica"), - ("Croatia", "Croatia"), - ("Cuba", "Cuba"), - ("Curaçao", "Curaçao"), - ("Cyprus", "Cyprus"), - ("Czechia", "Czechia"), - ("Côte d'Ivoire", "Côte d'Ivoire"), - ("Denmark", "Denmark"), - ("Djibouti", "Djibouti"), - ("Dominica", "Dominica"), - ("Dominican Republic (the)", "Dominican Republic (the)"), - ("Ecuador", "Ecuador"), - ("Egypt", "Egypt"), - ("El Salvador", "El Salvador"), - ("Equatorial Guinea", "Equatorial Guinea"), - ("Eritrea", "Eritrea"), - ("Estonia", "Estonia"), - ("Eswatini", "Eswatini"), - ("Ethiopia", "Ethiopia"), - ("Falkland Islands (the) [Malvinas]", "Falkland Islands (the) [Malvinas]"), - ("Faroe Islands (the)", "Faroe Islands (the)"), - ("Fiji", "Fiji"), - ("Finland", "Finland"), - ("France", "France"), - ("French Guiana", "French Guiana"), - ("French Polynesia", "French Polynesia"), - ("French Southern Territories (the)", "French Southern Territories (the)"), - ("Gabon", "Gabon"), - ("Gambia (the)", "Gambia (the)"), - ("Georgia", "Georgia"), - ("Germany", "Germany"), - ("Ghana", "Ghana"), - ("Gibraltar", "Gibraltar"), - ("Greece", "Greece"), - ("Greenland", "Greenland"), - ("Grenada", "Grenada"), - ("Guadeloupe", "Guadeloupe"), - ("Guam", "Guam"), - ("Guatemala", "Guatemala"), - ("Guernsey", "Guernsey"), - ("Guinea", "Guinea"), - ("Guinea-Bissau", "Guinea-Bissau"), - ("Guyana", "Guyana"), - ("Haiti", "Haiti"), - ("Heard Island and McDonald Islands", "Heard Island and McDonald Islands"), - ("Holy See (the)", "Holy See (the)"), - ("Honduras", "Honduras"), - ("Hong Kong", "Hong Kong"), - ("Hungary", "Hungary"), - ("Iceland", "Iceland"), - ("India", "India"), - ("Indonesia", "Indonesia"), - ("Iran (Islamic Republic of)", "Iran (Islamic Republic of)"), - ("Iraq", "Iraq"), - ("Ireland", "Ireland"), - ("Isle of Man", "Isle of Man"), - ("Israel", "Israel"), - ("Italy", "Italy"), - ("Jamaica", "Jamaica"), - ("Japan", "Japan"), - ("Jersey", "Jersey"), - ("Jordan", "Jordan"), - ("Kazakhstan", "Kazakhstan"), - ("Kenya", "Kenya"), - ("Kiribati", "Kiribati"), - ( + ) + CG = ("Congo (the)", "Congo (the)") + CK = ("Cook Islands (the)", "Cook Islands (the)") + CR = ("Costa Rica", "Costa Rica") + HR = ("Croatia", "Croatia") + CU = ("Cuba", "Cuba") + CW = ("Curaçao", "Curaçao") + CY = ("Cyprus", "Cyprus") + CZ = ("Czechia", "Czechia") + CI = ("Côte d'Ivoire", "Côte d'Ivoire") + DK = ("Denmark", "Denmark") + DJ = ("Djibouti", "Djibouti") + DM = ("Dominica", "Dominica") + DO = ("Dominican Republic (the)", "Dominican Republic (the)") + EC = ("Ecuador", "Ecuador") + EG = ("Egypt", "Egypt") + SV = ("El Salvador", "El Salvador") + GQ = ("Equatorial Guinea", "Equatorial Guinea") + ER = ("Eritrea", "Eritrea") + EE = ("Estonia", "Estonia") + SZ = ("Eswatini", "Eswatini") + ET = ("Ethiopia", "Ethiopia") + FK = ("Falkland Islands (the) [Malvinas]", "Falkland Islands (the) [Malvinas]") + FO = ("Faroe Islands (the)", "Faroe Islands (the)") + FJ = ("Fiji", "Fiji") + FI = ("Finland", "Finland") + FR = ("France", "France") + GF = ("French Guiana", "French Guiana") + PF = ("French Polynesia", "French Polynesia") + TF = ("French Southern Territories (the)", "French Southern Territories (the)") + GA = ("Gabon", "Gabon") + GM = ("Gambia (the)", "Gambia (the)") + GE = ("Georgia", "Georgia") + DE = ("Germany", "Germany") + GH = ("Ghana", "Ghana") + GI = ("Gibraltar", "Gibraltar") + GR = ("Greece", "Greece") + GL = ("Greenland", "Greenland") + GD = ("Grenada", "Grenada") + GP = ("Guadeloupe", "Guadeloupe") + GU = ("Guam", "Guam") + GT = ("Guatemala", "Guatemala") + GG = ("Guernsey", "Guernsey") + GN = ("Guinea", "Guinea") + GW = ("Guinea-Bissau", "Guinea-Bissau") + GY = ("Guyana", "Guyana") + HT = ("Haiti", "Haiti") + HM = ("Heard Island and McDonald Islands", "Heard Island and McDonald Islands") + VA = ("Holy See (the)", "Holy See (the)") + HN = ("Honduras", "Honduras") + HK = ("Hong Kong", "Hong Kong") + HU = ("Hungary", "Hungary") + IS = ("Iceland", "Iceland") + IN = ("India", "India") + ID = ("Indonesia", "Indonesia") + IR = ("Iran (Islamic Republic of)", "Iran (Islamic Republic of)") + IQ = ("Iraq", "Iraq") + IE = ("Ireland", "Ireland") + IM = ("Isle of Man", "Isle of Man") + IL = ("Israel", "Israel") + IT = ("Italy", "Italy") + JM = ("Jamaica", "Jamaica") + JP = ("Japan", "Japan") + JE = ("Jersey", "Jersey") + JO = ("Jordan", "Jordan") + KZ = ("Kazakhstan", "Kazakhstan") + KE = ("Kenya", "Kenya") + KI = ("Kiribati", "Kiribati") + KP = ( "Korea (the Democratic People's Republic of)", "Korea (the Democratic People's Republic of)", - ), - ("Korea (the Republic of)", "Korea (the Republic of)"), - ("Kuwait", "Kuwait"), - ("Kyrgyzstan", "Kyrgyzstan"), - ( + ) + KR = ("Korea (the Republic of)", "Korea (the Republic of)") + KW = ("Kuwait", "Kuwait") + KG = ("Kyrgyzstan", "Kyrgyzstan") + LA = ( "Lao People's Democratic Republic (the)", "Lao People's Democratic Republic (the)", - ), - ("Latvia", "Latvia"), - ("Lebanon", "Lebanon"), - ("Lesotho", "Lesotho"), - ("Liberia", "Liberia"), - ("Libya", "Libya"), - ("Liechtenstein", "Liechtenstein"), - ("Lithuania", "Lithuania"), - ("Luxembourg", "Luxembourg"), - ("Macao", "Macao"), - ("Madagascar", "Madagascar"), - ("Malawi", "Malawi"), - ("Malaysia", "Malaysia"), - ("Maldives", "Maldives"), - ("Mali", "Mali"), - ("Malta", "Malta"), - ("Marshall Islands (the)", "Marshall Islands (the)"), - ("Martinique", "Martinique"), - ("Mauritania", "Mauritania"), - ("Mauritius", "Mauritius"), - ("Mayotte", "Mayotte"), - ("Mexico", "Mexico"), - ("Micronesia (Federated States of)", "Micronesia (Federated States of)"), - ("Moldova (the Republic of)", "Moldova (the Republic of)"), - ("Monaco", "Monaco"), - ("Mongolia", "Mongolia"), - ("Montenegro", "Montenegro"), - ("Montserrat", "Montserrat"), - ("Morocco", "Morocco"), - ("Mozambique", "Mozambique"), - ("Myanmar", "Myanmar"), - ("Namibia", "Namibia"), - ("Nauru", "Nauru"), - ("Nepal", "Nepal"), - ("Netherlands (the)", "Netherlands (the)"), - ("New Caledonia", "New Caledonia"), - ("New Zealand", "New Zealand"), - ("Nicaragua", "Nicaragua"), - ("Niger (the)", "Niger (the)"), - ("Nigeria", "Nigeria"), - ("Niue", "Niue"), - ("Norfolk Island", "Norfolk Island"), - ("Northern Mariana Islands (the)", "Northern Mariana Islands (the)"), - ("Norway", "Norway"), - ("Oman", "Oman"), - ("Pakistan", "Pakistan"), - ("Palau", "Palau"), - ("Palestine, State of", "Palestine, State of"), - ("Panama", "Panama"), - ("Papua New Guinea", "Papua New Guinea"), - ("Paraguay", "Paraguay"), - ("Peru", "Peru"), - ("Philippines (the)", "Philippines (the)"), - ("Pitcairn", "Pitcairn"), - ("Poland", "Poland"), - ("Portugal", "Portugal"), - ("Puerto Rico", "Puerto Rico"), - ("Qatar", "Qatar"), - ("Republic of North Macedonia", "Republic of North Macedonia"), - ("Romania", "Romania"), - ("Russian Federation (the)", "Russian Federation (the)"), - ("Rwanda", "Rwanda"), - ("Réunion", "Réunion"), - ("Saint Barthélemy", "Saint Barthélemy"), - ( + ) + LV = ("Latvia", "Latvia") + LB = ("Lebanon", "Lebanon") + LS = ("Lesotho", "Lesotho") + LR = ("Liberia", "Liberia") + LY = ("Libya", "Libya") + LI = ("Liechtenstein", "Liechtenstein") + LT = ("Lithuania", "Lithuania") + LU = ("Luxembourg", "Luxembourg") + MO = ("Macao", "Macao") + MG = ("Madagascar", "Madagascar") + MW = ("Malawi", "Malawi") + MY = ("Malaysia", "Malaysia") + MV = ("Maldives", "Maldives") + ML = ("Mali", "Mali") + MT = ("Malta", "Malta") + MH = ("Marshall Islands (the)", "Marshall Islands (the)") + MQ = ("Martinique", "Martinique") + MR = ("Mauritania", "Mauritania") + MU = ("Mauritius", "Mauritius") + YT = ("Mayotte", "Mayotte") + MX = ("Mexico", "Mexico") + FM = ("Micronesia (Federated States of)", "Micronesia (Federated States of)") + MD = ("Moldova (the Republic of)", "Moldova (the Republic of)") + MC = ("Monaco", "Monaco") + MN = ("Mongolia", "Mongolia") + ME = ("Montenegro", "Montenegro") + MS = ("Montserrat", "Montserrat") + MA = ("Morocco", "Morocco") + MZ = ("Mozambique", "Mozambique") + MM = ("Myanmar", "Myanmar") + NA = ("Namibia", "Namibia") + NR = ("Nauru", "Nauru") + NP = ("Nepal", "Nepal") + NL = ("Netherlands (the)", "Netherlands (the)") + NC = ("New Caledonia", "New Caledonia") + NZ = ("New Zealand", "New Zealand") + NI = ("Nicaragua", "Nicaragua") + NE = ("Niger (the)", "Niger (the)") + NG = ("Nigeria", "Nigeria") + NU = ("Niue", "Niue") + NF = ("Norfolk Island", "Norfolk Island") + MP = ("Northern Mariana Islands (the)", "Northern Mariana Islands (the)") + NO = ("Norway", "Norway") + OM = ("Oman", "Oman") + PK = ("Pakistan", "Pakistan") + PW = ("Palau", "Palau") + PS = ("Palestine, State of", "Palestine, State of") + PA = ("Panama", "Panama") + PG = ("Papua New Guinea", "Papua New Guinea") + PY = ("Paraguay", "Paraguay") + PE = ("Peru", "Peru") + PH = ("Philippines (the)", "Philippines (the)") + PN = ("Pitcairn", "Pitcairn") + PL = ("Poland", "Poland") + PT = ("Portugal", "Portugal") + PR = ("Puerto Rico", "Puerto Rico") + QA = ("Qatar", "Qatar") + MK = ("Republic of North Macedonia", "Republic of North Macedonia") + RO = ("Romania", "Romania") + RU = ("Russian Federation (the)", "Russian Federation (the)") + RW = ("Rwanda", "Rwanda") + RE = ("Réunion", "Réunion") + BL = ("Saint Barthélemy", "Saint Barthélemy") + SH = ( "Saint Helena, Ascension and Tristan da Cunha", "Saint Helena, Ascension and Tristan da Cunha", - ), - ("Saint Kitts and Nevis", "Saint Kitts and Nevis"), - ("Saint Lucia", "Saint Lucia"), - ("Saint Martin (French part)", "Saint Martin (French part)"), - ("Saint Pierre and Miquelon", "Saint Pierre and Miquelon"), - ("Saint Vincent and the Grenadines", "Saint Vincent and the Grenadines"), - ("Samoa", "Samoa"), - ("San Marino", "San Marino"), - ("Sao Tome and Principe", "Sao Tome and Principe"), - ("Saudi Arabia", "Saudi Arabia"), - ("Senegal", "Senegal"), - ("Serbia", "Serbia"), - ("Seychelles", "Seychelles"), - ("Sierra Leone", "Sierra Leone"), - ("Singapore", "Singapore"), - ("Sint Maarten (Dutch part)", "Sint Maarten (Dutch part)"), - ("Slovakia", "Slovakia"), - ("Slovenia", "Slovenia"), - ("Solomon Islands", "Solomon Islands"), - ("Somalia", "Somalia"), - ("South Africa", "South Africa"), - ( + ) + KN = ("Saint Kitts and Nevis", "Saint Kitts and Nevis") + LC = ("Saint Lucia", "Saint Lucia") + MF = ("Saint Martin (French part)", "Saint Martin (French part)") + PM = ("Saint Pierre and Miquelon", "Saint Pierre and Miquelon") + VC = ("Saint Vincent and the Grenadines", "Saint Vincent and the Grenadines") + WS = ("Samoa", "Samoa") + SM = ("San Marino", "San Marino") + ST = ("Sao Tome and Principe", "Sao Tome and Principe") + SA = ("Saudi Arabia", "Saudi Arabia") + SN = ("Senegal", "Senegal") + RS = ("Serbia", "Serbia") + SC = ("Seychelles", "Seychelles") + SL = ("Sierra Leone", "Sierra Leone") + SG = ("Singapore", "Singapore") + SX = ("Sint Maarten (Dutch part)", "Sint Maarten (Dutch part)") + SK = ("Slovakia", "Slovakia") + SI = ("Slovenia", "Slovenia") + SB = ("Solomon Islands", "Solomon Islands") + SO = ("Somalia", "Somalia") + ZA = ("South Africa", "South Africa") + GS = ( "South Georgia and the South Sandwich Islands", "South Georgia and the South Sandwich Islands", - ), - ("South Sudan", "South Sudan"), - ("Spain", "Spain"), - ("Sri Lanka", "Sri Lanka"), - ("Sudan (the)", "Sudan (the)"), - ("Suriname", "Suriname"), - ("Svalbard and Jan Mayen", "Svalbard and Jan Mayen"), - ("Sweden", "Sweden"), - ("Switzerland", "Switzerland"), - ("Syrian Arab Republic", "Syrian Arab Republic"), - ("Taiwan (Province of China)", "Taiwan (Province of China)"), - ("Tajikistan", "Tajikistan"), - ("Tanzania, United Republic of", "Tanzania, United Republic of"), - ("Thailand", "Thailand"), - ("Timor-Leste", "Timor-Leste"), - ("Togo", "Togo"), - ("Tokelau", "Tokelau"), - ("Tonga", "Tonga"), - ("Trinidad and Tobago", "Trinidad and Tobago"), - ("Tunisia", "Tunisia"), - ("Turkey", "Turkey"), - ("Turkmenistan", "Turkmenistan"), - ("Turks and Caicos Islands (the)", "Turks and Caicos Islands (the)"), - ("Tuvalu", "Tuvalu"), - ("Uganda", "Uganda"), - ("Ukraine", "Ukraine"), - ("United Arab Emirates (the)", "United Arab Emirates (the)"), - ( + ) + SS = ("South Sudan", "South Sudan") + ES = ("Spain", "Spain") + LK = ("Sri Lanka", "Sri Lanka") + SD = ("Sudan (the)", "Sudan (the)") + SR = ("Suriname", "Suriname") + SJ = ("Svalbard and Jan Mayen", "Svalbard and Jan Mayen") + SE = ("Sweden", "Sweden") + CH = ("Switzerland", "Switzerland") + SY = ("Syrian Arab Republic", "Syrian Arab Republic") + TW = ("Taiwan (Province of China)", "Taiwan (Province of China)") + TJ = ("Tajikistan", "Tajikistan") + TZ = ("Tanzania, United Republic of", "Tanzania, United Republic of") + TH = ("Thailand", "Thailand") + TL = ("Timor-Leste", "Timor-Leste") + TG = ("Togo", "Togo") + TK = ("Tokelau", "Tokelau") + TO = ("Tonga", "Tonga") + TT = ("Trinidad and Tobago", "Trinidad and Tobago") + TN = ("Tunisia", "Tunisia") + TR = ("Turkey", "Turkey") + TM = ("Turkmenistan", "Turkmenistan") + TC = ("Turks and Caicos Islands (the)", "Turks and Caicos Islands (the)") + TV = ("Tuvalu", "Tuvalu") + UG = ("Uganda", "Uganda") + UA = ("Ukraine", "Ukraine") + AE = ("United Arab Emirates (the)", "United Arab Emirates (the)") + GB = ( "United Kingdom of Great Britain and Northern Ireland (the)", "United Kingdom of Great Britain and Northern Ireland (the)", - ), - ( + ) + UM = ( "United States Minor Outlying Islands (the)", "United States Minor Outlying Islands (the)", - ), - ("United States of America (the)", "United States of America (the)"), - ("Uruguay", "Uruguay"), - ("Uzbekistan", "Uzbekistan"), - ("Vanuatu", "Vanuatu"), - ( + ) + US = ("United States of America (the)", "United States of America (the)") + UY = ("Uruguay", "Uruguay") + UZ = ("Uzbekistan", "Uzbekistan") + VU = ("Vanuatu", "Vanuatu") + VE = ( "Venezuela (Bolivarian Republic of)", "Venezuela (Bolivarian Republic of)", - ), - ("Viet Nam", "Viet Nam"), - ("Virgin Islands (British)", "Virgin Islands (British)"), - ("Virgin Islands (U.S.)", "Virgin Islands (U.S.)"), - ("Wallis and Futuna", "Wallis and Futuna"), - ("Western Sahara", "Western Sahara"), - ("Yemen", "Yemen"), - ("Zambia", "Zambia"), - ("Zimbabwe", "Zimbabwe"), - ("Åland Islands", "Åland Islands"), - ] + ) + VN = ("Viet Nam", "Viet Nam") + VG = ("Virgin Islands (British)", "Virgin Islands (British)") + VI = ("Virgin Islands (U.S.)", "Virgin Islands (U.S.)") + WF = ("Wallis and Futuna", "Wallis and Futuna") + EH = ("Western Sahara", "Western Sahara") + YE = ("Yemen", "Yemen") + ZM = ("Zambia", "Zambia") + ZW = ("Zimbabwe", "Zimbabwe") + AX = ("Åland Islands", "Åland Islands") first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) - country = models.CharField(max_length=100, choices=COUNTRIES, blank=True) + country = models.CharField(max_length=100, choices=Countries.choices, blank=True) mobile_number = models.CharField(max_length=20, blank=True) From 9fdd85c5eb17124d40dd70fad90fcac9d5ea7baa Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 12:11:47 +1000 Subject: [PATCH 17/35] Change form to use UL --- .../templates/community_db/person_form_in_base.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/community_db/templates/community_db/person_form_in_base.html b/src/community_db/templates/community_db/person_form_in_base.html index b291a08..ea9b21e 100644 --- a/src/community_db/templates/community_db/person_form_in_base.html +++ b/src/community_db/templates/community_db/person_form_in_base.html @@ -4,7 +4,9 @@
    {% csrf_token %} - {{ form.as_p }} + + {{ form.as_ul }} +
    {% endblock %} \ No newline at end of file From a0d9e2a60b2a8b192d4eb81502dc9e52507de1e9 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 12:24:00 +1000 Subject: [PATCH 18/35] Add ChoicesField --- src/community_db/forms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/community_db/forms.py b/src/community_db/forms.py index cc1df70..e70c032 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -1,5 +1,7 @@ from django import forms +from .models import Person + class QuickSearchForm(forms.Form): search = forms.CharField(max_length=100, required=False) @@ -8,5 +10,5 @@ class QuickSearchForm(forms.Form): class PersonForm(forms.Form): first_name = forms.CharField(max_length=100) last_name = forms.CharField(max_length=100) - country = forms.CharField(max_length=100, required=False) + country = forms.ChoiceField(required=False, choices=Person.Countries.choices) mobile_number = forms.CharField(max_length=20, required=False) From f0363b9c752ccaafccecfc092d0cdcfa812f4006 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 12:32:03 +1000 Subject: [PATCH 19/35] Switch to using ModelForm --- src/community_db/forms.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/community_db/forms.py b/src/community_db/forms.py index e70c032..9afbdea 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -7,8 +7,8 @@ class QuickSearchForm(forms.Form): search = forms.CharField(max_length=100, required=False) -class PersonForm(forms.Form): - first_name = forms.CharField(max_length=100) - last_name = forms.CharField(max_length=100) - country = forms.ChoiceField(required=False, choices=Person.Countries.choices) - mobile_number = forms.CharField(max_length=20, required=False) +class PersonForm(forms.ModelForm): + class Meta: + model = Person + # fields = ["first_name", "last_name", "country", "mobile_number"] + exclude = [] From cc42fafe54905e9b70e89cbdff09e48202668c19 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Thu, 25 Aug 2022 12:32:23 +1000 Subject: [PATCH 20/35] Use instance feature of ModelForm --- src/community_db/views.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index bd4f2ca..1b0420c 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -32,23 +32,12 @@ def detail_person_with_template(request, pk): def edit_person_with_template(request, pk): person = get_object_or_404(Person, id=pk) if request.POST: - form = PersonForm(request.POST) + form = PersonForm(request.POST, instance=person) if form.is_valid(): - person.first_name = form.cleaned_data["first_name"] - person.last_name = form.cleaned_data["last_name"] - person.country = form.cleaned_data["country"] - person.mobile_number = form.cleaned_data["mobile_number"] - person.save() + form.save() return HttpResponseRedirect(reverse("fbv-person-detail", args=[person.id])) else: - form = PersonForm( - { - "first_name": person.first_name, - "last_name": person.last_name, - "country": person.country, - "mobile_number": person.mobile_number, - } - ) + form = PersonForm(instance=person) context = {"object": person, "form": form} return render(request, "community_db/person_form_in_base.html", context) From 541e9da4c24f4054570296a87a1cfae70151d70c Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 29 Aug 2022 13:51:18 +1000 Subject: [PATCH 21/35] Add PersonUpdateView --- src/community_db/views.py | 11 ++++++++++- src/pacificconnect/urls.py | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index 1b0420c..119dd2e 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse -from django.views.generic import DetailView, ListView +from django.views.generic import DetailView, ListView, UpdateView from .forms import PersonForm from .models import Person @@ -81,3 +81,12 @@ def get_context_data(self, **kwargs): class PersonDetailView(DetailView): model = Person template_name = "community_db/person_detail_in_base.html" + + +class PersonUpdateView(UpdateView): + model = Person + fields = ["first_name", "last_name", "country", "mobile_number"] + template_name = "community_db/person_form_in_base.html" + + def get_success_url(self): + return reverse("cbv-person-detail", args=[self.object.id]) diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index ad0c45c..0e0d83e 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -37,4 +37,9 @@ views.PersonDetailView.as_view(), name="cbv-person-detail", ), + path( + "cbv/people//edit/", + views.PersonUpdateView.as_view(), + name="cbv-person-edit", + ), ] From 9baad9d5ae90211faa33a321a1528dcb7ecce975 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 29 Aug 2022 14:21:04 +1000 Subject: [PATCH 22/35] Add basic check_my_auth view --- src/community_db/views.py | 11 ++++++++++- src/pacificconnect/urls.py | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/community_db/views.py b/src/community_db/views.py index 119dd2e..6612035 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,5 +1,5 @@ from django.db import models -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views.generic import DetailView, ListView, UpdateView @@ -90,3 +90,12 @@ class PersonUpdateView(UpdateView): def get_success_url(self): return reverse("cbv-person-detail", args=[self.object.id]) + + +def check_my_auth(request): + output = [""] + output.append(f"Is anonymous: {request.user.is_anonymous}") + output.append(f"Is authenticated: {request.user.is_authenticated}") + output.append(f"Username: {request.user.username}") + output.append("") + return HttpResponse("
    ".join(output)) diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 0e0d83e..e268a6f 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -20,6 +20,7 @@ urlpatterns = [ path("admin/", admin.site.urls), + path("check-my-auth/", views.check_my_auth), path("fbv/people/", views.list_persons_with_template, name="fbv-person-list"), path( "fbv/people//", From 893fbf4d8775d645f42a5971b64d5fb3e40edca3 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 29 Aug 2022 14:37:33 +1000 Subject: [PATCH 23/35] Login --- src/community_db/templates/cbv-login-form.html | 10 ++++++++++ src/community_db/views.py | 10 ++++++++++ src/pacificconnect/urls.py | 1 + 3 files changed, 21 insertions(+) create mode 100644 src/community_db/templates/cbv-login-form.html diff --git a/src/community_db/templates/cbv-login-form.html b/src/community_db/templates/cbv-login-form.html new file mode 100644 index 0000000..21316eb --- /dev/null +++ b/src/community_db/templates/cbv-login-form.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +
    +
    + {% csrf_token %} + {{ form.as_p }} + +
    +{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index 6612035..f620b90 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,5 @@ +from django.contrib.auth.decorators import login_required +from django.contrib.auth.views import LoginView from django.db import models from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render @@ -92,6 +94,7 @@ def get_success_url(self): return reverse("cbv-person-detail", args=[self.object.id]) +@login_required def check_my_auth(request): output = [""] output.append(f"Is anonymous: {request.user.is_anonymous}") @@ -99,3 +102,10 @@ def check_my_auth(request): output.append(f"Username: {request.user.username}") output.append("") return HttpResponse("
    ".join(output)) + + +class UserLoginView(LoginView): + template_name = "cbv-login-form.html" + + def get_success_url(self): + return reverse("cbv-person-list") diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index e268a6f..1bd02aa 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -43,4 +43,5 @@ views.PersonUpdateView.as_view(), name="cbv-person-edit", ), + path("accounts/login/", views.UserLoginView.as_view(), name="login"), ] From 793cdb56692ecdfcf51db83a75b120c1ee76496d Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Wed, 31 Aug 2022 20:26:19 +1000 Subject: [PATCH 24/35] Add permission check --- .../migrations/0003_alter_person_options.py | 17 +++++++++++++++++ src/community_db/models.py | 5 +++++ src/community_db/views.py | 6 +++--- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/community_db/migrations/0003_alter_person_options.py diff --git a/src/community_db/migrations/0003_alter_person_options.py b/src/community_db/migrations/0003_alter_person_options.py new file mode 100644 index 0000000..77145a6 --- /dev/null +++ b/src/community_db/migrations/0003_alter_person_options.py @@ -0,0 +1,17 @@ +# Generated by Django 4.1 on 2022-08-31 09:50 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("community_db", "0002_alter_person_country_alter_person_last_name"), + ] + + operations = [ + migrations.AlterModelOptions( + name="person", + options={"permissions": [("edit_profile", "Can edit a profile")]}, + ), + ] diff --git a/src/community_db/models.py b/src/community_db/models.py index 05ffc40..4a34633 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -284,3 +284,8 @@ class Countries(models.TextChoices): last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, choices=Countries.choices, blank=True) mobile_number = models.CharField(max_length=20, blank=True) + + class Meta: + permissions = [ + ("edit_profile", "Can edit a profile"), + ] diff --git a/src/community_db/views.py b/src/community_db/views.py index f620b90..b5cef96 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,4 +1,4 @@ -from django.contrib.auth.decorators import login_required +from django.contrib.auth.decorators import permission_required from django.contrib.auth.views import LoginView from django.db import models from django.http import HttpResponse, HttpResponseRedirect @@ -31,6 +31,7 @@ def detail_person_with_template(request, pk): return render(request, "community_db/person_detail_in_base.html", context) +@permission_required("edit_profile") def edit_person_with_template(request, pk): person = get_object_or_404(Person, id=pk) if request.POST: @@ -94,7 +95,6 @@ def get_success_url(self): return reverse("cbv-person-detail", args=[self.object.id]) -@login_required def check_my_auth(request): output = [""] output.append(f"Is anonymous: {request.user.is_anonymous}") @@ -107,5 +107,5 @@ def check_my_auth(request): class UserLoginView(LoginView): template_name = "cbv-login-form.html" - def get_success_url(self): + def get_default_success_url(self): return reverse("cbv-person-list") From 9790b0bdd8461b10fb8b7ec7cb76ed0c65870bb2 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Wed, 31 Aug 2022 21:26:13 +1000 Subject: [PATCH 25/35] Edit only own profile --- src/community_db/forms.py | 3 +-- .../migrations/0004_person_user.py | 25 +++++++++++++++++++ src/community_db/models.py | 4 +++ src/community_db/views.py | 10 +++++--- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/community_db/migrations/0004_person_user.py diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 9afbdea..40d6f81 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -10,5 +10,4 @@ class QuickSearchForm(forms.Form): class PersonForm(forms.ModelForm): class Meta: model = Person - # fields = ["first_name", "last_name", "country", "mobile_number"] - exclude = [] + fields = ["first_name", "last_name", "country", "mobile_number"] diff --git a/src/community_db/migrations/0004_person_user.py b/src/community_db/migrations/0004_person_user.py new file mode 100644 index 0000000..a849875 --- /dev/null +++ b/src/community_db/migrations/0004_person_user.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1 on 2022-08-31 10:58 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("community_db", "0003_alter_person_options"), + ] + + operations = [ + migrations.AddField( + model_name="person", + name="user", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to=settings.AUTH_USER_MODEL, + ), + ), + ] diff --git a/src/community_db/models.py b/src/community_db/models.py index 4a34633..f7c60a6 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.db import models @@ -284,6 +285,9 @@ class Countries(models.TextChoices): last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, choices=Countries.choices, blank=True) mobile_number = models.CharField(max_length=20, blank=True) + user = models.ForeignKey( + settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL + ) class Meta: permissions = [ diff --git a/src/community_db/views.py b/src/community_db/views.py index b5cef96..3eb05ca 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,5 +1,5 @@ -from django.contrib.auth.decorators import permission_required -from django.contrib.auth.views import LoginView +from django.contrib.auth.decorators import login_required +from django.contrib.auth.views import LoginView, redirect_to_login from django.db import models from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render @@ -31,9 +31,13 @@ def detail_person_with_template(request, pk): return render(request, "community_db/person_detail_in_base.html", context) -@permission_required("edit_profile") +@login_required def edit_person_with_template(request, pk): person = get_object_or_404(Person, id=pk) + + if person.user != request.user: + return redirect_to_login(request.path) + if request.POST: form = PersonForm(request.POST, instance=person) if form.is_valid(): From 427150ea154e25cc9e331dc80f47988ac03362bd Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Wed, 31 Aug 2022 22:03:45 +1000 Subject: [PATCH 26/35] Handle multiple users for profile --- .../0005_remove_person_user_person_users.py | 24 +++++++++++++++++++ src/community_db/models.py | 4 +--- src/community_db/views.py | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/community_db/migrations/0005_remove_person_user_person_users.py diff --git a/src/community_db/migrations/0005_remove_person_user_person_users.py b/src/community_db/migrations/0005_remove_person_user_person_users.py new file mode 100644 index 0000000..ae02162 --- /dev/null +++ b/src/community_db/migrations/0005_remove_person_user_person_users.py @@ -0,0 +1,24 @@ +# Generated by Django 4.1 on 2022-08-31 11:26 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("community_db", "0004_person_user"), + ] + + operations = [ + migrations.RemoveField( + model_name="person", + name="user", + ), + migrations.AddField( + model_name="person", + name="users", + field=models.ManyToManyField(to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/src/community_db/models.py b/src/community_db/models.py index f7c60a6..b7201c5 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -285,9 +285,7 @@ class Countries(models.TextChoices): last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, choices=Countries.choices, blank=True) mobile_number = models.CharField(max_length=20, blank=True) - user = models.ForeignKey( - settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL - ) + users = models.ManyToManyField(settings.AUTH_USER_MODEL) class Meta: permissions = [ diff --git a/src/community_db/views.py b/src/community_db/views.py index 3eb05ca..4212e88 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -35,7 +35,7 @@ def detail_person_with_template(request, pk): def edit_person_with_template(request, pk): person = get_object_or_404(Person, id=pk) - if person.user != request.user: + if not person.users.contains(request.user): return redirect_to_login(request.path) if request.POST: From 37a1e42ed5ed6e98c5f41b01d6e88f0dea031a3a Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 12:44:16 +1000 Subject: [PATCH 27/35] Initial advanced search --- src/community_db/forms.py | 7 +++ .../community_db/person_form_in_base.html | 4 +- .../community_db/person_search_form.html | 24 ++++++++++ src/community_db/views.py | 46 ++++++++++++++++++- src/pacificconnect/urls.py | 5 ++ 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/community_db/templates/community_db/person_search_form.html diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 40d6f81..3c6c114 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -11,3 +11,10 @@ class PersonForm(forms.ModelForm): class Meta: model = Person fields = ["first_name", "last_name", "country", "mobile_number"] + + +class PersonSearchForm(forms.Form): + first_name = forms.CharField(max_length=100, required=False) + last_name = forms.CharField(max_length=100, required=False) + country = forms.CharField(max_length=100, required=False) + mobile_number = forms.CharField(max_length=20, required=False) diff --git a/src/community_db/templates/community_db/person_form_in_base.html b/src/community_db/templates/community_db/person_form_in_base.html index ea9b21e..b291a08 100644 --- a/src/community_db/templates/community_db/person_form_in_base.html +++ b/src/community_db/templates/community_db/person_form_in_base.html @@ -4,9 +4,7 @@
    {% csrf_token %} - - {{ form.as_ul }} -
    + {{ form.as_p }}
    {% endblock %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_search_form.html b/src/community_db/templates/community_db/person_search_form.html new file mode 100644 index 0000000..ce1e6e3 --- /dev/null +++ b/src/community_db/templates/community_db/person_search_form.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} + +{% block content %} +
    +
    + {{ form.as_p }} + +
    + + {% if object_list %} + This is my list of folks + + {% else %} + No profiles match that search + {% endif %} +{% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index 4212e88..a24eb02 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,4 +1,4 @@ -from django.contrib.auth.decorators import login_required +from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.views import LoginView, redirect_to_login from django.db import models from django.http import HttpResponse, HttpResponseRedirect @@ -6,7 +6,7 @@ from django.urls import reverse from django.views.generic import DetailView, ListView, UpdateView -from .forms import PersonForm +from .forms import PersonForm, PersonSearchForm from .models import Person # FUNCTION BASED VIEWS @@ -49,6 +49,48 @@ def edit_person_with_template(request, pk): return render(request, "community_db/person_form_in_base.html", context) +def search_persons_with_template_AND(request): + search_form = PersonSearchForm(request.GET) + + persons = Person.objects.all() + search_form.is_valid() + cleaned_data = search_form.cleaned_data + if cleaned_data.get("first_name"): + persons = persons.filter(first_name__icontains=cleaned_data["first_name"]) + if cleaned_data.get("last_name"): + persons = persons.filter(last_name__icontains=cleaned_data["last_name"]) + if cleaned_data.get("country"): + persons = persons.filter(country=cleaned_data["country"]) + if cleaned_data["mobile_number"]: + persons = persons.filter(mobile_number__icontains=cleaned_data["mobile_number"]) + + context = {"object_list": persons, "form": search_form} + return render(request, "community_db/person_search_form.html", context) + + +def search_persons_with_template_OR(request): + search_form = PersonSearchForm(request.GET) + + search_form.is_valid() + cleaned_data = search_form.cleaned_data + search_filters = models.Q() + if cleaned_data.get("first_name"): + search_filters &= models.Q(first_name__icontains=cleaned_data["first_name"]) + if cleaned_data.get("last_name"): + search_filters &= models.Q(last_name__icontains=cleaned_data["last_name"]) + if cleaned_data.get("country"): + search_filters &= models.Q(country=cleaned_data["country"]) + if cleaned_data["mobile_number"]: + search_filters &= models.Q( + mobile_number__icontains=cleaned_data["mobile_number"] + ) + + persons = Person.objects.filter(search_filters) + + context = {"object_list": persons, "form": search_form} + return render(request, "community_db/person_search_form.html", context) + + # CLASS BASED VIEWS class PersonListView(ListView): model = Person diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 1bd02aa..ab546fc 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -32,6 +32,11 @@ views.edit_person_with_template, name="fbv-person-edit", ), + path( + "fbv/people/search/", + views.search_persons_with_template_AND, + name="fbv-person-search", + ), path("cbv/people/", views.PersonListView.as_view(), name="cbv-person-list"), path( "cbv/people//", From c1d173f30b7532e699e5a210cf4c533e3a00e57e Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 12:49:42 +1000 Subject: [PATCH 28/35] Template list include --- .../community_db/person_list_in_base.html | 15 +-------------- .../templates/community_db/person_list_part.html | 14 ++++++++++++++ .../community_db/person_search_form.html | 15 +-------------- 3 files changed, 16 insertions(+), 28 deletions(-) create mode 100644 src/community_db/templates/community_db/person_list_part.html diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index d003e30..2cf4ac1 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -1,18 +1,5 @@ {% extends "base.html" %} {% block content %} - {% if object_list %} - This is my list of folks - - {% else %} - No profiles match that search - {% endif %} +{% include "./person_list_part.html" %} {% endblock %} diff --git a/src/community_db/templates/community_db/person_list_part.html b/src/community_db/templates/community_db/person_list_part.html new file mode 100644 index 0000000..f0d82ee --- /dev/null +++ b/src/community_db/templates/community_db/person_list_part.html @@ -0,0 +1,14 @@ +{% if object_list %} +This is my list of folks + +{% else %} +No profiles match that search +{% endif %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_search_form.html b/src/community_db/templates/community_db/person_search_form.html index ce1e6e3..5568843 100644 --- a/src/community_db/templates/community_db/person_search_form.html +++ b/src/community_db/templates/community_db/person_search_form.html @@ -7,18 +7,5 @@ - {% if object_list %} - This is my list of folks - - {% else %} - No profiles match that search - {% endif %} + {% include "./person_list_part.html" %} {% endblock %} From fdd9d6a17f2ea0c74aaaa61b78054c8180db044a Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:00:00 +1000 Subject: [PATCH 29/35] Template include custom text --- .../templates/community_db/person_list_in_base.html | 2 +- src/community_db/templates/community_db/person_list_part.html | 4 ++-- .../templates/community_db/person_search_form.html | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index 2cf4ac1..641650e 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -1,5 +1,5 @@ {% extends "base.html" %} {% block content %} -{% include "./person_list_part.html" %} +{% include "./person_list_part.html" with list_message="This is my list of folks" empty_message="No profiles match that search" %} {% endblock %} diff --git a/src/community_db/templates/community_db/person_list_part.html b/src/community_db/templates/community_db/person_list_part.html index f0d82ee..8adbcac 100644 --- a/src/community_db/templates/community_db/person_list_part.html +++ b/src/community_db/templates/community_db/person_list_part.html @@ -1,5 +1,5 @@ {% if object_list %} -This is my list of folks +{{ list_message }}
      {% for person in object_list %}
    • @@ -10,5 +10,5 @@ {% endfor %}
    {% else %} -No profiles match that search +{{ empty_message }} {% endif %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_search_form.html b/src/community_db/templates/community_db/person_search_form.html index 5568843..7b1dd96 100644 --- a/src/community_db/templates/community_db/person_search_form.html +++ b/src/community_db/templates/community_db/person_search_form.html @@ -7,5 +7,5 @@ - {% include "./person_list_part.html" %} + {% include "./person_list_part.html" with list_message="Search Results" empty_message="No profiles found" %} {% endblock %} From 8103367f4beb92e555c826e79df6c4f461788bba Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:08:32 +1000 Subject: [PATCH 30/35] Template include custom text 2 --- .../community_db/person_list_in_base.html | 2 +- .../community_db/person_list_part.html | 26 +++++++++++-------- .../person_list_part_advanced_search.html | 9 +++++++ .../community_db/person_search_form.html | 4 +-- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 src/community_db/templates/community_db/person_list_part_advanced_search.html diff --git a/src/community_db/templates/community_db/person_list_in_base.html b/src/community_db/templates/community_db/person_list_in_base.html index 641650e..2cf4ac1 100644 --- a/src/community_db/templates/community_db/person_list_in_base.html +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -1,5 +1,5 @@ {% extends "base.html" %} {% block content %} -{% include "./person_list_part.html" with list_message="This is my list of folks" empty_message="No profiles match that search" %} +{% include "./person_list_part.html" %} {% endblock %} diff --git a/src/community_db/templates/community_db/person_list_part.html b/src/community_db/templates/community_db/person_list_part.html index 8adbcac..0a78534 100644 --- a/src/community_db/templates/community_db/person_list_part.html +++ b/src/community_db/templates/community_db/person_list_part.html @@ -1,14 +1,18 @@ {% if object_list %} -{{ list_message }} - + {% block person_list_heading %} + This is my list of folks + {% endblock %} + {% else %} -{{ empty_message }} + {% block empty_message %} + No profiles match that search + {% endblock %} {% endif %} \ No newline at end of file diff --git a/src/community_db/templates/community_db/person_list_part_advanced_search.html b/src/community_db/templates/community_db/person_list_part_advanced_search.html new file mode 100644 index 0000000..f776acb --- /dev/null +++ b/src/community_db/templates/community_db/person_list_part_advanced_search.html @@ -0,0 +1,9 @@ +{% extends "./person_list_part.html" %} + +{% block person_list_heading %} +Search Results +{% endblock %} + +{% block empty_message %} +No Profiles Found +{% endblock %} diff --git a/src/community_db/templates/community_db/person_search_form.html b/src/community_db/templates/community_db/person_search_form.html index 7b1dd96..a3486df 100644 --- a/src/community_db/templates/community_db/person_search_form.html +++ b/src/community_db/templates/community_db/person_search_form.html @@ -6,6 +6,6 @@ {{ form.as_p }} - - {% include "./person_list_part.html" with list_message="Search Results" empty_message="No profiles found" %} + + {% include "./person_list_part_advanced_search.html" %} {% endblock %} From 1df30a5bca2063633aed505e79d21460bcac85c2 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:09:49 +1000 Subject: [PATCH 31/35] Choices but no blank option --- src/community_db/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 3c6c114..3a5a7c1 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -16,5 +16,5 @@ class Meta: class PersonSearchForm(forms.Form): first_name = forms.CharField(max_length=100, required=False) last_name = forms.CharField(max_length=100, required=False) - country = forms.CharField(max_length=100, required=False) + country = forms.ChoiceField(required=False, choices=Person.Countries.choices) mobile_number = forms.CharField(max_length=20, required=False) From a768e06835797faffb51a4a4bc3a44563c9aa9bb Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:11:12 +1000 Subject: [PATCH 32/35] Choices with blank option --- src/community_db/forms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 3a5a7c1..5a556a4 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -14,7 +14,9 @@ class Meta: class PersonSearchForm(forms.Form): + COUNTRIES = [("", "")] + list(Person.Countries.choices) + first_name = forms.CharField(max_length=100, required=False) last_name = forms.CharField(max_length=100, required=False) - country = forms.ChoiceField(required=False, choices=Person.Countries.choices) + country = forms.ChoiceField(required=False, choices=COUNTRIES) mobile_number = forms.CharField(max_length=20, required=False) From e84a1f406a509b3e9010cbe6061a2482753708a4 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:50:18 +1000 Subject: [PATCH 33/35] Add sorting --- src/community_db/forms.py | 7 +++++++ src/community_db/views.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/community_db/forms.py b/src/community_db/forms.py index 5a556a4..008cddd 100644 --- a/src/community_db/forms.py +++ b/src/community_db/forms.py @@ -15,8 +15,15 @@ class Meta: class PersonSearchForm(forms.Form): COUNTRIES = [("", "")] + list(Person.Countries.choices) + SORT_BY_OPTIONS = [ + ("last_name", "Last Name"), + ("first_name", "First Name"), + ("country", "Country"), + ("mobile_number", "Mobile Number"), + ] first_name = forms.CharField(max_length=100, required=False) last_name = forms.CharField(max_length=100, required=False) country = forms.ChoiceField(required=False, choices=COUNTRIES) mobile_number = forms.CharField(max_length=20, required=False) + sort_by = forms.ChoiceField(choices=SORT_BY_OPTIONS, required=False) diff --git a/src/community_db/views.py b/src/community_db/views.py index a24eb02..4244fab 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -64,6 +64,10 @@ def search_persons_with_template_AND(request): if cleaned_data["mobile_number"]: persons = persons.filter(mobile_number__icontains=cleaned_data["mobile_number"]) + allowed_sort_options = ("first_name", "last_name", "country", "mobile_number") + if cleaned_data.get("sort_by") in allowed_sort_options: + persons = persons.order_by(cleaned_data["sort_by"]) + context = {"object_list": persons, "form": search_form} return render(request, "community_db/person_search_form.html", context) From fc31b674e05925ac5d377833efb233c1acc39dd9 Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 13:50:45 +1000 Subject: [PATCH 34/35] Only show results when search performed --- .../community_db/person_search_form.html | 4 +- src/community_db/views.py | 48 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/community_db/templates/community_db/person_search_form.html b/src/community_db/templates/community_db/person_search_form.html index a3486df..7ea7cbd 100644 --- a/src/community_db/templates/community_db/person_search_form.html +++ b/src/community_db/templates/community_db/person_search_form.html @@ -7,5 +7,7 @@ - {% include "./person_list_part_advanced_search.html" %} + {% if show_results %} + {% include "./person_list_part_advanced_search.html" %} + {% endif %} {% endblock %} diff --git a/src/community_db/views.py b/src/community_db/views.py index 4244fab..697d1f6 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -50,25 +50,37 @@ def edit_person_with_template(request, pk): def search_persons_with_template_AND(request): - search_form = PersonSearchForm(request.GET) - - persons = Person.objects.all() - search_form.is_valid() - cleaned_data = search_form.cleaned_data - if cleaned_data.get("first_name"): - persons = persons.filter(first_name__icontains=cleaned_data["first_name"]) - if cleaned_data.get("last_name"): - persons = persons.filter(last_name__icontains=cleaned_data["last_name"]) - if cleaned_data.get("country"): - persons = persons.filter(country=cleaned_data["country"]) - if cleaned_data["mobile_number"]: - persons = persons.filter(mobile_number__icontains=cleaned_data["mobile_number"]) - - allowed_sort_options = ("first_name", "last_name", "country", "mobile_number") - if cleaned_data.get("sort_by") in allowed_sort_options: - persons = persons.order_by(cleaned_data["sort_by"]) + if request.GET: + search_form = PersonSearchForm(request.GET) + show_results = True + + persons = Person.objects.all() + search_form.is_valid() + cleaned_data = search_form.cleaned_data + if cleaned_data.get("first_name"): + persons = persons.filter(first_name__icontains=cleaned_data["first_name"]) + if cleaned_data.get("last_name"): + persons = persons.filter(last_name__icontains=cleaned_data["last_name"]) + if cleaned_data.get("country"): + persons = persons.filter(country=cleaned_data["country"]) + if cleaned_data["mobile_number"]: + persons = persons.filter( + mobile_number__icontains=cleaned_data["mobile_number"] + ) - context = {"object_list": persons, "form": search_form} + allowed_sort_options = ("first_name", "last_name", "country", "mobile_number") + if cleaned_data.get("sort_by") in allowed_sort_options: + persons = persons.order_by(cleaned_data["sort_by"]) + else: + search_form = PersonSearchForm() + show_results = False + persons = Person.objects.none() + + context = { + "object_list": persons, + "form": search_form, + "show_results": show_results, + } return render(request, "community_db/person_search_form.html", context) From 8b3aa56b4cc2c8a0bd8d05d9e65681d0f4cec5bb Mon Sep 17 00:00:00 2001 From: Simeon J Morgan Date: Mon, 5 Sep 2022 16:27:43 +1000 Subject: [PATCH 35/35] add migration Put in correct field --- .../0006_person_favourite_project.py | 18 ++++++++++++++++++ src/community_db/models.py | 1 + 2 files changed, 19 insertions(+) create mode 100644 src/community_db/migrations/0006_person_favourite_project.py diff --git a/src/community_db/migrations/0006_person_favourite_project.py b/src/community_db/migrations/0006_person_favourite_project.py new file mode 100644 index 0000000..7cd2b3f --- /dev/null +++ b/src/community_db/migrations/0006_person_favourite_project.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1 on 2022-09-05 06:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("community_db", "0005_remove_person_user_person_users"), + ] + + operations = [ + migrations.AddField( + model_name="person", + name="favourite_project", + field=models.CharField(blank=True, max_length=200), + ), + ] diff --git a/src/community_db/models.py b/src/community_db/models.py index b7201c5..004aefd 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -285,6 +285,7 @@ class Countries(models.TextChoices): last_name = models.CharField(max_length=100) country = models.CharField(max_length=100, choices=Countries.choices, blank=True) mobile_number = models.CharField(max_length=20, blank=True) + favourite_project = models.CharField(max_length=200, blank=True) users = models.ManyToManyField(settings.AUTH_USER_MODEL) class Meta: