55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
from django.forms import model_to_dict
|
|
from django.shortcuts import render
|
|
from rest_framework.generics import ListAPIView
|
|
from .models import *
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from django.views.generic import ListView
|
|
from .serializers import *
|
|
from datetime import date
|
|
from django.db.models import Sum
|
|
from userauth.models import *
|
|
import pandas as pd
|
|
# Create your views here.
|
|
|
|
class Counter(ListAPIView):
|
|
permission_classes = (AllowAny,)
|
|
serializer_class = CountSerializer
|
|
|
|
def get(self, request):
|
|
res = CountVisitor.objects.values('page').annotate(Sum('count'))
|
|
users = CountVisitor.objects.values('page', 'user__username').annotate(Sum('count'))
|
|
outuser = DictEmployee.objects.using('user_directory').values('login', 'fio')
|
|
pUser = pd.DataFrame.from_records(users)
|
|
pOutUser = pd.DataFrame.from_records(outuser)
|
|
h = pUser.merge(pOutUser, how="left", left_on="user__username", right_on="login")
|
|
# print(pUser, '!!!!!!!!!!!!!!!!!!!!!!!!')
|
|
# print(pOutUser, '???????????????????????')
|
|
print(h.loc[h['login']=='9966-00-125']['fio'].unique()[0], '11111111111111111111111111111')
|
|
# print(h.nunique(login='9966-00-125'))
|
|
|
|
|
|
for x in users:
|
|
if x['user__username'] is not None:
|
|
x['user__username'] = h.loc[h['login']==x['user__username']]['fio'].unique()[0]
|
|
else:
|
|
x['user__username'] = 'Анонимные'
|
|
|
|
return Response({"pages":res, "users": users})
|
|
|
|
|
|
def post(self, request):
|
|
if request.data['user__id'] is not None:
|
|
watch,create = CountVisitor.objects.get_or_create(user = User(id=request.data['user__id']),page=request.data['page'], visitdate=date.today())
|
|
else:
|
|
watch,create = CountVisitor.objects.get_or_create(user__id__isnull=True, page=request.data['page'], visitdate=date.today())
|
|
watch.count+=1
|
|
watch.save()
|
|
|
|
res = {"totalvisit": CountVisitor.objects.filter(page=request.data['page']).aggregate(Sum('count'))['count__sum'],
|
|
"visittoday":CountVisitor.objects.filter(visitdate=date.today(), page=request.data['page']).aggregate(Sum('count'))['count__sum'],
|
|
"uniqtoday" : CountVisitor.objects.filter(visitdate=date.today(), page=request.data['page']).count()
|
|
}
|
|
|
|
return Response(res)
|
|
|