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
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.pyfrom 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
