Skip to content

Commit 3273ea0

Browse files
committed
add primary language to country
1 parent 0ce8be1 commit 3273ea0

File tree

3 files changed

+186
-34
lines changed

3 files changed

+186
-34
lines changed

bullet/countries/middleware.py

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from re import Match, Pattern
33

44
from django.conf import settings
5-
from django.http import HttpRequest, HttpResponseRedirect
5+
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
66
from django.urls import reverse
77
from django.utils import timezone, translation
88

99
from countries.logic import country
10-
from countries.logic.cache import get_country_cache
1110
from countries.models import BranchCountry
1211

1312
country_language_re: Pattern[str] = re.compile(r"^/([a-z]{2})/([^/]+)/")
13+
url_path_re: Pattern[str] = re.compile(r"^/[a-z]{2}/[^/]+/(.*)")
1414

1515

1616
def country_language_from_request(
@@ -27,47 +27,76 @@ class CountryLanguageMiddleware:
2727
def __init__(self, get_response):
2828
self.get_response = get_response
2929

30+
def redirect_to_selector(self):
31+
return HttpResponseRedirect(reverse("country_selector"))
32+
3033
def __call__(self, request: HttpRequest):
3134
c, lang = country_language_from_request(request)
3235

33-
if c and not request._is_admin_domain:
34-
cache: dict[int, dict[str, list[tuple[str, bool]]]] = get_country_cache()
35-
if request.BRANCH is not None and (
36-
c not in cache[request.BRANCH.id]
37-
or (
38-
(lang, True) not in cache[request.BRANCH.id][c]
39-
and (lang, False) not in cache[request.BRANCH.id][c]
40-
)
41-
):
42-
return HttpResponseRedirect(reverse("country_selector"))
43-
44-
country.activate(c)
45-
request.COUNTRY_CODE = c
46-
47-
branch_country = BranchCountry.objects.get(
48-
branch=request.BRANCH, country=c.upper()
49-
)
50-
timezone.activate(branch_country.timezone)
51-
52-
translation.activate(lang)
53-
request.LANGUAGE_CODE = translation.get_language()
54-
else:
36+
if not c or request._is_admin_domain:
5537
# Use default settings if not detected.
5638
timezone.activate(settings.TIME_ZONE)
5739
translation.activate(settings.LANGUAGE_CODE)
5840
request.LANGUAGE_CODE = translation.get_language()
41+
return self.get_response(request)
42+
43+
redirect = self.set_country_language(request, c, lang)
44+
if redirect:
45+
return redirect
5946

6047
response = self.get_response(request)
48+
self.set_cookies(request, response)
49+
return response
50+
51+
def get_url_part(self, request) -> str | None:
52+
match = url_path_re.match(request.path_info)
53+
if not match:
54+
return None
55+
56+
return match.groups()[0]
6157

62-
if c and not request._is_admin_domain:
63-
ckie = request.COOKIES.get("bullet_country", "")
64-
expected = f"{c}|{lang}"
65-
if ckie != expected:
66-
response.set_cookie(
67-
"bullet_country",
68-
expected,
69-
expires=365 * 24 * 60 * 60,
70-
samesite="Lax",
58+
def set_country_language(
59+
self, request: HttpRequest, country_name, language
60+
) -> HttpResponse | None:
61+
branch_country: BranchCountry = BranchCountry.objects.filter(
62+
branch=request.BRANCH, country=country_name.upper()
63+
).first()
64+
65+
if branch_country is None:
66+
return self.redirect_to_selector()
67+
68+
if language not in branch_country.languages:
69+
url_part = self.get_url_part(request)
70+
if branch_country.primary_language and url_part:
71+
new_url = (
72+
f"/{country_name}/{branch_country.primary_language}/{url_part}"
7173
)
74+
return HttpResponseRedirect(new_url)
75+
return self.redirect_to_selector()
7276

73-
return response
77+
timezone.activate(branch_country.timezone)
78+
79+
country.activate(country_name)
80+
request.COUNTRY_CODE = country_name
81+
82+
translation.activate(language)
83+
request.LANGUAGE_CODE = translation.get_language()
84+
85+
return None
86+
87+
def set_cookies(self, request, response):
88+
country = request.COUNTRY_CODE
89+
language = request.LANGUAGE_CODE
90+
91+
if not country or not language:
92+
return
93+
94+
ckie = request.COOKIES.get("bullet_country", "")
95+
expected = f"{country}|{language}"
96+
if ckie != expected:
97+
response.set_cookie(
98+
"bullet_country",
99+
expected,
100+
expires=365 * 24 * 60 * 60,
101+
samesite="Lax",
102+
)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Generated by Django 5.1 on 2024-10-28 08:36
2+
3+
import web.fields
4+
from django.db import migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
dependencies = [
9+
("countries", "0007_branchcountry_hidden_languages_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="branchcountry",
15+
name="primary_language",
16+
field=web.fields.LanguageField(
17+
blank=True,
18+
choices=[
19+
("af", "Afrikaans"),
20+
("ar", "Arabic"),
21+
("ar-dz", "Algerian Arabic"),
22+
("ast", "Asturian"),
23+
("az", "Azerbaijani"),
24+
("bg", "Bulgarian"),
25+
("be", "Belarusian"),
26+
("bn", "Bengali"),
27+
("br", "Breton"),
28+
("bs", "Bosnian"),
29+
("ca", "Catalan"),
30+
("ckb", "Central Kurdish (Sorani)"),
31+
("cs", "Czech"),
32+
("cy", "Welsh"),
33+
("da", "Danish"),
34+
("de", "German"),
35+
("dsb", "Lower Sorbian"),
36+
("el", "Greek"),
37+
("en", "English"),
38+
("en-au", "Australian English"),
39+
("en-gb", "British English"),
40+
("eo", "Esperanto"),
41+
("es", "Spanish"),
42+
("es-ar", "Argentinian Spanish"),
43+
("es-co", "Colombian Spanish"),
44+
("es-mx", "Mexican Spanish"),
45+
("es-ni", "Nicaraguan Spanish"),
46+
("es-ve", "Venezuelan Spanish"),
47+
("et", "Estonian"),
48+
("eu", "Basque"),
49+
("fa", "Persian"),
50+
("fi", "Finnish"),
51+
("fr", "French"),
52+
("fy", "Frisian"),
53+
("ga", "Irish"),
54+
("gd", "Scottish Gaelic"),
55+
("gl", "Galician"),
56+
("he", "Hebrew"),
57+
("hi", "Hindi"),
58+
("hr", "Croatian"),
59+
("hsb", "Upper Sorbian"),
60+
("hu", "Hungarian"),
61+
("hy", "Armenian"),
62+
("ia", "Interlingua"),
63+
("id", "Indonesian"),
64+
("ig", "Igbo"),
65+
("io", "Ido"),
66+
("is", "Icelandic"),
67+
("it", "Italian"),
68+
("ja", "Japanese"),
69+
("ka", "Georgian"),
70+
("kab", "Kabyle"),
71+
("kk", "Kazakh"),
72+
("km", "Khmer"),
73+
("kn", "Kannada"),
74+
("ko", "Korean"),
75+
("ky", "Kyrgyz"),
76+
("lb", "Luxembourgish"),
77+
("lt", "Lithuanian"),
78+
("lv", "Latvian"),
79+
("mk", "Macedonian"),
80+
("ml", "Malayalam"),
81+
("mn", "Mongolian"),
82+
("mr", "Marathi"),
83+
("ms", "Malay"),
84+
("my", "Burmese"),
85+
("nb", "Norwegian Bokmål"),
86+
("ne", "Nepali"),
87+
("nl", "Dutch"),
88+
("nn", "Norwegian Nynorsk"),
89+
("os", "Ossetic"),
90+
("pa", "Punjabi"),
91+
("pl", "Polish"),
92+
("pt", "Portuguese"),
93+
("pt-br", "Brazilian Portuguese"),
94+
("ro", "Romanian"),
95+
("ru", "Russian"),
96+
("sk", "Slovak"),
97+
("sl", "Slovenian"),
98+
("sq", "Albanian"),
99+
("sr", "Serbian"),
100+
("sr-latn", "Serbian Latin"),
101+
("sv", "Swedish"),
102+
("sw", "Swahili"),
103+
("ta", "Tamil"),
104+
("te", "Telugu"),
105+
("tg", "Tajik"),
106+
("th", "Thai"),
107+
("tk", "Turkmen"),
108+
("tr", "Turkish"),
109+
("tt", "Tatar"),
110+
("udm", "Udmurt"),
111+
("ug", "Uyghur"),
112+
("uk", "Ukrainian"),
113+
("ur", "Urdu"),
114+
("uz", "Uzbek"),
115+
("vi", "Vietnamese"),
116+
("zh-hans", "Simplified Chinese"),
117+
("zh-hant", "Traditional Chinese"),
118+
],
119+
max_length=8,
120+
),
121+
),
122+
]

bullet/countries/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class BranchCountry(models.Model):
99
branch = BranchField()
1010
country = CountryField()
11+
primary_language = LanguageField(blank=True)
1112
languages = ChoiceArrayField(LanguageField())
1213
timezone = TimeZoneField()
1314
email = models.EmailField()

0 commit comments

Comments
 (0)