From b407504fbd3d55b7dfd64e82c1ab12d3a8d688bf Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 1 Jun 2021 13:04:25 +0200 Subject: [PATCH] Stats migrations. --- src/stats/admin.py | 9 +++++- src/stats/migrations/0002_visits_date.py | 19 ++++++++++++ src/stats/migrations/0003_migrate_months.py | 29 +++++++++++++++++++ .../migrations/0004_auto_20210601_1303.py | 21 ++++++++++++++ src/stats/models.py | 5 ++-- 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/stats/migrations/0002_visits_date.py create mode 100644 src/stats/migrations/0003_migrate_months.py create mode 100644 src/stats/migrations/0004_auto_20210601_1303.py diff --git a/src/stats/admin.py b/src/stats/admin.py index 979f18022..11dfe7e89 100644 --- a/src/stats/admin.py +++ b/src/stats/admin.py @@ -2,4 +2,11 @@ from django.contrib import admin from . import models -admin.site.register(models.Visits) +class VisitsAdmin(admin.ModelAdmin): + list_display = ['book', 'date', 'views', 'unique_views'] + raw_id_fields = ['book'] + date_hierarchy = 'date' + + + +admin.site.register(models.Visits, VisitsAdmin) diff --git a/src/stats/migrations/0002_visits_date.py b/src/stats/migrations/0002_visits_date.py new file mode 100644 index 000000000..4abcafe39 --- /dev/null +++ b/src/stats/migrations/0002_visits_date.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.19 on 2021-06-01 10:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stats', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='visits', + name='date', + field=models.DateField(default='1970-01-01'), + preserve_default=False, + ), + ] diff --git a/src/stats/migrations/0003_migrate_months.py b/src/stats/migrations/0003_migrate_months.py new file mode 100644 index 000000000..077819ad9 --- /dev/null +++ b/src/stats/migrations/0003_migrate_months.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.19 on 2021-06-01 10:58 + +import datetime +from django.db import migrations + + +def ints_to_dates(apps, schema_editor): + Visits = apps.get_model('stats', 'Visits') + for visit in Visits.objects.all(): + visit.date = datetime.date(visit.year, visit.month, 1) + visit.save() + +def dates_to_ints(apps, schema_editor): + Visits = apps.get_model('stats', 'Visits') + for visit in Visits.objects.all(): + visit.year = visit.date.year + visit.month = visit.date.month + visit.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('stats', '0002_visits_date'), + ] + + operations = [ + migrations.RunPython(ints_to_dates, dates_to_ints) + ] diff --git a/src/stats/migrations/0004_auto_20210601_1303.py b/src/stats/migrations/0004_auto_20210601_1303.py new file mode 100644 index 000000000..523af9b51 --- /dev/null +++ b/src/stats/migrations/0004_auto_20210601_1303.py @@ -0,0 +1,21 @@ +# Generated by Django 2.2.19 on 2021-06-01 11:03 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('stats', '0003_migrate_months'), + ] + + operations = [ + migrations.RemoveField( + model_name='visits', + name='month', + ), + migrations.RemoveField( + model_name='visits', + name='year', + ), + ] diff --git a/src/stats/models.py b/src/stats/models.py index 7d850af11..9edc17be7 100644 --- a/src/stats/models.py +++ b/src/stats/models.py @@ -7,8 +7,7 @@ from django.db import models class Visits(models.Model): book = models.ForeignKey('catalogue.Book', models.CASCADE) - year = models.PositiveSmallIntegerField() - month = models.PositiveSmallIntegerField() + date = models.DateField() views = models.IntegerField() unique_views = models.IntegerField() @@ -16,7 +15,7 @@ class Visits(models.Model): def build_month(cls, year, month): Book = apps.get_model('catalogue', 'Book') - date = f'{year}-{month:02d}' + date = f'{year}-{month:02d}-01' url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period=month&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1' data = urlopen(url).read().decode('utf-16') lines = data.split('\n')[1:] -- 2.20.1