-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
169 lines (117 loc) · 4.58 KB
/
app.py
File metadata and controls
169 lines (117 loc) · 4.58 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, render_template, flash, redirect, url_for
from config import Development
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_required
from flask_migrate import Migrate
from flask_moment import Moment
from flask_redis import Redis
from persiantools.jdatetime import JalaliDateTime
from flask_ckeditor import CKEditor
from flask_wtf.csrf import CSRFProtect
base_dir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config.from_object(Development)
# ================= CONFIGS_OF_LOGIN_MANAGER ==========================
login = LoginManager()
login.login_view = 'login'
login.login_message_category = 'info'
login.init_app(app)
# ====================================================================
db = SQLAlchemy(app)
migrate = Migrate(app, db, compare_type=True)
moment = Moment(app)
redis = Redis(app)
ckeditor = CKEditor(app)
csrf = CSRFProtect(app)
# ================= SET_GLOBAL_VARIABLES ==========================
@app.context_processor
def user_list():
# users = User.query.order_by(User.id.desc()).all()
users = User.query.order_by(User.created_at.desc()).all()
prime_users = []
active_users = []
for user in users:
if user.active == 1:
active_users.append(user)
if user.role == 1:
prime_users.append(user)
return dict(all_users=users, userslist=users, prime_users=prime_users, active_users=active_users)
# ================= END_OF_SET_GLOBAL_VARIABLES ==========================
@app.context_processor
def blog_funcs():
from blog.models import Comment, News
comments_not_confirm = Comment.query.filter_by(show=False).all()
all_news_draft = News.query.filter_by(draft=1).order_by(News.created_at.desc()).all()
return dict(comments_not_confirm=comments_not_confirm,all_news_draft=all_news_draft)
# @app.context_processor
# def show_category():
# from blog.models import Category
# categories = Category.query.all()
# return dict(categories=categories)
@login_required
@app.route('/')
def index():
return redirect(url_for('admin.index'))
# ================= ADD_BLUEPRINTS =============================
from auth import auth
from admin import admin
from blog import blog
from social import social
from quiz import quiz
from clinic import clinic
app.register_blueprint(auth)
app.register_blueprint(admin)
app.register_blueprint(blog)
app.register_blueprint(social)
app.register_blueprint(quiz)
app.register_blueprint(clinic)
# ==============================================================
# ================================ User Handler ==========================
from auth.models import User
@login.user_loader
def userLoader(user_id):
return User.query.get(user_id)
@login.unauthorized_handler
def unauthorized():
"""Redirect unauthorized users to Login page."""
flash('You must be logged in to view that page.', 'danger')
return redirect(url_for('auth.login'))
# ==========================================================================
@app.errorhandler(404)
def NotFound(error):
return render_template('404.html', error=error)
# @app.before_request
# def before_request_func():
# from blog.models import News
# news = News.query.filter_by(draft=1).all()
# from datetime import datetime
# for ns in news:
# if ns.published_at >= datetime.now():
# ns.draft = 0
# db.session.add(ns)
# db.session.commit()
# ================================ SCHEDULERING METHODS ============================================
def app_scheduler():
with app.app_context():
from blog.models import News
from datetime import datetime
all_news = News.query.filter_by(draft=1).all()
for news in all_news:
if news.published_at < datetime.today():
news.draft = 0
print(f'news by title {news.title} dont be draft from now')
db.session.add(news)
db.session.commit()
# print(datetime.today().strftime("%Y-%m-%d %H:%M:%S"))
# print('---------------- programmer : mosi ------------------------')
scheduler = BackgroundScheduler()
scheduler.add_job(func=app_scheduler, trigger="interval", seconds=60)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
# ====================================================================================================
if __name__ == '__main__':
app.run(debug=True)