mysql config & cmd test
Showing
7 changed files
with
93 additions
and
28 deletions
1 | certifi==2016.2.28 | 1 | certifi==2016.2.28 |
2 | Django==2.1 | 2 | Django==2.1 |
3 | # django-mysqlpool @ https://github.com/smartfile/django-mysqlpool/archive/master.zip | ||
3 | djangorestframework==3.9.0 | 4 | djangorestframework==3.9.0 |
4 | djangorestframework-jwt==1.11.0 | 5 | djangorestframework-jwt==1.11.0 |
6 | marshmallow==3.6.1 | ||
5 | PyJWT==1.7.1 | 7 | PyJWT==1.7.1 |
6 | PyMuPDF==1.17.0 | 8 | PyMuPDF==1.17.0 |
9 | PyMySQL==0.9.3 | ||
7 | pytz==2020.1 | 10 | pytz==2020.1 |
8 | # simple-config @ http://gitlab.situdata.com/zhouweiqi/simple_config/repository/archive.tar.gz?ref=master | 11 | # simple-config @ http://gitlab.situdata.com/zhouweiqi/simple_config/repository/archive.tar.gz?ref=master |
9 | # situlogger @ http://gitlab.situdata.com/zhouweiqi/situlogger/repository/archive.tar.gz?ref=master | 12 | # situlogger @ http://gitlab.situdata.com/zhouweiqi/situlogger/repository/archive.tar.gz?ref=master |
10 | six==1.14.0 | 13 | six==1.14.0 |
14 | SQLAlchemy==0.9.10 | ||
15 | webargs==6.1.0 | ... | ... |
1 | http://gitlab.situdata.com/zhouweiqi/simple_config/repository/archive.tar.gz?ref=master | 1 | http://gitlab.situdata.com/zhouweiqi/simple_config/repository/archive.tar.gz?ref=master |
2 | http://gitlab.situdata.com/zhouweiqi/situlogger/repository/archive.tar.gz?ref=master | 2 | http://gitlab.situdata.com/zhouweiqi/situlogger/repository/archive.tar.gz?ref=master |
3 | https://github.com/smartfile/django-mysqlpool/archive/master.zip | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
src/apps/doc/management/commands/test_cmd.py
0 → 100644
... | @@ -3,7 +3,7 @@ from django.db import models | ... | @@ -3,7 +3,7 @@ from django.db import models |
3 | # Create your models here. | 3 | # Create your models here. |
4 | 4 | ||
5 | 5 | ||
6 | class Document(models.Model): | 6 | class UploadDocRecords(models.Model): |
7 | id = models.AutoField(primary_key=True, verbose_name="id") | 7 | id = models.AutoField(primary_key=True, verbose_name="id") |
8 | application_id = models.CharField(max_length=64, verbose_name="申请id") | 8 | application_id = models.CharField(max_length=64, verbose_name="申请id") |
9 | main_applicant = models.CharField(max_length=16, verbose_name="主申请人") | 9 | main_applicant = models.CharField(max_length=16, verbose_name="主申请人") |
... | @@ -21,5 +21,5 @@ class Document(models.Model): | ... | @@ -21,5 +21,5 @@ class Document(models.Model): |
21 | 21 | ||
22 | class Meta: | 22 | class Meta: |
23 | managed = False | 23 | managed = False |
24 | db_table = 'document' | 24 | db_table = 'upload_doc_records' |
25 | 25 | ... | ... |
... | @@ -3,7 +3,7 @@ from webargs import fields | ... | @@ -3,7 +3,7 @@ from webargs import fields |
3 | from webargs.djangoparser import use_args, parser | 3 | from webargs.djangoparser import use_args, parser |
4 | from common.mixins import GenericView | 4 | from common.mixins import GenericView |
5 | from common import response | 5 | from common import response |
6 | from .models import Document | 6 | from .models import UploadDocRecords |
7 | 7 | ||
8 | # Create your views here. | 8 | # Create your views here. |
9 | 9 | ||
... | @@ -14,16 +14,16 @@ def load_data(request, schema): | ... | @@ -14,16 +14,16 @@ def load_data(request, schema): |
14 | return request.data | 14 | return request.data |
15 | 15 | ||
16 | 16 | ||
17 | application_data = {'applicationId': fields.Str(required=True)} | 17 | application_data_args = {'applicationId': fields.Str(required=True)} |
18 | 18 | ||
19 | applicant_data = { | 19 | applicant_data_args = { |
20 | 'mainApplicantName': fields.Str(required=True), | 20 | 'mainApplicantName': fields.Str(required=True), |
21 | 'coApplicantName': fields.Str(required=True), | 21 | 'coApplicantName': fields.Str(required=True), |
22 | 'guarantor1Name': fields.Str(required=True), | 22 | 'guarantor1Name': fields.Str(required=True), |
23 | 'guarantor2Name': fields.Str(required=True), | 23 | 'guarantor2Name': fields.Str(required=True), |
24 | } | 24 | } |
25 | 25 | ||
26 | document = { | 26 | document_args = { |
27 | 'documentName': fields.Str(required=True), | 27 | 'documentName': fields.Str(required=True), |
28 | 'documentScheme': fields.Str(required=True), | 28 | 'documentScheme': fields.Str(required=True), |
29 | 'businessType': fields.Str(required=True), | 29 | 'businessType': fields.Str(required=True), |
... | @@ -32,10 +32,10 @@ document = { | ... | @@ -32,10 +32,10 @@ document = { |
32 | 'metadataVersionId': fields.Str(required=True), | 32 | 'metadataVersionId': fields.Str(required=True), |
33 | } | 33 | } |
34 | 34 | ||
35 | doc_upload = { | 35 | doc_upload_args = { |
36 | 'applicationData': fields.Nested(application_data, required=True), | 36 | 'applicationData': fields.Nested(application_data_args, required=True), |
37 | 'applicantData': fields.Nested(applicant_data, required=True), | 37 | 'applicantData': fields.Nested(applicant_data_args, required=True), |
38 | 'document': fields.Nested(document, required=True), | 38 | 'document': fields.Nested(document_args, required=True), |
39 | } | 39 | } |
40 | 40 | ||
41 | 41 | ||
... | @@ -43,20 +43,23 @@ class DocView(GenericView): | ... | @@ -43,20 +43,23 @@ class DocView(GenericView): |
43 | permission_classes = [] | 43 | permission_classes = [] |
44 | 44 | ||
45 | # 创建模型 | 45 | # 创建模型 |
46 | @use_args(doc_upload, location='data') | 46 | @use_args(doc_upload_args, location='data') |
47 | def post(self, request, args): | 47 | def post(self, request, args): |
48 | Document.objects.create( | 48 | application_data = args.get('applicationData') |
49 | application_id=args.get('applicationId'), | 49 | applicant_data = args.get('applicantData') |
50 | main_applicant=args.get('mainApplicantName'), | 50 | document = args.get('document') |
51 | co_applicant=args.get('coApplicantName'), | 51 | UploadDocRecords.objects.create( |
52 | guarantor_1=args.get('guarantor1Name'), | 52 | application_id=application_data.get('applicationId'), |
53 | guarantor_2=args.get('guarantor2Name'), | 53 | main_applicant=applicant_data.get('mainApplicantName'), |
54 | document_name=args.get('documentName'), | 54 | co_applicant=applicant_data.get('coApplicantName'), |
55 | document_scheme=args.get('documentScheme'), | 55 | guarantor_1=applicant_data.get('guarantor1Name'), |
56 | business_type=args.get('businessType'), | 56 | guarantor_2=applicant_data.get('guarantor2Name'), |
57 | data_source=args.get('dataSource'), | 57 | document_name=document.get('documentName'), |
58 | metadata_version_id=args.get('metadataVersionId'), | 58 | document_scheme=document.get('documentScheme'), |
59 | upload_finish_time=args.get('uploadFinishTime'), | 59 | business_type=document.get('businessType'), |
60 | data_source=document.get('dataSource'), | ||
61 | metadata_version_id=document.get('metadataVersionId'), | ||
62 | upload_finish_time=document.get('uploadFinishTime'), | ||
60 | ) | 63 | ) |
61 | self.running_log.info('[doc upload success] [args={0}]'.format(args)) | 64 | self.running_log.info('[doc upload success] [args={0}]'.format(args)) |
62 | return response.ok() | 65 | return response.ok() | ... | ... |
... | @@ -11,7 +11,7 @@ https://docs.djangoproject.com/en/2.1/ref/settings/ | ... | @@ -11,7 +11,7 @@ https://docs.djangoproject.com/en/2.1/ref/settings/ |
11 | """ | 11 | """ |
12 | 12 | ||
13 | import os | 13 | import os |
14 | from . import conf | 14 | from . import conf, database |
15 | from logging import config | 15 | from logging import config |
16 | 16 | ||
17 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | 17 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) |
... | @@ -42,6 +42,7 @@ INSTALLED_APPS = [ | ... | @@ -42,6 +42,7 @@ INSTALLED_APPS = [ |
42 | 'rest_framework', | 42 | 'rest_framework', |
43 | 'common', | 43 | 'common', |
44 | 'apps.account', | 44 | 'apps.account', |
45 | 'apps.doc', | ||
45 | ] | 46 | ] |
46 | 47 | ||
47 | MIDDLEWARE = [ | 48 | MIDDLEWARE = [ |
... | @@ -78,12 +79,18 @@ WSGI_APPLICATION = 'wsgi.application' | ... | @@ -78,12 +79,18 @@ WSGI_APPLICATION = 'wsgi.application' |
78 | # Database | 79 | # Database |
79 | # https://docs.djangoproject.com/en/2.1/ref/settings/#databases | 80 | # https://docs.djangoproject.com/en/2.1/ref/settings/#databases |
80 | 81 | ||
82 | # DATABASES = { | ||
83 | # 'default': { | ||
84 | # 'ENGINE': 'django.db.backends.sqlite3', | ||
85 | # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
86 | # } | ||
87 | # } | ||
88 | |||
81 | DATABASES = { | 89 | DATABASES = { |
82 | 'default': { | 90 | 'default': conf.get_namespace('MYSQL_') |
83 | 'ENGINE': 'django.db.backends.sqlite3', | ||
84 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
85 | } | ||
86 | } | 91 | } |
92 | DATABASE_ROUTERS = ['settings.database.DBRouter'] | ||
93 | MYSQLPOOL_ARGUMENTS = database.MYSQLPOOL_ARGUMENTS | ||
87 | 94 | ||
88 | 95 | ||
89 | # Password validation | 96 | # Password validation | ... | ... |
src/settings/database.py
0 → 100644
1 | """ | ||
2 | Database | ||
3 | https://docs.djangoproject.com/en/1.11/ref/settings/#databases | ||
4 | """ | ||
5 | import pymysql | ||
6 | from django.db.models import options | ||
7 | |||
8 | pymysql.install_as_MySQLdb() | ||
9 | |||
10 | # 同一App下的Model可能会存储到不同的db,所以根据app_label来做db_router不是特别合适 | ||
11 | # 所以添加situ_db_label来映射model到db | ||
12 | # 为Django的model的Meta添加额外的属性situ_db_label | ||
13 | options.DEFAULT_NAMES = tuple(list(options.DEFAULT_NAMES) + ['situ_db_label']) | ||
14 | |||
15 | # 数据库连接池配置 | ||
16 | MYSQLPOOL_ARGUMENTS = { | ||
17 | 'recycle': 30, | ||
18 | 'pool_size': 128, | ||
19 | 'max_overflow': 10, | ||
20 | 'timeout': 5, | ||
21 | 'use_threadlocal': True, | ||
22 | } | ||
23 | |||
24 | |||
25 | class DBRouter(object): | ||
26 | |||
27 | def db_for_read(self, model, **hints): | ||
28 | if hasattr(model._meta, 'situ_db_label'): | ||
29 | return model._meta.aft_db_label | ||
30 | return None | ||
31 | |||
32 | def db_for_write(self, model, **hints): | ||
33 | if hasattr(model._meta, 'situ_db_label'): | ||
34 | return model._meta.aft_db_label | ||
35 | return None | ||
36 | |||
37 | def allow_relation(self, obj1, obj2, **hints): | ||
38 | return True |
-
Please register or sign in to post a comment