Skip to content

Commit e099dba

Browse files
authored
Merge pull request #68 from TaleLin/fix/file_upload
Fix/file upload
2 parents e3e9d5f + 866b014 commit e099dba

10 files changed

Lines changed: 101 additions & 31 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ __pycache__
66
*.pytest_cache/
77
*.db
88
*.pyc
9-
*.cpython-36.pyc
9+
*.cpython-36.pyc
10+
app/assets/*

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Flask = "==1.0.2"
1212
Flask-SQLAlchemy = "==2.3.2"
1313
Flask-WTF = "==0.14.2"
1414
Flask-Cors = "==2.1.0"
15-
Lin-CMS = "==0.1.1b4"
15+
Lin-CMS = "==0.2.0b1"
1616

1717
[dev-packages]
1818
pytest = "*"

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ Lin-CMS 是林间有风团队经过大量项目实践所提炼出的一套**内
3535

3636
## 最新版本
3737

38-
核心库:0.1.1b4
38+
核心库:0.2.0b1
3939

40-
示例工程:0.1.0-beta.3
40+
示例工程:0.2.0-beta.1
4141

4242

4343
### 文档地址
@@ -174,3 +174,9 @@ pipenv shell
174174
“心上无垢,林间有风"
175175

176176
这证明你已经成功的将 Lin 运行起来了,Congratulations!
177+
178+
## 下个版本开发计划
179+
180+
- [x] 系统访问日志、错误日志
181+
- [ ] 完善文档
182+
- [ ] 重构核心库结构

app/api/cms/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def set_avatar():
143143
form = AvatarUpdateForm().validate_for_api()
144144
user = get_current_user()
145145
with db.auto_commit():
146-
user.avatar = form.avatar.data
146+
user._avatar = form.avatar.data
147147
return Success(msg='更新头像成功')
148148

149149

app/app.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ def log_response(resp):
6262
return resp
6363

6464

65-
def create_app(register_all=True):
65+
def create_app(register_all=True, environment='production'):
6666
app = Flask(__name__, static_folder='./assets')
67-
app.config.from_object('app.config.setting')
68-
app.config.from_object('app.config.secure')
67+
app.config['ENV'] = environment
68+
env = app.config.get('ENV')
69+
if env == 'production':
70+
app.config.from_object('app.config.setting.ProductionConfig')
71+
app.config.from_object('app.config.secure.ProductionSecure')
72+
elif env == 'development':
73+
app.config.from_object('app.config.setting.DevelopmentConfig')
74+
app.config.from_object('app.config.secure.DevelopmentSecure')
6975
app.config.from_object('app.config.log')
7076
if register_all:
7177
register_blueprints(app)

app/config/secure.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
"""
55

66
# 安全性配置
7-
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
7+
from app.config.setting import BaseConfig
88

9-
SQLALCHEMY_ECHO = False
109

11-
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'
10+
class DevelopmentSecure(BaseConfig):
11+
"""
12+
开发环境安全性配置
13+
"""
14+
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
15+
16+
SQLALCHEMY_ECHO = False
17+
18+
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'
19+
20+
21+
class ProductionSecure(BaseConfig):
22+
"""
23+
生产环境安全性配置
24+
"""
25+
SQLALCHEMY_DATABASE_URI = 'mysql+cymysql://root:123456@localhost:3306/lin-cms'
26+
27+
SQLALCHEMY_ECHO = False
28+
29+
SECRET_KEY = '\x88W\xf09\x91\x07\x98\x89\x87\x96\xa0A\xc68\xf9\xecJJU\x17\xc5V\xbe\x8b\xef\xd7\xd8\xd3\xe6\x95*4'

app/config/setting.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,54 @@
55

66
from datetime import timedelta
77

8-
# 分页配置
9-
COUNT_DEFAULT = 10
10-
PAGE_DEFAULT = 0
11-
12-
# 令牌配置
13-
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
14-
15-
# 屏蔽 sql alchemy 的 FSADeprecationWarning
16-
SQLALCHEMY_TRACK_MODIFICATIONS = False
17-
18-
# 插件模块暂时没有开启,以下配置可忽略
19-
# plugin config写在字典里面
20-
PLUGIN_PATH = {
21-
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
22-
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete', 'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com', 'bucket_name': 'not complete', 'upload_folder': 'app', 'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
23-
}
8+
9+
class BaseConfig(object):
10+
"""
11+
基础配置
12+
"""
13+
# 分页配置
14+
COUNT_DEFAULT = 10
15+
PAGE_DEFAULT = 0
16+
17+
# 屏蔽 sql alchemy 的 FSADeprecationWarning
18+
SQLALCHEMY_TRACK_MODIFICATIONS = False
19+
20+
21+
class DevelopmentConfig(BaseConfig):
22+
"""
23+
开发环境普通配置
24+
"""
25+
DEBUG = True
26+
27+
# 令牌配置
28+
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
29+
30+
# 插件模块暂时没有开启,以下配置可忽略
31+
# plugin config写在字典里面
32+
PLUGIN_PATH = {
33+
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
34+
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete',
35+
'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com',
36+
'bucket_name': 'not complete', 'upload_folder': 'app',
37+
'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
38+
}
39+
40+
41+
class ProductionConfig(BaseConfig):
42+
"""
43+
生产环境普通配置
44+
"""
45+
DEBUG = False
46+
47+
# 令牌配置
48+
JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=1)
49+
50+
# 插件模块暂时没有开启,以下配置可忽略
51+
# plugin config写在字典里面
52+
PLUGIN_PATH = {
53+
'poem': {'path': 'app.plugins.poem', 'enable': True, 'version': '0.0.1', 'limit': 20},
54+
'oss': {'path': 'app.plugins.oss', 'enable': True, 'version': '0.0.1', 'access_key_id': 'not complete',
55+
'access_key_secret': 'not complete', 'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com',
56+
'bucket_name': 'not complete', 'upload_folder': 'app',
57+
'allowed_extensions': ['jpg', 'gif', 'png', 'bmp']}
58+
}

app/extensions/file/local_uploader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from flask import current_app
24
from werkzeug.utils import secure_filename
35

@@ -20,7 +22,8 @@ def upload(self):
2022
ret.append({
2123
"key": single.name,
2224
"id": exists.id,
23-
"url": site_domain + '/assets/' + exists.path
25+
"path": exists.path,
26+
"url": site_domain + os.path.join(current_app.static_url_path, exists.path)
2427
})
2528
else:
2629
absolute_path, relative_path, real_name = self._get_store_path(single.filename)
@@ -37,6 +40,7 @@ def upload(self):
3740
ret.append({
3841
"key": single.name,
3942
"id": file.id,
40-
"url": site_domain + '/assets/' + file.path
43+
"path": file.path,
44+
"url": site_domain + os.path.join(current_app.static_url_path, file.path)
4145
})
4246
return ret

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Flask-WTF==0.14.2
1010
idna==2.6
1111
itsdangerous==1.1.0
1212
Jinja2==2.10
13-
Lin-CMS==0.1.1b4
13+
Lin-CMS==0.2.0b1
1414
MarkupSafe==1.1.1
1515
pipfile==0.0.2
1616
PyJWT==1.7.1

starter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from app.app import create_app
77

8-
app = create_app()
8+
app = create_app(environment='development')
99

1010

1111
@app.route('/', methods=['GET'], strict_slashes=False)

0 commit comments

Comments
 (0)