Python Forum
How can flask find the controller?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can flask find the controller?
#1
Hello! How do I call a function from the controller in routes.py?
In app_ctrl.py I have function 'get_user' and I want to call it in routes.py(in 'def registration'), it display me 'NameError: name 'app_ctrl' is not defined'.
Please help me!

routes.py
 from datetime import datetime
 from application import app
 from flask import jsonify, redirect, render_template, request, url_for
 from application.forms import RegisterForm
 from application.Controller.app_ctrl import AppController
 from application.Controller.currencies_ctrl import CurrenciesController
 from application.Controller.users import UsersController
 from application.Controller.users_accounts import UsersAccountsController
 from application.Controller.users_cards import UsersCardsController
 from application.Controller.users_credentials import UsersCredentialsController
 from application.Controller.users_deposits_ctrl import UsersDepositsController
 from application.Controller.users_transactions import UsersTransactionsController
 from application.Model.Repository.currencies import DBCurrenciesRepository
 from application.Model.Repository.users import DBUsersRepository
 from application.Model.Repository.users_accounts import DBUsersAccountsRepository
 from application.Model.Repository.users_cards import DBUsersCardsRepository
 from application.Model.Repository.users_credentials import DBUsersCredentialsRepository
 from application.Model.Repository.users_deposits import DBUsersDepositsRepository
 from application.Model.Repository.users_transactions import DBUsersTransactionsRepository
 
 @app.route('/api/v1/register', methods=['POST', 'GET'])
 def register():
     form = RegisterForm()
     if form.validate_on_submit():
         user_id = form.user_id.data
         verify_user_id = app_ctrl.get_user(user_id)
         print(user_id)
         if verify_user_id:
             return jsonify(message='That user id already exists'), 404 #409
         else:
             first_name = form.first_name.data
             last_name = form.last_name.data
             email = form.email.data
             address = form.address.data
             phone_number = form.phone_number.data
             date_of_birth = form.date_of_birth.data
             join_date = datetime.now()
 
             username = form.username.data
             verify_username = app_ctrl.get_username(username)
             print(verify_username)
             if verify_username:
                 return jsonify(message='That user id already exists'), 404
             else:
                 password = form.password.data
 
         app_ctrl.register_user(user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date, username, password)
         return redirect(url_for('index'))
     return render_template('register.html', title='register', form=form)
 
 if __name__ == '__main__':
     users_rep = DBUsersRepository()
     users_account_repo = DBUsersAccountsRepository()
     users_transactions_repo = DBUsersTransactionsRepository()
     currencies_repo = DBCurrenciesRepository()
     users_deposits_repo = DBUsersDepositsRepository()
     users_cards_repo = DBUsersCardsRepository()
     users_credentials_repo = DBUsersCredentialsRepository()
 
     users_ctrl = UsersController(users_rep)
     users_account_ctrl = UsersAccountsController(users_account_repo)
     users_transactions_ctrl = UsersTransactionsController(users_transactions_repo)
     currencies_ctrl = CurrenciesController(currencies_repo)
     users_deposits_ctrl = UsersDepositsController(users_deposits_repo)
     users_cards_ctrl = UsersCardsController(users_cards_repo)
     users_credentials_ctrl = UsersCredentialsController(users_credentials_repo)
 
     app_ctrl = AppController(users_ctrl, users_account_ctrl, users_transactions_ctrl,currencies_ctrl, users_deposits_ctrl, users_cards_ctrl, users_credentials_ctrl)
app_ctrl.py

from application.Controller.currencies_ctrl import CurrenciesController
from application.Controller.users import UsersController
from application.Controller.users_accounts import UsersAccountsController
from application.Controller.users_cards import UsersCardsController
from application.Controller.users_credentials import UsersCredentialsController
from application.Controller.users_deposits_ctrl import UsersDepositsController
from application.Controller.users_transactions import UsersTransactionsController
from application.Model.Repository.currencies import DBCurrenciesRepository
from application.Model.Repository.users import DBUsersRepository
from application.Model.Repository.users_accounts import DBUsersAccountsRepository
from application.Model.Repository.users_cards import DBUsersCardsRepository
from application.Model.Repository.users_credentials import DBUsersCredentialsRepository
from application.Model.Repository.users_deposits import DBUsersDepositsRepository
from application.Model.Repository.users_transactions import DBUsersTransactionsRepository

