Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1013 from yverdon/feature/yc-1210
Browse files Browse the repository at this point in the history
YC-1210: Add country to user profile and contacts
  • Loading branch information
rbovard authored Jun 20, 2024
2 parents 952e515 + d08586d commit 465caf5
Show file tree
Hide file tree
Showing 29 changed files with 303 additions and 38 deletions.
1 change: 1 addition & 0 deletions geocity/apps/accounts/dootix/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def save_user(self, request, sociallogin: SocialLogin, form=None):
address=form.cleaned_data["address"],
zipcode=form.cleaned_data["zipcode"],
city=form.cleaned_data["city"],
country=form.cleaned_data["country"],
phone_first=form.cleaned_data["phone_first"],
phone_second=form.cleaned_data["phone_second"],
company_name=form.cleaned_data["company_name"],
Expand Down
19 changes: 12 additions & 7 deletions geocity/apps/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from django.conf import settings
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm, BaseUserCreationForm
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
from django.core.validators import MinValueValidator, RegexValidator
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField

from geocity.fields import AddressWidget

Expand Down Expand Up @@ -226,8 +227,8 @@ def __init__(self, *args, **kwargs):
)

zipcode = forms.IntegerField(
label=_("NPA"),
validators=[MinValueValidator(1000), MaxValueValidator(9999)],
label=_("Code postal"),
validators=[MinValueValidator(1)],
widget=forms.NumberInput(attrs={"required": "required"}),
)
city = forms.CharField(
Expand All @@ -245,6 +246,7 @@ class Meta:
"address",
"zipcode",
"city",
"country",
"phone_first",
"phone_second",
"company_name",
Expand Down Expand Up @@ -295,26 +297,29 @@ class SocialSignupForm(SignupForm):
)

zipcode = forms.IntegerField(
label=_("NPA"),
min_value=1000,
max_value=9999,
label=_("Code postal"),
min_value=1,
widget=forms.NumberInput(attrs={"required": "required"}),
)

city = forms.CharField(
max_length=100,
label=_("Ville"),
widget=forms.TextInput(
attrs={"placeholder": "ex: Yverdon", "required": "required"}
),
)

country = CountryField().formfield(initial="CH")

