From d8cae10bd3b4493e6d5167e6e0e8a558048b1c54 Mon Sep 17 00:00:00 2001 From: Benature Date: Sat, 18 Apr 2020 15:10:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E4=B8=BA`.conf`=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 ++++--- README.md | 12 +++++++----- WordReview/__init__.py | 4 ++-- WordReview/settings.py | 40 +++++++++++++++++++++++----------------- config.py | 11 +++++++++++ config_sample.conf | 20 ++++++++++++++++++++ config_sample.py | 38 -------------------------------------- doc/install.md | 14 +++++++------- manage.py | 4 ++-- 9 files changed, 76 insertions(+), 74 deletions(-) create mode 100644 config.py create mode 100644 config_sample.conf delete mode 100644 config_sample.py diff --git a/.gitignore b/.gitignore index b1b2f162..ccac3e7c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,16 +14,17 @@ logs/ /media/ # config -/config.py +/config.conf # PyInstaller /build/ /dist/ -/*.spec +*.spec # local data/ *.sqlite3 /backup/ /pypi/ -WordReview.wiki/ \ No newline at end of file +WordReview.wiki/ +*_ig.* \ No newline at end of file diff --git a/README.md b/README.md index b19ff10e..58336b4d 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,24 @@ - + # Word Review 单词复习 -![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/Benature/WordReview) ![GitHub stars](https://img.shields.io/github/stars/Benature/WordReview?style=flat) ![GitHub stars](https://img.shields.io/github/forks/Benature/WordReview?style=flat) ![GitHub issues](https://img.shields.io/github/issues/Benature/WordReview) ![GitHub closed issues](https://img.shields.io/github/issues-closed/Benature/WordReview) +![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/Benature/WordReview) ![GitHub](https://img.shields.io/github/license/Benature/WordReview) Django + MySQL + Pug + JS - Python 3.7+ - Django 3 -- Mysql 8 +- Mysql 8 / sqlite 3 -简单录了一个 DEMO 视频,上传到了[B站](https://www.bilibili.com/video/av90579311/),欢迎康康。 +二月的时候简单录了一个 DEMO 视频,上传到了[微信公众号](https://mp.weixin.qq.com/s/3VjiflLdzKv0sjxm1noxDw)和[B站](https://www.bilibili.com/video/av90579311/)(BV 号:**BV1q7411w7r9**),欢迎康康。 还有一个[在线试玩](https://benature.github.io/WordReview/),可以先感受一下。 +>在线体验的版本对应`master`分支,现在默认显示的是`ben`分支(开发分支) 如果觉得还不错的话,不如给个 stars🌟呗( ̄▽ ̄)~* @@ -155,7 +156,7 @@ tain ### List 的记忆率 - 蓝条:历史记忆率,对 List 内单词的总记忆率取平均 -- 绿条:上轮记忆率,按 List 内单词的 **最新两次** 记忆情况就散平均记忆率 +- 绿条:上轮记忆率,按 List 内单词的 **最新两次** 记忆情况计算平均记忆率 ### 单词的`flag` @@ -170,6 +171,7 @@ tain ### 更新日志 - 2020.04 + - update: 配置文件更换为`.conf`文件(以支持默认参数) @04-18 - feature: 命令行启动后自动打开浏览器 ([Issue#4](https://github.com/Benature/WordReview/issues/4)) @04-16 - feature: 新增[在线预览](https://benature.github.io/WordReview/) @04-16 - feature: 对的后端爬虫 API ([Issue#2](https://github.com/Benature/WordReview/issues/2)) @04-15 diff --git a/WordReview/__init__.py b/WordReview/__init__.py index 6147e734..bcd18a61 100644 --- a/WordReview/__init__.py +++ b/WordReview/__init__.py @@ -1,5 +1,5 @@ -import config -if config.database_type == 'mysql': +from config import config +if config.get('custom', 'db_type') == 'mysql': import pymysql pymysql.install_as_MySQLdb() # import pymysql diff --git a/WordReview/settings.py b/WordReview/settings.py index 6db94886..fcfee67b 100644 --- a/WordReview/settings.py +++ b/WordReview/settings.py @@ -12,7 +12,7 @@ import os import sys -import config +from config import config # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -89,22 +89,28 @@ # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases -DATABASES = config.DATABASES -# { -# # 'default': { -# # 'ENGINE': 'django.db.backends.sqlite3', -# # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), -# # } -# 'default': { -# 'ENGINE': 'django.db.backends.mysql', -# 'NAME': 'tg_word_db', # database name -# 'USER': 'tg_word_user', # user name -# 'PASSWORD': 'tg_word2020', # user pwd -# 'HOST': 'localhost', # '116.62.12.178', -# 'PORT': '', -# } -# } - +db_type = config.get('custom', 'db_type') +if db_type == 'mysql': + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': config.get('mysql', 'mysql_name', fallback='word_db'), + 'USER': config.get('mysql', 'mysql_user', fallback='word_user'), + 'PASSWORD': config.get('mysql', 'mysql_password', fallback='word2020'), + 'HOST': config.get('mysql', 'mysql_host', fallback='localhost'), + 'PORT': config.get('mysql', 'mysql_port', fallback=''), + 'OPTIONS': {'charset': 'utf8mb4'}, + } + } +elif db_type == 'sqlite': + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } + } +else: + raise ValueError(f'请选择正确的数据库:`mysql`、`sqlite`,而非{db_type}') # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators diff --git a/config.py b/config.py new file mode 100644 index 00000000..e991b13b --- /dev/null +++ b/config.py @@ -0,0 +1,11 @@ +__all__ = ['config'] + +import configparser + +default_dict = { + 'db_type': 'sqlite', + 'auto_open_browser': 'yes', +} + +config = configparser.ConfigParser(default_dict) +config.read('config.conf') diff --git a/config_sample.conf b/config_sample.conf new file mode 100644 index 00000000..ef2e427e --- /dev/null +++ b/config_sample.conf @@ -0,0 +1,20 @@ +# ====================================================== +# 用户自定义配置区 +# ====================================================== +[custom] +# 是否自动打开浏览器 +auto_open_browser = yes + +# 使用数据库类型:`sqlite`(默认)、`mysql` +db_type = sqlite + +# ====================================================== +# 数据库使用配置 +# (除非你知道你在干嘛,否则请勿修改下面代码) +# ====================================================== +[mysql] +mysql_name = 'word_db' +mysql_user = 'word_user' +mysql_password = 'word2020' +mysql_host = 'localhost' +mysql_port = '' \ No newline at end of file diff --git a/config_sample.py b/config_sample.py deleted file mode 100644 index ff857b4d..00000000 --- a/config_sample.py +++ /dev/null @@ -1,38 +0,0 @@ -import os - -# ====================================================== -# 用户自定义配置区 -# ====================================================== - -# 使用数据库类型:`mysql`、`sqlite` -database_type = 'sqlite' - -auto_open_browser = True - -# ====================================================== -# 数据库使用配置 -# (除非你知道你在干嘛,否则请勿修改下面代码) -# ====================================================== - -if database_type == 'mysql': - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'tg_word_db', - 'USER': 'tg_word_user', - 'PASSWORD': 'tg_word2020', - 'HOST': 'localhost', - 'PORT': '', - 'OPTIONS': {'charset': 'utf8mb4'}, - } - } -elif database_type == 'sqlite': - BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } - } -else: - raise ValueError(f'请选择正确的数据库:`mysql`、`sqlite`,而非{database_type}') diff --git a/doc/install.md b/doc/install.md index 96bebda3..34868d0c 100644 --- a/doc/install.md +++ b/doc/install.md @@ -85,14 +85,14 @@ pip: command not found `config.py`文件下,找到下面这个变量,定义为`sqlite`。(默认就是这个,一般不用动了) -```python +```conf # 使用数据库类型:`mysql`、`sqlite` -database_type = 'sqlite' +db_type = sqlite ``` ->不过我是用 MySQL 的,如果想直接操作数据库的话,主要靠你自己百度的,你来问我我也是去百度的。 ->当然,只要你操作正常,一般没必要去直接操作数据库的。 ->再其实,就算要直接操作数据库,也可以借助 GUI 工具,工具有哪些可以[自己找找看](https://www.bing.com/search?q=sqlite+GUI)。 + +>如果需要直接操作数据库,可以借助 GUI 工具,工具有哪些可以[在这里找找看](https://www.bing.com/search?q=sqlite+GUI)。 ## 3.2. 选择二:MySQL @@ -182,9 +182,9 @@ flush privileges; -- 刷新系统权限表 `config.py`文件下,找到下面这个变量,定义为`mysql`。(默认就是这个,一般不用动了) -```python +```conf # 使用数据库类型:`mysql`、`sqlite` -database_type = 'mysql' +db_type = mysql ``` diff --git a/manage.py b/manage.py index 0f9ce54b..bade5d4f 100755 --- a/manage.py +++ b/manage.py @@ -2,7 +2,7 @@ """Django's command-line utility for administrative tasks.""" import os import sys -from config import auto_open_browser +from config import config BASE_DIR = os.path.abspath(__file__) sys.path.append(os.path.join(BASE_DIR, "pypi")) @@ -74,7 +74,7 @@ def main(): "forget to activate a virtual environment?" ) from exc - if Is_child_processing() and auto_open_browser: + if Is_child_processing() and config.getboolean('custom', 'auto_open_browser'): import threading t = threading.Thread( target=enable_browser_with_delay, args=(sys.argv, 1))