Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
ca65d190
Commit
ca65d190
authored
Nov 20, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
21734a2e
Changes
17
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
7 additions
and
310 deletions
+7
-310
application/config/config.yaml
application/config/config.yaml
+1
-1
application/dao/__init__.py
application/dao/__init__.py
+0
-1
application/dao/log.py
application/dao/log.py
+1
-2
application/dao/user.py
application/dao/user.py
+0
-31
application/extensions/init_sqlalchemy.py
application/extensions/init_sqlalchemy.py
+0
-11
application/libs/tasks/task.py
application/libs/tasks/task.py
+0
-7
application/models/__init__.py
application/models/__init__.py
+0
-1
application/models/log.py
application/models/log.py
+0
-13
application/models/user.py
application/models/user.py
+0
-66
application/schemas/__init__.py
application/schemas/__init__.py
+0
-2
application/schemas/user.py
application/schemas/user.py
+0
-49
application/script/cmdline.py
application/script/cmdline.py
+5
-7
application/services/__init__.py
application/services/__init__.py
+0
-1
application/services/user.py
application/services/user.py
+0
-28
application/views/__init__.py
application/views/__init__.py
+0
-2
application/views/user/__init__.py
application/views/user/__init__.py
+0
-19
application/views/user/user.py
application/views/user/user.py
+0
-69
No files found.
application/config/config.yaml
View file @
ca65d190
...
...
@@ -9,7 +9,7 @@ Database:
Host
:
localhost
Port
:
5432
DB
:
elp
TableName
:
user
s
TableName
:
log
s
Elasticsearch
:
Host
:
172.28.5.39
...
...
application/dao/__init__.py
View file @
ca65d190
...
...
@@ -10,5 +10,4 @@
# @Description :
"""
from
.user
import
UserDao
from
.log
import
LogDao
application/dao/log.py
View file @
ca65d190
...
...
@@ -13,10 +13,9 @@
from
loguru
import
logger
from
application.utils
import
ElasticsearchUtil
from
application.libs.helper
import
MySQLHelper
from
application.utils
import
ParseUtil
from
application.models
import
Log
from
application.utils
import
ElasticsearchUtil
,
ParseUtil
class
LogDao
:
...
...
application/dao/user.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/28 23:56
# @File : user.py
# @Description :
"""
from
application.extensions.init_sqlalchemy
import
db
from
application.models.user
import
User
class
UserDao
:
@
classmethod
def
get_all
(
cls
):
return
User
.
query
.
all
()
@
classmethod
def
get_by_id
(
cls
,
user_id
:
int
):
return
User
.
query
.
get
(
user_id
)
@
classmethod
def
create
(
cls
,
username
:
str
,
password
:
str
,
email
:
str
):
new_user
=
User
(
username
=
username
,
email
=
email
,
password
=
password
)
db
.
session
.
add
(
new_user
)
db
.
session
.
commit
()
return
new_user
application/extensions/init_sqlalchemy.py
View file @
ca65d190
...
...
@@ -42,17 +42,6 @@ def init_database(app: Flask) -> None:
with
app
.
app_context
():
db
.
session
.
execute
(
text
(
f
"""
CREATE TABLE IF NOT EXISTS
{
cfg
.
TableName
}
(
id SERIAL NOT NULL,
username VARCHAR(80) NOT NULL,
email VARCHAR(120) NOT NULL,
password VARCHAR(255) NOT NULL,
active BOOLEAN,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
"""
))
db
.
session
.
execute
(
text
(
"""
CREATE TABLE IF NOT EXISTS logs (
id SERIAL NOT NULL,
date_time TIMESTAMP WITH TIME ZONE NOT NULL,
uuid varchar(100) DEFAULT NULL,
...
...
application/libs/tasks/task.py
View file @
ca65d190
...
...
@@ -12,7 +12,6 @@
import
datetime
from
application.extensions.init_sqlalchemy
import
db
from
application.models
import
User
from
application.libs.helper
import
MySQLHelper
...
...
@@ -33,9 +32,3 @@ def task():
}
with
MySQLHelper
(
**
options
)
as
helper
:
print
(
helper
.
execute
(
cfg
.
Sql
))
print
(
"create user"
)
# user = User(username="CWzz", email="Stuu@outlook.com", password="qwe!2345", active=True,
# created_at=datetime.datetime.utcnow())
# user.save()
print
(
"created user admin"
)
application/models/__init__.py
View file @
ca65d190
...
...
@@ -10,5 +10,4 @@
# @Description :
"""
from
.user
import
User
from
.log
import
Log
application/models/log.py
View file @
ca65d190
...
...
@@ -69,19 +69,6 @@ class Log(db.Model):
_dic
[
key
]
=
value
return
_dic
def
save
(
self
):
partition_date
=
self
.
date_time
.
strftime
(
'%Y_%m'
)
partition_name
=
f
'
{
self
.
__tablename__
}
_
{
partition_date
}
'
with
db
.
session
.
begin_nested
():
db
.
session
.
execute
(
text
(
f
"""
CREATE TABLE IF NOT EXISTS
{
partition_name
}
PARTITION OF
{
self
.
__tablename__
}
FOR VALUES FROM ('
{
self
.
date_time
.
strftime
(
'%Y-%m-01'
)
}
') TO
('
{
self
.
date_time
.
strftime
(
'%Y-%m-01'
)
}
'::date + interval '1 month');
"""
))
db
.
session
.
add
(
self
)
db
.
session
.
commit
()
@
classmethod
def
batch_save
(
cls
,
objects
):
with
db
.
session
.
begin_nested
():
...
...
application/models/user.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/28 22:12
# @File : user.py
# @Description :
"""
import
os
from
sqlalchemy
import
text
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
application.extensions.init_bcrypt
import
bcrypt
from
application.extensions.init_sqlalchemy
import
db
class
User
(
db
.
Model
):
"""Basic user model"""
__tablename__
=
os
.
environ
.
get
(
'TableName'
,
'users'
)
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
,
nullable
=
False
)
email
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
,
nullable
=
False
)
_password
=
db
.
Column
(
"password"
,
db
.
String
(
255
),
nullable
=
False
)
active
=
db
.
Column
(
db
.
Boolean
,
default
=
True
)
created_at
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
)
@
hybrid_property
def
password
(
self
):
return
self
.
_password
@
password
.
setter
def
password
(
self
,
password
:
str
):
self
.
_password
=
bcrypt
.
generate_password_hash
(
password
).
decode
(
'utf-8'
)
def
check_password
(
self
,
password
:
str
):
# 判断传过来的密码是否与数据库存的密码一致
return
bcrypt
.
check_password_hash
(
self
.
_password
,
password
)
def
to_dict
(
self
)
->
dict
:
"""object to dict"""
return
{
'id'
:
self
.
id
,
'username'
:
self
.
username
,
'email'
:
self
.
email
,
}
def
__repr__
(
self
)
->
str
:
return
f
'<User
{
self
.
username
}
>'
def
save
(
self
):
partition_date
=
self
.
created_at
.
strftime
(
'%Y_%m'
)
partition_name
=
f
'
{
self
.
__tablename__
}
_
{
partition_date
}
'
with
db
.
session
.
begin_nested
():
db
.
session
.
execute
(
text
(
f
"""
CREATE TABLE IF NOT EXISTS
{
partition_name
}
PARTITION OF
{
self
.
__tablename__
}
FOR VALUES FROM ('
{
self
.
created_at
.
strftime
(
'%Y-%m-01'
)
}
') TO
('
{
self
.
created_at
.
strftime
(
'%Y-%m-01'
)
}
'::date + interval '1 month');
"""
))
db
.
session
.
add
(
self
)
db
.
session
.
commit
()
application/schemas/__init__.py
View file @
ca65d190
...
...
@@ -10,6 +10,4 @@
# @Description :
"""
from
.user
import
CreateUserItem
from
.user
import
UserSchema
from
.request
import
ParseLogRequestItem
application/schemas/user.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/29 00:07
# @File : user.py
# @Description :
"""
from
pydantic
import
BaseModel
,
EmailStr
,
field_validator
from
application.extensions.init_marshmallow
import
ma
from
application.extensions.init_sqlalchemy
import
db
from
application.models
import
User
from
marshmallow
import
validates
,
ValidationError
class
CreateUserItem
(
BaseModel
):
username
:
str
password
:
str
email
:
EmailStr
@
classmethod
@
field_validator
(
"username"
)
def
name_must_contain_only_characters
(
cls
,
value
):
if
not
value
.
isalpha
():
raise
ValueError
(
"Username must contain only characters."
)
return
value
class
UserSchema
(
ma
.
SQLAlchemyAutoSchema
):
id
=
ma
.
Int
(
dump_only
=
True
)
username
=
ma
.
Str
(
required
=
True
)
password
=
ma
.
String
(
load_only
=
True
,
required
=
True
)
email
=
ma
.
Email
(
required
=
True
)
class
Meta
:
model
=
User
sqla_session
=
db
.
session
load_instance
=
True
exclude
=
(
"_password"
,)
@
validates
(
'username'
)
def
validate_username
(
self
,
username
):
if
len
(
username
)
<
3
:
raise
ValidationError
(
'Username must be at least 3 characters.'
)
\ No newline at end of file
application/script/cmdline.py
View file @
ca65d190
...
...
@@ -17,12 +17,10 @@ from flask.cli import with_appcontext
@
click
.
command
(
"init"
)
@
with_appcontext
def
init
():
"""Create a new
admin user
"""
"""Create a new
log
"""
from
application.extensions.init_sqlalchemy
import
db
from
application.models
import
User
click
.
echo
(
"create user"
)
user
=
User
(
username
=
"StudentCWZ"
,
email
=
"StudentCWZ@outlook.com"
,
password
=
"qwe!2345"
,
active
=
True
)
db
.
session
.
add
(
user
)
db
.
session
.
commit
()
click
.
echo
(
"created user admin"
)
# click.echo("create log")
# db.session.add(user)
# db.session.commit()
# click.echo("created log")
application/services/__init__.py
View file @
ca65d190
...
...
@@ -10,5 +10,4 @@
# @Description :
"""
from
.user
import
UserService
from
.log
import
LogService
application/services/user.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/29 00:03
# @File : user.py
# @Description :
"""
from
application.dao
import
UserDao
class
UserService
:
@
classmethod
def
get_all_users
(
cls
):
return
UserDao
.
get_all
()
@
classmethod
def
get_user_by_id
(
cls
,
user_id
:
int
):
return
UserDao
.
get_by_id
(
user_id
)
@
classmethod
def
create_user
(
cls
,
username
:
str
,
password
:
str
,
email
:
str
):
return
UserDao
.
create
(
username
,
password
,
email
)
application/views/__init__.py
View file @
ca65d190
...
...
@@ -12,10 +12,8 @@
from
flask
import
Flask
from
.user
import
register_user_views
from
.log
import
register_log_views
def
init_views
(
app
:
Flask
)
->
None
:
register_user_views
(
app
)
register_log_views
(
app
)
application/views/user/__init__.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/29 00:18
# @File : __init__.py.py
# @Description :
"""
from
flask
import
Flask
from
.user
import
user_api
def
register_user_views
(
app
:
Flask
):
app
.
register_blueprint
(
user_api
)
application/views/user/user.py
deleted
100644 → 0
View file @
21734a2e
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/10/29 00:08
# @File : user.py
# @Description :
"""
import
json
from
flask
import
Blueprint
,
current_app
,
request
,
jsonify
from
flask_restful
import
Api
from
application.services
import
UserService
from
marshmallow
import
ValidationError
from
application.schemas
import
UserSchema
from
application.utils
import
ElasticsearchUtil
from
application.libs.helper
import
ConfigHelper
user_api
=
Blueprint
(
'user_api'
,
__name__
)
api_users
=
Api
(
user_api
)
@
user_api
.
route
(
'/users'
,
methods
=
[
'GET'
])
def
get_users
():
users
=
UserService
.
get_all_users
()
return
jsonify
([
user
.
to_dict
()
for
user
in
users
])
@
user_api
.
route
(
'/users/<int:user_id>'
,
methods
=
[
'GET'
])
def
get_user
(
user_id
):
user
=
UserService
.
get_user_by_id
(
user_id
)
if
user
is
None
:
return
jsonify
({
'error'
:
'User not found'
}),
404
return
jsonify
(
user
.
to_dict
())
@
user_api
.
route
(
'/users'
,
methods
=
[
'POST'
])
def
create_user
():
user_schema
=
UserSchema
()
try
:
data
=
user_schema
.
load
(
request
.
json
)
except
ValidationError
as
e
:
return
jsonify
(
e
.
messages
),
400
username
=
data
.
username
password
=
data
.
password
email
=
data
.
email
user
=
UserService
.
create_user
(
username
,
password
,
email
)
return
jsonify
(
user_schema
.
dump
(
user
)),
201
@
user_api
.
route
(
'/tests'
,
methods
=
[
'POST'
])
def
test_user
():
# data = request.get_json()
# start = data.get('start')
# end = data.get('end')
# dsl = ElasticsearchUtil.dsl(start, end)
# index = current_app.config.Elasticsearch.Index
# res = ElasticsearchUtil.search(index, dsl)
# print(len(res))
res
=
{
"user_name"
:
"libai"
,
"user_age"
:
18
,
}
res_json
=
json
.
dumps
(
res
)
return
res_json
,
200
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment