Esempio n. 1
0
def api_upload():
    form = UploadForm()
    fname = None
    name = None
    selected_date = None
    existed_record = 0
    recorded = 0
    if form.validate_on_submit():

        selected_date = form.date.data
        name = form.name.data
        #f = request.files['file']
        #fname = secure_filename(f.filename)
        fname = secure_filename(form.file.data.filename)

        file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])  # 文件路径
        if not os.path.exists(file_dir):  # 如果文件夹不存在,则创建文件夹
            os.makedirs(file_dir)

        # 文件名重复判断
        ii = 0
        for parent, dirnames, filenames in os.walk(file_dir):  # 遍历目录并调用数据库写入函数
            for filename in filenames:
                if filename == fname:
                    ii = ii + 1
        if ii == 0:
            form.file.data.save(os.path.join(file_dir, fname))  # 保存文件到upload目录
        else:
            return render_template('upload2.html',
                                   form=form,
                                   filename=fname,
                                   message=u'该文件已存在,请重新上传!')

        # 读取文件,并对比数据库

        with open(os.path.join(file_dir, fname)) as csvFile:
            dictreader = csv.DictReader(csvFile)
            for line in dictreader:
                if main.find_one_record(
                        line['\xcc\xf5\xc2\xeb']):  # 如果单号已经存在于数据中
                    existed_record = existed_record + 1
                else:  # 若单号不在数据库中,则写入数据库
                    recorded = recorded + 1
                    main.add_one_record(name, line['\xcc\xf5\xc2\xeb'],
                                        selected_date)

        # 创建当天日期文件夹,并将文件转移至该文件夹
        currentday_file_dir = os.path.join(file_dir, selected_date)
        if not os.path.exists(currentday_file_dir):  # 如果文件夹不存在,则创建文件夹
            os.makedirs(currentday_file_dir)
        shutil.move(os.path.join(file_dir, fname), currentday_file_dir)

    return render_template('upload2.html',
                           form=form,
                           filename=fname,
                           name=name,
                           date=selected_date,
                           recorded=recorded,
                           existed_record=existed_record)
Esempio n. 2
0
def open_csv_to_save(f =None):
    with open(os.path.join(file_dir, f)) as csvFile:
        dictreader = csv.DictReader(csvFile)
    countcsv = 0
    for line in dictreader:
        if main.find_one_record(line['\xcc\xf5\xc2\xeb'])>0:
            countcsv = countcsv +1
    if countcsv >= 3:
        return True
    else:
        return False
Esempio n. 3
0
def index():
    # 从网页获取单号
    danhao = None
    form = DanhaoForm()
    if form.validate_on_submit():
        danhao = form.danhao.data
        danhao = danhao.strip()  # strip()的作用是去掉左右空格
        form.danhao.data = ''

    # 查询单号(本月及上月)
    allDanhao = main.find_one_record(danhao)

    # 本月快递总数
    allCount = main.all_wuliu_count()
    # 本月各快递总数
    kuaidicount = main.wuliu_month_count()
    # 本月所有人发件数
    everyoneCount = main.everyone_month_count()
    # 昨天各人发件数
    yesterdayCount = main.everyone_yesterday_count()
    # 今天各人发件数
    todayCount = main.everyone_today_count()
    # 上个月各快递公司总数
    lastMonth_kuaidicount = main.wuliu_last_month_count()
    # 每人上个月发件数
    lastMonth_everyoneCount = main.everyone_last_month_count()

    return render_template('index.html',
                           form=form,
                           check_record=allDanhao,
                           total=allCount,
                           kuaiditotal=kuaidicount,
                           everyoneCount=everyoneCount,
                           yesterdayCount=yesterdayCount,
                           todayCount=todayCount,
                           lastMonth_kuaidicount=lastMonth_kuaidicount,
                           lastMonth_everyoneCount=lastMonth_everyoneCount)
Esempio n. 4
0
def api_upload():

    file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])  # 文件路径
    if not os.path.exists(file_dir):  # 如果文件夹不存在,则创建文件夹
        os.makedirs(file_dir)

    f = request.files['myfile']  # 从表单的file字段获取文件,myfile为该表单的name值

    # 文件名后缀判断
    if f and allowed_file(f.filename):  # 判断是否是允许上传的文件类型
        fname = secure_filename(f.filename)
        print fname
    else:
        return render_template('upload.html', message=u"文件名错误,上传失败!")

    # 文件名重复判断
    ii = 0
    for parent, dirnames, filenames in os.walk(file_dir):  # 遍历目录并调用数据库写入函数
        for filename in filenames:
            if filename == fname:
                ii = ii + 1
    if ii == 0:
        f.save(os.path.join(file_dir, fname))  # 保存文件到upload目录
    else:
        return render_template('upload.html',
                               filename=fname,
                               message=u'该文件已存在,请重新上传!')

    # 读取文件,并对比数据库
    with open(os.path.join(file_dir, fname)) as csvFile:
        dictreader = csv.DictReader(csvFile)
        existed_record = 0
        recorded = 0
        for line in dictreader:
            if main.find_one_record(line['\xcc\xf5\xc2\xeb']):  # 如果单号已经存在于数据中
                existed_record = existed_record + 1
            else:  # 若单号不在数据库中,则写入数据库
                recorded = recorded + 1
                if 'HS' in fname:
                    main.add_one_record('HuangShuo', line['\xcc\xf5\xc2\xeb'],
                                        currentdate)
                elif 'LY' in fname:
                    main.add_one_record('LiuYun', line['\xcc\xf5\xc2\xeb'],
                                        currentdate)
                elif 'LJS' in fname:
                    main.add_one_record('LiJinSheng', line['\xcc\xf5\xc2\xeb'],
                                        currentdate)
                elif 'WS' in fname:
                    main.add_one_record('WeiSui', line['\xcc\xf5\xc2\xeb'],
                                        currentdate)

    # 创建当天日期文件夹,并将文件转移至该文件夹
    currentday_file_dir = os.path.join(
        file_dir, time.strftime("%Y-%m-%d", time.localtime()))
    if not os.path.exists(currentday_file_dir):  # 如果文件夹不存在,则创建文件夹
        os.makedirs(currentday_file_dir)
    shutil.move(os.path.join(file_dir, fname), currentday_file_dir)

    return render_template('upload.html',
                           filename=fname,
                           existed_record=existed_record,
                           recorded=recorded)