From 71bda36b84bc241d723b4296fc253775c825525d Mon Sep 17 00:00:00 2001 From: Ivan Vazhenin Date: Wed, 30 Aug 2023 19:01:27 +0300 Subject: [PATCH] First commit --- .gitignore | 3 + main.py | 187 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 84 +++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92c7c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/venv +/__pycache__ +/.idea \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..3b6be8e --- /dev/null +++ b/main.py @@ -0,0 +1,187 @@ +from typing import List, Optional +from sqlalchemy import create_engine, String, select, ForeignKey +from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column, relationship +import strawberry +import json +import uvicorn +from fastapi import FastAPI +from strawberry.fastapi import GraphQLRouter +from starlette.middleware.cors import CORSMiddleware +from strawberry.scalars import JSON + + +def connect(): + return create_engine("postgresql+psycopg://postgres:Root12345678@10.10.8.83:32101/db", echo=True) + + +class Base(DeclarativeBase): + pass + + +class User(Base): + __tablename__ = "users" + + id: Mapped[int] = mapped_column(primary_key=True) + username: Mapped[str] + passwd: Mapped[str] + bndname: Mapped[str] + newbnd: Mapped[bool] + active: Mapped[bool] + + profiles: Mapped[List["Profile"]] = 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})" + + def to_dict(self) -> dict: + return { + 'id': self.id, + 'username': self.username, + 'bndname': self.bndname, + 'newbnd': self.newbnd, + 'active': self.active, + 'profiles': [x.to_dict() for x in self.profiles] + } + +@strawberry.type +class UsersGQL: + users: JSON + + +class Profile(Base): + __tablename__ = "profiles" + + id: Mapped[int] = mapped_column(primary_key=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id")) + scheme: Mapped[str] + json: Mapped[str] + + user: Mapped["User"] = relationship(back_populates="profiles") + + def to_dict(self) -> dict: + return { + 'id': self.id, + 'user_id': self.user_id, + 'json': self.json, + } + + +@strawberry.type +class Query: + @strawberry.field() + def users(self) -> UsersGQL: + sync_engine = connect() + with Session(sync_engine) as session: + stmt = select(User) + return UsersGQL(users=[json.dumps(user.to_dict()) for user in session.scalars(stmt)]) + + +@strawberry.type +class Mutation: + @strawberry.mutation + def update_user(self, id_: int, bndname: str, newbnd: bool, active: 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 + session.commit() + return True + + @strawberry.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) + session.add_all([user]) + session.commit() + return user.id + + @strawberry.mutation + def update_profile(self, id_: int, scheme: str, json: str) -> bool: + sync_engine = connect() + with Session(sync_engine) as session: + profile = session.query(Profile).get(id_) + profile.scheme = scheme + profile.json = json + session.commit() + return True + + @strawberry.mutation + def create_profile(self, user_id: int, scheme: str, json: str) -> int: + sync_engine = connect() + with (Session(sync_engine) as session): + user = session.query(User).get(user_id) + if not user: + return 0 + profile = Profile(user_id=user_id, scheme=scheme, json=json) + session.add_all([profile]) + session.commit() + return profile.id + + @strawberry.mutation + def remove_user(self, id_: int) -> bool: + sync_engine = connect() + with (Session(sync_engine) as session): + user = session.query(User).get(id_) + if user: + session.delete(user) + session.commit() + return True + + @strawberry.mutation + def remove_profile(self, id_: int) -> bool: + sync_engine = connect() + with (Session(sync_engine) as session): + profile = session.query(Profile).get(id_) + if profile: + session.delete(profile) + session.commit() + return True + + +def init(): + sync_engine = connect() + Base.metadata.drop_all(sync_engine) + Base.metadata.create_all(sync_engine) + with Session(sync_engine) as session: + bnd127 = User( + username="bnd127", + passwd="gost_2012$a742ec53198ec2a5027086fba8814a89982a57112d1a72d02260161108f39b50", + bndname="bnd127", + newbnd=True, + active=True, + ) + bnd128 = User( + username="bnd128", + passwd="gost_2012$a742ec53198ec2a5027086fba8814a89982a57112d1a72d02260161108f39b50", + bndname="bnd128", + newbnd=True, + active=True, + ) + session.add_all([bnd127, bnd128]) + session.commit() + + +def main(): + uvicorn.run("main:app", port=9000, log_level="info") + + +schema = strawberry.Schema(query=Query, mutation=Mutation) + +graphql_app = GraphQLRouter(schema, graphiql=True) + +app = FastAPI() +app.add_middleware(CORSMiddleware, + allow_origins=['http://127.0.0.1'], + allow_credentials=True, + allow_methods=['*'], + allow_headers=['*'] + ) +app.include_router(graphql_app, prefix='/graphql') + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1d244d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,84 @@ +attrs==22.2.0 +Babel==2.10.3 +blinker==1.5 +Brlapi==0.8.4 +certifi==2022.9.24 +chardet==5.1.0 +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