<jupyter_start><jupyter_text>Zkouska vygenerovaného programu: BaseModels:<jupyter_code>pip install wait4it
from sqlalchemy import Column, Integer, String, BigInteger, Sequence, Table, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

BaseModel = declarative_base()

class publicusers(BaseModel):
	__tablename__ = 'users' 

	id = Column(Integer, primary_key=True)
	name = Column(String)

	usersRel = relationship('publicusergroups', back_populates='usersRelation')



class publicusergroups(BaseModel):
	__tablename__ = 'usergroups' 

	id = Column(Integer, primary_key=True)
	user_id = Column(Integer, ForeignKey('users.id', onupdate = "NO ACTION", ondelete = "NO ACTION"))
	group_id = Column(Integer, ForeignKey('groups.id', onupdate = "NO ACTION", ondelete = "NO ACTION"))

	usersRelation = relationship('publicusers', uselist=True, back_populates='usersRel')
	groupsRelation = relationship('publicgroups', uselist=True, back_populates='groupsRel')


class publicgroups(BaseModel):
	__tablename__ = 'groups' 

	id = Column(Integer, primary_key=True)
	name = Column(String)

	groupsRel = relationship('publicusergroups', back_populates='groupsRelation')



class publicuzivatel(BaseModel):
	__tablename__ = 'uzivatel' 

	uzivatele_id = Column(Integer, Sequence('uzivatel_uzivatele_id_seq'), primary_key=True)
	jmeno = Column(String(60))
	prijmeni = Column(String(60))
	datum_narozeni = Column(DateTime)
	pocet_clanku = Column(Integer)<jupyter_output><empty_output><jupyter_text>Schemas:<jupyter_code>from typing import List, Optional
from datetime import date, datetime
from pydantic import BaseModel as BaseSchema       


class usersCreateSchema(BaseSchema):
	id: int
	name: str

class usersUpdateSchema(BaseSchema):
	id: int
	name: str
	class Config:
		orm_mode = True
	pass

class usersGetSchema(BaseSchema):
	id: int
	name: str
	class Config:
		orm_mode = True
	pass

class usersDeleteSchema(BaseSchema):
	id: int
	name: str




class usergroupsCreateSchema(BaseSchema):
	id: int
	user_id: int
	group_id: int

class usergroupsUpdateSchema(BaseSchema):
	id: int
	user_id: int
	group_id: int
	class Config:
		orm_mode = True
	pass

class usergroupsGetSchema(BaseSchema):
	id: int
	user_id: int
	group_id: int
	class Config:
		orm_mode = True
	pass

class usergroupsDeleteSchema(BaseSchema):
	id: int
	user_id: int
	group_id: int




class groupsCreateSchema(BaseSchema):
	id: int
	name: str

class groupsUpdateSchema(BaseSchema):
	id: int
	name: str
	class Config:
		orm_mode = True
	pass

class groupsGetSchema(BaseSchema):
	id: int
	name: str
	class Config:
		orm_mode = True
	pass

class groupsDeleteSchema(BaseSchema):
	id: int
	name: str




class uzivatelCreateSchema(BaseSchema):
	uzivatele_id: int
	jmeno: str
	prijmeni: str
	datum_narozeni: date
	pocet_clanku: int

class uzivatelUpdateSchema(BaseSchema):
	uzivatele_id: int
	jmeno: str
	prijmeni: str
	datum_narozeni: date
	pocet_clanku: int
	class Config:
		orm_mode = True
	pass

class uzivatelGetSchema(BaseSchema):
	uzivatele_id: int
	jmeno: str
	prijmeni: str
	datum_narozeni: date
	pocet_clanku: int
	class Config:
		orm_mode = True
	pass

class uzivatelDeleteSchema(BaseSchema):
	uzivatele_id: int
	jmeno: str
	prijmeni: str
	datum_narozeni: date
	pocet_clanku: int<jupyter_output><empty_output><jupyter_text>Aplikace (FastAPI)<jupyter_code>from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import Session
import uvicorn
from fastapi import FastAPI
from fastapi import Depends
from fastapi import HTTPException

from multiprocessing import Process
from wait4it import wait_for

app = FastAPI()

def run():
	uvicorn.run(app, port=9992, host='0.0.0.0', root_path='')


def start_api(runNew=True):
	global _api_process
	if _api_process:
		_api_process.terminate()
		_api_process.join()
	if runNew:
		_api_process = Process(target=run, daemon=True)
		_api_process.start()
		wait_for(port=9992)

