fix db
This commit is contained in:
103
main.py
103
main.py
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from sqlalchemy import create_engine, String, select, ForeignKey
|
||||
from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column, relationship
|
||||
@@ -21,10 +22,19 @@ def exec_function(function: str, **kwargs):
|
||||
return result
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class UsersGQL:
|
||||
users: JSON
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
def tow(day: int, hour: int, minute: int):
|
||||
return minute + hour * 60 + day * 60 * 24
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'users'
|
||||
|
||||
@@ -40,10 +50,18 @@ class User(Base):
|
||||
back_populates='user', cascade='all, delete-orphan'
|
||||
)
|
||||
|
||||
income_branches: Mapped[List['IncomeBranch']] = relationship(
|
||||
back_populates='user', cascade='all, delete-orphan'
|
||||
)
|
||||
|
||||
schedule: Mapped[List['Schedule']] = relationship(
|
||||
back_populates='user', cascade='all, delete-orphan'
|
||||
)
|
||||
|
||||
queue: Mapped[List['Queue']] = relationship(
|
||||
back_populates='user', cascade='all, delete-orphan'
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'User(id={self.id!r}, username={self.username!r}, password={self.passwd!r}, newbnd={self.newbnd})'
|
||||
|
||||
@@ -57,12 +75,20 @@ class User(Base):
|
||||
'upstream': self.upstream,
|
||||
'profiles': [x.to_dict() for x in self.profiles],
|
||||
'schedule': [x.to_dict() for x in self.schedule],
|
||||
'queue': [x.to_dict() for x in self.queue],
|
||||
'income_branches': [x.to_dict() for x in self.income_branches],
|
||||
}
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class UsersGQL:
|
||||
users: JSON
|
||||
def is_active_now(self):
|
||||
if not len(self.schedule):
|
||||
return True
|
||||
dt = datetime.now()
|
||||
curr_tow = tow(dt.weekday(), dt.hour, dt.minute)
|
||||
for x in self.schedule:
|
||||
if (tow(x.day_start, x.hour_start, x.minute_start) <= curr_tow
|
||||
<= tow(x.day_end, x.hour_end, x.minute_end)):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class Profile(Base):
|
||||
@@ -71,18 +97,40 @@ class Profile(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
||||
scheme: Mapped[str]
|
||||
json: Mapped[str]
|
||||
branch: Mapped[str] = mapped_column(String, nullable=True)
|
||||
json: Mapped[str] = mapped_column(String, nullable=True)
|
||||
|
||||
user: Mapped['User'] = relationship(back_populates='profiles')
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
'id': self.id,
|
||||
'user_id': self.user_id,
|
||||
'scheme': self.scheme,
|
||||
'branch': self.branch,
|
||||
'json': self.json,
|
||||
}
|
||||
|
||||
|
||||
class IncomeBranch(Base):
|
||||
__tablename__ = 'income_branches'
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
||||
scheme: Mapped[str]
|
||||
branch: Mapped[str]
|
||||
local_scheme: Mapped[str]
|
||||
|
||||
user: Mapped['User'] = relationship(back_populates='income_branches')
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
'id': self.id,
|
||||
'scheme': self.scheme,
|
||||
'branch': self.branch,
|
||||
'local_scheme': self.local_scheme,
|
||||
}
|
||||
|
||||
|
||||
class Schedule(Base):
|
||||
__tablename__ = 'schedule'
|
||||
|
||||
@@ -109,6 +157,33 @@ class Schedule(Base):
|
||||
}
|
||||
|
||||
|
||||
class Queue(Base):
|
||||
__tablename__ = 'queue'
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
|
||||
commit_id: Mapped[str]
|
||||
schema: Mapped[str]
|
||||
|
||||
user: Mapped['User'] = relationship(back_populates='queue')
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
'id': self.id,
|
||||
'bnd': self.user.bndname,
|
||||
'commit_id': self.commit_id,
|
||||
'schema': self.schema,
|
||||
}
|
||||
|
||||
|
||||
class Schemas(Base):
|
||||
__tablename__ = 'schemas'
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
schema: Mapped[str]
|
||||
schema_type: Mapped[str]
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class Query:
|
||||
@strawberry.field()
|
||||
@@ -118,17 +193,25 @@ class Query:
|
||||
stmt = select(User)
|
||||
return UsersGQL(users=[json.dumps(user.to_dict()) for user in session.scalars(stmt)])
|
||||
|
||||
@strawberry.field()
|
||||
def queue(self) -> UsersGQL:
|
||||
sync_engine = connect()
|
||||
with Session(sync_engine) as session:
|
||||
stmt = select(Queue)
|
||||
return UsersGQL(users=[json.dumps(queue.to_dict()) for queue in session.scalars(stmt)])
|
||||
|
||||
|
||||
@strawberry.type
|
||||
class Mutation:
|
||||
@strawberry.mutation
|
||||
def update_user(self, id_: int, bndname: str, newbnd: bool, active: bool) -> bool:
|
||||
def update_user(self, id_: int, bndname: str, newbnd: bool, active: bool, upstream: bool) -> bool:
|
||||
sync_engine = connect()
|
||||
with Session(sync_engine) as session:
|
||||
user = session.query(User).get(id_)
|
||||
user.bndname = bndname
|
||||
user.newbnd = newbnd
|
||||
user.active = active
|
||||
user.upstream = upstream
|
||||
session.commit()
|
||||
return True
|
||||
|
||||
@@ -136,7 +219,7 @@ class Mutation:
|
||||
def create_user(self, username: str, passwd: str) -> int:
|
||||
sync_engine = connect()
|
||||
with Session(sync_engine) as session:
|
||||
user = User(username=username, passwd=passwd, bndname=username, active=False, newbnd=True)
|
||||
user = User(username=username, passwd=passwd, bndname=username, active=False, newbnd=True, upstream=False)
|
||||
session.add_all([user])
|
||||
session.commit()
|
||||
return user.id
|
||||
@@ -249,7 +332,7 @@ def init():
|
||||
|
||||
|
||||
def main():
|
||||
init()
|
||||
# init()
|
||||
uvicorn.run("main:app", port=9000, log_level="info")
|
||||
|
||||
|
||||
@@ -259,7 +342,7 @@ graphql_app = GraphQLRouter(schema, graphiql=True)
|
||||
|
||||
app = FastAPI(root_path="/graphql")
|
||||
app.add_middleware(CORSMiddleware,
|
||||
allow_origins=['http://127.0.0.1'],
|
||||
allow_origins=['http://127.0.0.1:3000'],
|
||||
allow_credentials=True,
|
||||
allow_methods=['*'],
|
||||
allow_headers=['*']
|
||||
|
||||
Reference in New Issue
Block a user