Search code examples
pythonflasklob

Trouble accessing class attributes in Python


I have a route in flask that I'm using to pass numerous variables into my function via http. Everything is coming in fine except for the "biz" variable. Here is my function...

@app.route("/sendcheck/<name>/<address_object>/<amount>/<memo>/<biz>", methods=['GET', 'POST'])
def create_check(name, address_object, amount, memo, biz):
    check = lob.Check.create(
        description = 'Check for {}'.format(name),
        to_address = address_object,
        from_address = biz.address,
        bank_account = biz.bank,
        amount = amount,
        memo = memo,
        logo = open(biz_0.logo, 'rb'),
        check_bottom = '<h1 style="padding-top:4in;">{{name}}, attached is your check', #add a custom bottom
        data = {
          'name': name
        }
    )

The biz variable accesses a class with various attributes. So if the value for the variable is biz_1 then inputting biz.address should be same as biz_1.address but it doesn't work. I'm getting...

AttributeError: 'unicode' object has no attribute 'address'

What am I doing wrong here?


Solution

  • It's far from ideal but this is how I solved it. I'm open to suggestions.

    @app.route("/sendcheck/<name>/<address_object>/<amount>/<memo>/<biz>", methods=['GET', 'POST'])
    def create_check(name, address_object, amount, memo, biz):
        if biz == 'biz_0':
            biz = biz_0
        elif biz == 'biz_1':
            biz = biz_1
        elif biz == 'biz_2':
            biz = biz_2
        elif biz == 'biz_3':
            biz = biz_3
        check = lob.Check.create(
            description = 'Check for {}'.format(name),
            to_address = address_object,
            from_address = biz.address,
            bank_account = biz.bank,
            amount = amount,
            memo = memo,
            logo = open(biz_0.logo, 'rb'),
            check_bottom = '<h1 style="padding-top:4in;">{{name}}, attached is your check', #add a custom bottom
            data = {
              'name': name
            }
        )