Skip to content

Commit

Permalink
Merge pull request eee555#19 from putianyi889/patch-19
Browse files Browse the repository at this point in the history
初步简化`video_query`
  • Loading branch information
eee555 authored Apr 28, 2024
2 parents 9f63066 + 52e2273 commit 36188cc
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 38 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Back-end Test

on:
push:
branches:
- main # Adjust the branch name as needed
pull_request:

jobs:
check_flag:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Python
uses: actions/setup-python@v2
with:
python-version: '3.10' # Choose the Python version you want to use

- name: Run Test
run: python -m unittest back_end/saolei/tests.py -v
3 changes: 3 additions & 0 deletions back_end/saolei/config/flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
EMAIL_SKIP = True # 是否跳过邮箱验证。用于调试
BAIDU_VERIFY_SKIP = False # 是否跳过审查步骤。用于调试
DESIGNATOR_SKIP = False # 是否跳过标识检查。用于调试
13 changes: 0 additions & 13 deletions back_end/saolei/saolei/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,6 @@
EMAIL_HOST_PASSWORD = "TdS2ZPt2izAkY25b" # 密码 (注意:这里的密码指的是授权码)
# EMAIL_USE_SSL = True
# EMAIL_FROM = "wangjianing@88.com" # 邮箱来自
EMAIL_SKIP = False # 是否跳过邮箱验证。用于调试
if EMAIL_SKIP: # 以防万一忘了改EMAIL_SKIP
print("已跳过邮箱验证步骤!")
user_input = input("Do you want to continue? (y/n): ")
if user_input.lower() != 'y':
raise SystemExit("Program terminated.")

BAIDU_VERIFY_SKIP = False
if BAIDU_VERIFY_SKIP: # 以防万一忘了改BAIDU_VERIFY_SKIP
print("已跳过网络审查步骤!")
user_input = input("Do you want to continue? (y/n): ")
if user_input.lower() != 'y':
raise SystemExit("Program terminated.")

SESSION_COOKIE_NAME = "session_id" # Session的cookie保存在浏览器上时的key
SESSION_COOKIE_PATH = "/" # Session的cookie保存的路径(默认)
Expand Down
21 changes: 21 additions & 0 deletions back_end/saolei/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest

import sys
import os

# Get the parent directory of the current directory (my_script)
parent_dir = os.path.abspath(os.path.dirname(__file__))

# Add the parent directory to the Python path
sys.path.append(parent_dir)

from config.flags import *

class TestFlags(unittest.TestCase):
def test_skip(self):
self.assertFalse(EMAIL_SKIP)
self.assertFalse(BAIDU_VERIFY_SKIP)
self.assertFalse(DESIGNATOR_SKIP)

if __name__ == "__main__":
unittest.main()
5 changes: 3 additions & 2 deletions back_end/saolei/userprofile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from django_ratelimit.decorators import ratelimit
from django.utils import timezone
from django.conf import settings
from config.flags import EMAIL_SKIP

User = get_user_model()

