Skip to content

Commit

Permalink
设置session使用redis存储
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryJi529 committed Jun 4, 2024
1 parent e4ff270 commit b11c2fc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 25 deletions.
37 changes: 28 additions & 9 deletions Morningstar/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms
from django_recaptcha.fields import ReCaptchaField
from django.core.validators import RegexValidator
from django.http import HttpRequest
from django_redis import get_redis_connection
import re

Expand All @@ -16,7 +17,9 @@ class LoginForm(forms.Form):
label="账户",
initial="",
required=True,
widget=forms.TextInput(attrs={"placeholder": "用户名/邮箱/手机号", "class": "w-full"}),
widget=forms.TextInput(
attrs={"placeholder": "用户名/邮箱/手机号", "class": "w-full"}
),
)
password = forms.CharField(
label="密码",
Expand Down Expand Up @@ -56,9 +59,9 @@ def clean_identity(self):

def clean_image_captcha(self):
image_captcha = self.cleaned_data["image_captcha"]
request = self.request
request: HttpRequest = self.request

session_key = request.session._session_key
session_key = request.session.session_key

conn = get_redis_connection("default")
redis_image_captcha = conn.get(f"{session_key}-image-captcha")
Expand All @@ -84,7 +87,9 @@ class RegisterForm(forms.Form):
required=True,
widget=forms.EmailInput(attrs={"placeholder": FAKE_EMAIL, "class": "w-full"}),
validators=[
RegexValidator(r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", "邮箱格式错误"),
RegexValidator(
r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", "邮箱格式错误"
),
],
)

Expand All @@ -99,7 +104,10 @@ class RegisterForm(forms.Form):
],
min_length=6,
max_length=16,
error_messages={"min_length": "密码长度不能小于6个字符", "max_length": "密码长度不能大于16个字符"},
error_messages={
"min_length": "密码长度不能小于6个字符",
"max_length": "密码长度不能大于16个字符",
},
)

confirm_password = forms.CharField(
Expand All @@ -110,7 +118,10 @@ class RegisterForm(forms.Form):
),
min_length=6,
max_length=16,
error_messages={"min_length": "密码长度不能小于6个字符", "max_length": "密码长度不能大于16个字符"},
error_messages={
"min_length": "密码长度不能小于6个字符",
"max_length": "密码长度不能大于16个字符",
},
)

captcha = ReCaptchaField(label="人机验证")
Expand Down Expand Up @@ -182,7 +193,9 @@ class UpdateEmailForm(forms.Form):
email = forms.EmailField(
label="邮箱",
validators=[
RegexValidator(r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", "邮箱格式错误"),
RegexValidator(
r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", "邮箱格式错误"
),
],
)
email_code = forms.CharField(
Expand Down Expand Up @@ -246,7 +259,10 @@ class UpdatePasswordForm(forms.Form):
],
min_length=6,
max_length=16,
error_messages={"min_length": "密码长度不能小于6个字符", "max_length": "密码长度不能大于16个字符"},
error_messages={
"min_length": "密码长度不能小于6个字符",
"max_length": "密码长度不能大于16个字符",
},
)

confirm_password = forms.CharField(
Expand All @@ -257,7 +273,10 @@ class UpdatePasswordForm(forms.Form):
),
min_length=6,
max_length=16,
error_messages={"min_length": "密码长度不能小于6个字符", "max_length": "密码长度不能大于16个字符"},
error_messages={
"min_length": "密码长度不能小于6个字符",
"max_length": "密码长度不能大于16个字符",
},
)

def clean_confirm_password(self):
Expand Down
5 changes: 5 additions & 0 deletions Morningstar/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ def filter(self, record):
else 5
)

"""会话"""
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"
SESSION_SERIALIZER = "django.contrib.sessions.serializers.JSONSerializer"

"""Matomo"""
MATOMO_SITE_ID = 1
MATOMO_URL = "https://matomo.morningstar369.com/"
Expand Down
2 changes: 1 addition & 1 deletion Morningstar/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@


"""缓存"""
CACHE_TIMEOUT = 5
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 1000, "encoding": "utf-8"},
"PASSWORD": "1234asdw",
},
}
}
Expand Down
1 change: 0 additions & 1 deletion Morningstar/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
}

"""缓存"""
CACHE_TIMEOUT = 60 * 5
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
Expand Down
4 changes: 2 additions & 2 deletions Morningstar/views/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
def get_image_captcha(request: HttpRequest):
"""生成图片验证码"""
image_object, code = generate_image()
session_key = request.session._session_key

request.session.cycle_key()
session_key = request.session.session_key
conn = get_redis_connection("default")
conn.set(f"{session_key}-image-captcha", code, ex=60)

Expand Down
3 changes: 2 additions & 1 deletion apps/share/frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {useRoute} from 'vue-router'
import Cookies from 'js-cookie'
const route = useRoute()
const BASE_URL = process.env.BASE_URL
const currentPath = ref(window.location.href)
const endpoint = "/submit/"
Expand Down Expand Up @@ -51,7 +52,7 @@ const handleSubmit = async ()=>{
</a>
</div>
<div class="flex justify-center">
<img :src="`${baseURL}qrcode/`" alt="链接二维码" class="h-[8rem] md:h-[16rem]">
<img :src="`${BASE_URL}qrcode/`" alt="链接二维码" class="h-[8rem] md:h-[16rem]">
</div>
</div>
</template>
Expand Down
14 changes: 3 additions & 11 deletions apps/share/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ def route(request: HttpRequest, id):


def get_qrcode(request: HttpRequest):
session_key = request.session._session_key
conn = get_redis_connection("default")
link = conn.get(f"{session_key}-share-qrcode")
link = request.session.get("share-qrcode")
back_color = (255, 255, 255)
center_color = (250, 200, 100)
edge_color = (75, 150, 60)
icon_path = BASIC_STATICFILES_DIR / "base/img/logo.png"

qrcode_image = qrcoder.make_qrcode(
data=link,
image_size=(200, 200),
image_size=(400, 400),
box_radius_ratio=0.5,
icon_path=icon_path,
back_color=back_color,
Expand All @@ -69,11 +67,5 @@ def submit(request: HttpRequest):
item.save()
id = item.id
link = f"redirect/{id}/"
session_key = request.session._session_key
conn = get_redis_connection("default")
conn.set(
f"{session_key}-share-qrcode",
"https://morningstar369.com/share/" + link,
ex=60 * 10,
)
request.session["share-qrcode"] = "https://morningstar369.com/share/" + link
return Response({"link": link})

0 comments on commit b11c2fc

Please sign in to comment.