Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
ba0523cd
Commit
ba0523cd
authored
Apr 02, 2024
by
Timothy J. Baek
Browse files
refac: api_key field moved to user
parent
38c34b44
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
9 deletions
+59
-9
backend/apps/web/internal/migrations/003_add_auth_api_key.py
backend/apps/web/internal/migrations/003_add_auth_api_key.py
+48
-0
backend/apps/web/models/auths.py
backend/apps/web/models/auths.py
+2
-9
backend/apps/web/models/users.py
backend/apps/web/models/users.py
+9
-0
No files found.
backend/apps/web/internal/migrations/003_add_auth_api_key.py
0 → 100644
View file @
ba0523cd
"""Peewee migrations -- 002_add_local_sharing.py.
Some examples (model - class or model name)::
> Model = migrator.orm['table_name'] # Return model in current state by name
> Model = migrator.ModelClass # Return model in current state by name
> migrator.sql(sql) # Run custom SQL
> migrator.run(func, *args, **kwargs) # Run python function with the given args
> migrator.create_model(Model) # Create a model (could be used as decorator)
> migrator.remove_model(model, cascade=True) # Remove a model
> migrator.add_fields(model, **fields) # Add fields to a model
> migrator.change_fields(model, **fields) # Change fields
> migrator.remove_fields(model, *field_names, cascade=True)
> migrator.rename_field(model, old_field_name, new_field_name)
> migrator.rename_table(model, new_table_name)
> migrator.add_index(model, *col_names, unique=False)
> migrator.add_not_null(model, *field_names)
> migrator.add_default(model, field_name, default)
> migrator.add_constraint(model, name, sql)
> migrator.drop_index(model, *col_names)
> migrator.drop_not_null(model, *field_names)
> migrator.drop_constraints(model, *constraints)
"""
from
contextlib
import
suppress
import
peewee
as
pw
from
peewee_migrate
import
Migrator
with
suppress
(
ImportError
):
import
playhouse.postgres_ext
as
pw_pext
def
migrate
(
migrator
:
Migrator
,
database
:
pw
.
Database
,
*
,
fake
=
False
):
"""Write your migrations here."""
migrator
.
add_fields
(
"user"
,
api_key
=
pw
.
CharField
(
max_length
=
255
,
null
=
True
,
unique
=
True
)
)
def
rollback
(
migrator
:
Migrator
,
database
:
pw
.
Database
,
*
,
fake
=
False
):
"""Write your rollback migrations here."""
migrator
.
remove_fields
(
"user"
,
"api_key"
)
backend/apps/web/models/auths.py
View file @
ba0523cd
...
...
@@ -25,7 +25,6 @@ class Auth(Model):
email
=
CharField
()
password
=
CharField
()
active
=
BooleanField
()
api_key
=
CharField
(
null
=
True
,
unique
=
True
)
class
Meta
:
database
=
DB
...
...
@@ -36,7 +35,6 @@ class AuthModel(BaseModel):
email
:
str
password
:
str
active
:
bool
=
True
api_key
:
Optional
[
str
]
=
None
####################
...
...
@@ -136,13 +134,8 @@ class AuthsTable:
return
None
try
:
auth
=
Auth
.
get
(
Auth
.
api_key
==
api_key
,
Auth
.
active
==
True
)
if
auth
:
user
=
Users
.
get_user_by_id
(
auth
.
id
)
return
user
else
:
return
None
user
=
Users
.
get_user_by_api_key
(
api_key
)
return
user
if
user
else
None
except
:
return
False
...
...
backend/apps/web/models/users.py
View file @
ba0523cd
...
...
@@ -20,6 +20,7 @@ class User(Model):
role
=
CharField
()
profile_image_url
=
CharField
()
timestamp
=
DateField
()
api_key
=
CharField
(
null
=
True
,
unique
=
True
)
class
Meta
:
database
=
DB
...
...
@@ -32,6 +33,7 @@ class UserModel(BaseModel):
role
:
str
=
"pending"
profile_image_url
:
str
=
"/user.png"
timestamp
:
int
# timestamp in epoch
api_key
:
Optional
[
str
]
=
None
####################
...
...
@@ -82,6 +84,13 @@ class UsersTable:
except
:
return
None
def
get_user_by_api_key
(
self
,
api_key
:
str
)
->
Optional
[
UserModel
]:
try
:
user
=
User
.
get
(
User
.
api_key
==
api_key
)
return
UserModel
(
**
model_to_dict
(
user
))
except
:
return
None
def
get_user_by_email
(
self
,
email
:
str
)
->
Optional
[
UserModel
]:
try
:
user
=
User
.
get
(
User
.
email
==
email
)
...
...
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