def register_user(self, user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date, username, password):
    self.users_ctrl.create_user(user_id, first_name, last_name, email, address, phone_number, date_of_birth, join_date) 
    self.users_credentials_ctrl(username, password)

def get_username(self, username):
    return self.user_credentials_ctrl.get_username(username)

def get_user(self, user_id):
    return users_ctrl.get_user(user_id)
    

# def exchange(self, user_id, amount, from_currency, to_currency):
# Check balance in from_currency
# if self.
# Remove from user balance(from_currency)
# Get latest exchange rate from ExchangeRates table and apply it to the amount
# Add to user balance(to_currency)
if name == ‘main’:
users_repo = DBUsersRepository()
users_account_repo = DBUsersAccountsRepository()
users_transactions_repo = DBUsersTransactionsRepository()
currencies_repo = DBCurrenciesRepository()
users_deposits_repo = DBUsersDepositsRepository()
users_cards_repo = DBUsersCardsRepository()
users_credentials_repo = DBUsersCredentialsRepository()

users_ctrl = UsersController(users_repo)
users_accounts_ctrl = UsersAccountsController(users_account_repo)
users_transactions_ctrl = UsersTransactionsController(users_transactions_repo)
currencies_ctrl = CurrenciesController(currencies_repo)
users_deposits_ctrl = UsersDepositsController(users_deposits_repo)
users_cards_ctrl = UsersCardsController(users_cards_repo)
users_credentials_ctrl = UsersCredentialsController(users_credentials_repo)

app_ctrl = AppController(users_ctrl, users_accounts_ctrl, users_transactions_ctrl, c
Reply
#2
if you want to use the abbreviation, it must be defined.
I think that if you change line 5 in 1st listing to:
from application.Controller.app_ctrl import AppController as app_ctrl
you should be OK.

Or change line 26, 40, 47 and 68 to AppController...
example line 26:
verify_user_id = AppController.get_user(user_id)
but not both.
Reply
#3
I tried this
from application.Controller.app_ctrl import AppController as app_ctrl
and it required me another parameter
Error:
TypeError: get_user() missing 1 required positional argument: 'user_id'
. 'get_user' is a function in AppController class and have 2 parameter: self and user_id

(Jul-07-2022, 09:55 PM)Larz60+ Wrote: if you want to use the abbreviation, it must be defined.
I think that if you change line 5 in 1st listing to:
from application.Controller.app_ctrl import AppController as app_ctrl
you should be OK.

Or change line 26, 40, 47 and 68 to AppController...
example line 26:
verify_user_id = AppController.get_user(user_id)
but not both.
Reply
#4
user_id must be declared prior to use. Error is stating that hasn't been done.

'self' is used in the class definition of objects and methods of a class, used to represent the instance of a class.
is is not used when calling a method of the class.

clarification example:
class myclass:
    def __init__(self):
        self.counter = 0         # visible from class instances
        someothervariable = 45   # local use only
    
    def myFunction(self, name):
        self.counter += 1
        print(f"name: {name}, count: {self.counter}")


class mycode:
    def __init__(self):
        self.mc = myclass()
    
    def newfunction(self):
        self.mc.myFunction('James')     # calling method of class, do not pass self
        self.mc.myFunction('Roger')      # calling method of class, do not pass self

        print(f"I can also access counter directly: {self.mc.counter}")


def main():
    code = mycode()
    code.newfunction()


if __name__ == '__main__':
    main()
Output:
name: James, count: 1 name: Roger, count: 2 I can also access counter directly: 2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and DMX (aka, light controller) ScottAF 4 2,677 Apr-06-2023, 07:09 PM
Last Post: ScottAF
  Controller Buzzer as Distance Decreases barkster 6 2,100 Nov-01-2021, 03:26 PM
Last Post: barkster
  Auto re-pair / re-sync Controller via Script? User3000 2 2,346 Nov-30-2020, 11:42 AM
Last Post: User3000
  Tuning PID controller Sancho_Pansa 4 4,404 Nov-09-2020, 07:51 AM
Last Post: Sancho_Pansa
  Xbox Controller arki 0 1,726 Jun-30-2020, 10:32 AM
Last Post: arki
  Please help! Akai APC20 midi controller script versusliveact 0 2,145 Feb-14-2020, 05:37 AM
Last Post: versusliveact
  managing command codes for external controller box Oolongtea 0 1,920 Sep-19-2019, 08:32 AM
Last Post: Oolongtea
  Interfacing with a USB game controller barryjo 2 8,893 Oct-31-2016, 08:21 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020