X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/c4c73f2204ba7136b9573c33b2a0f9a01829b78a..dce4cbe605cb926b5583f7ddd325704b527e7e5c:/src/stats/models.py?ds=inline diff --git a/src/stats/models.py b/src/stats/models.py index c1b4d16d8..e95e75951 100644 --- a/src/stats/models.py +++ b/src/stats/models.py @@ -7,17 +7,15 @@ 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() @classmethod def build_month(cls, year, month): Book = apps.get_model('catalogue', 'Book') - ### TODO: Delete existing? - 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:] @@ -33,7 +31,7 @@ class Visits(models.Model): except Book.DoesNotExist: continue else: - cls.objects.create( - book=book, year=year, month=month, - views=views, unique_views=uviews + cls.objects.update_or_create( + book=book, date=date, + defaults={'views': views, 'unique_views': uviews} )