pokeapi/hits/models.py

45 lines
927 B
Python
Raw Normal View History

2014-12-04 11:11:46 +00:00
from django.db import models
2016-03-05 11:40:41 +00:00
from datetime import date
2014-12-04 11:11:46 +00:00
class ViewManager(models.Manager):
def increment_view_count(self, version):
2014-12-04 11:11:46 +00:00
view, _ = ResourceView.objects.get_or_create(
version=version,
2014-12-04 11:11:46 +00:00
date=date.today()
)
view.count = view.count + 1
print view
2014-12-04 11:11:46 +00:00
view.save()
def total_count(self, version=0):
if version:
objects = ResourceView.objects.filter(version=version)
else:
objects = ResourceView.objects.all()
2014-12-04 11:11:46 +00:00
t = 0
for v in objects:
2014-12-04 11:11:46 +00:00
t += v.count
return t
class ResourceView(models.Model):
objects = ViewManager()
def __unicode__(self):
return str(self.date) + ' - ' + str(self.count)
count = models.IntegerField(max_length=1000, default=0)
version = models.IntegerField(max_length=1, default=1)
2014-12-04 11:11:46 +00:00
date = models.DateField(auto_now=True)