Ejemplo n.º 1
0
    def post(self, date, plan_or_real):
        """Post schedule data, by date & plan_or_real type. like:
        POST: /api/v1.0/schedules/20140702/plan --data "file=file content&fname=file origin name"-->
        resp: {
            "message": "success or failed",
            "status": 200, or 410
            "version": "v1.0"
        };
        of course, it also support JSONP request.
        url: /api/v1.0/schedules/20140702/01/plan?callback=?
        """
        args = post_parser.parse_args()
        file = args.file
        file_orgin_name = args.fname
        file_name = 'TEMP_{}_{}-{}.csv'.format(
            plan_or_real, date, file_orgin_name)  # TEMP_PLAN_20150101.csv

        upload_dir = current_app.config[
            'LINE_DATA_UPLOADS_DEFAULT_URL'] or 'dac/static/schedules/'
        full_file_name = os.path.join(upload_dir, file_name)
        try:
            f = open(full_file_name, mode='w')
            f.write(file)
        except Exception as e:
            print("DAC DateScheduleList post ERROR", e)
            return make_json_response_2(
                410,
                message="File < {} > upload failed. {}".format(file_name,
                                                               e)), 410
        finally:
            f.close()
        try:
            pid = os.fork()
            if pid == 0:
                from dac.data_center.database import Mongodb
                from dac.data_center.csv.reader import ScheduleCSVReader
                m_db = Mongodb(app=current_app)
                schedule_reader = ScheduleCSVReader(full_file_name,
                                                    plan_or_real)
                schedule_reader.to_mongodb(database=m_db.db)
                m_db.close()
                print('{} schedule to mongodb done.'.format(plan_or_real))
                os._exit(0)
            else:
                return make_json_response_2(
                    200,
                    message="File < {} > upload success. Refresh to reload.".
                    format(file_name)), 200
        except OSError as e:
            print("DAC DateScheduleList post OSError", e)
            return make_json_response_2(
                410,
                message="File < {} > upload failed.".format(file_name)), 410
Ejemplo n.º 2
0
    def post(self, date, plan_or_real):
        """Post schedule data, by date & plan_or_real type. like:
        POST: /api/v1.0/schedules/20140702/plan --data "file=file content&fname=file origin name"-->
        resp: {
            "message": "success or failed",
            "status": 200, or 410
            "version": "v1.0"
        };
        of course, it also support JSONP request.
        url: /api/v1.0/schedules/20140702/01/plan?callback=?
        """
        args = post_parser.parse_args()
        print('args:', args)
        file = args.file
        file_orgin_name = args.fname
        file_name = 'TEMP_{}_{}-{}.csv'.format(
            plan_or_real, date, file_orgin_name)  # TEMP_PLAN_20150101.csv

        upload_dir = current_app.config[
            'LINE_DATA_UPLOADS_DEFAULT_URL'] or 'dac/static/schedules/'
        full_file_name = os.path.join(upload_dir, file_name)
        print('full_file_name:', full_file_name)
        try:
            f = open(full_file_name, mode='w')  #读取文件内部数据
            f.write(file)
        except Exception as e:
            print("DAC DateScheduleList post ERROR,read")
            return make_json_response_2(
                410,
                message="File < {} > upload failed. {}".format(file_name,
                                                               e)), 410
        finally:
            f.close()
        try:
            m_db = Mongodb(app=current_app)
            schedule_reader = ScheduleCSVReader(full_file_name, plan_or_real)
            schedule_reader.to_mongodb(database=m_db.db)
            m_db.close()
            print('{} schedule to mongodb done.'.format(plan_or_real))

            p2 = multiprocessing.Process(target=worker_2, args=(file_name, ))
            p2.start()

        except:
            print("DAC DateScheduleList post Error")
            return make_json_response_2(
                410,
                message="File < {} > upload failed.".format(file_name)), 410
        p2.join()
