I am currently working with a flask application and am trying to send out emails once the user has registered with the site. I am having difficulties with circular imports between the main.py where the app is instantiated and the data_inserts.py where the data is committed to the db and a password is emailed back to the user. For the email functionality, I use Flask-mail extension. The error I get is given below:
ImportError: Cannot import name from 'DataInserts' from relevant_folder.data_inserts
Given below are the details:
main.py:
from relevant_folder.data_inserts import DataInserts
from flask import Flask
from flask_mail import Mail
from conf.mail_settings.py import mail_settings
app = Flask(__name__)
app.config.update[mail_settings]
mail = Mail(app)
@app.route("/register")
def register():
params = request.json
DataInserts.add_user(params)
relevant_folder.data_inserts.py:
from main import app
from main.app import mail
from flask_mail import message
class DataInserts():
def add_user(self, new_user_json):
''' add user name and email to db logic goes here'''
msg = Message(subject="Subject",
sender=app.config.get("MAIL_USERNAME"),
recipients=[new_user_json["email"]],
body="Hello " + new_user_json["name"] + ", your password is password")
mail.send(msg)
I feel I have not structured my application properly. Any help greatly appreciated