from django.conf import settings
from django.db import models
class Visits(models.Model):
book = models.ForeignKey('catalogue.Book', models.CASCADE)
from django.conf import settings
from django.db import models
class Visits(models.Model):
book = models.ForeignKey('catalogue.Book', models.CASCADE)
views = models.IntegerField()
unique_views = models.IntegerField()
@classmethod
def build_month(cls, year, month):
views = models.IntegerField()
unique_views = models.IntegerField()
@classmethod
def build_month(cls, year, month):
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:]
m = re.match('^/katalog/lektura/([^,]+)\.html,', line)
if m is not None:
slug = m.group(1)
m = re.match('^/katalog/lektura/([^,]+)\.html,', line)
if m is not None:
slug = m.group(1)
- _url, uviews, views = line.split(',', 3)
+ _url, uviews, views, _rest = line.split(',', 3)
- cls.objects.create(
- book=book, year=year, month=month,
- views=views, uviews=uviews
+ cls.objects.update_or_create(
+ book=book, date=date,
+ defaults={'views': views, 'unique_views': uviews}