Expand Down Expand Up @@ -152,9 +153,9 @@ def user_register(request):
email_captcha = request.POST.get("email_captcha", None)
get_email_captcha = EmailVerifyRecord.objects.filter(hashkey=emailHashkey).first()
# get_email_captcha = EmailVerifyRecord.objects.filter(hashkey="5f0db744-180b-4d9f-af5a-2986f4a78769").first()
if settings.EMAIL_SKIP or (get_email_captcha and email_captcha and get_email_captcha.code == email_captcha and\
if EMAIL_SKIP or (get_email_captcha and email_captcha and get_email_captcha.code == email_captcha and\
get_email_captcha.email == request.POST.get("email", None)):
if settings.EMAIL_SKIP or (timezone.now() - get_email_captcha.send_time).seconds <= 3600:
if EMAIL_SKIP or (timezone.now() - get_email_captcha.send_time).seconds <= 3600:
new_user = user_register_form.save(commit=False)
# 设置密码(哈希)
new_user.set_password(
Expand Down
2 changes: 1 addition & 1 deletion back_end/saolei/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.http import HttpResponse, JsonResponse, FileResponse
from django.shortcuts import render, redirect
import requests
from saolei.settings import BAIDU_VERIFY_SKIP
from config.flags import BAIDU_VERIFY_SKIP

def generate_code(code_len):
"""
Expand Down
42 changes: 20 additions & 22 deletions back_end/saolei/videomanager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# import ms_toollib as ms
from django.utils.encoding import escape_uri_path

from config.flags import DESIGNATOR_SKIP

logging.getLogger('apscheduler.scheduler').setLevel(logging.WARNING)
logger = logging.getLogger(__name__)

Expand All @@ -41,7 +43,7 @@ def video_upload(request):
# print(video_form)
if video_form.is_valid():
data = video_form.cleaned_data
if data["designator"] not in request.user.userms.designators:
if not DESIGNATOR_SKIP and data["designator"] not in request.user.userms.designators:
# 如果标识是首次使用的,需要得到管理员的审核
data['review_code'] = 2

Expand Down Expand Up @@ -163,15 +165,14 @@ def video_download(request):
# return response
# except Exception:
# return JsonResponse({"status": 104, "msg": "file not exist!"})



# 录像查询(无需登录)
# 按任何基础指标+难度+模式,排序,分页
@ratelimit(key='ip', rate='20/m')
def video_query(request):
if request.method == 'GET':
data = request.GET
# videos = VideoModel.objects.filter(*data["filter"]).order_by(*data["order_by"]).values(*data["values"])
index = data["index"]
if index[0] == '-':
order_index = "-video__" + index[1:]
Expand All @@ -180,32 +181,29 @@ def video_query(request):
order_index = values_index = "video__" + index

if data["mode"] != "00":
filter = {"level": data["level"], "mode": data["mode"]}
if index in {"id", "upload_time", "bv", "bvs", "-upload_time", "-bv", "-bvs"}:
videos = VideoModel.objects.filter(level=data["level"], mode=data["mode"])\
.order_by(index, "timems")\
.values("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
orderby = (index, "timems")
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
elif index == "timems" or index == "-timems":
videos = VideoModel.objects.filter(level=data["level"], mode=data["mode"])\
.order_by(index)\
.values("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
orderby = (index,)
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
else:
videos = VideoModel.objects.filter(level=data["level"], mode=data["mode"])\
.order_by(order_index, "timems")\
.values("id", "upload_time", "player__realname", "player__id", "bv",
"bvs", "timems", values_index)
orderby = (order_index, "timems")
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems", values_index)
videos = VideoModel.objects.filter(**filter).order_by(*orderby).values(*values)
else:
filter = {"level": data["level"]}
if index in {"id", "upload_time", "bv", "bvs", "-upload_time", "-bv", "-bvs"}:
videos = VideoModel.objects.filter(Q(mode="00")|Q(mode="12")).filter(level=data["level"])\
.order_by(index, "timems")\
.values("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
orderby = (index, "timems")
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
elif index == "timems" or index == "-timems":
videos = VideoModel.objects.filter(Q(mode="00")|Q(mode="12")).filter(level=data["level"])\
.order_by(index)\
.values("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
orderby = (index,)
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems")
else:
videos = VideoModel.objects.filter(Q(mode="00")|Q(mode="12")).filter(level=data["level"])\
.order_by(order_index, "timems")\
.values("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems", values_index)
orderby = (order_index, "timems")
values = ("id", "upload_time", "player__realname", "player__id", "bv", "bvs", "timems", values_index)
videos = VideoModel.objects.filter(Q(mode="00")|Q(mode="12")).filter(**filter).order_by(*orderby).values(*values)

# print(videos)
paginator = Paginator(videos, 20) # 每页20条数据
Expand Down

0 comments on commit 36188cc

Please sign in to comment.