#--HINT:--engine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')
KnownDatabase = 'postgresql+psycopg2://postgres:example@postgres/postgres'

engine = create_engine(KnownDatabase)
SessionLocal = sessionmaker(bind=engine)

_api_process = None

def get_db():
	db = SessionLocal()
	try:
		yield db
	finally:
		db.close()

def connectAllEndpoints(app):

	@app.get("/usersname/{name_ed}", response_model=List[usersGetSchema])
	def read_users(name_ed: str, db: Session=Depends(get_db)):
		usersResult_name = db.query(publicusers).filter(publicusers.name == name_ed).all()
		if (usersResult_name is None)or(len(usersResult_name)<=0):
			raise HTTPException(status_code=404, detail="Error: This name wasnt found!")
		else:
			return usersResult_name

	@app.get("/allusers", response_model=List[usersGetSchema])
	def getAllusers(skip: int = 0, limit: int = 100, db: Session=Depends(get_db)):
		usersResult = db.query(publicusers).offset(skip).limit(limit).all()
		return usersResult

	@app.post("/users/{id_ed}/{name_ed}", response_model=usersGetSchema)
	def createusers(id_ed: int, name_ed: str, users:usersCreateSchema,db: Session=Depends(get_db)):
		usersRow_id = db.query(publicusers).filter(publicusers.id == id_ed).first()
		if usersRow_id:
			raise HTTPException(status_code=400, detail="Error: This id already exist!")
		#gen_id=0
		#while (db.query(publicusers).filter(publicusers.id == gen_id).first()):
			#gen_id=gen_id+1
		#usersCreateRow_name = publicusers(id = gen_id, name = name_ed)
		usersCreateRow_name = publicusers(id=id_ed, name=name_ed)
		db.add(usersCreateRow_name)
		db.commit()
		db.refresh(usersCreateRow_name)
		result = (usersCreateRow_name)
		return result

	@app.put("/users/{id_ed}/{name_ed}", response_model=usersGetSchema)
	def updateusers(id_ed: int, name_ed: str, users:usersUpdateSchema,db: Session=Depends(get_db)):
		usersToUpdate = db.query(publicusers).filter(publicusers.id == id_ed).first()
		if usersToUpdate is None:
			raise HTTPException(status_code=400, detail="Error: This id doesnt exist!")
		if (usersToUpdate.id==id_ed) and (usersToUpdate.name==name_ed):
			raise HTTPException(status_code=400, detail="Warning: No change done!")
		else:
			db.delete(usersToUpdate)
			usersToUpdate = publicusers(id=id_ed, name=name_ed)
			db.add(usersToUpdate)
			db.commit()
			db.refresh(usersToUpdate)
		return usersToUpdate

	@app.delete("/users/{id_ed}")
	def deleteusers(id_ed: int, users: usersDeleteSchema, db: Session=Depends(get_db)):
		users_to_delete = db.query(publicusers).filter(publicusers.id == id_ed).first()
		if not users_to_delete:
			raise HTTPException(status_code=400, detail="users id wasnt found")
		db.delete(users_to_delete)
		db.commit()
		return {"message": "users id: " + str(id_ed) + " SUCESFULLY DELETED"}

	@app.get("/usergroupsuser_id/{user_id_ed}", response_model=List[usergroupsGetSchema])
	def read_usergroups(user_id_ed: int, db: Session=Depends(get_db)):
		usergroupsResult_user_id = db.query(publicusergroups).filter(publicusergroups.user_id == user_id_ed).all()
		if (usergroupsResult_user_id is None)or(len(usergroupsResult_user_id)<=0):
			raise HTTPException(status_code=404, detail="Error: This user_id wasnt found!")
		else:
			return usergroupsResult_user_id

	@app.get("/usergroupsgroup_id/{group_id_ed}", response_model=List[usergroupsGetSchema])
	def read_usergroups(group_id_ed: int, db: Session=Depends(get_db)):
		usergroupsResult_group_id = db.query(publicusergroups).filter(publicusergroups.group_id == group_id_ed).all()
		if (usergroupsResult_group_id is None)or(len(usergroupsResult_group_id)<=0):
			raise HTTPException(status_code=404, detail="Error: This group_id wasnt found!")
		else:
			return usergroupsResult_group_id

	@app.get("/allusergroups", response_model=List[usergroupsGetSchema])
	def getAllusergroups(skip: int = 0, limit: int = 100, db: Session=Depends(get_db)):
		usergroupsResult = db.query(publicusergroups).offset(skip).limit(limit).all()
		return usergroupsResult

	@app.post("/usergroups/{id_ed}/{user_id_ed}/{group_id_ed}", response_model=usergroupsGetSchema)
	def createusergroups(id_ed: int, user_id_ed: int, group_id_ed: int, usergroups:usergroupsCreateSchema,db: Session=Depends(get_db)):
		usergroupsRow_id = db.query(publicusergroups).filter(publicusergroups.id == id_ed).first()
		if usergroupsRow_id:
			raise HTTPException(status_code=400, detail="Error: This id already exist!")
		#gen_id=0
		#while (db.query(publicusergroups).filter(publicusergroups.id == gen_id).first()):
			#gen_id=gen_id+1
		#usergroupsCreateRow_group_id = publicusergroups(id = gen_id, group_id = group_id_ed)
		usergroupsCreateRow_group_id = publicusergroups(id=id_ed, user_id=user_id_ed, group_id=group_id_ed)
		db.add(usergroupsCreateRow_group_id)
		db.commit()
		db.refresh(usergroupsCreateRow_group_id)
		result = (usergroupsCreateRow_group_id)
		return result

	@app.put("/usergroups/{id_ed}/{user_id_ed}/{group_id_ed}", response_model=usergroupsGetSchema)
	def updateusergroups(id_ed: int, user_id_ed: int, group_id_ed: int, usergroups:usergroupsUpdateSchema,db: Session=Depends(get_db)):
		usergroupsToUpdate = db.query(publicusergroups).filter(publicusergroups.id == id_ed).first()
		if usergroupsToUpdate is None:
			raise HTTPException(status_code=400, detail="Error: This id doesnt exist!")
		if (usergroupsToUpdate.id==id_ed) and (usergroupsToUpdate.user_id==user_id_ed) and (usergroupsToUpdate.group_id==group_id_ed):
			raise HTTPException(status_code=400, detail="Warning: No change done!")
		else:
			db.delete(usergroupsToUpdate)
			usergroupsToUpdate = publicusergroups(id=id_ed, user_id=user_id_ed, group_id=group_id_ed)
			db.add(usergroupsToUpdate)
			db.commit()
			db.refresh(usergroupsToUpdate)
		return usergroupsToUpdate

	@app.delete("/usergroups/{id_ed}")
	def deleteusergroups(id_ed: int, usergroups: usergroupsDeleteSchema, db: Session=Depends(get_db)):
		usergroups_to_delete = db.query(publicusergroups).filter(publicusergroups.id == id_ed).first()
		if not usergroups_to_delete:
			raise HTTPException(status_code=400, detail="usergroups id wasnt found")
		db.delete(usergroups_to_delete)
		db.commit()
		return {"message": "usergroups id: " + str(id_ed) + " SUCESFULLY DELETED"}

	@app.get("/groupsname/{name_ed}", response_model=List[groupsGetSchema])
	def read_groups(name_ed: str, db: Session=Depends(get_db)):
		groupsResult_name = db.query(publicgroups).filter(publicgroups.name == name_ed).all()
		if (groupsResult_name is None)or(len(groupsResult_name)<=0):
			raise HTTPException(status_code=404, detail="Error: This name wasnt found!")
		else:
			return groupsResult_name

	@app.get("/allgroups", response_model=List[groupsGetSchema])
	def getAllgroups(skip: int = 0, limit: int = 100, db: Session=Depends(get_db)):
		groupsResult = db.query(publicgroups).offset(skip).limit(limit).all()
		return groupsResult

	@app.post("/groups/{id_ed}/{name_ed}", response_model=groupsGetSchema)
	def creategroups(id_ed: int, name_ed: str, groups:groupsCreateSchema,db: Session=Depends(get_db)):
		groupsRow_id = db.query(publicgroups).filter(publicgroups.id == id_ed).first()
		if groupsRow_id:
			raise HTTPException(status_code=400, detail="Error: This id already exist!")
		#gen_id=0
		#while (db.query(publicgroups).filter(publicgroups.id == gen_id).first()):
			#gen_id=gen_id+1
		#groupsCreateRow_name = publicgroups(id = gen_id, name = name_ed)
		groupsCreateRow_name = publicgroups(id=id_ed, name=name_ed)
		db.add(groupsCreateRow_name)
		db.commit()
		db.refresh(groupsCreateRow_name)
		result = (groupsCreateRow_name)
		return result

	@app.put("/groups/{id_ed}/{name_ed}", response_model=groupsGetSchema)
	def updategroups(id_ed: int, name_ed: str, groups:groupsUpdateSchema,db: Session=Depends(get_db)):
		groupsToUpdate = db.query(publicgroups).filter(publicgroups.id == id_ed).first()
		if groupsToUpdate is None:
			raise HTTPException(status_code=400, detail="Error: This id doesnt exist!")
		if (groupsToUpdate.id==id_ed) and (groupsToUpdate.name==name_ed):
			raise HTTPException(status_code=400, detail="Warning: No change done!")
		else:
			db.delete(groupsToUpdate)
			groupsToUpdate = publicgroups(id=id_ed, name=name_ed)
			db.add(groupsToUpdate)
			db.commit()
			db.refresh(groupsToUpdate)
		return groupsToUpdate

	@app.delete("/groups/{id_ed}")
	def deletegroups(id_ed: int, groups: groupsDeleteSchema, db: Session=Depends(get_db)):
		groups_to_delete = db.query(publicgroups).filter(publicgroups.id == id_ed).first()
		if not groups_to_delete:
			raise HTTPException(status_code=400, detail="groups id wasnt found")
		db.delete(groups_to_delete)
		db.commit()
		return {"message": "groups id: " + str(id_ed) + " SUCESFULLY DELETED"}

	@app.get("/uzivateljmeno/{jmeno_ed}", response_model=List[uzivatelGetSchema])
	def read_uzivatel(jmeno_ed: str, db: Session=Depends(get_db)):
		uzivatelResult_jmeno = db.query(publicuzivatel).filter(publicuzivatel.jmeno == jmeno_ed).all()
		if (uzivatelResult_jmeno is None)or(len(uzivatelResult_jmeno)<=0):
			raise HTTPException(status_code=404, detail="Error: This jmeno wasnt found!")
		else:
			return uzivatelResult_jmeno

	@app.get("/uzivatelprijmeni/{prijmeni_ed}", response_model=List[uzivatelGetSchema])
	def read_uzivatel(prijmeni_ed: str, db: Session=Depends(get_db)):
		uzivatelResult_prijmeni = db.query(publicuzivatel).filter(publicuzivatel.prijmeni == prijmeni_ed).all()
		if (uzivatelResult_prijmeni is None)or(len(uzivatelResult_prijmeni)<=0):
			raise HTTPException(status_code=404, detail="Error: This prijmeni wasnt found!")
		else:
			return uzivatelResult_prijmeni

	@app.get("/uzivateldatum_narozeni/{datum_narozeni_ed}", response_model=List[uzivatelGetSchema])
	def read_uzivatel(datum_narozeni_ed: date, db: Session=Depends(get_db)):
		uzivatelResult_datum_narozeni = db.query(publicuzivatel).filter(publicuzivatel.datum_narozeni == datum_narozeni_ed).all()
		if (uzivatelResult_datum_narozeni is None)or(len(uzivatelResult_datum_narozeni)<=0):
			raise HTTPException(status_code=404, detail="Error: This datum_narozeni wasnt found!")
		else:
			return uzivatelResult_datum_narozeni

	@app.get("/uzivatelpocet_clanku/{pocet_clanku_ed}", response_model=List[uzivatelGetSchema])
	def read_uzivatel(pocet_clanku_ed: int, db: Session=Depends(get_db)):
		uzivatelResult_pocet_clanku = db.query(publicuzivatel).filter(publicuzivatel.pocet_clanku == pocet_clanku_ed).all()
		if (uzivatelResult_pocet_clanku is None)or(len(uzivatelResult_pocet_clanku)<=0):
			raise HTTPException(status_code=404, detail="Error: This pocet_clanku wasnt found!")
		else:
			return uzivatelResult_pocet_clanku

	@app.get("/alluzivatel", response_model=List[uzivatelGetSchema])
	def getAlluzivatel(skip: int = 0, limit: int = 100, db: Session=Depends(get_db)):
		uzivatelResult = db.query(publicuzivatel).offset(skip).limit(limit).all()
		return uzivatelResult

	@app.post("/uzivatel/{uzivatele_id_ed}/{jmeno_ed}/{prijmeni_ed}/{datum_narozeni_ed}/{pocet_clanku_ed}", response_model=uzivatelGetSchema)
	def createuzivatel(uzivatele_id_ed: int, jmeno_ed: str, prijmeni_ed: str, datum_narozeni_ed: date, pocet_clanku_ed: int, uzivatel:uzivatelCreateSchema,db: Session=Depends(get_db)):
		uzivatelRow_uzivatele_id = db.query(publicuzivatel).filter(publicuzivatel.uzivatele_id == uzivatele_id_ed).first()
		if uzivatelRow_uzivatele_id:
			raise HTTPException(status_code=400, detail="Error: This uzivatele_id already exist!")
		#gen_id=0
		#while (db.query(publicuzivatel).filter(publicuzivatel.id == gen_id).first()):
			#gen_id=gen_id+1
		#uzivatelCreateRow_pocet_clanku = publicuzivatel(id = gen_id, pocet_clanku = pocet_clanku_ed)
		uzivatelCreateRow_pocet_clanku = publicuzivatel(uzivatele_id=uzivatele_id_ed, jmeno=jmeno_ed, prijmeni=prijmeni_ed, datum_narozeni=datum_narozeni_ed, pocet_clanku=pocet_clanku_ed)
		db.add(uzivatelCreateRow_pocet_clanku)
		db.commit()
		db.refresh(uzivatelCreateRow_pocet_clanku)
		result = (uzivatelCreateRow_pocet_clanku)
		return result

	@app.put("/uzivatel/{uzivatele_id_ed}/{jmeno_ed}/{prijmeni_ed}/{datum_narozeni_ed}/{pocet_clanku_ed}", response_model=uzivatelGetSchema)
	def updateuzivatel(uzivatele_id_ed: int, jmeno_ed: str, prijmeni_ed: str, datum_narozeni_ed: date, pocet_clanku_ed: int, uzivatel:uzivatelUpdateSchema,db: Session=Depends(get_db)):
		uzivatelToUpdate = db.query(publicuzivatel).filter(publicuzivatel.uzivatele_id == uzivatele_id_ed).first()
		if uzivatelToUpdate is None:
			raise HTTPException(status_code=400, detail="Error: This uzivatele_id doesnt exist!")
		if (uzivatelToUpdate.uzivatele_id==uzivatele_id_ed) and (uzivatelToUpdate.jmeno==jmeno_ed) and (uzivatelToUpdate.prijmeni==prijmeni_ed) and (uzivatelToUpdate.datum_narozeni==datum_narozeni_ed) and (uzivatelToUpdate.pocet_clanku==pocet_clanku_ed):
			raise HTTPException(status_code=400, detail="Warning: No change done!")
		else:
			db.delete(uzivatelToUpdate)
			uzivatelToUpdate = publicuzivatel(uzivatele_id=uzivatele_id_ed, jmeno=jmeno_ed, prijmeni=prijmeni_ed, datum_narozeni=datum_narozeni_ed, pocet_clanku=pocet_clanku_ed)
			db.add(uzivatelToUpdate)
			db.commit()
			db.refresh(uzivatelToUpdate)
		return uzivatelToUpdate

	@app.delete("/uzivatel/{uzivatele_id_ed}")
	def deleteuzivatel(uzivatele_id_ed: int, uzivatel: uzivatelDeleteSchema, db: Session=Depends(get_db)):
		uzivatel_to_delete = db.query(publicuzivatel).filter(publicuzivatel.uzivatele_id == uzivatele_id_ed).first()
		if not uzivatel_to_delete:
			raise HTTPException(status_code=400, detail="uzivatel uzivatele_id wasnt found")
		db.delete(uzivatel_to_delete)
		db.commit()
		return {"message": "uzivatel uzivatele_id: " + str(uzivatele_id_ed) + " SUCESFULLY DELETED"}


connectAllEndpoints(app)


try:
	start_api()
	print("FastAPI spusteno")
	pass
except Exception as e:
	start_api(False)
	print("Neco se nepovedlo")<jupyter_output>INFO:     Started server process [472]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:9992 (Press CTRL+C to quit)<jupyter_text>Ukonceni aplikace (FastAPI)<jupyter_code>if _api_process:
    _api_process.terminate()
    _api_process.join()
start_api(False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11609498680738786, 'fraction_numerical': 0.007420844327176781, 'mean_word_length': 7.107397504456328, 'pattern_counts': {'":': 4, '<': 24, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '608256', 'license': 'MIT', 'n_tokens_mistral': 7064, 'n_tokens_neox': 6675, 'n_words': 1139, 'path': 'APIgen/APIgen/notebooks/runFastAPI.ipynb', 'repo_name': 'Lubos-source/Informatika-projekt-22-5KB', 'size': '18192'}