You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from sqlalchemy import func , and_
from typing import List
from . . models . database import database
from . . models . users import users_table , users_rights , rights_table
from . . schemas . users import UserCreate
async def get_user_by_username ( username : str ) :
''' Метод для получения строки с данными пользователя из базы данных по
username. '''
query = users_table . select ( ) . where ( users_table . c . login == username )
user_data = await database . fetch_one ( query )
query = ( users_table .
join ( users_rights ) .
join ( rights_table ) .
select ( ) .
where ( and_ ( users_table . c . id == users_rights . c . user_id ,
users_table . c . login == username ,
rights_table . c . id == users_rights . c . right_id ) ) .
with_only_columns ( [ users_table . c . id ,
users_table . c . login ,
users_table . c . password ,
func . group_concat ( rights_table . c . name ,
' ' ) . label ( " rights " ) ] ) .
group_by ( users_table . c . id ) )
response = await database . fetch_one ( query )
user_data = dict ( response )
user_data [ ' rights ' ] = user_data [ ' rights ' ] . split ( )
return user_data
async def create_user ( username : str , hashed_password : str , rights : List [ str ] ) :
user = UserCreate ( login = username ,
password = hashed_password ,
rights = rights )