![]() |
How can flask find the controller? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can flask find the controller? (/thread-37669.html) |
How can flask find the controller? - 3lnyn0 - Jul-07-2022 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 RE: How can flask find the controller? - Larz60+ - Jul-07-2022 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_ctrlyou 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. RE: How can flask find the controller? - 3lnyn0 - Jul-08-2022 I tried this from application.Controller.app_ctrl import AppController as app_ctrland it required me another parameter . '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. RE: How can flask find the controller? - Larz60+ - Jul-08-2022 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()
|