def load_modules(): global modules_loaded global module if modules_loaded: return module APP = get_env('APP') if APP is None: raise Exception('Environment variable `APP` is required') module = LationModule(APP) module.load() modules_loaded = True return module
import functools import os import time from sqlalchemy import create_engine, inspect from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.schema import CreateSchema, DropSchema, MetaData from sqlalchemy.types import JSON, Boolean from lation.core.env import get_env from lation.core.logger import create_logger from lation.core.module import modules from lation.modules.base.file_system import FileSystem APP = get_env('APP') DEBUG_SQL = get_env('DEBUG_SQL') class Database(): @staticmethod def get_metadata(): return MetaData(schema=APP) def __init__(self, url=None, dialect=None, driver=None, username=None, password=None, host=None, port=None, database=None, model_agnostic=False): from lation.core.orm import Base from lation.modules.base.models.lation_data import LationData if not url:
from typing import Optional import requests from fastapi import APIRouter, Depends, status from sqlalchemy.orm import Session from starlette.responses import RedirectResponse from lation.core.env import get_env from lation.modules.base_fastapi.decorators import managed_transaction from lation.modules.base_fastapi.dependencies import get_session from lation.modules.customer.decorators import oauth_login_flow from lation.modules.customer.models.oauth_user import GoogleUser, GoogleUserToken, LineUser, LineUserToken from lation.modules.customer.oauth import GoogleScheme, LineScheme from lation.modules.customer.schemas.oauth import GoogleAuthorizationSchema, LineAuthorizationSchema HOST = get_env('HOST') FRONTEND_HOST = get_env('FRONTEND_HOST') OAUTH_SUCCESS_REDIRECT_URL = f'{FRONTEND_HOST}/auth/result' OAUTH_FAIL_REDIRECT_URL = f'{FRONTEND_HOST}/auth/result' OAUTH_GOOGLE_CLIENT_ID = get_env('OAUTH_GOOGLE_CLIENT_ID') OAUTH_GOOGLE_CLIENT_SECRET = get_env('OAUTH_GOOGLE_CLIENT_SECRET') OAUTH_LINE_CLIENT_ID = get_env('OAUTH_LINE_CLIENT_ID') OAUTH_LINE_CLIENT_SECRET = get_env('OAUTH_LINE_CLIENT_SECRET') google_scheme = GoogleScheme(client_id=OAUTH_GOOGLE_CLIENT_ID, client_secret=OAUTH_GOOGLE_CLIENT_SECRET, redirect_uri=f'{HOST}/auth/google/callback') line_scheme = LineScheme(client_id=OAUTH_LINE_CLIENT_ID, client_secret=OAUTH_LINE_CLIENT_SECRET, redirect_uri=f'{HOST}/auth/line/callback') router = APIRouter()
import click from lation.core.command import Mutex, cli from lation.core.migration.migration import Migration from lation.core.env import get_env APP = get_env('APP') @cli.group('migration') @click.pass_context @click.option('--url', cls=Mutex, not_required_if=[ 'dialect', 'driver', 'username', 'password', 'host', 'port', 'database' ]) @click.option('--dialect', cls=Mutex, not_required_if=['url']) @click.option('--driver', cls=Mutex, not_required_if=['url']) @click.option('--username', cls=Mutex, not_required_if=['url']) @click.option('--password', cls=Mutex, not_required_if=['url']) @click.option('--host', cls=Mutex, not_required_if=['url']) @click.option('--port', cls=Mutex, not_required_if=['url']) @click.option('--database', cls=Mutex, not_required_if=['url']) def migration(ctx, url, dialect, driver, username, password, host, port, database): migration = Migration(url=url, dialect=dialect, driver=driver, username=username, password=password,
import shortuuid from sqlalchemy import Column, ForeignKey from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import Session, backref, object_session, relationship from lation.core.database.types import STRING_M_SIZE, STRING_S_SIZE, STRING_XS_SIZE, Integer, Numeric, String from lation.core.env import get_env from lation.core.orm import Base, JoinedTableInheritanceMixin, SingleTableInheritanceMixin from lation.modules.base.models.currency import Currency from lation.modules.base.models.job import JobProducer from lation.modules.base_fastapi.routers.schemas import StatusEnum from lation.modules.base.vendors.ecpay_payment_sdk import ECPayPaymentSdk HOST = get_env('HOST') FRONTEND_HOST = get_env('FRONTEND_HOST') PAYMENT_GATEWAY_ECPAY_MERCHANT_ID = get_env('PAYMENT_GATEWAY_ECPAY_MERCHANT_ID') PAYMENT_GATEWAY_ECPAY_HASH_KEY = get_env('PAYMENT_GATEWAY_ECPAY_HASH_KEY') PAYMENT_GATEWAY_ECPAY_HASH_IV = get_env('PAYMENT_GATEWAY_ECPAY_HASH_IV') PAYMENT_REDIRECT_URL=f'{FRONTEND_HOST}/payment/result' class Payment(Base): __tablename__ = 'payment' payment_gateway_trade_id = Column(Integer, ForeignKey('payment_gateway_trade.id'), index=True) payment_gateway_trade = relationship('PaymentGatewayTrade', foreign_keys=[payment_gateway_trade_id]) billed_amount = Column(Numeric) billed_currency_id = Column(Integer, ForeignKey('currency.id'), index=True)
from lation.core.env import get_env from lation.modules.base.http_client import HttpClient, Response LINE_CHANNEL_ACCESS_TOKEN = get_env('LINE_CHANNEL_ACCESS_TOKEN') class LineAPIClient(HttpClient): def __init__(self, access_token=None): super().__init__(host='https://api.line.me') if not access_token: access_token = LINE_CHANNEL_ACCESS_TOKEN self.access_token = access_token # https://developers.line.biz/zh-hant/docs/line-login/link-a-bot/#use-social-api def get_friendship_status(self) -> dict: return self.get_json( '/friendship/v1/status', headers={'Authorization': f'Bearer {self.access_token}'}) def push_message(self, to: str, messages) -> dict: return self.post_json( '/v2/bot/message/push', headers={'Authorization': f'Bearer {self.access_token}'}, json={ 'to': to, 'messages': messages })
from datetime import datetime, timedelta from fastapi import APIRouter, Depends, HTTPException, Response as FastAPIResponse, status from sqlalchemy.orm import Session from starlette.responses import RedirectResponse from lation.core.env import get_env from lation.modules.base_fastapi.dependencies import get_session from lation.modules.base_fastapi.line_api_client import LineAPIClient from lation.modules.base_fastapi.routers.schemas import ResponseSchema as Response, StatusEnum from lation.modules.customer.dependencies import login_required, get_current_user from lation.modules.customer.models.oauth_user import LineUserToken, OAuthUser from lation.modules.customer.routers.schemas import EndUserSchema, LineFriendshipSchema LINE_BOT_BASIC_ID = get_env('LINE_BOT_BASIC_ID') router = APIRouter() @router.post('/logout', tags=['end_user'], dependencies=[Depends(login_required)], response_model=Response) def logout(response: FastAPIResponse, end_user=Depends(get_current_user)): response.lation_unset_access_token() return Response(status=StatusEnum.SUCCESS) @router.get('/me', tags=['end_user'], dependencies=[Depends(login_required)], response_model=Response[EndUserSchema])
from typing import Optional from fastapi import FastAPI, Response from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware from lation.core.database.database import Database from lation.core.env import DEV, get_env from lation.modules.base.base import BaseLationApp from lation.modules.base_fastapi.routers import system DB_URL = get_env('DB_URL') FRONTEND_HOST = get_env('FRONTEND_HOST') class BaseFastAPI(BaseLationApp, FastAPI): def __init__(self): super().__init__() if not DEV: self.add_middleware(HTTPSRedirectMiddleware) # FIXME: The order of CORSMiddleware matters # https://github.com/tiangolo/fastapi/issues/1663 self.add_middleware(GZipMiddleware) self.add_middleware(CORSMiddleware, allow_origins=[FRONTEND_HOST], allow_credentials=True, allow_methods=['*'], allow_headers=['*'])
from lation.core.env import get_env FTX_API_KEY_ROOT = get_env('FTX_API_KEY_ROOT') FTX_API_SECRET_ROOT = get_env('FTX_API_SECRET_ROOT') FTX_API_KEY_ME = get_env('FTX_API_KEY_ME') FTX_API_SECRET_ME = get_env('FTX_API_SECRET_ME') FTX_API_KEY_MOM = get_env('FTX_API_KEY_MOM') FTX_API_SECRET_MOM = get_env('FTX_API_SECRET_MOM') FTX_API_KEY_SISTER = get_env('FTX_API_KEY_SISTER') FTX_API_SECRET_SISTER = get_env('FTX_API_SECRET_SISTER')
import functools import time from datetime import datetime from croniter import croniter from sqlalchemy import Column, ForeignKey from sqlalchemy.orm import relationship from lation.core.database.database import Database from lation.core.database.types import STRING_L_SIZE, STRING_S_SIZE, STRING_XS_SIZE, Boolean, DateTime, Integer, String from lation.core.env import get_env from lation.core.orm import Base from lation.core.utils import call_fn, coro from lation.modules.base.models.message_queue import Publisher, Subscriber DB_URL = get_env('DB_URL') class CronJobLog(Base): __tablename__ = 'cron_job_log' cron_job_id = Column(Integer, ForeignKey('cron_job.id'), index=True) cron_job = relationship('CronJob', foreign_keys=[cron_job_id]) execute_time = Column(DateTime) finish_time = Column(DateTime) exception = Column(String(STRING_L_SIZE)) return_value = Column(String(STRING_L_SIZE)) class CronJob(Base):