我正在生成短URL并生成与其对应的哈希值,并将其保存在DB中。对于每个哈希我也保存一些客户端数据。现在我想获得每个月给定哈希的总点击次数
Like for DCmuih their are 20 clicks in January and 30 in Feb and so on
models.py
import ast
import uuid
import base64
from django.db import models
from django.contrib import admin
from qlu.settings import HOST_NAME
from django.core.validators import URLValidator
#------------------------------------------------------------------------------
class short_url(models.Model):
"""
This is a short_url class
"""
blocked = models.BooleanField(default=False) # To check whether URL is blocked or not
updated_at = models.DateTimeField(auto_now=True) # When URL is updated
url = models.TextField(validators=[URLValidator()]) # URL entered by the user
created_at = models.DateTimeField(auto_now_add=True) # When URL is created
url_hash = models.CharField(max_length=10,unique=True,db_index=True) # base64 encoded URL id
def _generateShortUrl(self):
"""
This function will generate base64 encoded URL hash
"""
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
while hash_exist:
hash = base64.urlsafe_b64encode(uuid.uuid1().bytes)[:6]
hash_exist = short_url.objects.filter(url_hash=hash)
continue
return hash
def save(self, *args, **kwargs):
"""
Custom Save method for link model
"""
self.url_hash = self._generateShortUrl()
super(short_url, self).save(*args, **kwargs)
def get_short_url(self):
"""
This method returns the url_hash related to the url
"""
return HOST_NAME + self.url_hash
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.url)
class click_info(models.Model):
"""
This is a click_info class
"""
user_ip = models.TextField() # Store the user_ip
user_agent = models.TextField() # Store the user_agent
http_refrer = models.TextField() # Store the http_refrer
hash = models.ForeignKey(short_url) # base64 encoded URL id
get_parameters = models.TextField() # Store other get_parameters
request_time = models.DateTimeField() # When user made the request_time
updated_at = models.DateTimeField(auto_now=True) # When click_info is updated
created_at = models.DateTimeField(auto_now_add=True) # When click is created
def get_parameters_dict(self):
"""
This method returns the get parameter dict
"""
return ast.literal_eval(self.get_parameters)
def __unicode__(self):
"""
This method convert Django model object to the user readable string
"""
return unicode(self.hash)
#------------------------------------------------------------------------------
class short_url_admin(admin.ModelAdmin):
"""
short_url_admin class
"""
list_display = ('url','blocked','updated_at',
'created_at','url_hash')
exclude = ('url_hash',)
class url_info_admin(admin.ModelAdmin):
"""
url_info_admin class
"""
list_display = ('user_ip','user_agent','http_refrer',
'hash','request_time','get_parameters_dict')
#------------------------------------------------------------------------------
admin.site.register(short_url,short_url_admin)
admin.site.register(click_info,url_info_admin)
答案 0 :(得分:1)
只需查询时间范围:
import datetime
from models import click_info
def get_click_by_month(month, year, hash):
d1 = datetime.datetime(year=year, month=month, day=1)
d2 = datetime.datetime(year=year, month=month + 1, day=1)
clicks = click_info.objects.filter(url_hash=hash, created_at__range=[d1, d2])
return len(clicks)
P.S。 PEP8建议使用CamelCase来命名类。当然,这不是强制性的,但至少团队内部的人必须使用相同的代码编写原则。使用PEP8比创建自己的规则更容易。