前提要开通访问策略和1521端口
import schedule import time import datetime import csv import shutil from django.core.management.base import BaseCommand, CommandError from d3.models import Student, Xueji, TempXueji from userapp.models import Jg import cx_Oracle as db import os from django.db.models import F, Func from django.db.models.functions import Length, Upper from django.db.models import Q, Count, Max, Min, Sum, Avg os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' def queryServerdb(sql): username = "xxxxx" passwd = "xxxxxx" host = "xxxxxx" port = "1521" sid = "xxxxxx" dsn = db.makedsn(host, port, service_name=sid) con = db.connect(user=username, password=passwd, dsn=dsn) cur = con.cursor() cur.execute(sql) result = cur.fetchall() cur.close() con.close() return (result) def queryLocaldb(sql): username = "lxg" passwd = "lxg" host = "127.0.0.1" port = "1521" sid = "XE" dsn = db.makedsn(host, port, service_name=sid) con = db.connect(user=username, password=passwd, dsn=dsn) cur = con.cursor() cur.execute(sql) result = cur.fetchall() cur.close() con.close() return (result) #取得原始的学籍信息(读取oracle库) def job_02_TempXueji(): print('====begin==job_02_TempXueji==') sql = "select " "xs_jbxx_id,sfzjlxm,sfzjh,xm,xxsszgjyxzdm," "sheng,shi,xian,xxbsm,xxmc," "jyjd_mc,xjnjdm_mc,jdztm_mc,sfzx_mc,sfby_mc," "gxsj,grbsm from qzk_cjxs.ZXX_CJXS_BDXX" rows = queryServerdb(sql) TempXueji.objects.all().delete() tempXueji_list = [] for row in rows: # try: # obj = TempXueji.objects.get(xs_jbxx_id=row[0], gxsj=row[15]) # except TempXueji.DoesNotExist: # TempXueji.objects.filter(xs_jbxx_id=row[0]).delete() if row[13] == '是': status = '在校生' elif row[14] == '是': status = '初中毕业' else: status = '无学籍' obj = TempXueji( xs_jbxx_id=row[0], sfzjlxm=row[1], sfzjh=row[2], xm=row[3], xxsszgjyxzdm=row[4], sheng=row[5], shi=row[6], xian=row[7], xxbsm=row[8], xxmc=row[9], jyjd_mc=row[10], xjnjdm_mc=row[11], jdztm_mc=row[12], sfzx_mc=row[13], sfby_mc=row[14], gxsj=row[15], grbsm=row[16], status=status ) tempXueji_list.append(obj) TempXueji.objects.bulk_create(tempXueji_list) print('====end==job_02_TempXueji==') print(str(TempXueji.objects.all().count()))
import schedule
import time
import datetime
import csv
import shutil
from django.core.management.base import BaseCommand, CommandError
from d3.models import Student, Xueji, TempXueji
from userapp.models import Jg
import cx_Oracle as db
import os
from django.db.models import F, Func
from django.db.models.functions import Length, Upper
from django.db.models import Q, Count, Max, Min, Sum, Avg
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
class Command(BaseCommand):
help = 'working in command!'
def handle(self, *args, **options):
schedule.every().day.at("20:00").do(job_00)
schedule.every().day.at("20:20").do(job_01)
schedule.every().day.at("06:00").do(job_02_TempXueji)
schedule.every().day.at("06:40").do(job_03_final)
# schedule.every(2).seconds.do(job_02_TempXueji)
# schedule.every(2).seconds.do(job_04_final())
# schedule.every(2).seconds.do(job_00)
# schedule.every(2).seconds.do(job_01)
while True:
schedule.run_pending()
time.sleep(10)
# 共享学生信息-导出csv
def job_00():
stuquery = Student.objects.all()
with open('/home/sftp/sftpuser/students.csv', 'w') as csvfile:
writer = csv.writer(csvfile, quotechar='"', quoting=csv.QUOTE_ALL)
columns = ["数据年份", "应届往届",
"机构id", "身份证", "姓名", "性别", "残疾类别 ", "残疾等级",
"初核状态", "不安置原因", "安置方式"]
rows = ([
my_row.year, my_row.year_type,
my_row.jg_id, my_row.sfz, my_row.name, my_row.get_gender_display(), my_row.get_dis_type_display(), my_row.get_dis_grade_display(),
my_row.ifchuhe, my_row.noanzhi_type, my_row.anzhi_fangshi, ]
for my_row in stuquery)
writer.writerow(columns)
for row in rows:
writer.writerow(row)
print("job_00working...")
# 共享机构信息-导出csv
def job_01():
jgquery = Jg.objects.all()
with open('/home/sftp/sftpuser/jg.csv', 'w') as csvfile:
writer = csv.writer(csvfile, quotechar='"', quoting=csv.QUOTE_ALL)
columns = ["机构id", "全称", "简称", "层级", "上级", "是否初核"]
rows = ([
my_row.p_code, my_row.full_name,
my_row.short_name, my_row.level, my_row.parent_code, my_row.ifchuhe]
for my_row in jgquery)
writer.writerow(columns)
for row in rows:
writer.writerow(row)
print("job_01working...")
def queryServerdb(sql):
username = "cjxs"
passwd = "Tgds_901"
host = "172.18.35.200"
port = "1521"
sid = "jcqzdb"
dsn = db.makedsn(host, port, service_name=sid)
con = db.connect(user=username, password=passwd, dsn=dsn)
cur = con.cursor()
cur.execute(sql)
result = cur.fetchall()
cur.close()
con.close()
return (result)
def queryLocaldb(sql):
username = "lxg"
passwd = "lxg"
host = "127.0.0.1"
port = "1521"
sid = "XE"
dsn = db.makedsn(host, port, service_name=sid)
con = db.connect(user=username, password=passwd, dsn=dsn)
cur = con.cursor()
cur.execute(sql)
result = cur.fetchall()
cur.close()
con.close()
return (result)
#取得原始的学籍信息(读取oracle库)
def job_02_TempXueji():
print('====begin==job_02_TempXueji==')
sql = "select "
"xs_jbxx_id,sfzjlxm,sfzjh,xm,xxsszgjyxzdm,"
"sheng,shi,xian,xxbsm,xxmc,"
"jyjd_mc,xjnjdm_mc,jdztm_mc,sfzx_mc,sfby_mc,"
"gxsj,grbsm from qzk_cjxs.ZXX_CJXS_BDXX"
rows = queryServerdb(sql)
TempXueji.objects.all().delete()
tempXueji_list = []
for row in rows:
# try:
# obj = TempXueji.objects.get(xs_jbxx_id=row[0], gxsj=row[15])
# except TempXueji.DoesNotExist:
# TempXueji.objects.filter(xs_jbxx_id=row[0]).delete()
if row[13] == '是':
status = '在校生'
elif row[14] == '是':
status = '初中毕业'
else:
status = '无学籍'
obj = TempXueji(
xs_jbxx_id=row[0],
sfzjlxm=row[1],
sfzjh=row[2],
xm=row[3],
xxsszgjyxzdm=row[4],
sheng=row[5],
shi=row[6],
xian=row[7],
xxbsm=row[8],
xxmc=row[9],
jyjd_mc=row[10],
xjnjdm_mc=row[11],
jdztm_mc=row[12],
sfzx_mc=row[13],
sfby_mc=row[14],
gxsj=row[15],
grbsm=row[16],
status=status
)
tempXueji_list.append(obj)
TempXueji.objects.bulk_create(tempXueji_list)
print('====end==job_02_TempXueji==')
print(str(TempXueji.objects.all().count()))