Retrieve country list from odoo
This commit is contained in:
parent
c751f9d710
commit
4c0cd2a4fe
2 changed files with 50 additions and 20 deletions
|
@ -87,6 +87,33 @@ class OdooClient:
|
|||
|
||||
CLIENT = OdooClient()
|
||||
|
||||
# Odoo countries do not change, so they are fetched once per process
|
||||
COUNTRIES = []
|
||||
|
||||
|
||||
def get_odoo_countries():
|
||||
global COUNTRIES
|
||||
if COUNTRIES:
|
||||
return COUNTRIES
|
||||
|
||||
try:
|
||||
odoo_countries_data = CLIENT.search_read(
|
||||
model="res.country", domain=[], fields=["id", "name"]
|
||||
)
|
||||
# Format as Django choices: [(value, label), ...]
|
||||
COUNTRIES = [
|
||||
(country["id"], country["name"]) for country in odoo_countries_data
|
||||
]
|
||||
# Sort by country name for better UX in dropdowns
|
||||
COUNTRIES.sort(key=lambda x: x[1])
|
||||
except Exception as e:
|
||||
# Log the error or handle it as appropriate for your application
|
||||
# For now, return an empty list or a default if Odoo is unavailable
|
||||
print(f"Error fetching Odoo countries: {e}")
|
||||
return [("", "Error fetching countries")] # Or just []
|
||||
|
||||
return COUNTRIES
|
||||
|
||||
|
||||
def get_invoice_addresses(user):
|
||||
"""Used during organization creation: retrieves all invoice
|
||||
|
|
|
@ -3,7 +3,7 @@ from django.forms import ModelForm
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from servala.core.models import Organization
|
||||
from servala.core.odoo import get_invoice_addresses
|
||||
from servala.core.odoo import get_invoice_addresses, get_odoo_countries
|
||||
from servala.frontend.forms.mixins import HtmxMixin
|
||||
|
||||
|
||||
|
@ -28,33 +28,36 @@ class OrganizationCreateForm(OrganizationForm):
|
|||
required=False,
|
||||
)
|
||||
|
||||
# Fields for creating a new billing address in Odoo, prefixed with 'ba_'
|
||||
ba_name = forms.CharField(
|
||||
label=_("Contact Person / Company Name"), required=False, max_length=100
|
||||
# Fields for creating a new billing address in Odoo, prefixed with 'invoice_'
|
||||
invoice_street = forms.CharField(label=_("Line 1"), required=False, max_length=100)
|
||||
invoice_street2 = forms.CharField(label=_("Line 2"), required=False, max_length=100)
|
||||
invoice_city = forms.CharField(label=_("City"), required=False, max_length=100)
|
||||
invoice_zip = forms.CharField(label=_("Postal Code"), required=False, max_length=20)
|
||||
invoice_country = forms.ChoiceField(
|
||||
label=_("Country"),
|
||||
required=False,
|
||||
choices=get_odoo_countries(),
|
||||
)
|
||||
ba_street = forms.CharField(label=_("Street"), required=False, max_length=100)
|
||||
ba_street2 = forms.CharField(
|
||||
label=_("Street 2 (Optional)"), required=False, max_length=100
|
||||
invoice_email = forms.EmailField(label=_("Billing Email"), required=False)
|
||||
invoice_phone = forms.CharField(
|
||||
label=_("Billing Phone"), required=False, max_length=30
|
||||
)
|
||||
ba_city = forms.CharField(label=_("City"), required=False, max_length=100)
|
||||
ba_zip = forms.CharField(label=_("ZIP Code"), required=False, max_length=20)
|
||||
# For state & country, Odoo uses structured data. For now, text input.
|
||||
# These will need mapping logic when actual Odoo creation is implemented.
|
||||
ba_state_name = forms.CharField(
|
||||
label=_("State / Province"), required=False, max_length=100
|
||||
)
|
||||
ba_country_name = forms.CharField(
|
||||
label=_("Country"), required=False, max_length=100
|
||||
)
|
||||
ba_email = forms.EmailField(label=_("Billing Email"), required=False)
|
||||
ba_phone = forms.CharField(label=_("Billing Phone"), required=False, max_length=30)
|
||||
ba_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50)
|
||||
invoice_vat = forms.CharField(label=_("VAT ID"), required=False, max_length=50)
|
||||
|
||||
class Meta(OrganizationForm.Meta):
|
||||
pass
|
||||
|
||||
def __init__(self, *args, user=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if not self.initial.get("invoice_country"):
|
||||
default_country_name = "Switzerland"
|
||||
country_choices = self.fields["invoice_country"].choices
|
||||
for country_id, country_name_label in country_choices:
|
||||
if country_name_label == default_country_name:
|
||||
self.initial["invoice_country"] = country_id
|
||||
break
|
||||
|
||||
self.user = user
|
||||
self.odoo_addresses = get_invoice_addresses(self.user)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue