Предварительная сохраняшка
This commit is contained in:
1 parent
ba33de3d77
commit
17f35a0f5d
875 files changed
+146196
-124
No files matched your search
Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class WatcherConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'watcher'
|
||||
@@ -0,0 +1,64 @@
|
||||
import json
|
||||
import base64
|
||||
from asgiref.sync import async_to_sync
|
||||
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||
from urllib.parse import parse_qs
|
||||
import jwt
|
||||
import redis
|
||||
|
||||
class WatcherConsumer(AsyncWebsocketConsumer):
|
||||
async def connect(self):
|
||||
self.client = redis.Redis(host='127.0.0.1')
|
||||
user = parse_qs(self.scope["query_string"].decode("utf8"))["token"][0]
|
||||
user = jwt.decode(user, options={"verify_signature": False})
|
||||
|
||||
# self.client.sadd('online',json.dumps(user['user'],indent=4))
|
||||
self.room_group_name = "online"
|
||||
self.client.hset('online',str(self.channel_name), user['user']['login'] )
|
||||
# Join room group
|
||||
await self.channel_layer.group_add(
|
||||
self.room_group_name, self.channel_name
|
||||
)
|
||||
online = [x.decode('utf-8') for x in self.client.hvals("online")]
|
||||
await self.channel_layer.group_send(
|
||||
self.room_group_name,
|
||||
{
|
||||
'type': 'chat_message',
|
||||
'message': {'action':'onliner','data':online}
|
||||
})
|
||||
|
||||
await self.accept()
|
||||
|
||||
|
||||
async def disconnect(self, close_code):
|
||||
# Leave room group
|
||||
self.client.hdel("online", self.channel_name)
|
||||
|
||||
await self.channel_layer.group_discard(
|
||||
self.room_group_name, self.channel_name
|
||||
)
|
||||
|
||||
online = [x.decode('utf-8') for x in self.client.hvals("online")]
|
||||
await self.channel_layer.group_send(
|
||||
self.room_group_name,
|
||||
{
|
||||
'type': 'chat_message',
|
||||
'message': {'action':'onliner','data':online}
|
||||
})
|
||||
|
||||
# Receive message from WebSocket
|
||||
async def receive(self, text_data):
|
||||
text_data_json = json.loads(text_data)
|
||||
message = text_data_json["message"]
|
||||
|
||||
# Send message to room group
|
||||
await self.channel_layer.group_send(
|
||||
self.room_group_name, {"type": "chat_message", "message": message}
|
||||
)
|
||||
|
||||
# Receive message from room group
|
||||
async def chat_message(self, event):
|
||||
message = event["message"]
|
||||
|
||||
# Send message to WebSocket
|
||||
await self.send(text_data=json.dumps({"message": message}))
|
||||
@@ -0,0 +1,17 @@
|
||||
from django.db import models
|
||||
from accounts.models import User
|
||||
# Create your models here.
|
||||
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)
|
||||
def __str__(self):
|
||||
return str(self.page)
|
||||
class Meta:
|
||||
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=['page', 'user', 'visitdate'], name='unique appversion')
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
# re_path(r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
|
||||
path("ws/watcher/", consumers.WatcherConsumer.as_asgi()),
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
from rest_framework import serializers
|
||||
from .models import *
|
||||
|
||||
|
||||
class CountSerializer(serializers.BaseSerializer):
|
||||
class Meta:
|
||||
|
||||
pass
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('counter/', Counter.as_view(),name="CountVisitors")
|
||||
# path('', views.Watchuser.as_view()),
|
||||
|
||||
|
||||
]
|
||||
@@ -0,0 +1,38 @@
|
||||
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
|
||||
|
||||
# Create your views here.
|
||||
|
||||
class Counter(ListAPIView):
|
||||
permission_classes = (AllowAny,)
|
||||
serializer_class = CountSerializer
|
||||
|
||||
# def get(self, request):
|
||||
# qs = CountVisitor.objects.values()
|
||||
# return Response(qs)
|
||||
|
||||
def post(self, request):
|
||||
print(request.data['user__id'])
|
||||
if request.data['user__id'] is not None:
|
||||
# print("sdddddd")
|
||||
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.aggregate(Sum('count'))['count__sum'],
|
||||
"visittoday":CountVisitor.objects.filter(visitdate=date.today()).aggregate(Sum('count'))['count__sum'],
|
||||
"uniqtoday" : CountVisitor.objects.filter(visitdate=date.today()).count()
|
||||
|
||||
}
|
||||
return Response(res)
|
||||
|
||||
Reference in new issue
Block a user