Ejemplo n.º 3
0
    def post(self, date, plan_or_real):
        """Post schedule data, by date & plan_or_real type. like:
        POST: /api/v1.0/schedules/20140702/plan --data "file=file content&fname=file origin name"-->
        resp: {
            "message": "success or failed",
            "status": 200, or 410
            "version": "v1.0"
        };
        of course, it also support JSONP request.
        url: /api/v1.0/schedules/20140702/01/plan?callback=?
        """
        args = post_parser.parse_args()
        file = args.file
        file_orgin_name = args.fname
        file_name = "TEMP_{}_{}-{}.csv".format(plan_or_real, date, file_orgin_name)  # TEMP_PLAN_20150101.csv

        upload_dir = current_app.config["LINE_DATA_UPLOADS_DEFAULT_URL"] or "dac/static/schedules/"
        full_file_name = os.path.join(upload_dir, file_name)
        try:
            f = open(full_file_name, mode="w")
            f.write(file)
        except Exception as e:
            print("DAC DateScheduleList post ERROR", e)
            return make_json_response_2(410, message="File < {} > upload failed. {}".format(file_name, e)), 410
        finally:
            f.close()
        try:
            pid = os.fork()
            if pid == 0:
                from dac.data_center.database import Mongodb
                from dac.data_center.csv.reader import ScheduleCSVReader

                m_db = Mongodb(app=current_app)
                schedule_reader = ScheduleCSVReader(full_file_name, plan_or_real)
                schedule_reader.to_mongodb(database=m_db.db)
                m_db.close()
                print("{} schedule to mongodb done.".format(plan_or_real))
                os._exit(0)
            else:
                return (
                    make_json_response_2(
                        200, message="File < {} > upload success. Refresh to reload.".format(file_name)
                    ),
                    200,
                )
        except OSError as e:
            print("DAC DateScheduleList post OSError", e)
            return make_json_response_2(410, message="File < {} > upload failed.".format(file_name)), 410
Ejemplo n.º 4
0
    def post(self, line_no):
        args = post_parser.parse_args()
        file = args.file
        file_name = 'LINE{}_STN_CFG.csv'.format(line_no)
        upload_dir = current_app.config[
            'LINE_CONFIG_UPLOADS_DEFAULT_URL'] or 'dac/static/configs/'

        full_file_name = os.path.join(upload_dir, file_name)
        # with open(full_file_name, mode='w') as f:
        #     f.write(file)
        try:
            f = open(full_file_name, mode='w')
            f.write(file)
        except Exception as e:
            print("DAC LineConfig post ERROR", e)
            return make_json_response_2(
                410,
                message="File < {} > upload failed. {}".format(file_name,
                                                               e)), 410
        finally:
            f.close()

        try:
            pid = os.fork()
            if pid == 0:
                from dac.data_center.database import Mongodb
                header_reader = LineConfigCSVReader(line_no, full_file_name)
                m_db = Mongodb(app=current_app)
                # conn, db = create_new_conn_db()
                header_reader.to_mongodb(database=m_db.db)
                m_db.close()
                print('line config to mongodb done.')
                os._exit(0)
            else:
                return make_json_response_2(
                    200,
                    message="File < {} > upload success. Refresh to reload.".
                    format(file_name)), 200
        except OSError as e:
            print("DAC LineConfig post OSError", e)
            return make_json_response_2(
                410,
                message="File < {} > upload failed.".format(file_name)), 410
Ejemplo n.º 5
0
    def post(self, line_no):
        args = post_parser.parse_args()
        file = args.file
        file_name = 'LINE{}_STN_CFG.csv'.format(line_no)
        upload_dir = current_app.config['LINE_CONFIG_UPLOADS_DEFAULT_URL'] or 'dac/static/configs/'

        full_file_name = os.path.join(upload_dir, file_name)
        # with open(full_file_name, mode='w') as f:
        #     f.write(file)
        try:
            f = open(full_file_name, mode='w')
            f.write(file)
        except Exception as e:
            print("DAC LineConfig post ERROR", e)
            return make_json_response_2(410, message="File < {} > upload failed. {}".format(file_name, e)), 410
        finally:
            f.close()


        try:
            pid = os.fork()
            if pid == 0:
                from dac.data_center.database import Mongodb
                header_reader = LineConfigCSVReader(line_no, full_file_name)
                m_db = Mongodb(app=current_app)
                # conn, db = create_new_conn_db()
                header_reader.to_mongodb(database=m_db.db)
                m_db.close()
                print('line config to mongodb done.')
                os._exit(0)
            else:
                return make_json_response_2(200, message="File < {} > upload success. Refresh to reload."
                                            .format(file_name)), 200
        except OSError as e:
            print("DAC LineConfig post OSError", e)
            return make_json_response_2(410, message="File < {} > upload failed.".format(file_name)), 410