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)
--- /dev/null
+# 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,
+ ),
+ ]
--- /dev/null
+# 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)
+ ]
--- /dev/null
+# 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',
+ ),
+ ]
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()
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:]