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

Commit

Permalink
Add country to contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
rbovard committed May 30, 2024
1 parent 643719c commit d2ff456
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 8 deletions.
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 @@ -28,6 +28,9 @@
{% 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 %}
7 changes: 6 additions & 1 deletion geocity/apps/submissions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from django_select2.forms import Select2MultipleWidget, Select2Widget

from geocity.apps.accounts.models import (
Expand Down Expand Up @@ -898,8 +899,8 @@ class SubmissionContactForm(forms.ModelForm):
"company_name",
"vat_number",
"address",
"address",
"city",
"country",
"phone",
"zipcode",
"email",
Expand Down Expand Up @@ -975,6 +976,10 @@ class SubmissionContactForm(forms.ModelForm):
}
),
)
country = CountryField().formfield(
label=_("Pays"),
widget=forms.Select(attrs={"class": "country"}),
)
company_name = forms.CharField(
required=False,
label=_("Raison sociale"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.11 on 2024-05-29 15:04

import django_countries.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("submissions", "0030_alter_servicefeetype_fix_price_editable"),
]

operations = [
migrations.AddField(
model_name="contact",
name="country",
field=django_countries.fields.CountryField(
max_length=2, null=True, verbose_name="Pays"
),
),
migrations.AddField(
model_name="historicalcontact",
name="country",
field=django_countries.fields.CountryField(
max_length=2, null=True, verbose_name="Pays"
),
),
]
6 changes: 6 additions & 0 deletions geocity/apps/submissions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from django.utils.functional import cached_property
from django.utils.html import escape, format_html
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from pdf2image import convert_from_path
from PIL import Image
from simple_history.models import HistoricalRecords
Expand Down Expand Up @@ -1467,6 +1468,11 @@ class Contact(models.Model):
_("Ville"),
max_length=100,
)
country = CountryField(
_("Pays"),
null=True,
blank=False,
)
phone = models.CharField(
_("Téléphone"),
max_length=20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ <h5>{{ contact_form }}</h5>
<div class="grid-element">
<dl>
<dt> {{ field_label }} </dt>
<dd> {{ field_value }} </dd>
{% if field_label == "Pays" %}
<dd> {{ field_value.name }} </dd>
{% else %}
<dd> {{ field_value }} </dd>
{% endif %}
</dl>
</div>
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions geocity/apps/submissions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ def submission_contacts(request, submission_id):
"address": request.user.userprofile.address,
"zipcode": request.user.userprofile.zipcode,
"city": request.user.userprofile.city,
"country": request.user.userprofile.country.code,
"phone": request.user.userprofile.phone_first,
}

Expand Down

0 comments on commit d2ff456

Please sign in to comment.