臺灣言語平臺是母語問答系統的後端主機程式。前端網頁藉由GET/POST,將資料傳來後端主機,主機並以json的格式回傳。
virtualenv venv --python python3 # 設置環境檔
sudo apt-get install -y python3 python-virtualenv g++ python3-dev zlib1g-dev libbz2-dev liblzma-dev libboost-all-dev libyaml-dev libxslt1-dev libav-tools libmp3lame0 libavcodec-extra-* # 安裝資料庫的套件 for Ubuntu
sudo apt-get install -y libffi-dev rabbitmq-server # 為了連google oauth2, message queue
. venv/bin/activate # 載入環境
pip install tai5-uan5_gian5-gi2_phing5-tai5
python manage.py migrate #建立資料庫欄位
除了自己臺灣言語平臺
的設定外,還需要設定cors的套件django-cors-headers
佮django-allauth
。
在setting.py
最後加上
TIME_ZONE = 'Asia/Taipei'
# 套件設定
INSTALLED_APPS += (
'臺灣言語資料庫',
'臺灣言語平臺',
)
MOTHER_TONGUE = '臺灣語言'
FOREIGN_LANGUAGE = '華語'
# 使用者上傳檔案
MEDIA_ROOT = os.path.join(BASE_DIR, "資料庫影音檔案")
MEDIA_URL = '/影音檔案/'
# django-cors-headers
CORS_ORIGIN_REGEX_WHITELIST = ('^.*$', )
CORS_ALLOW_CREDENTIALS = True
INSTALLED_APPS += (
'corsheaders',
)
MIDDLEWARE_CLASSES += (
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
# django-allauth,佮使用者有關係
AUTH_USER_MODEL = '臺灣言語平臺.使用者表'
ACCOUNT_ADAPTER = '臺灣言語平臺.使用者模型.使用者一般接口'
SOCIALACCOUNT_ADAPTER = '臺灣言語平臺.使用者模型.使用者社群接口'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
SITE_ID = 1
INSTALLED_APPS += (
# The Django sites framework is required for allauth
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
)
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend",
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Required by allauth template tags
'django.contrib.auth.context_processors.auth',
# `allauth` needs this from django
'django.template.context_processors.request',
],
},
},
]
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
'SCOPE': ['email', ],
'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
'METHOD': 'js_sdk',
'LOCALE_FUNC': lambda request: 'zh_TW',
'VERIFIED_EMAIL': False,
'VERSION': 'v2.3',
}
}
# celery
# For better celery performance
CELERY_IGNORE_RESULT = True
CELERY_DISABLE_RATE_LIMITS = True
# Only accept json for safety and upcoming celery version default setting
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
from celery.schedules import crontab
CELERY_TIMEZONE = TIME_ZONE
CELERYBEAT_SCHEDULE = {
'半瞑自sheets掠轉資料庫': {
'task': '臺灣言語平臺.tasks.半瞑自sheets掠轉資料庫',
'schedule': crontab(hour=3),
'args': ()
},
}
urls.py
要加django-allauth
、臺灣言語平臺
佮影音檔案
的路徑,網頁管理介面admin
可以需要更改
urlpatterns = [
url(r'^', include('臺灣言語平臺.網址')),
url(r'^accounts/', include('allauth.urls')),
url(r'^影音檔案/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^admin/', include(admin.site.urls)),
]
需同時開django
、celery worker
跟celery beat
三個服務,可用screen
python manage.py runserver
開啟celery worker
,負責加資料到Google Sheets
celery -A itaigi worker -l info
開啟celery beat
,負責半夜從Google Sheets把正規化的資料抓回來
celery -A itaigi beat -l info
可以參考gunicorn來啟動django
,取代runserver
請看各專案說定,或參考臺灣言語資料庫的資料匯入。
python manage.py createsuperuser
email
和密碼
隨意輸入
- 用瀏覽器進入 /admin
- 輸入剛剛的
email
和密碼
- social application
- provider:FB
- id:590065061070994
- key:db4f3fa26d26890e720d17a83ff5a6fe
- 最後左下角choose all site
- 其他欄位隨便填
參考Obtain OAuth2 credentials from Google Developers Console
- 申請服務
- 開啟Drive API
- 用Service Account得到一個
服務帳戶json
檔案
python manage.py 加sheet的json 服務帳戶json 網址
python manage.py 顯示全部sheet狀態
Google Sheets
右上角的Share
Can edit
處輸入服務帳戶json
檔案裡的client_email
virtualenv venv --python python3 # 設置環境檔
. venv/bin/activate # 載入環境
pip install -r requirements.txt # 安裝套件
sudo apt-get install -y python3 python-virtualenv g++ python3-dev zlib1g-dev libbz2-dev liblzma-dev libboost-all-dev libyaml-dev libxslt1-dev libav-tools libmp3lame0 libavcodec-extra-* # 安裝資料庫的套件 for Ubuntu
sudo apt-get install -y libffi-dev # 為了連google oauth2
python manage.py behave
python manage.py test
python manage.py 整理全部sheet到資料庫