Django保存整个统计数据请求,有什么可用?

前端之家收集整理的这篇文章主要介绍了Django保存整个统计数据请求,有什么可用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想保存可用于统计信息的所有内容,例如引荐来源网址,操作系统,浏览器等.有什么可用的,最好的方法是存储?

这对于项目中的1个应用程序(1页)是非常重要的,其他页面将使用一些标准分析产品,如谷歌分析.

我看了django跟踪,但似乎这是过度的,因为我只想在1视图中使用它.理想的情况是,将整个请求对象传递给TaskQue,然后进行处理.所以用户首先被重定向,分析处理将在幕后完成.

解决方法

我们使用一些简单的中间件..下面是摘录.您可以修改它直接在视图中使用.
class WebRequest(models.Model):
    time = models.DateTimeField(auto_now_add=True)
    host = models.CharField(max_length=1000)
    path = models.CharField(max_length=1000)
    method = models.CharField(max_length=50)
    uri = models.CharField(max_length=2000)
    status_code = models.IntegerField()
    user_agent = models.CharField(max_length=1000,blank=True,null=True)
    remote_addr = models.IPAddressField()
    remote_addr_fwd = models.IPAddressField(blank=True,null=True)
    Meta = models.TextField()
    cookies = models.TextField(blank=True,null=True)
    get = models.TextField(blank=True,null=True)
    post = models.TextField(blank=True,null=True)
    raw_post = models.TextField(blank=True,null=True)
    is_secure = models.BooleanField()
    is_ajax = models.BooleanField()
    user = models.ForeignKey(User,null=True)

def dumps(value):
    return json.dumps(value,default=lambda o:None)

class WebRequestMiddleware(object):
    def process_view(self,request,view_func,view_args,view_kwargs):
        setattr(request,'hide_post',view_kwargs.pop('hide_post',False))


    def process_response(self,response):

        if request.path.endswith('/favicon.ico'):
            return response

        if type(response) == HttpResponsePermanentRedirect and settings.APPEND_SLASH:
            new_location = response.get('location',None)
            content_length = response.get('content-length',None)

            if new_location and content_length is '0':
                new_parsed = urlparse(new_location)

                old = (('http','https')[request.is_secure()],request.get_host(),'{0}/'.format(request.path),request.Meta['QUERY_STRING'])
                new = (new_parsed.scheme,new_parsed.netloc,new_parsed.path,new_parsed.query)

                if old == new:
                    #dont log - it's just adding a /
                    return response
        try:
            self.save(request,response)
        except Exception as e:
            print >> sys.stderr,"Error saving request log",e

        return response

    def save(self,response):
        if hasattr(request,'user'):
            user = request.user if type(request.user) == User else None
        else:
            user = None

        Meta = request.Meta.copy()
        Meta.pop('QUERY_STRING',None)
        Meta.pop('HTTP_COOKIE',None)
        remote_addr_fwd = None

        if 'HTTP_X_FORWARDED_FOR' in Meta:
            remote_addr_fwd = Meta['HTTP_X_FORWARDED_FOR'].split(",")[0].strip()
            if remote_addr_fwd == Meta['HTTP_X_FORWARDED_FOR']:
                Meta.pop('HTTP_X_FORWARDED_FOR')

        post = None
        uri = request.build_absolute_uri()
        if request.POST and uri != '/login/':
            post = dumps(request.POST)

        models.WebRequest(
            host = request.get_host(),path = request.path,method = request.method,uri = request.build_absolute_uri(),status_code = response.status_code,user_agent = Meta.pop('HTTP_USER_AGENT',None),remote_addr = Meta.pop('REMOTE_ADDR',remote_addr_fwd = remote_addr_fwd,Meta = None if not Meta else dumps(Meta),cookies = None if not request.COOKIES else dumps(request.COOKIES),get = None if not request.GET else dumps(request.GET),post = None if (not request.POST or getattr(request,'hide_post') == True) else dumps(request.POST),raw_post = None if getattr(request,'hide_post') else request.raw_post_data,is_secure = request.is_secure(),is_ajax = request.is_ajax(),user = user
        ).save()
原文链接:https://www.f2er.com/python/186698.html

猜你在找的Python相关文章