Счетчик
This commit is contained in:
1 parent
339f854494
commit
1b1220b9e7
11 files changed
+112
-80
No files matched your search
+22
-11
@@ -1,27 +1,38 @@
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from accounts.models import User
|
||||
# Create your models here.
|
||||
import pandas as pd
|
||||
|
||||
class CountVisitor(models.Model):
|
||||
page = models.CharField(db_index=True, max_length=255)
|
||||
visitdate = models.DateField()
|
||||
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.PROTECT)
|
||||
count = models.IntegerField(default=0)
|
||||
fullPath = models.CharField(max_length=255)
|
||||
class Pages(models.Model):
|
||||
page = models.CharField(max_length=255)
|
||||
fullPath = models.CharField(max_length=255)
|
||||
ico = models.CharField(verbose_name='Иконка', max_length=150, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.page)
|
||||
return str(self.page)
|
||||
|
||||
|
||||
class CountVisitor(models.Model):
|
||||
# page = models.CharField(db_index=True, max_length=255)
|
||||
pages = models.ForeignKey(Pages, on_delete=models.PROTECT, related_name='get_pages', verbose_name='Страница', null=True)
|
||||
visitdate = models.DateField()
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, verbose_name='Пользователь', null=True, blank=True)
|
||||
count = models.IntegerField(default=0)
|
||||
# fullPath = models.CharField(max_length=255)
|
||||
# ico = models.CharField(verbose_name='Иконка', max_length=150, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.pages)
|
||||
|
||||
|
||||
|
||||
class Meta:
|
||||
# class Meta:
|
||||
|
||||
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['page', 'user', 'visitdate', 'fullPath', 'ico'], name='unique appversion')
|
||||
]
|
||||
# constraints = [
|
||||
# models.UniqueConstraint(fields=['page', 'user', 'visitdate', 'fullPath', 'ico'], name='unique appversion')
|
||||
# ]
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('counter/', Counter.as_view(),name="CountVisitors"),
|
||||
path('selectedlinks/', SelectedLinksAPIView.as_view(),name="SelectedLinks")
|
||||
# path('selectedlinks/', SelectedLinksAPIView.as_view(),name="SelectedLinks")
|
||||
# path('', views.Watchuser.as_view()), selectedlinks
|
||||
|
||||
|
||||
|
||||
+38
-27
@@ -16,49 +16,60 @@ 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.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")
|
||||
# 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.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")
|
||||
|
||||
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'] = 'Анонимные'
|
||||
# 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})
|
||||
# return Response({"pages":res, "users": users})
|
||||
|
||||
|
||||
def post(self, request):
|
||||
|
||||
print('ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd')
|
||||
|
||||
|
||||
|
||||
|
||||
myfullPath = request.data['fullPath']
|
||||
if "?" in myfullPath:
|
||||
myfullPath= myfullPath.split("?")[0]
|
||||
# print(fullPath.split("?")[0], '---request.data-------------------------')
|
||||
print(request.data['page'], 'request.data[page]-------')
|
||||
page, create = Pages.objects.get_or_create(page=request.data['page'], fullPath=myfullPath, ico=request.data['ico'])
|
||||
|
||||
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(), fullPath=myfullPath, ico=request.data['ico'])
|
||||
else:
|
||||
watch,create = CountVisitor.objects.get_or_create(user__id__isnull=True, page=request.data['page'], visitdate=date.today(), fullPath=myfullPath, ico=request.data['ico'])
|
||||
watch,create = CountVisitor.objects.get_or_create(user = User(id=request.data['user__id']),pages=page, visitdate=date.today())
|
||||
print('if ----------------------------')
|
||||
else:
|
||||
watch,create = CountVisitor.objects.get_or_create(user__id__isnull=True, pages=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()
|
||||
|
||||
res = {"totalvisit": CountVisitor.objects.filter(pages=page.id).aggregate(Sum('count'))['count__sum'],
|
||||
"visittoday":CountVisitor.objects.filter(visitdate=date.today(), pages=page.id).aggregate(Sum('count'))['count__sum'],
|
||||
"uniqtoday" : CountVisitor.objects.filter(visitdate=date.today(), pages=page.id).count()
|
||||
}
|
||||
|
||||
|
||||
|
||||
return Response(res)
|
||||
|
||||
class SelectedLinksAPIView(ListAPIView):
|
||||
# class SelectedLinksAPIView(ListAPIView):
|
||||
|
||||
def get(self, request):
|
||||
user = User.objects.get(username=self.request.user)
|
||||
res = CountVisitor.objects.values('page','fullPath','ico').filter(user_id=user).annotate(Sum('count')).exclude(fullPath='/')
|
||||
sort = res.order_by('-count__sum')[:20]
|
||||
# def get(self, request):
|
||||
# user = User.objects.get(username=self.request.user)
|
||||
# res = CountVisitor.objects.values('page','fullPath','ico').filter(user_id=user).annotate(Sum('count')).exclude(fullPath='/')
|
||||
# sort = res.order_by('-count__sum')[:20]
|
||||
|
||||
|
||||
return Response(sort)
|
||||
# return Response(sort)
|
||||
|
||||
Reference in new issue
Block a user