Skip to content

Commit b7f77b7

Browse files
committed
feature #24: simplify docker deploy steps
1 parent 0a1be8c commit b7f77b7

File tree

11 files changed

+104
-147
lines changed

11 files changed

+104
-147
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
config.py
21
.idea/
32
mongo_data/
43
pg96_data/

Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# image: weeklyreport:<yymmdd>
1+
# image: weeklyreport:0.2
22
FROM centos:7
33
MAINTAINER CodingCrush
44
ENV LANG en_US.UTF-8
@@ -16,11 +16,12 @@ WORKDIR /opt/weeklyreport
1616

1717

1818
# Start wp-server container
19-
# docker run \
20-
# -d --restart=always \
19+
20+
# docker run -d
21+
# --restart=unless-stopped \
2122
# --name weeklyreport-server \
22-
# --net='host' \
23+
# --net=host \
2324
# -v /etc/localtime:/etc/localtime:ro \
2425
# -v $PWD:/opt/weeklyreport \
25-
# weeklyreport:<yymmdd> \
26-
# gunicorn wsgi:app --bind <host>:<port> -w <N> --log-file logs/awsgi.log --log-level=DEBUG
26+
# weeklyreport:0.2 \
27+
# gunicorn wsgi:app --bind 0.0.0.0:8000 -w 2 --log-file logs/awsgi.log --log-level=DEBUG

app/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from flask_mail import Mail
77
from flask_login import LoginManager
88
from flask_sqlalchemy import SQLAlchemy
9-
from config import config
109
from .json_encoder import JSONEncoder
1110

1211

@@ -30,10 +29,9 @@ def get_locale():
3029
return 'zh_Hans_CN'
3130

3231

33-
def create_app(config_name):
32+
def create_app(config_file):
3433

35-
app.config.from_object(config[config_name])
36-
config[config_name].init_app(app)
34+
app.config.from_pyfile(config_file)
3735

3836
mail.init_app(app)
3937
bootstrap.init_app(app)

app/auth/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ def register():
4040
(str(dept.id), dept.name) for dept in Department.query.all()]
4141

4242
if form.validate_on_submit():
43+
exist_users = bool(User.query.all())
44+
4345
user = User(
4446
email=form.email.data,
4547
username=form.username.data,
4648
password=form.password.data,
4749
role=Role.query.filter_by(name='EMPLOYEE').first(),
4850
department=Department.query.get(form.department.data))
4951

50-
if user.email == current_app.config['FLASK_ADMIN_EMAIL']:
52+
if not exist_users:
5153
user.role = Role.query.filter_by(name='ADMINISTRATOR').first()
54+
user.is_super_admin = True
5255
user.is_ignored = True
5356

5457
db.session.add(user)

app/main/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010
from .. import admin, db
1111
from ..models import Permission, User, Role, Report, Department
1212
from ..utils import permission_required, is_allowed_file
13+
from sqlalchemy.exc import OperationalError
1314

1415

1516
@main.route('/', methods=['GET', 'POST'])
1617
def index():
18+
# check if the database is initialized.
19+
try:
20+
User.query.all()
21+
except OperationalError:
22+
db.create_all()
23+
Role.insert_roles()
24+
Department.insert_departments()
25+
1726
if not current_user.is_authenticated:
1827
return redirect(url_for('auth.login'))
1928
return redirect(url_for('report.read'))

app/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from datetime import datetime
22
from werkzeug.security import generate_password_hash, check_password_hash
3-
from . import db, login_manager
43
from flask import current_app
54
from flask_login import UserMixin, AnonymousUserMixin
65
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
6+
from . import db, login_manager
77
from .utils import get_week_count, get_last_week
88

99

@@ -74,6 +74,7 @@ class User(db.Model, UserMixin):
7474
is_ignored = db.Column(db.Boolean, default=False)
7575
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
7676
department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
77+
is_super_admin = db.Column(db.Boolean, default=False)
7778

7879
@property
7980
def password(self):
@@ -108,8 +109,7 @@ def reset_password(self, token, new_password):
108109

109110
@property
110111
def is_admin(self):
111-
return self.email == current_app.config['FLASK_ADMIN_EMAIL'] or \
112-
self.role.name == 'ADMINISTRATOR'
112+
return self.role.name == 'ADMINISTRATOR' or self.is_super_admin
113113

