mirror of
https://github.com/Llloooggg/Dyxless.git
synced 2026-03-06 02:36:24 +03:00
Добавлена работа с email
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,3 +4,5 @@ __pycache__
|
|||||||
venv
|
venv
|
||||||
|
|
||||||
db.sqlite
|
db.sqlite
|
||||||
|
|
||||||
|
config.json
|
||||||
|
|||||||
@@ -1,19 +1,31 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
from flask_mail import Mail
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_login import LoginManager
|
from flask_login import LoginManager
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
mail = Mail()
|
||||||
|
|
||||||
|
with open("dyxless/config.json") as config_file:
|
||||||
|
config_data = json.load(config_file)
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask("__name__", template_folder="dyxless/templates")
|
app = Flask("__name__", template_folder="dyxless/templates")
|
||||||
|
|
||||||
app.config["SECRET_KEY"] = "monastyrka-says-helloworld"
|
main_settings = config_data["main_settings"]
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite"
|
app.config.update(main_settings)
|
||||||
|
|
||||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
db_settings = config_data["db_settings"]
|
||||||
|
app.config.update(db_settings)
|
||||||
|
|
||||||
|
mail_settings = config_data["mail_settings"]
|
||||||
|
app.config.update(mail_settings)
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
mail.init_app(app)
|
||||||
|
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
login_manager.login_view = "auth.login"
|
login_manager.login_view = "auth.login"
|
||||||
@@ -33,4 +45,8 @@ def create_app():
|
|||||||
|
|
||||||
app.register_blueprint(main_blueprint)
|
app.register_blueprint(main_blueprint)
|
||||||
|
|
||||||
|
from .mails import mails as mails_blueprint
|
||||||
|
|
||||||
|
app.register_blueprint(mails_blueprint)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
17
dyxless/config.json_template
Normal file
17
dyxless/config.json_template
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"main_settings": {
|
||||||
|
"SECRET_KEY": "some-very-secret-key",
|
||||||
|
"SQLALCHEMY_TRACK_MODIFICATIONS": false
|
||||||
|
},
|
||||||
|
"db_settings": {
|
||||||
|
"SQLALCHEMY_DATABASE_URI": "sqlite:///db.sqlite"
|
||||||
|
},
|
||||||
|
"mail_settings": {
|
||||||
|
"MAIL_SERVER": "smtp.gmail.com",
|
||||||
|
"MAIL_PORT": 465,
|
||||||
|
"MAIL_USE_TLS": false,
|
||||||
|
"MAIL_USE_SSL": true,
|
||||||
|
"MAIL_USERNAME": "yourmail@gmail.com",
|
||||||
|
"MAIL_PASSWORD": "yourpass"
|
||||||
|
}
|
||||||
|
}
|
||||||
9
dyxless/decorators.py
Normal file
9
dyxless/decorators.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
|
def async_work(f):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
thr = Thread(target=f, args=args, kwargs=kwargs)
|
||||||
|
thr.start()
|
||||||
|
|
||||||
|
return wrapper
|
||||||
25
dyxless/mails.py
Normal file
25
dyxless/mails.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
from flask import Blueprint
|
||||||
|
from flask_mail import Message
|
||||||
|
|
||||||
|
from . import mail
|
||||||
|
from .decorators import async_work
|
||||||
|
|
||||||
|
mails = Blueprint("mails", __name__)
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_msg(subject, sender, recipients, text_body, html_body):
|
||||||
|
msg = Message(subject, sender=sender, recipients=recipients)
|
||||||
|
msg.body = text_body
|
||||||
|
msg.html = html_body
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
@async_work
|
||||||
|
def send_async_email(subject, sender, recipients, text_body, html_body):
|
||||||
|
msg = prepare_msg(subject, sender, recipients, text_body, html_body)
|
||||||
|
mail.send(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def send_mail(subject, sender, recipients, text_body, html_body):
|
||||||
|
msg = prepare_msg(subject, sender, recipients, text_body, html_body)
|
||||||
|
mail.send(msg)
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>Flask Auth Example</title>
|
<title>Dyxless</title>
|
||||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
blinker==1.4
|
||||||
click==8.0.1
|
click==8.0.1
|
||||||
colorama==0.4.4
|
colorama==0.4.4
|
||||||
Flask==2.0.1
|
Flask==2.0.1
|
||||||
Flask-Login==0.5.0
|
Flask-Login==0.5.0
|
||||||
|
Flask-Mail==0.9.1
|
||||||
Flask-SQLAlchemy==2.5.1
|
Flask-SQLAlchemy==2.5.1
|
||||||
greenlet==1.1.0
|
greenlet==1.1.0
|
||||||
itsdangerous==2.0.1
|
itsdangerous==2.0.1
|
||||||
|
|||||||
Reference in New Issue
Block a user