From f5b5335e7f612f781489574fa82c9d5ec62e38fb Mon Sep 17 00:00:00 2001 From: thinkAmi Date: Tue, 13 Sep 2022 20:17:35 +0900 Subject: [PATCH 1/4] =?UTF-8?q?DB=E3=82=92PostgreSQL=E3=81=8B=E3=82=89SQLi?= =?UTF-8?q?te=E3=81=B8=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ apps/api/views.py | 3 ++- apps/tweets/models.py | 8 ++++---- dj_ringo_tabetter/settings.py | 9 ++++----- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 6274207..3e6e070 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ pip-selfcheck.json #### backup files *.dump +dump.json +ringo.db ### Python ### # Byte-compiled / optimized / DLL files diff --git a/apps/api/views.py b/apps/api/views.py index c9ee451..010fd8b 100644 --- a/apps/api/views.py +++ b/apps/api/views.py @@ -66,7 +66,8 @@ def get(self, request, *args, **kwargs) -> RingoJsonResponse: name = tweet['name'] quantities = [0] * 12 - quantities[tweet['month'] - 1] = tweet['quantity'] + # SQLiteの場合、日付は文字列での表現なため、int()で数値に変換する + quantities[int(tweet['month']) - 1] = tweet['quantity'] results.append({ 'name': name, diff --git a/apps/tweets/models.py b/apps/tweets/models.py index 1c5d52a..e0545e1 100644 --- a/apps/tweets/models.py +++ b/apps/tweets/models.py @@ -23,10 +23,10 @@ def calculate_total_by_name_and_month(cls) -> QuerySet: PostgreSQLの関数を使っているため、SQLiteなどでは動作しない """ - return cls.objects.extra(select={'month': "date_part('month', tweeted_at)::int"}) \ - .values('name', 'month') \ - .annotate(quantity=models.Count('name')) \ - .order_by('name', 'month') + return cls.objects.extra(select={'month': "strftime('%m', tweeted_at)"}) \ + .values('name', 'month') \ + .annotate(quantity=models.Count('name')) \ + .order_by('name', 'month') class LastSearch(models.Model): diff --git a/dj_ringo_tabetter/settings.py b/dj_ringo_tabetter/settings.py index 2412ba6..0941ca3 100644 --- a/dj_ringo_tabetter/settings.py +++ b/dj_ringo_tabetter/settings.py @@ -111,11 +111,10 @@ else: # 開発環境の場合、engineも渡さないとエラーとなる DATABASES = { - 'default': - dj_database_url.config( - default='postgres://ringo:postgres@127.0.0.1:19876/ringo_tabetter_py', - engine=dj_database_url.SCHEMES['postgresql'] - ) + 'default': { + 'NAME': os.path.join(BASE_DIR, 'ringo.db'), + 'ENGINE': 'django.db.backends.sqlite3', + } } From e1f3fecc73e8242ebaa52fd2e0061ef8525295cc Mon Sep 17 00:00:00 2001 From: thinkAmi Date: Tue, 13 Sep 2022 20:40:26 +0900 Subject: [PATCH 2/4] =?UTF-8?q?pip-review=20--auto=20=E3=81=A7=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3=E3=82=A2=E3=83=83=E3=83=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 85 +++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/requirements.txt b/requirements.txt index 98fcd91..14000da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,42 +1,47 @@ -atomicwrites==1.3.0 -attrs==19.1.0 -certifi==2019.6.16 -chardet==3.0.4 -dj-database-url==0.5.0 -Django==2.2.16 +asgiref==3.5.2 +atomicwrites==1.4.1 +attrs==22.1.0 +backports.zoneinfo==0.2.1 +certifi==2022.6.15.1 +chardet==5.0.0 +charset-normalizer==2.1.1 +dj-database-url==1.0.0 +Django==4.1.1 django-dotenv==1.4.2 -factory-boy==2.12.0 -Faker==8.1.0 -freezegun==0.3.12 -gunicorn==19.9.0 -idna==2.8 -importlib-metadata==0.20 -Jinja2==2.10.1 -MarkupSafe==1.1.1 -more-itertools==7.2.0 -oauthlib==3.1.0 -packaging==19.1 -pip-review==1.0 -pluggy==0.12.0 -psycopg2==2.8.6 -py==1.8.0 -pyparsing==2.4.2 -PySocks==1.7.0 -pytest==5.1.2 -pytest-django==3.5.1 -pytest-freezegun==0.3.0.post1 -python-dateutil==2.8.0 -python-dotenv==0.10.3 -pytz==2019.2 -PyYAML==5.1.2 -requests==2.22.0 -requests-oauthlib==1.2.0 -six==1.12.0 -slacker==0.13.0 -sqlparse==0.3.0 +factory-boy==3.2.1 +Faker==14.2.0 +freezegun==1.2.2 +gunicorn==20.1.0 +idna==3.3 +importlib-metadata==4.12.0 +iniconfig==1.1.1 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +more-itertools==8.14.0 +oauthlib==3.2.1 +packaging==21.3 +pip-review==1.2.0 +pluggy==1.0.0 +psycopg2==2.9.3 +py==1.11.0 +pyparsing==3.0.9 +PySocks==1.7.1 +pytest==7.1.3 +pytest-django==4.5.2 +pytest-freezegun==0.4.2 +python-dateutil==2.8.2 +python-dotenv==0.21.0 +pytz==2022.2.1 +PyYAML==6.0 +requests==2.28.1 +requests-oauthlib==1.3.1 +six==1.16.0 +slacker==0.14.0 +sqlparse==0.4.2 text-unidecode==1.3 -tweepy==3.8.0 -urllib3==1.25.3 -wcwidth==0.1.7 -whitenoise==4.1.3 -zipp==0.6.0 +tomli==2.0.1 +tweepy==4.10.1 +urllib3==1.26.12 +wcwidth==0.2.5 +whitenoise==6.2.0 +zipp==3.8.1 From 1c3de884df98cc292a50292785ac5a824a243ce6 Mon Sep 17 00:00:00 2001 From: thinkAmi Date: Tue, 13 Sep 2022 20:40:53 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E8=90=BD=E3=81=A1=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=9F=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/tests/test_views.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/apps/api/tests/test_views.py b/apps/api/tests/test_views.py index 0929fee..7e8dae9 100644 --- a/apps/api/tests/test_views.py +++ b/apps/api/tests/test_views.py @@ -18,16 +18,16 @@ def total_apples_expected(): TweetsFactory(name='シナノゴールド') return '''[ - { - "name": "シナノドルチェ", - "y": 2, - "color": "AntiqueWhite" - }, { "name": "シナノゴールド", "y": 5, "color": "Gold" }, + { + "name": "シナノドルチェ", + "y": 2, + "color": "AntiqueWhite" + }, { "name": "フジ", "y": 3, @@ -63,13 +63,13 @@ def total_apples_by_month_expected(): return '''[ { - "name": "フジ", + "name": "シナノゴールド", "data": [ 1, 1, 1, - 0, - 0, + 1, + 1, 0, 0, 0, @@ -78,16 +78,16 @@ def total_apples_by_month_expected(): 0, 0 ], - "color": "Red" + "color": "Gold" }, { - "name": "シナノゴールド", + "name": "シナノドルチェ", "data": [ 1, 1, - 1, - 1, - 1, + 0, + 0, + 0, 0, 0, 0, @@ -96,14 +96,14 @@ def total_apples_by_month_expected(): 0, 0 ], - "color": "Gold" + "color": "AntiqueWhite" }, { - "name": "シナノドルチェ", + "name": "フジ", "data": [ 1, 1, - 0, + 1, 0, 0, 0, @@ -114,7 +114,7 @@ def total_apples_by_month_expected(): 0, 0 ], - "color": "AntiqueWhite" + "color": "Red" } ]''' From 9879b9d6dd3617af49b94f5d722263f71ea6e305 Mon Sep 17 00:00:00 2001 From: thinkAmi Date: Wed, 14 Sep 2022 21:34:30 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Python=203.10.7=20&=20Django=204.1.1=20?= =?UTF-8?q?=E3=81=B8=E3=82=A2=E3=83=83=E3=83=97=E3=83=87=E3=83=BC=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .python-version | 2 +- ...002_alter_lastsearch_id_alter_tweets_id.py | 23 +++++++++++++++++++ dj_ringo_tabetter/settings.py | 3 +++ requirements.txt | 1 - 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 apps/tweets/migrations/0002_alter_lastsearch_id_alter_tweets_id.py diff --git a/.gitignore b/.gitignore index 3e6e070..522018e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ Scripts/ pyvenv.cfg pip-selfcheck.json +env*/ #### backup files *.dump diff --git a/.python-version b/.python-version index 2e14a95..1281604 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.8.6 +3.10.7 diff --git a/apps/tweets/migrations/0002_alter_lastsearch_id_alter_tweets_id.py b/apps/tweets/migrations/0002_alter_lastsearch_id_alter_tweets_id.py new file mode 100644 index 0000000..e1582b8 --- /dev/null +++ b/apps/tweets/migrations/0002_alter_lastsearch_id_alter_tweets_id.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.1 on 2022-09-13 11:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tweets', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='lastsearch', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + migrations.AlterField( + model_name='tweets', + name='id', + field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), + ), + ] diff --git a/dj_ringo_tabetter/settings.py b/dj_ringo_tabetter/settings.py index 0941ca3..4d210d9 100644 --- a/dj_ringo_tabetter/settings.py +++ b/dj_ringo_tabetter/settings.py @@ -173,3 +173,6 @@ STATIC_ROOT = 'staticfiles' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' + +# Django3.2以降の対応:主キーのフィールドを明示的に設定 +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/requirements.txt b/requirements.txt index 14000da..d4b56f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,6 @@ asgiref==3.5.2 atomicwrites==1.4.1 attrs==22.1.0 -backports.zoneinfo==0.2.1 certifi==2022.6.15.1 chardet==5.0.0 charset-normalizer==2.1.1