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/forms.py b/src/community_db/forms.py new file mode 100644 index 0000000..40d6f81 --- /dev/null +++ b/src/community_db/forms.py @@ -0,0 +1,13 @@ +from django import forms + +from .models import Person + + +class QuickSearchForm(forms.Form): + search = forms.CharField(max_length=100, required=False) + + +class PersonForm(forms.ModelForm): + class Meta: + model = Person + fields = ["first_name", "last_name", "country", "mobile_number"] 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/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/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/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 f0853ab..b7201c5 100644 --- a/src/community_db/models.py +++ b/src/community_db/models.py @@ -1,8 +1,293 @@ +from django.conf import settings from django.db import models class Person(models.Model): + 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)", + ) + 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)", + ) + 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)", + ) + 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)", + ) + 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", + ) + 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", + ) + 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)", + ) + 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)", + ) + 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, blank=True) - country = models.CharField(max_length=100, blank=True) + 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) + users = models.ManyToManyField(settings.AUTH_USER_MODEL) + + class Meta: + permissions = [ + ("edit_profile", "Can edit a profile"), + ] diff --git a/src/community_db/templates/base.html b/src/community_db/templates/base.html new file mode 100644 index 0000000..182305f --- /dev/null +++ b/src/community_db/templates/base.html @@ -0,0 +1,19 @@ + + + +

Welcome to the Pacific Connect Community Database

+ On this site, you can find details of members of the Pacific Connect Community Database +
+
+ + + +
+ {% if search_text %} + Search results for: {{ search_text }} + {% endif %} +
+ {% block content %}{% endblock %} + + + \ No newline at end of file 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/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..a5d318c --- /dev/null +++ b/src/community_db/templates/community_db/person_detail_in_base.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +
+ Back to list +
+ This is the detail of a person: + + 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..ea9b21e --- /dev/null +++ b/src/community_db/templates/community_db/person_form_in_base.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block content %} +
+
+ {% csrf_token %} + + {{ form.as_ul }} +
+ +
+{% 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..d003e30 --- /dev/null +++ b/src/community_db/templates/community_db/person_list_in_base.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block content %} + {% 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 91ea44a..4212e88 100644 --- a/src/community_db/views.py +++ b/src/community_db/views.py @@ -1,3 +1,115 @@ -from django.shortcuts import render +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 +from django.urls import reverse +from django.views.generic import DetailView, ListView, UpdateView -# Create your views here. +from .forms import PersonForm +from .models import Person + +# FUNCTION BASED VIEWS + +# 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") + + 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} + return render(request, "community_db/person_list_in_base.html", context) + + +def detail_person_with_template(request, pk): + person = get_object_or_404(Person, id=pk) + context = {"object": person} + return render(request, "community_db/person_detail_in_base.html", context) + + +@login_required +def edit_person_with_template(request, pk): + person = get_object_or_404(Person, id=pk) + + if not person.users.contains(request.user): + return redirect_to_login(request.path) + + if request.POST: + form = PersonForm(request.POST, instance=person) + if form.is_valid(): + form.save() + return HttpResponseRedirect(reverse("fbv-person-detail", args=[person.id])) + else: + form = PersonForm(instance=person) + context = {"object": person, "form": form} + return render(request, "community_db/person_form_in_base.html", context) + + +# CLASS BASED VIEWS +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 + + 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 + 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]) + + +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)) + + +class UserLoginView(LoginView): + template_name = "cbv-login-form.html" + + def get_default_success_url(self): + return reverse("cbv-person-list") diff --git a/src/pacificconnect/urls.py b/src/pacificconnect/urls.py index 5a454ad..1bd02aa 100644 --- a/src/pacificconnect/urls.py +++ b/src/pacificconnect/urls.py @@ -16,6 +16,32 @@ from django.contrib import admin from django.urls import path +from community_db import views + 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//", + 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//", + views.PersonDetailView.as_view(), + name="cbv-person-detail", + ), + path( + "cbv/people//edit/", + views.PersonUpdateView.as_view(), + name="cbv-person-edit", + ), + path("accounts/login/", views.UserLoginView.as_view(), name="login"), ]