Jul-07-2022, 07:31 PM
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
app_ctrl.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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 |