Can I use swagger created server stub models as the models for sqlAlchemy?

260 Views Asked by At

SO this is probably straightforwards and I am missing something as I am completely new to python, I have created a server stub in swagger 3.0 for python flask and this created a bunch of model classes based on my swagger documentation, it also uses connexion Example :

# coding: utf-8

from __future__ import absolute_import
from datetime import date, datetime  # noqa: F401

from typing import List, Dict  # noqa: F401

from src.models.base_model_ import Model
from src.models.accounting_bills import AccountingBills  # noqa: F401,E501
from src.models.accounting_payments_made import AccountingPaymentsMade  # noqa: F401,E501
from src.models.accounting_payments_pending import AccountingPaymentsPending  # noqa: F401,E501
from src.models.accounts_invoice import AccountsInvoice  # noqa: F401,E501
from src.models.accounts_payable import AccountsPayable  # noqa: F401,E501
from src.models.accounts_receivable import AccountsReceivable  # noqa: F401,E501
from src import util


class Accounting(Model):
"""NOTE: This class is auto generated by the swagger code generator program.

Do not edit the class manually.
"""
def __init__(self, accounts_receivable: AccountsReceivable=None, accounts_payable: AccountsPayable=None, invoices: AccountsInvoice=None, payments_pending: AccountingPaymentsPending=None, payments_made: AccountingPaymentsMade=None, bills: AccountingBills=None):  # noqa: E501
    """Accounting - a model defined in Swagger

    :param accounts_receivable: The accounts_receivable of this Accounting.  # noqa: E501
    :type accounts_receivable: AccountsReceivable
    :param accounts_payable: The accounts_payable of this Accounting.  # noqa: E501
    :type accounts_payable: AccountsPayable
    :param invoices: The invoices of this Accounting.  # noqa: E501
    :type invoices: AccountsInvoice
    :param payments_pending: The payments_pending of this Accounting.  # noqa: E501
    :type payments_pending: AccountingPaymentsPending
    :param payments_made: The payments_made of this Accounting.  # noqa: E501
    :type payments_made: AccountingPaymentsMade
    :param bills: The bills of this Accounting.  # noqa: E501
    :type bills: AccountingBills
    """
    self.swagger_types = {
        'accounts_receivable': AccountsReceivable,
        'accounts_payable': AccountsPayable,
        'invoices': AccountsInvoice,
        'payments_pending': AccountingPaymentsPending,
        'payments_made': AccountingPaymentsMade,
        'bills': AccountingBills
    }

    self.attribute_map = {
        'accounts_receivable': 'Accounts receivable',
        'accounts_payable': 'Accounts payable',
        'invoices': 'Invoices',
        'payments_pending': 'Payments Pending',
        'payments_made': 'Payments Made',
        'bills': 'Bills'
    }
    self._accounts_receivable = accounts_receivable
    self._accounts_payable = accounts_payable
    self._invoices = invoices
    self._payments_pending = payments_pending
    self._payments_made = payments_made
    self._bills = bills

@classmethod
def from_dict(cls, dikt) -> 'Accounting':
    """Returns the dict as a model

    :param dikt: A dict.
    :type: dict
    :return: The Accounting of this Accounting.  # noqa: E501
    :rtype: Accounting
    """
    return util.deserialize_model(dikt, cls)

@property
def accounts_receivable(self) -> AccountsReceivable:
    """Gets the accounts_receivable of this Accounting.


    :return: The accounts_receivable of this Accounting.
    :rtype: AccountsReceivable
    """
    return self._accounts_receivable

@accounts_receivable.setter
def accounts_receivable(self, accounts_receivable: AccountsReceivable):
    """Sets the accounts_receivable of this Accounting.


    :param accounts_receivable: The accounts_receivable of this Accounting.
    :type accounts_receivable: AccountsReceivable
    """

    self._accounts_receivable = accounts_receivable

@property
def accounts_payable(self) -> AccountsPayable:
    """Gets the accounts_payable of this Accounting.


    :return: The accounts_payable of this Accounting.
    :rtype: AccountsPayable
    """
    return self._accounts_payable

@accounts_payable.setter
def accounts_payable(self, accounts_payable: AccountsPayable):
    """Sets the accounts_payable of this Accounting.


    :param accounts_payable: The accounts_payable of this Accounting.
    :type accounts_payable: AccountsPayable
    """

    self._accounts_payable = accounts_payable

