Skip to content

Commit

Permalink
Revert "Use poetry as a dependency management tool (#184)" (#191)
Browse files Browse the repository at this point in the history
This reverts commit 67aa128.
  • Loading branch information
lihuacai168 authored Feb 24, 2024
1 parent 67aa128 commit aa75927
Show file tree
Hide file tree
Showing 110 changed files with 2,415 additions and 4,213 deletions.
11 changes: 0 additions & 11 deletions .pre-commit-config.yaml

This file was deleted.

22 changes: 3 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
# 使用 python:3.11-buster 作为基础镜像
FROM python:3.11-buster as Base

# 安装 Poetry
RUN pip install poetry

# 复制 pyproject.toml 和 poetry.lock 文件
COPY pyproject.toml poetry.lock ./

# 使用 Poetry 生成 requirements.txt 文件
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
COPY requirements.txt .

# 安装依赖
ARG DEBIAN_REPO="deb.debian.org"
ARG PIP_INDEX_URL="https://pypi.org/simple"

Expand All @@ -28,11 +21,9 @@ RUN apt-get update && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/*

# 使用 python:3.11-buster 作为基础镜像
FROM python:3.11-buster
ENV TZ=Asia/Shanghai

# 设置时区
ARG DEBIAN_REPO="deb.debian.org"
RUN echo "deb http://$DEBIAN_REPO/debian/ buster main contrib non-free" > /etc/apt/sources.list && \
echo "deb-src http://$DEBIAN_REPO/debian/ buster main contrib non-free" >> /etc/apt/sources.list && \
Expand All @@ -41,26 +32,19 @@ RUN echo "deb http://$DEBIAN_REPO/debian/ buster main contrib non-free" > /etc/a
echo "deb http://$DEBIAN_REPO/debian/ buster-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb-src http://$DEBIAN_REPO/debian/ buster-updates main contrib non-free" >> /etc/apt/sources.list


RUN apt-get update && \
apt-get install -y tzdata && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone && \
rm -rf /var/lib/apt/lists/*

# 复制依赖
COPY --from=Base /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages

# 设置工作目录
WORKDIR /app

# 复制项目文件
COPY . /app

# 设置权限
RUN chmod +x /app/start.sh

# 收集静态文件
RUN python manage.py collectstatic --settings=FasterRunner.settings.docker --no-input

# 设置入口点
ENTRYPOINT ["/app/start.sh"]

1 change: 1 addition & 0 deletions FasterRunner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# 替换mysqlclient
pymysql.version_info = (1, 4, 6, "final", 0) # 修改版本号为兼容版本
pymysql.install_as_MySQLdb()

57 changes: 29 additions & 28 deletions FasterRunner/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from django.utils.translation import gettext as _
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from rest_framework_jwt.authentication import (
JSONWebTokenAuthentication,
jwt_get_username_from_payload,
)
from rest_framework_jwt.authentication import JSONWebTokenAuthentication, jwt_get_username_from_payload
from rest_framework_jwt.settings import api_settings

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
Expand All @@ -21,9 +18,11 @@
def is_admin(token):
is_permission = models.MyUser.objects.filter(is_superuser=1).first()
if not is_permission:
raise exceptions.PermissionDenied(
{"code": "9996", "msg": "权限不足,请联系管理员", "success": False}
)
raise exceptions.PermissionDenied({
"code": "9996",
"msg": "权限不足,请联系管理员",
"success": False
})
else:
return True

Expand All @@ -34,12 +33,12 @@ class OnlyGetAuthenticator(BaseAuthentication):
"""

def authenticate(self, request):
if request.method != "GET":
if request.method != 'GET':
token = request.query_params.get("token", None)
is_admin(token)

def authenticate_header(self, request):
return "PermissionDenied"
return 'PermissionDenied'


class Authenticator(BaseAuthentication):
Expand All @@ -48,25 +47,26 @@ class Authenticator(BaseAuthentication):
"""

def authenticate(self, request):

token = request.query_params.get("token", None)
obj = models.UserToken.objects.filter(token=token).first()

if not obj:
raise exceptions.AuthenticationFailed(
{"code": "9998", "msg": "用户未认证", "success": False}
)
raise exceptions.AuthenticationFailed({
"code": "9998",
"msg": "用户未认证",
"success": False
})

update_time = int(obj.update_time.timestamp())
current_time = int(time.time())

if current_time - update_time >= INVALID_TIME:
raise exceptions.AuthenticationFailed(
{
"code": "9997",
"msg": "登陆超时,请重新登陆",
"success": False,
}
)
raise exceptions.AuthenticationFailed({
"code": "9997",
"msg": "登陆超时,请重新登陆",
"success": False
})

# valid update valid time
obj.token = token
Expand All @@ -75,7 +75,7 @@ def authenticate(self, request):
return obj.user, obj

def authenticate_header(self, request):
return "Auth Failed"
return 'Auth Failed'


class DeleteAuthenticator(BaseAuthentication):
Expand All @@ -84,29 +84,30 @@ class DeleteAuthenticator(BaseAuthentication):
"""

def authenticate(self, request):
if request.method == "DELETE":
if request.method == 'DELETE':
token = request.query_params.get("token", None)
is_admin(token)

def authenticate_header(self, request):
return "PermissionDenied"
return 'PermissionDenied'


class MyJWTAuthentication(JSONWebTokenAuthentication):

def authenticate(self, request):
"""
Returns a two-tuple of `User` and token if a valid signature has been
supplied using JWT-based authentication. Otherwise returns `None`.
"""
# jwt_value = request.query_params.get("token", None)
jwt_value = request.headers.get("authorization", None)
jwt_value = request.headers.get('authorization', None)
try:
payload = jwt_decode_handler(jwt_value)
except jwt.ExpiredSignature:
msg = "签名过期"
msg = '签名过期'
raise exceptions.AuthenticationFailed(msg)
except jwt.DecodeError:
msg = "签名解析失败"
msg = '签名解析失败'
raise exceptions.AuthenticationFailed(msg)
except jwt.InvalidTokenError:
raise exceptions.AuthenticationFailed()
Expand All @@ -123,17 +124,17 @@ def authenticate_credentials(self, payload):
username = jwt_get_username_from_payload(payload)

if not username:
msg = _("Invalid payload.")
msg = _('Invalid payload.')
raise exceptions.AuthenticationFailed(msg)

try:
user = User.objects.get_by_natural_key(username)
except User.DoesNotExist:
msg = "用户不存在"
msg = '用户不存在'
raise exceptions.AuthenticationFailed(msg)

if not user.is_active:
msg = "用户已禁用"
msg = '用户已禁用'
raise exceptions.AuthenticationFailed(msg)

return user
14 changes: 8 additions & 6 deletions FasterRunner/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-

# @Author:梨花菜
# @File: database.py
# @File: database.py
# @Time : 2019/6/13 17:08
# @Email: [email protected]
# @Software: PyCharm
Expand All @@ -10,16 +11,17 @@

class AutoChoiceDataBase:
run_system = platform.system()
is_windows = run_system == "Windows"
is_windows = run_system == 'Windows'

def db_for_read(self, model, **hints):
if self.is_windows:
return "default"
return 'default'
else:
return "remote"
return 'remote'

def db_for_write(self, model, **hints):
if self.is_windows:
return "default"
return 'default'
else:
return "remote"
return 'remote'

3 changes: 1 addition & 2 deletions FasterRunner/log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-

# @Author: 花菜
# @File: log.py
Expand All @@ -7,11 +8,9 @@

import logging


class DatabaseLogHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
from system.models import LogRecord # 引入上面定义的LogRecord模型

LogRecord.objects.create(
request_id=record.request_id,
level=record.levelname,
Expand Down
1 change: 0 additions & 1 deletion FasterRunner/mycelery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os

from celery import Celery
from celery.signals import after_setup_logger

Expand Down
6 changes: 2 additions & 4 deletions FasterRunner/mycelery_dev.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
# from django.conf import settings
from FasterRunner.settings import dev as settings

# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FasterRunner.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FasterRunner.settings.dev")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FasterRunner.settings.dev')

app = Celery("FasterRunner")
app = Celery('FasterRunner')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
Expand Down
10 changes: 5 additions & 5 deletions FasterRunner/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ class MyCursorPagination(pagination.CursorPagination):
"""
Cursor 光标分页 性能高,安全
"""

page_size = 9
ordering = "-update_time"
ordering = '-update_time'
page_size_query_param = "pages"
max_page_size = 20

Expand All @@ -16,8 +15,9 @@ class MyPageNumberPagination(pagination.PageNumberPagination):
"""
普通分页,数据量越大性能越差
"""

page_size = 11
page_size_query_param = "size"
page_query_param = "page"
page_size_query_param = 'size'
page_query_param = 'page'
max_page_size = 20


14 changes: 8 additions & 6 deletions FasterRunner/routers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-

# @Author:梨花菜
# @File: database.py
# @File: database.py
# @Time : 2019/6/13 17:08
# @Email: [email protected]
# @Software: PyCharm
Expand All @@ -10,16 +11,17 @@

class AutoChoiceDataBase:
run_system = platform.system()
is_windows = run_system == "Windows"
is_windows = run_system == 'Windows'

def db_for_read(self, model, **hints):
if self.is_windows:
return "default"
return 'default'
else:
return "remote"
return 'remote'

def db_for_write(self, model, **hints):
if self.is_windows:
return "default"
return 'default'
else:
return "remote"
return 'remote'

7 changes: 3 additions & 4 deletions FasterRunner/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# !/usr/bin/python3
# -*- coding: utf-8 -*-

# @Author:梨花菜
# @File: __init__.py.py
# @File: __init__.py.py
# @Time : 2019/6/14 17:05
# @Email: [email protected]
# @Software: PyCharm

# 兼容已经移除的方法
import django
from django.utils.encoding import smart_str

django.utils.encoding.smart_text = smart_str

from django.utils.translation import gettext_lazy

django.utils.translation.ugettext = gettext_lazy
django.utils.translation.ugettext_lazy = gettext_lazy
django.utils.translation.ugettext_lazy = gettext_lazy
Loading

0 comments on commit aa75927

Please sign in to comment.