Stats migrations.
authorRadek Czajka <rczajka@rczajka.pl>
Tue, 1 Jun 2021 11:04:25 +0000 (13:04 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Tue, 1 Jun 2021 11:04:25 +0000 (13:04 +0200)
src/stats/admin.py
src/stats/migrations/0002_visits_date.py [new file with mode: 0644]
src/stats/migrations/0003_migrate_months.py [new file with mode: 0644]
src/stats/migrations/0004_auto_20210601_1303.py [new file with mode: 0644]
src/stats/models.py

index 979f180..11dfe7e 100644 (file)
@@ -2,4 +2,11 @@ from django.contrib import admin
 from . import models
 
 
 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 (file)
index 0000000..4abcafe
--- /dev/null
@@ -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 (file)
index 0000000..077819a
--- /dev/null
@@ -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 (file)
index 0000000..523af9b
--- /dev/null
@@ -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',
+        ),
+    ]
index 7d850af..9edc17b 100644 (file)
@@ -7,8 +7,7 @@ from django.db import models
 
 class Visits(models.Model):
     book = models.ForeignKey('catalogue.Book', models.CASCADE)
 
 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()
 
     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')
 
     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:]
         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:]