phone_first = forms.CharField(
label=_("Téléphone principal"),
max_length=20,
required=True,
widget=forms.TextInput(attrs={"placeholder": "ex: 024 111 22 22"}),
validators=[
RegexValidator(
regex=r"^(((\+41)\s?)|(0))?(\d{2})\s?(\d{3})\s?(\d{2})\s?(\d{2})$",
regex=r"^(?:\+(?:[0-9] ?){6,14}[0-9]|0\d(?: ?\d){8,13})$",
message="Seuls les chiffres et les espaces sont autorisés.",
)
],
Expand Down
1 change: 1 addition & 0 deletions geocity/apps/accounts/geomapfish/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def save_user(self, request, sociallogin: SocialLogin, form=None):
address=form.cleaned_data["address"],
zipcode=form.cleaned_data["zipcode"],
city=form.cleaned_data["city"],
country=form.cleaned_data["country"],
phone_first=form.cleaned_data["phone_first"],
phone_second=form.cleaned_data["phone_second"],
company_name=form.cleaned_data["company_name"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.11 on 2024-05-29 09:09

import django_countries.fields
from django.db import migrations


def set_country_to_null(apps, schema_editor):
UserProfile = apps.get_model("accounts", "UserProfile")
HistoricalUserProfile = apps.get_model("accounts", "HistoricalUserProfile")

UserProfile.objects.all().update(country=None)
HistoricalUserProfile.objects.all().update(country=None)


class Migration(migrations.Migration):

dependencies = [
("accounts", "0019_administrativeentity_agenda_domain"),
]

operations = [
migrations.AddField(
model_name="historicaluserprofile",
name="country",
field=django_countries.fields.CountryField(
default="CH", max_length=2, null=True, verbose_name="Pays"
),
),
migrations.AddField(
model_name="userprofile",
name="country",
field=django_countries.fields.CountryField(
default="CH", max_length=2, null=True, verbose_name="Pays"
),
),
# Dont add the default value to the existing users
migrations.RunPython(set_country_to_null),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Generated by Django 4.2.11 on 2024-05-30 08:58

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0020_historicaluserprofile_country_userprofile_country"),
]

operations = [
migrations.AlterField(
model_name="administrativeentity",
name="phone",
field=models.CharField(
blank=True,
max_length=20,
validators=[
django.core.validators.RegexValidator(
message="Seuls les chiffres et les espaces sont autorisés.",
regex="^(?:\\+(?:[0-9] ?){6,14}[0-9]|0\\d(?: ?\\d){8,13})$",
)
],
verbose_name="Téléphone",
),
),
migrations.AlterField(
model_name="historicaluserprofile",
name="phone_first",
field=models.CharField(
max_length=20,
validators=[
django.core.validators.RegexValidator(
message="Seuls les chiffres et les espaces sont autorisés.",
regex="^(?:\\+(?:[0-9] ?){6,14}[0-9]|0\\d(?: ?\\d){8,13})$",
)
],
verbose_name="Téléphone principal",
),
),
migrations.AlterField(
model_name="historicaluserprofile",
name="phone_second",
field=models.CharField(
blank=True,
max_length=20,
validators=[
django.core.validators.RegexValidator(
message="Seuls les chiffres et les espaces sont autorisés.",
regex="^(?:\\+(?:[0-9] ?){6,14}[0-9]|0\\d(?: ?\\d){8,13})$",
)
],
verbose_name="Téléphone secondaire",
),
),
migrations.AlterField(
model_name="historicaluserprofile",
name="zipcode",
field=models.PositiveIntegerField(verbose_name="Code postal"),
),
migrations.AlterField(
model_name="userprofile",
name="phone_first",
field=models.CharField(
max_length=20,
validators=[
django.core.validators.RegexValidator(
message="Seuls les chiffres et les espaces sont autorisés.",
regex="^(?:\\+(?:[0-9] ?){6,14}[0-9]|0\\d(?: ?\\d){8,13})$",
)
],
verbose_name="Téléphone principal",
),
),
migrations.AlterField(
model_name="userprofile",
name="phone_second",
field=models.CharField(
blank=True,
max_length=20,
validators=[
django.core.validators.RegexValidator(
message="Seuls les chiffres et les espaces sont autorisés.",
regex="^(?:\\+(?:[0-9] ?){6,14}[0-9]|0\\d(?: ?\\d){8,13})$",
)
],
verbose_name="Téléphone secondaire",
),
),
migrations.AlterField(
model_name="userprofile",
name="zipcode",
field=models.PositiveIntegerField(verbose_name="Code postal"),
),
]
23 changes: 12 additions & 11 deletions geocity/apps/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
from django.contrib.gis.db import models as geomodels
from django.contrib.sites.models import Site
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.validators import (
FileExtensionValidator,
MaxValueValidator,
MinValueValidator,
RegexValidator,
)
from django.core.validators import FileExtensionValidator, RegexValidator
from django.db import models
from django.db.models import BooleanField, Count, ExpressionWrapper, Q, UniqueConstraint
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from simple_history.models import HistoricalRecords
from taggit.managers import TaggableManager

Expand Down Expand Up @@ -291,7 +287,7 @@ class AdministrativeEntity(models.Model):
max_length=20,
validators=[
RegexValidator(
regex=r"^(((\+41)\s?)|(0))?(\d{2})\s?(\d{3})\s?(\d{2})\s?(\d{2})$",
regex=r"^(?:\+(?:[0-9] ?){6,14}[0-9]|0\d(?: ?\d){8,13})$",
message="Seuls les chiffres et les espaces sont autorisés.",
)
],
Expand Down Expand Up @@ -550,19 +546,24 @@ class UserProfile(models.Model):
max_length=100,
)
zipcode = models.PositiveIntegerField(
_("NPA"),
validators=[MinValueValidator(1000), MaxValueValidator(9999)],
_("Code postal"),
)
city = models.CharField(
_("Ville"),
max_length=100,
)
country = CountryField(
_("Pays"),
null=True,
blank=False,
default="CH",
)
phone_first = models.CharField(
_("Téléphone principal"),
max_length=20,
validators=[
RegexValidator(
regex=r"^(((\+41)\s?)|(0))?(\d{2})\s?(\d{3})\s?(\d{2})\s?(\d{2})$",
regex=r"^(?:\+(?:[0-9] ?){6,14}[0-9]|0\d(?: ?\d){8,13})$",
message="Seuls les chiffres et les espaces sont autorisés.",
)
],
Expand All @@ -573,7 +574,7 @@ class UserProfile(models.Model):
max_length=20,
validators=[
RegexValidator(
regex=r"^(((\+41)\s?)|(0))?(\d{2})\s?(\d{3})\s?(\d{2})\s?(\d{2})$",
regex=r"^(?:\+(?:[0-9] ?){6,14}[0-9]|0\d(?: ?\d){8,13})$",
message="Seuls les chiffres et les espaces sont autorisés.",
)
],
Expand Down
1 change: 1 addition & 0 deletions geocity/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class Meta:
"zipcode",
"user_id",
"city",
"country",
"company_name",
"vat_number",
"iban",
Expand Down
5 changes: 5 additions & 0 deletions geocity/apps/core/static/css/contactform.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ select[readonly] {
border: 0;
font-weight: bold;
}

.disabled-dropdown {
background-color: #e9ecef;
pointer-events: none;
}
31 changes: 25 additions & 6 deletions geocity/apps/core/static/js/submission_contacts.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
let update_form_value = function(item, userprofile) {

if (document.getElementById(`self_contact_${parseInt(item) + 1}`).checked == true) {
document.getElementById(`id_form-${item}-first_name`).value = userprofile.first_name;
for (const [key, value] of Object.entries(userprofile)) {
document.getElementById(`id_form-${item}-${key}`).value = value;
document.getElementById(`id_form-${item}-${key}`).readOnly = true;
let element = document.getElementById(`id_form-${item}-${key}`);
if (element !== null) {
element.value = value;
// Pseudo disable the dropdown with CSS instead of setting it read-only
if (key === "country") {
element.classList.add("disabled-dropdown");
} else {
element.readOnly = true;
}
}
}
} else {
for (const [key, value] of Object.entries(userprofile)) {
document.getElementById(`id_form-${item}-${key}`).value = '';
document.getElementById(`id_form-${item}-${key}`).readOnly = false;
let element = document.getElementById(`id_form-${item}-${key}`);
if (element !== null) {
element.value = '';
element.readOnly = false;
if (key === "country") {
element.classList.remove("disabled-dropdown");
} else {
element.readOnly = false;
}
}
}
}
}

// Create a label to replace .form-control without .extra-form in classes inside of forms-container
window.addEventListener('load', function () {
var forms_control = document.querySelectorAll("[id=forms-container] select[class*=form-control]");
for (form_control of forms_control) {
for (let form_control of forms_control) {
// Skip the country select field
if (form_control.classList.contains("country")) {
continue;
}
let elem = document.createElement('label');
let text = form_control.querySelector("option[selected]").text
let div = form_control.closest('.col-md-9');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
<span class="bold">Adresse : </span>{{submission.author.userprofile.address}}<br>
{% endif %}
{% if submission.author.userprofile.zipcode %}
<span class="bold">NPA : </span>{{submission.author.userprofile.zipcode}}<br>
<span class="bold">Code postal : </span>{{submission.author.userprofile.zipcode}}<br>
{% endif %}
{% if submission.author.userprofile.city %}
<span class="bold">Localité : </span>{{submission.author.userprofile.city}}<br>
{% endif %}
{% if submission.author.userprofile.country %}
<span class="bold">Pays : </span>{{submission.author.userprofile.country.name}}<br>
{% endif %}
{% if submission.author.userprofile.company_name %}
<span class="bold">Raison sociale : </span>{{submission.author.userprofile.company_name}}<br>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
<span class="bold">Adresse : </span>{{contact.address}}<br>
{% endif %}
{% if contact.zipcode %}
<span class="bold">NPA : </span>{{contact.zipcode}}<br>
<span class="bold">Code postal : </span>{{contact.zipcode}}<br>
{% endif %}
{% if contact.city %}
<span class="bold">Localité : </span>{{contact.city}}<br>
{% endif %}
{% if contact.country %}
<span class="bold">Pays : </span>{{contact.country.name}}<br>
{% endif %}
{% if contact.company_name %}
<span class="bold">Raison sociale : </span>{{contact.company_name}}<br>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
{{recipient.last_name}} {{recipient.first_name}}<br>
{{recipient.address}}<br>
{{recipient.zipcode}} {{recipient.city}}
{% if recipient.country %}
<br>{{recipient.country.name}}
{% endif %}
</div>
</div>
{% endblock %}
Loading

0 comments on commit 465caf5

Please sign in to comment.