114114
@property
115115
def is_hr(self):

app/utils.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from functools import wraps
3-
from flask import abort, current_app
3+
from flask import abort
44
from flask_login import current_user
55

66

@@ -16,23 +16,6 @@ def decorated_function(*args, **kwargs):
1616
return decorator
1717

1818

19-
def email_check(email_address):
20-
def decorator(f):
21-
@wraps(f)
22-
def decorated_function(*args, **kwargs):
23-
if not current_user.email == email_address:
24-
abort(403)
25-
return f(*args, **kwargs)
26-
return decorated_function
27-
return decorator
28-
29-
30-
def admin_required(f):
31-
from .models import Permission
32-
return email_check(current_app.config['FLASK_ADMIN_EMAIL'])(f) or \
33-
permission_required(Permission.ADMINISTER)(f)
34-
35-
3619
def get_week_count(at=datetime.datetime.now()):
3720
return at.isocalendar()[1]
3821

config.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import os
2+
3+
4+
base_dir = os.path.dirname(os.path.realpath(__file__))
5+
6+
DEBUG = True
7+
8+
SECRET_KEY = os.environ.get('SECRET_KEY') or 'nobody knows the password'
9+
PER_PAGE = 10
10+
11+
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
12+
SQLALCHEMY_TRACK_MODIFICATIONS = False
13+
SQLALCHEMY_RECORD_QUERIES = True
14+
15+
IMAGE_UPLOAD_DIR = 'static/upload/'
16+
UPLOAD_FOLDER = os.path.join(base_dir, 'app/static/upload/')
17+
18+
MAIL_SERVER = 'smtp.163.com'
19+
MAIL_PORT = 465
20+
MAIL_USE_SSL = True
21+
MAIL_USERNAME = '<EMAIL@ADDRESS>'
22+
MAIL_PASSWORD = '<EMAIL_PASSWORD>'
23+
24+
WR_MAIL_SUBJECT_PREFIX = '[WeeklyReport]'
25+
WR_MAIL_SENDER = 'WeeklyReport <[email protected]>'
26+
27+
28+
DEPARTMENTS = (
29+
'人事行政部',
30+
'软件测试部',
31+
'产品开发部',
32+
'新技术研发部'
33+
)
34+
35+
DEFAULT_CONTENT = "<p><strong>本周工作内容:</strong></p><ol><li></li></ol>" \
36+
"<p>&nbsp;<strong>下周计划:</strong></p><ol><li></li></ol>"
37+
38+
39+
# SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:postgres@localhost/wr_prd'
40+
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(base_dir, 'wr_prd.sqlite')

config.py.sample

Lines changed: 0 additions & 75 deletions
This file was deleted.

manage.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

wsgi.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
1-
from app import create_app
1+
from app import create_app, db
2+
from app.models import Role, Department, Report
3+
from flask_migrate import Migrate, MigrateCommand
4+
from flask_script import Manager, Shell
25
import os
36

47

5-
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
8+
config_file = os.path.join(
9+
os.path.dirname(os.path.realpath(__file__)), 'config.py')
10+
11+
app = create_app(config_file)
12+
613

714
if __name__ == '__main__':
8-
app.run()
15+
manager = Manager(app)
16+
migrate = Migrate(app, db)
17+
18+
19+
def make_shell_context():
20+
return dict(app=app, db=db, Role=Role,
21+
Department=Department, Report=Report)
22+
23+
24+
manager.add_command("shell", Shell(make_context=make_shell_context))
25+
manager.add_command('db', MigrateCommand)
26+
27+
28+
@manager.command
29+
def profile(length=25, profile_dir=None):
30+
"""Start the application under the code profiler."""
31+
from werkzeug.contrib.profiler import ProfilerMiddleware
32+
app.wsgi_app = ProfilerMiddleware(
33+
app.wsgi_app, restrictions=[length], profile_dir=profile_dir)
34+
app.run()
35+
36+
37+
@manager.command
38+
def deploy():
39+
db.create_all()
40+
Role.insert_roles()
41+
Department.insert_departments()
42+
43+
manager.run()

0 commit comments

Comments
 (0)