update project
This commit is contained in:
parent
02a29d4509
commit
4c0ffc747f
|
|
@ -22,46 +22,46 @@ class SubscriptionRep:
|
|||
|
||||
@staticmethod
|
||||
def get_(_id: int):
|
||||
with get_db() as db:
|
||||
row = db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).first()
|
||||
if not row:
|
||||
return None
|
||||
for item in ['plan', 'owner']:
|
||||
getattr(row, item)
|
||||
return row
|
||||
db = next(get_db())
|
||||
row = db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).first()
|
||||
if not row:
|
||||
return None
|
||||
for item in ['plan', 'owner']:
|
||||
getattr(row, item)
|
||||
return row
|
||||
|
||||
@staticmethod
|
||||
def update_(_id: int, _input: dict):
|
||||
with get_db() as db:
|
||||
_input['updated_at'] = datetime.datetime.now()
|
||||
db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).update(_input)
|
||||
db.commit()
|
||||
return True
|
||||
db = next(get_db())
|
||||
_input['updated_at'] = datetime.datetime.now()
|
||||
db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).update(_input)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
async def sub_list(body: dict):
|
||||
with get_db() as db:
|
||||
return await NewTableEngine.mssql(
|
||||
query_class=db,
|
||||
model=SubscriptionContract,
|
||||
extra_filter=[],
|
||||
search_cols=[],
|
||||
options=[
|
||||
joinedload(SubscriptionContract.plan),
|
||||
joinedload(SubscriptionContract.owner)
|
||||
],
|
||||
inputs=body.get('te', {})).to_dict()
|
||||
db = next(get_db())
|
||||
return await NewTableEngine.mssql(
|
||||
query_class=db,
|
||||
model=SubscriptionContract,
|
||||
extra_filter=[],
|
||||
search_cols=[],
|
||||
options=[
|
||||
joinedload(SubscriptionContract.plan),
|
||||
joinedload(SubscriptionContract.owner)
|
||||
],
|
||||
inputs=body.get('te', {})).to_dict()
|
||||
|
||||
@staticmethod
|
||||
async def plan_list(body: dict):
|
||||
with get_db() as db:
|
||||
return await NewTableEngine.mssql(
|
||||
query_class=db,
|
||||
model=SubscriptionPlan,
|
||||
extra_filter=[],
|
||||
search_cols=[],
|
||||
options=[],
|
||||
inputs=body.get('te', {})).to_dict()
|
||||
db = next(get_db())
|
||||
return await NewTableEngine.mssql(
|
||||
query_class=db,
|
||||
model=SubscriptionPlan,
|
||||
extra_filter=[],
|
||||
search_cols=[],
|
||||
options=[],
|
||||
inputs=body.get('te', {})).to_dict()
|
||||
|
||||
# @staticmethod
|
||||
# async def send_invoice(body: dict, contract_id: int):
|
||||
|
|
@ -126,28 +126,28 @@ class SubscriptionRep:
|
|||
|
||||
@staticmethod
|
||||
def send_invoice(request, _id):
|
||||
with get_db() as db:
|
||||
contract = db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).first()
|
||||
to_emails = request.get("to_emails", [])
|
||||
if len(to_emails) == 0:
|
||||
return "at least enter one mail."
|
||||
pdf_file = SubscriptionRep.invoice_pdf(contract)
|
||||
html_tmp = SubscriptionRep.invoice_html(contract)
|
||||
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
||||
server.login(user="info@ira-lex.com", password="Iralex2022")
|
||||
message = MIMEMultipart("alternative")
|
||||
message["subject"] = "Invoice"
|
||||
message["from"] = "info@ira-lex.com"
|
||||
message["to"] = ", ".join(to_emails)
|
||||
with open(pdf_file, "rb") as fil:
|
||||
part = MIMEApplication(
|
||||
fil.read(),
|
||||
Name=basename(pdf_file)
|
||||
)
|
||||
message_body = MIMEText(html_tmp, "html")
|
||||
message.attach(message_body)
|
||||
message.attach(part)
|
||||
server.sendmail(
|
||||
"info@ira-lex.com", to_emails, message.as_string()
|
||||
db = next(get_db())
|
||||
contract = db.query(SubscriptionContract).filter(SubscriptionContract.id == _id).first()
|
||||
to_emails = request.get("to_emails", [])
|
||||
if len(to_emails) == 0:
|
||||
return "at least enter one mail."
|
||||
pdf_file = SubscriptionRep.invoice_pdf(contract)
|
||||
html_tmp = SubscriptionRep.invoice_html(contract)
|
||||
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
|
||||
server.login(user="info@ira-lex.com", password="Iralex2022")
|
||||
message = MIMEMultipart("alternative")
|
||||
message["subject"] = "Invoice"
|
||||
message["from"] = "info@ira-lex.com"
|
||||
message["to"] = ", ".join(to_emails)
|
||||
with open(pdf_file, "rb") as fil:
|
||||
part = MIMEApplication(
|
||||
fil.read(),
|
||||
Name=basename(pdf_file)
|
||||
)
|
||||
return True
|
||||
message_body = MIMEText(html_tmp, "html")
|
||||
message.attach(message_body)
|
||||
message.attach(part)
|
||||
server.sendmail(
|
||||
"info@ira-lex.com", to_emails, message.as_string()
|
||||
)
|
||||
return True
|
||||
|
|
|
|||
|
|
@ -24,22 +24,22 @@ def get_model_route(model: str):
|
|||
|
||||
@app.app.post(f'{conf.PREFIX_API_URL}/auth/login')
|
||||
def login(body: dict = Body(...)):
|
||||
with get_db() as db:
|
||||
to_encode = body.copy()
|
||||
expire = datetime.utcnow() + timedelta(days=10)
|
||||
username = body.get('email')
|
||||
password = body.get('password')
|
||||
user = db.query(AccountAdmin).filter(AccountAdmin.email == username).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=422, detail='user not found!!')
|
||||
if not PasswordMaker.verify_password(plain_password=password, hashed_password=user.password):
|
||||
raise HTTPException(status_code=401, detail='username and password not found!!')
|
||||
to_encode.update({"exp": expire, 'id': user.id})
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode,
|
||||
conf.AUTH_SECRET_KEY,
|
||||
algorithm=conf.AUTH_ALGORITHM)
|
||||
return {"token": f"Bearer {encoded_jwt}"}
|
||||
db = next(get_db())
|
||||
to_encode = body.copy()
|
||||
expire = datetime.utcnow() + timedelta(days=10)
|
||||
username = body.get('email')
|
||||
password = body.get('password')
|
||||
user = db.query(AccountAdmin).filter(AccountAdmin.email == username).first()
|
||||
if not user:
|
||||
raise HTTPException(status_code=422, detail='user not found!!')
|
||||
if not PasswordMaker.verify_password(plain_password=password, hashed_password=user.password):
|
||||
raise HTTPException(status_code=401, detail='username and password not found!!')
|
||||
to_encode.update({"exp": expire, 'id': user.id})
|
||||
encoded_jwt = jwt.encode(
|
||||
to_encode,
|
||||
conf.AUTH_SECRET_KEY,
|
||||
algorithm=conf.AUTH_ALGORITHM)
|
||||
return {"token": f"Bearer {encoded_jwt}"}
|
||||
|
||||
|
||||
@router.get(f'{conf.PREFIX_API_URL}/auth/me')
|
||||
|
|
@ -50,8 +50,8 @@ async def me():
|
|||
|
||||
@router.get('/{_model}/{_id}')
|
||||
def get(_model: str, _id: int):
|
||||
with get_db() as db:
|
||||
return AutoController.get_(db, get_model_route(_model), _id)
|
||||
db = next(get_db())
|
||||
return AutoController.get_(db, get_model_route(_model), _id)
|
||||
|
||||
|
||||
@router.put('/{_model}/{_id}')
|
||||
|
|
@ -59,27 +59,27 @@ def put(
|
|||
_model: str,
|
||||
_id: int,
|
||||
payload=Body(...)):
|
||||
with get_db() as db:
|
||||
return AutoController.update(db, get_model_route(_model), _id, payload)
|
||||
db = next(get_db())
|
||||
return AutoController.update(db, get_model_route(_model), _id, payload)
|
||||
|
||||
|
||||
@router.post('/list/{_model}')
|
||||
async def _list(_model: str, body: dict = Body(...)):
|
||||
with get_db() as db:
|
||||
return await AutoController.list_(db, get_model_route(_model), body=body)
|
||||
db = next(get_db())
|
||||
return await AutoController.list_(db, get_model_route(_model), body=body)
|
||||
|
||||
|
||||
@router.post('/create/{_model}')
|
||||
async def create(_model: str, body: dict = Body(...)):
|
||||
with get_db() as db:
|
||||
return AutoController.create(db, get_model_route(_model), body)
|
||||
db = next(get_db())
|
||||
return AutoController.create(db, get_model_route(_model), body)
|
||||
|
||||
|
||||
@router.delete('/{_model}/{_id}')
|
||||
async def delete(_model: str,
|
||||
_id: int):
|
||||
with get_db() as db:
|
||||
return AutoController.delete(db, get_model_route(_model), _id)
|
||||
db = next(get_db())
|
||||
return AutoController.delete(db, get_model_route(_model), _id)
|
||||
|
||||
|
||||
app.app.include_router(router)
|
||||
|
|
|
|||
20
main.py
20
main.py
|
|
@ -16,16 +16,16 @@ def ping():
|
|||
|
||||
@app.get('/create')
|
||||
def create_user():
|
||||
with get_db() as db:
|
||||
new_admin = AccountAdmin(
|
||||
first_name='Admin',
|
||||
last_name='Supper',
|
||||
email='admin@irelex.com',
|
||||
gender=1, password=PasswordMaker.get_password_hash('Iralex2021')
|
||||
)
|
||||
db.add(new_admin)
|
||||
db.commit()
|
||||
return True
|
||||
db = next(get_db())
|
||||
new_admin = AccountAdmin(
|
||||
first_name='Admin',
|
||||
last_name='Supper',
|
||||
email='admin@irelex.com',
|
||||
gender=1, password=PasswordMaker.get_password_hash('Iralex2021')
|
||||
)
|
||||
db.add(new_admin)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
origins = [
|
||||
|
|
|
|||
24
utils.py
24
utils.py
|
|
@ -31,20 +31,20 @@ async def current_user(token: str = Depends(schema.oauth2)):
|
|||
detail="Could not validate credentials.",
|
||||
headers={"WWW-Authenticate": authenticate_value},
|
||||
)
|
||||
with get_db() as db:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, conf.AUTH_SECRET_KEY, algorithms=[
|
||||
conf.AUTH_ALGORITHM])
|
||||
username: str = payload.get("id")
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, conf.AUTH_SECRET_KEY, algorithms=[
|
||||
conf.AUTH_ALGORITHM])
|
||||
username: str = payload.get("id")
|
||||
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
user = db.query(AccountAdmin).filter(AccountAdmin.id == username).first()
|
||||
app.app.state.user = user
|
||||
except (JWTError, ValidationError):
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
return username
|
||||
db = next(get_db())
|
||||
user = db.query(AccountAdmin).filter(AccountAdmin.id == username).first()
|
||||
app.app.state.user = user
|
||||
except (JWTError, ValidationError):
|
||||
raise credentials_exception
|
||||
return username
|
||||
|
||||
|
||||
async def get_current_active_user(username=Security(current_user, scopes=["account"]), ):
|
||||
|
|
|
|||
Loading…
Reference in New Issue