first commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
from django import forms
|
||||
|
||||
|
||||
class BootStrap:
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# 循环ModelForm中的所有字段,给每个字段的插件设置
|
||||
for name, field in self.fields.items():
|
||||
# 字段中有属性,保留原来的属性,没有属性,才增加。
|
||||
if field.widget.attrs:
|
||||
field.widget.attrs["class"] = "form-control"
|
||||
field.widget.attrs["placeholder"] = field.label
|
||||
else:
|
||||
field.widget.attrs = {
|
||||
"class": "form-control",
|
||||
"placeholder": field.label
|
||||
}
|
||||
|
||||
|
||||
class BootStrapModelForm(BootStrap, forms.ModelForm):
|
||||
pass
|
||||
|
||||
|
||||
class BootStrapForm(BootStrap, forms.Form):
|
||||
pass
|
||||
@@ -0,0 +1,55 @@
|
||||
import random
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
||||
|
||||
|
||||
def check_code(width=120, height=30, char_length=5, font_file='Monaco.ttf', font_size=28):
|
||||
code = []
|
||||
img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
|
||||
draw = ImageDraw.Draw(img, mode='RGB')
|
||||
|
||||
def rndChar():
|
||||
"""
|
||||
生成随机字母
|
||||
:return:
|
||||
"""
|
||||
# return str(random.randint(0, 9))
|
||||
return chr(random.randint(65, 90))
|
||||
|
||||
|
||||
def rndColor():
|
||||
"""
|
||||
生成随机颜色
|
||||
:return:
|
||||
"""
|
||||
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
|
||||
|
||||
# 写文字
|
||||
font = ImageFont.truetype(font_file, font_size)
|
||||
for i in range(char_length):
|
||||
char = rndChar()
|
||||
code.append(char)
|
||||
h = random.randint(0, 4)
|
||||
draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
|
||||
|
||||
# 写干扰点
|
||||
for i in range(40):
|
||||
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
|
||||
|
||||
# 写干扰圆圈
|
||||
for i in range(40):
|
||||
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
|
||||
x = random.randint(0, width)
|
||||
y = random.randint(0, height)
|
||||
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
|
||||
|
||||
# 画干扰线
|
||||
for i in range(5):
|
||||
x1 = random.randint(0, width)
|
||||
y1 = random.randint(0, height)
|
||||
x2 = random.randint(0, width)
|
||||
y2 = random.randint(0, height)
|
||||
|
||||
draw.line((x1, y1, x2, y2), fill=rndColor())
|
||||
|
||||
img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
|
||||
return img, ''.join(code)
|
||||
@@ -0,0 +1,8 @@
|
||||
from django.conf import settings
|
||||
import hashlib
|
||||
|
||||
|
||||
def md5(data_string):
|
||||
obj = hashlib.md5(settings.SECRET_KEY.encode('utf-8'))
|
||||
obj.update(data_string.encode('utf-8'))
|
||||
return obj.hexdigest()
|
||||
@@ -0,0 +1,66 @@
|
||||
from aipeople_project.aipeople_project.login import models
|
||||
from django.core.validators import RegexValidator
|
||||
from django.core.exceptions import ValidationError
|
||||
from django import forms
|
||||
from .bootstrap import BootStrapModelForm
|
||||
|
||||
|
||||
class UserModelForm(BootStrapModelForm):
|
||||
name = forms.CharField(
|
||||
min_length=3,
|
||||
label="用户名",
|
||||
widget=forms.TextInput(attrs={"class": "form-control"})
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = models.UserInfo
|
||||
fields = ["name", "password", "age", 'account', 'create_time', "gender", "depart"]
|
||||
|
||||
|
||||
class PrettyModelForm(BootStrapModelForm):
|
||||
# 验证:方式1
|
||||
mobile = forms.CharField(
|
||||
label="手机号",
|
||||
validators=[RegexValidator(r'^1[3-9]\d{9}$', '手机号格式错误'), ],
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = models.PrettyNum
|
||||
# fields = "__all__"
|
||||
# exclude = ['level']
|
||||
fields = ["mobile", 'price', 'level', 'status']
|
||||
|
||||
# 验证:方式2
|
||||
def clean_mobile(self):
|
||||
txt_mobile = self.cleaned_data["mobile"]
|
||||
|
||||
exists = models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
|
||||
if exists:
|
||||
raise ValidationError("手机号已存在")
|
||||
|
||||
# 验证通过,用户输入的值返回
|
||||
return txt_mobile
|
||||
|
||||
|
||||
class PrettyEditModelForm(BootStrapModelForm):
|
||||
# mobile = forms.CharField(disabled=True, label="手机号")
|
||||
mobile = forms.CharField(
|
||||
label="手机号",
|
||||
validators=[RegexValidator(r'^1[3-9]\d{9}$', '手机号格式错误'), ],
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = models.PrettyNum
|
||||
fields = ['mobile', 'price', 'level', 'status']
|
||||
|
||||
# 验证:方式2
|
||||
def clean_mobile(self):
|
||||
# 当前编辑的哪一行的ID
|
||||
# print(self.instance.pk)
|
||||
txt_mobile = self.cleaned_data["mobile"]
|
||||
exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
|
||||
if exists:
|
||||
raise ValidationError("手机号已存在")
|
||||
|
||||
# 验证通过,用户输入的值返回
|
||||
return txt_mobile
|
||||
@@ -0,0 +1,147 @@
|
||||
"""
|
||||
自定义的分页组件,以后如果想要使用这个分页组件,你需要做如下几件事:
|
||||
|
||||
在视图函数中:
|
||||
def pretty_list(request):
|
||||
|
||||
# 1.根据自己的情况去筛选自己的数据
|
||||
queryset = models.PrettyNum.objects.all()
|
||||
|
||||
# 2.实例化分页对象
|
||||
page_object = Pagination(request, queryset)
|
||||
|
||||
context = {
|
||||
"queryset": page_object.page_queryset, # 分完页的数据
|
||||
"page_string": page_object.html() # 生成页码
|
||||
}
|
||||
return render(request, 'pretty_list.html', context)
|
||||
|
||||
在HTML页面中
|
||||
|
||||
{% for obj in queryset %}
|
||||
{{obj.xx}}
|
||||
{% endfor %}
|
||||
|
||||
<ul class="pagination">
|
||||
{{ page_string }}
|
||||
</ul>
|
||||
|
||||
"""
|
||||
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
|
||||
class Pagination(object):
|
||||
|
||||
def __init__(self, request, queryset, page_size=10, page_param="page", plus=5):
|
||||
"""
|
||||
:param request: 请求的对象
|
||||
:param queryset: 符合条件的数据(根据这个数据给他进行分页处理)
|
||||
:param page_size: 每页显示多少条数据
|
||||
:param page_param: 在URL中传递的获取分页的参数,例如:/etty/list/?page=12
|
||||
:param plus: 显示当前页的 前或后几页(页码)
|
||||
"""
|
||||
|
||||
from django.http.request import QueryDict
|
||||
import copy
|
||||
query_dict = copy.deepcopy(request.GET)
|
||||
query_dict._mutable = True
|
||||
self.query_dict = query_dict
|
||||
|
||||
self.page_param = page_param
|
||||
page = request.GET.get(page_param, "1")
|
||||
|
||||
if page.isdecimal():
|
||||
page = int(page)
|
||||
else:
|
||||
page = 1
|
||||
|
||||
self.page = page
|
||||
self.page_size = page_size
|
||||
|
||||
self.start = (page - 1) * page_size
|
||||
self.end = page * page_size
|
||||
|
||||
self.page_queryset = queryset[self.start:self.end]
|
||||
|
||||
total_count = queryset.count()
|
||||
total_page_count, div = divmod(total_count, page_size)
|
||||
if div:
|
||||
total_page_count += 1
|
||||
self.total_page_count = total_page_count
|
||||
self.plus = plus
|
||||
|
||||
def html(self):
|
||||
# 计算出,显示当前页的前5页、后5页
|
||||
if self.total_page_count <= 2 * self.plus + 1:
|
||||
# 数据库中的数据比较少,都没有达到11页。
|
||||
start_page = 1
|
||||
end_page = self.total_page_count
|
||||
else:
|
||||
# 数据库中的数据比较多 > 11页。
|
||||
|
||||
# 当前页<5时(小极值)
|
||||
if self.page <= self.plus:
|
||||
start_page = 1
|
||||
end_page = 2 * self.plus + 1
|
||||
else:
|
||||
# 当前页 > 5
|
||||
# 当前页+5 > 总页面
|
||||
if (self.page + self.plus) > self.total_page_count:
|
||||
start_page = self.total_page_count - 2 * self.plus
|
||||
end_page = self.total_page_count
|
||||
else:
|
||||
start_page = self.page - self.plus
|
||||
end_page = self.page + self.plus
|
||||
|
||||
# 页码
|
||||
page_str_list = []
|
||||
|
||||
self.query_dict.setlist(self.page_param, [1])
|
||||
page_str_list.append('<li><a href="?{}">首页</a></li>'.format(self.query_dict.urlencode()))
|
||||
|
||||
# 上一页
|
||||
if self.page > 1:
|
||||
self.query_dict.setlist(self.page_param, [self.page - 1])
|
||||
prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
|
||||
else:
|
||||
self.query_dict.setlist(self.page_param, [1])
|
||||
prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
|
||||
page_str_list.append(prev)
|
||||
|
||||
# 页面
|
||||
for i in range(start_page, end_page + 1):
|
||||
self.query_dict.setlist(self.page_param, [i])
|
||||
if i == self.page:
|
||||
ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
|
||||
else:
|
||||
ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
|
||||
page_str_list.append(ele)
|
||||
|
||||
# 下一页
|
||||
if self.page < self.total_page_count:
|
||||
self.query_dict.setlist(self.page_param, [self.page + 1])
|
||||
prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
|
||||
else:
|
||||
self.query_dict.setlist(self.page_param, [self.total_page_count])
|
||||
prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
|
||||
page_str_list.append(prev)
|
||||
|
||||
# 尾页
|
||||
self.query_dict.setlist(self.page_param, [self.total_page_count])
|
||||
page_str_list.append('<li><a href="?{}">尾页</a></li>'.format(self.query_dict.urlencode()))
|
||||
|
||||
search_string = """
|
||||
<li>
|
||||
<form style="float: left;margin-left: -1px" method="get">
|
||||
<input name="page"
|
||||
style="position: relative;float:left;display: inline-block;width: 80px;border-radius: 0;"
|
||||
type="text" class="form-control" placeholder="页码">
|
||||
<button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>
|
||||
</form>
|
||||
</li>
|
||||
"""
|
||||
|
||||
page_str_list.append(search_string)
|
||||
page_string = mark_safe("".join(page_str_list))
|
||||
return page_string
|
||||
Binary file not shown.
@@ -0,0 +1,34 @@
|
||||
from gradio_client import Client
|
||||
import wave
|
||||
import os
|
||||
# client = Client("http://127.0.0.1:7860/")
|
||||
def create_sound(text,models,speed,url):
|
||||
client = Client(f"{url}")
|
||||
result = client.predict(
|
||||
text,
|
||||
models,
|
||||
"简体中文",
|
||||
speed,
|
||||
fn_index=0
|
||||
)
|
||||
print(result)
|
||||
# 获取当前目录
|
||||
current_dir = os.getcwd()
|
||||
# print("=================================")
|
||||
# print(current_dir)
|
||||
# second_dir = os.chdir("./login/static/sounds/")
|
||||
# current_dir = os.getcwd()
|
||||
my_file_dir = f"{current_dir}/{models}"
|
||||
print(my_file_dir)
|
||||
if not os.path.exists(f"{current_dir}/{models}"):
|
||||
os.mkdir(my_file_dir)
|
||||
# 构建目标文件路径
|
||||
target_file_path = os.path.join(my_file_dir, 'audio.wav')
|
||||
print(target_file_path)
|
||||
# 复制文件到当前目录
|
||||
with open(result[1], 'rb') as source_file:
|
||||
with open(target_file_path, 'wb') as target_file:
|
||||
target_file.write(source_file.read())
|
||||
return 1
|
||||
# if __name__ == '__main__':
|
||||
# create_sounds("123","example",0.81)
|
||||
Reference in New Issue
Block a user