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


Messages In This Thread
How can flask find the controller? - by 3lnyn0 - Jul-07-2022, 07:31 PM
RE: How can flask find the controller? - by Larz60+ - Jul-07-2022, 09:55 PM
RE: How can flask find the controller? - by 3lnyn0 - Jul-08-2022, 06:27 AM
RE: How can flask find the controller? - by Larz60+ - Jul-08-2022, 10:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and DMX (aka, light controller) ScottAF 4 2,722 Apr-06-2023, 07:09 PM
Last Post: ScottAF
  Controller Buzzer as Distance Decreases barkster 6 2,132 Nov-01-2021, 03:26 PM
Last Post: barkster
  Auto re-pair / re-sync Controller via Script? User3000 2 2,373 Nov-30-2020, 11:42 AM
Last Post: User3000
  Tuning PID controller Sancho_Pansa 4 4,451 Nov-09-2020, 07:51 AM
Last Post: Sancho_Pansa
  Xbox Controller arki 0 1,735 Jun-30-2020, 10:32 AM
Last Post: arki
  Please help! Akai APC20 midi controller script versusliveact 0 2,158 Feb-14-2020, 05:37 AM
Last Post: versusliveact
  managing command codes for external controller box Oolongtea 0 1,939 Sep-19-2019, 08:32 AM
Last Post: Oolongtea
  Interfacing with a USB game controller barryjo 2 8,929 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