Skip to content

Commit

Permalink
chore: Update database engine to use django.db.backends.mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Aug 28, 2024
1 parent a7dd002 commit d5ae762
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 100 deletions.
2 changes: 1 addition & 1 deletion apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import pendulum
from django.conf import settings
from django.contrib.auth.decorators import login_required, permission_required
from django.db.models.query import QuerySet
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.db.models.query import QuerySet

from apps.ext import lock
from apps.proxy import models as m
Expand Down
21 changes: 2 additions & 19 deletions apps/sspanel/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import datetime
import random
import re
import time
from decimal import Decimal
from socket import timeout
from urllib.parse import urlencode
from uuid import uuid4

import markdown
import pendulum
from django.conf import settings
from django.contrib import messages
Expand All @@ -17,6 +15,7 @@
from django.db import IntegrityError, models, transaction
from django.utils import functional, timezone
from redis.exceptions import LockError
from trix_editor.fields import TrixEditorField

from apps import constants as c
from apps.ext import cache, encoder, lock, pay
Expand Down Expand Up @@ -1008,7 +1007,7 @@ class Announcement(models.Model):
"""公告"""

time = models.DateTimeField("时间", auto_now_add=True)
body = models.TextField("主体")
body = TrixEditorField("主体")

class Meta:
verbose_name = "系统公告"
Expand All @@ -1018,11 +1017,6 @@ class Meta:
def __str__(self):
return "日期:{}".format(str(self.time)[:9])

def save(self, *args, **kwargs):
md = markdown.Markdown(extensions=["markdown.extensions.extra"])
self.body = md.convert(self.body)
super(Announcement, self).save(*args, **kwargs)

@classmethod
def send_first_visit_msg(cls, request):
anno = cls.objects.order_by("-time").first()
Expand All @@ -1031,17 +1025,6 @@ def send_first_visit_msg(cls, request):
request.session["first_visit"] = True
messages.warning(request, anno.plain_text, extra_tags="最新通知!")

@property
def plain_text(self):
# TODO 现在db里存的是md转换成的html,这里之后可能要优化。转换的逻辑移到前端去
re_br = re.compile("<br\s*?/?>") # 处理换行
re_h = re.compile("</?\w+[^>]*>") # HTML标签
s = re_br.sub("", self.body) # 去掉br
s = re_h.sub("", s) # 去掉HTML 标签
blank_line = re.compile("\n+") # 去掉多余的空行
s = blank_line.sub("", s)
return s


class Ticket(models.Model):
"""工单"""
Expand Down
6 changes: 1 addition & 5 deletions apps/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
name="password_reset",
), # NOTE 重写了重置密码的逻辑 一定要在`django.contrib.auth.urls`之前注册,不然会被覆盖
path("accounts/", include("django.contrib.auth.urls")),
path("trix-editor/", include("trix_editor.urls")),
]

# append sspanel template urls
Expand All @@ -29,11 +30,6 @@
path("admin/", admin.site.urls, name="admin"),
)

# append prometheus urls
urlpatterns.append(
path("prom/", include("django_prometheus.urls")),
)

# append openapi urls
urlpatterns.append(
path("openapi/v1/", include(openapi_router.urls)),
Expand Down
4 changes: 1 addition & 3 deletions configs/default/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"trix_editor",
"corsheaders",
"django_prometheus",
"django_telegram_login",
"anymail",
"apps.sspanel",
Expand All @@ -25,7 +25,6 @@


MIDDLEWARE = [
"django_prometheus.middleware.PrometheusBeforeMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -35,7 +34,6 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"apps.mw.ErrorHandlerMiddleware",
"django_prometheus.middleware.PrometheusAfterMiddleware",
]

ROOT_URLCONF = "apps.urls"
Expand Down
2 changes: 1 addition & 1 deletion configs/default/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# mysql 设置
DATABASES = {
"default": {
"ENGINE": "django_prometheus.db.backends.mysql",
"ENGINE": "django.db.backends.mysql",
"NAME": "sspanel",
"PASSWORD": os.getenv("MYSQL_PASSWORD", "yourpass"),
"HOST": os.getenv("MYSQL_HOST", "127.0.0.1"),
Expand Down
2 changes: 1 addition & 1 deletion configs/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# NOTE for django debug toolbar
DEBUG = True
INTERNAL_IPS = os.getenv("DEBUG_INTERNAL_IPS", "127.0.0.1,0.0.0.0").split(",")
INSTALLED_APPS.insert(INSTALLED_APPS.index("django_prometheus"), "debug_toolbar")
INSTALLED_APPS.insert(INSTALLED_APPS.index("django_telegram_login"), "debug_toolbar")
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
61 changes: 16 additions & 45 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ package-mode = false
[tool.poetry.dependencies]
aiohttp = "^3.9.3"
Django = "^5.1"
Markdown = "^3.3.4"
PyMySQL = "^1.1.0"
celery = "^5.3.4"
django-anymail = "^11.1"
django-cors-headers = "^4.2.0"
django-countries = "^7.1"
django-debug-toolbar = "^4.2.0"
django-prometheus = "^2.1.0"
django-simpleui = "^2024.4.1"
django-telegram-login = "^0.2.3"
ipicn = "^2021.5.5"
Expand All @@ -54,6 +52,7 @@ short_url = "^1.2.2"
tomd = "^0.1.3"
uWSGI = "^2.0.24"
djangorestframework = "^3.14.0"
django-trix-editor = "^0.3"

[tool.poetry.group.dev.dependencies]
autoflake = "^2.2.1"
Expand Down
35 changes: 13 additions & 22 deletions templates/web/announcement.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@ <h1 class="title">公告列表</h1>
</section>
</div>
<div class="column is-12">
<div class="box">
<div class="content">
<p class="subtitle">公告记录:</p>
<table class="table is-striped">
<thead>
<tr>
<th>ID</th>
<th>时间</th>
<th>内容</th>
</tr>
</thead>
{% for record in anno %}
<tr>
<td>#{{ record.pk }}</td>
<td>{{ record.time }}</td>
<td>{{ record.body |safe }}</td>
</tr>
{% empty %}
<h2 class="subtitle">暂时还没发出公告</h2>
{% endfor %}
</table>
</div>
<div class="panel">
{% for record in anno %}
<div class="panel-block is-block">
<div class="content">{{ record.body|safe }}</div>
<div class="is-flex is-justify-content-flex-end mt-2">
<small>{{ record.time }}</small>
</div>
</div>
{% empty %}
<div class="panel-block">
<h2 class="subtitle">暂时还没发出公告</h2>
</div>
{% endfor %}
</div>
</div>
{% endblock authed %}
2 changes: 1 addition & 1 deletion templates/web/user_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h1 class="title">用户中心</h1>
<div class="message-header">
<p>最新公告</p>
</div>
<div class="message-body">
<div class="content">
{% if anno %}
{{ anno.body | safe }}
{% else %}
Expand Down

0 comments on commit d5ae762

Please sign in to comment.