First commit

This commit is contained in:
Ivan Vazhenin
2023-09-09 08:11:16 +03:00
parent 71bda36b84
commit 91ec63eb9d
2 changed files with 98 additions and 93 deletions

101
main.py
View File

@@ -8,18 +8,25 @@ from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter from strawberry.fastapi import GraphQLRouter
from starlette.middleware.cors import CORSMiddleware from starlette.middleware.cors import CORSMiddleware
from strawberry.scalars import JSON from strawberry.scalars import JSON
from xmlrpc.client import ServerProxy
def connect(): def connect():
return create_engine("postgresql+psycopg://postgres:Root12345678@10.10.8.83:32101/db", echo=True) return create_engine("postgresql+psycopg://postgres:Root12345678@10.10.8.83:32101/db", echo=True)
def exec_function(function: str, **kwargs):
proxy = ServerProxy('http://10.10.8.70:7000/xmlrpc')
result = proxy.__getattr__(function)(kwargs)
return result
class Base(DeclarativeBase): class Base(DeclarativeBase):
pass pass
class User(Base): class User(Base):
__tablename__ = "users" __tablename__ = 'users'
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True)
username: Mapped[str] username: Mapped[str]
@@ -27,13 +34,18 @@ class User(Base):
bndname: Mapped[str] bndname: Mapped[str]
newbnd: Mapped[bool] newbnd: Mapped[bool]
active: Mapped[bool] active: Mapped[bool]
upstream: Mapped[bool]
profiles: Mapped[List["Profile"]] = relationship( profiles: Mapped[List['Profile']] = relationship(
back_populates="user", cascade="all, delete-orphan" back_populates='user', cascade='all, delete-orphan'
)
schedule: Mapped[List['Schedule']] = relationship(
back_populates='user', cascade='all, delete-orphan'
) )
def __repr__(self) -> str: def __repr__(self) -> str:
return f"User(id={self.id!r}, username={self.username!r}, password={self.passwd!r}, newbnd={self.newbnd})" return f'User(id={self.id!r}, username={self.username!r}, password={self.passwd!r}, newbnd={self.newbnd})'
def to_dict(self) -> dict: def to_dict(self) -> dict:
return { return {
@@ -42,23 +54,26 @@ class User(Base):
'bndname': self.bndname, 'bndname': self.bndname,
'newbnd': self.newbnd, 'newbnd': self.newbnd,
'active': self.active, 'active': self.active,
'profiles': [x.to_dict() for x in self.profiles] 'upstream': self.upstream,
'profiles': [x.to_dict() for x in self.profiles],
'schedule': [x.to_dict() for x in self.schedule],
} }
@strawberry.type @strawberry.type
class UsersGQL: class UsersGQL:
users: JSON users: JSON
class Profile(Base): class Profile(Base):
__tablename__ = "profiles" __tablename__ = 'profiles'
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id")) user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
scheme: Mapped[str] scheme: Mapped[str]
json: Mapped[str] json: Mapped[str]
user: Mapped["User"] = relationship(back_populates="profiles") user: Mapped['User'] = relationship(back_populates='profiles')
def to_dict(self) -> dict: def to_dict(self) -> dict:
return { return {
@@ -68,6 +83,32 @@ class Profile(Base):
} }
class Schedule(Base):
__tablename__ = 'schedule'
id: Mapped[int] = mapped_column(primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
day_start: Mapped[int]
hour_start: Mapped[int]
minute_start: Mapped[int]
day_end: Mapped[int]
hour_end: Mapped[int]
minute_end: Mapped[int]
user: Mapped['User'] = relationship(back_populates='schedule')
def to_dict(self) -> dict:
return {
'id': self.id,
'day_start': self.day_start,
'hour_start': self.hour_start,
'minute_start': self.minute_start,
'day_end': self.day_end,
'hour_end': self.hour_end,
'minute_end': self.minute_end,
}
@strawberry.type @strawberry.type
class Query: class Query:
@strawberry.field() @strawberry.field()
@@ -122,6 +163,35 @@ class Mutation:
session.commit() session.commit()
return profile.id return profile.id
@strawberry.mutation
def update_schedule(self, id_: int, day_start: int, day_end: int, hour_start: int, hour_end: int,
minute_start: int, minute_end: int) -> bool:
sync_engine = connect()
with Session(sync_engine) as session:
schedule = session.query(Schedule).get(id_)
schedule.day_start = day_start
schedule.day_end = day_end
schedule.hour_start = hour_start
schedule.hour_end = hour_end
schedule.minute_start = minute_start
schedule.minute_end = minute_end
session.commit()
return True
@strawberry.mutation
def create_schedule(self, user_id: int, day_start: int, day_end: int, hour_start: int, hour_end: int,
minute_start: int, minute_end: int) -> int:
sync_engine = connect()
with (Session(sync_engine) as session):
user = session.query(User).get(user_id)
if not user:
return 0
schedule = Schedule(user_id=user_id, day_start=day_start, day_end=day_end, hour_start=hour_start,
hour_end=hour_end, minute_start=minute_start, minute_end=minute_end)
session.add_all([schedule])
session.commit()
return schedule.id
@strawberry.mutation @strawberry.mutation
def remove_user(self, id_: int) -> bool: def remove_user(self, id_: int) -> bool:
sync_engine = connect() sync_engine = connect()
@@ -142,6 +212,16 @@ class Mutation:
session.commit() session.commit()
return True return True
@strawberry.mutation
def remove_schedule(self, id_: int) -> bool:
sync_engine = connect()
with (Session(sync_engine) as session):
schedule = session.query(Schedule).get(id_)
if schedule:
session.delete(schedule)
session.commit()
return True
def init(): def init():
sync_engine = connect() sync_engine = connect()
@@ -154,6 +234,7 @@ def init():
bndname="bnd127", bndname="bnd127",
newbnd=True, newbnd=True,
active=True, active=True,
upstream=False
) )
bnd128 = User( bnd128 = User(
username="bnd128", username="bnd128",
@@ -161,12 +242,14 @@ def init():
bndname="bnd128", bndname="bnd128",
newbnd=True, newbnd=True,
active=True, active=True,
upstream=False
) )
session.add_all([bnd127, bnd128]) session.add_all([bnd127, bnd128])
session.commit() session.commit()
def main(): def main():
init()
uvicorn.run("main:app", port=9000, log_level="info") uvicorn.run("main:app", port=9000, log_level="info")
@@ -174,7 +257,7 @@ schema = strawberry.Schema(query=Query, mutation=Mutation)
graphql_app = GraphQLRouter(schema, graphiql=True) graphql_app = GraphQLRouter(schema, graphiql=True)
app = FastAPI() app = FastAPI(root_path="/graphql")
app.add_middleware(CORSMiddleware, app.add_middleware(CORSMiddleware,
allow_origins=['http://127.0.0.1'], allow_origins=['http://127.0.0.1'],
allow_credentials=True, allow_credentials=True,

View File

@@ -1,84 +1,6 @@
attrs==22.2.0 uvicorn~=0.23.2
Babel==2.10.3 SQLAlchemy~=2.0.20
blinker==1.5 fastapi~=0.103.1
Brlapi==0.8.4 starlette~=0.27.0
certifi==2022.9.24 strawberry-graphql~=0.205.0
chardet==5.1.0 psycopg~=3.1.10
click==8.1.3
cloud-init==23.2.2
colorama==0.4.6
command-not-found==0.3
configobj==5.0.8
cryptography==38.0.4
cupshelpers==1.0
dbus-python==1.3.2
defer==1.0.6
distlib==0.3.6
distro==1.8.0
distro-info==1.5
evdev==1.6.1
fido2==0.9.1
filelock==3.9.0
httplib2==0.20.4
idna==3.3
importlib-metadata==4.12.0
jaraco.classes==3.2.1
jeepney==0.8.0
Jinja2==3.1.2
jsonpatch==1.32
jsonpointer==2.0
jsonschema==4.6.0
keyring==23.9.3
language-selector==0.1
launchpadlib==1.11.0
lazr.restfulclient==0.14.5
lazr.uri==1.0.6
louis==3.24.0
markdown-it-py==2.1.0
MarkupSafe==2.1.2
mdurl==0.1.2
more-itertools==8.10.0
netifaces==0.11.0
oauthlib==3.2.2
olefile==0.46
pexpect==4.8.0
Pillow==9.4.0
platformdirs==3.0.0
protobuf==4.21.12
psutil==5.9.4
ptyprocess==0.7.0
pycairo==1.20.1
pycups==2.0.1
Pygments==2.14.0
PyGObject==3.44.1
PyJWT==2.6.0
pyparsing==3.0.9
pyrsistent==0.18.1
pyscard==2.0.5
pyserial==3.5
python-apt==2.5.3+ubuntu1
python-dateutil==2.8.2
python-debian==0.1.49+ubuntu2
python-xlib==0.33
pytz==2022.7.1
pyudev==0.24.0
pyxdg==0.28
PyYAML==6.0
reportlab==3.6.12
requests==2.28.1
rich==13.3.1
SecretStorage==3.3.3
six==1.16.0
systemd-python==235
ubuntu-advantage-tools==8001
ubuntu-drivers-common==0.0.0
ufw==0.36.1
unattended-upgrades==0.1
urllib3==1.26.12
virtualenv==20.19.0+ds
voluptuous==0.12.2
wadllib==1.3.6
xdg==5
xkit==0.0.0
yubikey-manager==4.0.9
zipp==1.0.0