@property
def invoices(self) -> AccountsInvoice:
    """Gets the invoices of this Accounting.


    :return: The invoices of this Accounting.
    :rtype: AccountsInvoice
    """
    return self._invoices

@invoices.setter
def invoices(self, invoices: AccountsInvoice):
    """Sets the invoices of this Accounting.


    :param invoices: The invoices of this Accounting.
    :type invoices: AccountsInvoice
    """

    self._invoices = invoices

@property
def payments_pending(self) -> AccountingPaymentsPending:
    """Gets the payments_pending of this Accounting.


    :return: The payments_pending of this Accounting.
    :rtype: AccountingPaymentsPending
    """
    return self._payments_pending

@payments_pending.setter
def payments_pending(self, payments_pending: AccountingPaymentsPending):
    """Sets the payments_pending of this Accounting.


    :param payments_pending: The payments_pending of this Accounting.
    :type payments_pending: AccountingPaymentsPending
    """

    self._payments_pending = payments_pending

@property
def payments_made(self) -> AccountingPaymentsMade:
    """Gets the payments_made of this Accounting.


    :return: The payments_made of this Accounting.
    :rtype: AccountingPaymentsMade
    """
    return self._payments_made

@payments_made.setter
def payments_made(self, payments_made: AccountingPaymentsMade):
    """Sets the payments_made of this Accounting.


    :param payments_made: The payments_made of this Accounting.
    :type payments_made: AccountingPaymentsMade
    """

    self._payments_made = payments_made

@property
def bills(self) -> AccountingBills:
    """Gets the bills of this Accounting.


    :return: The bills of this Accounting.
    :rtype: AccountingBills
    """
    return self._bills

@bills.setter
def bills(self, bills: AccountingBills):
    """Sets the bills of this Accounting.


    :param bills: The bills of this Accounting.
    :type bills: AccountingBills
    """

    self._bills = bills

I can use this model in another class as below :

def accounts_receivable(user_id):  # noqa: E501
"""Get accounts receivable for the given user by id

 # noqa: E501

:param user_id: The id of Alitus Users
:type user_id: int

:rtype: the accounts receivable information
"""
accounts = []

for i in range(10):
    accounts.append(AccountsReceivable(1+i, user_id, 10+i, "harry",
                                       "simple terms"+str(i), get_timestamp(), 2345+i, 45667+i, get_timestamp(), 456+i, 23+i, 4+i))

return accounts

I used a preexisting database and created a models.py class to try and use sqlPython (also new to this):

from sqlalchemy import BigInteger, Column, Date, DateTime, Integer, JSON, Table, Text
from sqlalchemy.dialects.mysql import ENUM, TINYINT
from src.core.database import Base

metadata = Base.metadata


class Accounting(Base):
__tablename__ = 'accounting'
__table_args__ = {'comment': 'Alitus users Accounting information'}

Id = Column(Integer, primary_key=True)
Accounts_receivable = Column('Accounts receivable', Text(collation='utf8mb4_unicode_ci'))
Accounts_payable = Column('Accounts payable', Text(collation='utf8mb4_unicode_ci'))
Invoices = Column(Text(collation='utf8mb4_unicode_ci'))
Payments_Pending = Column('Payments Pending', Text(collation='utf8mb4_unicode_ci'))
Payments_Made = Column('Payments Made', Text(collation='utf8mb4_unicode_ci'))
Bills = Column(Text(collation='utf8mb4_unicode_ci'))
def __repr__(self):
 return "<User(Accounts_receivable='%s', Accounts_payable='%s', Invoices='%s')>" % (
                        self.Accounts_receivable, self.Accounts_payable, self.Invoices)
def __init__(self, accounts_receivable: AccountsReceivable=None, accounts_payable: AccountsPayable=None, invoices: AccountsInvoice=None, payments_pending: AccountingPaymentsPending=None, payments_made: AccountingPaymentsMade=None, bills: AccountingBills=None):  # noqa: E501
    self.Accounts_receivable = accounts_receivable
    self.Accounts_payable = accounts_payable
    .....

Is there a way I can use the swagger generated Models with the Alchemy Models say in the def init so I am not writing the code twice ? If so how would I do this ?
Is this the correct thing to be trying to do or am I going about this all in the wrong way ?
Any pointers or help would be appreciated.

0

There are 0 best solutions below