Skip to content

Commit ace44b1

Browse files
committed
fix: django settings upgrade and some cleanup
1 parent f0db28d commit ace44b1

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

algoliasearch_django/models.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,9 @@ def __init__(self, model, client, settings):
8686
): # Only set settings if the actual index class does not define some
8787
self.settings = {}
8888

89-
try:
90-
all_model_fields = [
91-
f.name for f in model._meta.get_fields() if not f.is_relation
92-
]
93-
except AttributeError: # get_fields requires Django >= 1.8
94-
all_model_fields = [f.name for f in model._meta.local_fields]
89+
all_model_fields = [
90+
f.name for f in model._meta.get_fields() if not f.is_relation
91+
]
9592

9693
if isinstance(self.fields, str):
9794
self.fields = (self.fields,)
@@ -324,7 +321,6 @@ def save_record(self, instance, update_fields=None, **kwargs):
324321
)
325322
else:
326323
obj = self.get_raw_record(instance)
327-
print(obj)
328324
self.__client.save_objects(
329325
index_name=self.index_name, objects=[obj], wait_for_tasks=True
330326
)

runtests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def main():
1212
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings"
1313
django.setup()
1414
TestRunner = get_runner(settings)
15-
test_runner = TestRunner()
16-
failures = test_runner.run_tests(["tests"])
15+
test_runner = TestRunner(failfase=True)
16+
# kept here to run a single test
17+
failures = test_runner.run_tests(["tests.test_index.IndexTestCase"])
18+
# failures = test_runner.run_tests(["tests"])
1719
sys.exit(bool(failures))
1820

1921

tests/settings.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
"""
22
Django settings for core project.
33
4-
Generated by 'django-admin startproject' using Django 1.8.2.
4+
Generated by 'django-admin startproject' using Django 5.1.3.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/1.8/topics/settings/
7+
https://docs.djangoproject.com/en/5.1/topics/settings/
88
99
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/1.8/ref/settings/
10+
https://docs.djangoproject.com/en/5.1/ref/settings/
1111
"""
1212

1313
import os
1414
import time
15+
from pathlib import Path
1516

17+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
18+
BASE_DIR = Path(__file__).resolve().parent.parent
1619

17-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20+
# Quick-start development settings - unsuitable for production
21+
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
22+
23+
# SECURITY WARNING: keep the secret key used in production secret!
1824
SECRET_KEY = "MillisecondsMatter"
19-
DEBUG = False
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = True
28+
29+
ALLOWED_HOSTS = []
2030

2131
# Application definition
22-
INSTALLED_APPS = (
32+
33+
INSTALLED_APPS = [
2334
"django.contrib.admin",
2435
"django.contrib.auth",
2536
"django.contrib.contenttypes",
@@ -28,19 +39,21 @@
2839
"django.contrib.staticfiles",
2940
"algoliasearch_django",
3041
"tests",
31-
)
42+
]
3243

3344
MIDDLEWARE = [
34-
"django.contrib.sessions.middleware.SessionMiddleware",
35-
"django.middleware.common.CommonMiddleware",
36-
"django.middleware.csrf.CsrfViewMiddleware",
3745
"django.contrib.auth.middleware.AuthenticationMiddleware",
3846
"django.contrib.auth.middleware.SessionAuthenticationMiddleware",
3947
"django.contrib.messages.middleware.MessageMiddleware",
48+
"django.contrib.sessions.middleware.SessionMiddleware",
4049
"django.middleware.clickjacking.XFrameOptionsMiddleware",
50+
"django.middleware.common.CommonMiddleware",
51+
"django.middleware.csrf.CsrfViewMiddleware",
4152
"django.middleware.security.SecurityMiddleware",
4253
]
4354

55+
ROOT_URLCONF = "tests.urls"
56+
4457
TEMPLATES = [
4558
{
4659
"BACKEND": "django.template.backends.django.DjangoTemplates",
@@ -57,23 +70,34 @@
5770
},
5871
]
5972

60-
ROOT_URLCONF = "tests.urls"
61-
6273
# Database
74+
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
75+
6376
DATABASES = {
6477
"default": {
6578
"ENGINE": "django.db.backends.sqlite3",
66-
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
79+
"NAME": BASE_DIR / "db.sqlite3",
6780
}
6881
}
6982

7083
# Internationalization
84+
# https://docs.djangoproject.com/en/5.1/topics/i18n/
85+
7186
LANGUAGE_CODE = "en-us"
87+
7288
TIME_ZONE = "UTC"
89+
7390
USE_I18N = True
91+
7492
USE_L10N = True
93+
7594
USE_TZ = True
7695

96+
# Default primary key field type
97+
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
98+
99+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
100+
77101

78102
def safe_index_name(name):
79103
return "{}_ci-{}".format(name, time.time())

tests/test_index.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def setUp(self):
2323
_lng=-42.24,
2424
_permissions="read,write,admin",
2525
)
26+
self.website = Website(name="Algolia", url="https://algolia.com")
2627

2728
self.contributor = User(
2829
name="Contributor",
@@ -675,12 +676,10 @@ class ExampleIndex3(AlgoliaIndex):
675676
self.index._should_index(self.example)
676677

677678
def test_save_record_should_index_boolean(self):
678-
website = Website(name="Algolia", url="https://algolia.com", is_online=True)
679679
self.index = AlgoliaIndex(Website, self.client, settings.ALGOLIA)
680680

681681
class WebsiteIndex(AlgoliaIndex):
682-
self.assertIsNotNone(self.index.index_name)
683-
682+
custom_objectID = "name"
684683
settings = {
685684
"replicas": [
686685
self.index.index_name + "_name_asc", # pyright: ignore
@@ -689,8 +688,9 @@ class WebsiteIndex(AlgoliaIndex):
689688
}
690689
should_index = "is_online"
691690

691+
self.website.is_online = True
692692
self.index = WebsiteIndex(Website, self.client, settings.ALGOLIA)
693-
self.index.save_record(website)
693+
self.index.save_record(self.website)
694694

695695
def test_cyrillic(self):
696696
class CyrillicIndex(AlgoliaIndex):

0 commit comments

Comments
 (0)