Exemplo n.º 1
0
 def post(self, uid):  # 修改头像
     avatar_file = request.files.get('image', None)
     if not avatar_file:
         return jsonify({'message': '修改成功!'})
     avatar_folder = os.path.join(BASE_DIR,
                                  "fileStorage/avatars/{}/".format(uid))
     if not os.path.exists(avatar_folder):
         os.makedirs(avatar_folder)
     avatar_file_name = avatar_file.filename
     avatar_hash_name = hash_filename(avatar_file_name)
     file_path = os.path.join(avatar_folder, avatar_hash_name)
     file_save_url = "avatars/{}/{}".format(uid, avatar_hash_name)
     avatar_file.save(file_path)
     db_connection = MySQLConnection()
     cursor = db_connection.get_cursor()
     # 找出旧头像文件删除
     select_old_avatar = "SELECT `avatar` FROM `info_user` WHERE `id`=%s;"
     cursor.execute(select_old_avatar, uid)
     fetch_avatar = cursor.fetchone()
     if fetch_avatar:
         old_url = fetch_avatar['avatar']
         old_path = os.path.join(BASE_DIR, 'fileStorage/{}'.format(old_url))
         if os.path.exists(old_path):
             os.remove(old_path)
     save_statement = "UPDATE `info_user` SET `avatar`=%s WHERE `id`=%s;"
     cursor.execute(save_statement, (file_save_url, uid))
     db_connection.commit()
     db_connection.close()
     return jsonify({'message': '修改成功!', 'avatar_url': file_save_url})
Exemplo n.º 2
0
    def post(self):
        body_form = request.form
        utoken = body_form.get('utoken')
        user_info = verify_json_web_token(utoken)
        if not user_info or user_info['role_num'] > enums.COLLECTOR:
            return jsonify({"message": "登录已过期或不能进行操作。"}), 400
        title = body_form.get('title', None)
        notice_file = request.files.get('notice_file')
        category = body_form.get('category', 0)
        if not all([title, notice_file]):
            return jsonify({"message": "参数错误!"}), 400
        db_connection = MySQLConnection()
        cursor = db_connection.get_cursor()
        notice_filename = notice_file.filename
        file_hash_name = hash_filename(notice_filename)
        today = datetime.datetime.today()
        year = today.year
        month = "%.2d" % today.month
        day = "%.2d" % today.day
        exnotice_folder = os.path.join(
            BASE_DIR,
            "fileStorage/homepage/exnotice/{}/{}/".format(year, month))
        if not os.path.exists(exnotice_folder):
            os.makedirs(exnotice_folder)
        prefix_filename = "{}_".format(day)
        file_path = os.path.join(exnotice_folder,
                                 prefix_filename + file_hash_name)
        file_save_url = "homepage/exnotice/{}/{}/".format(
            year, month) + prefix_filename + file_hash_name
        notice_file.save(file_path)

        save_statement = "INSERT INTO `info_exnotice` (`create_time`,`title`,`file_url`,`category`,`author_id`) " \
                         "VALUES (%s,%s,%s,%s,%s);"
        try:
            user_id = int(user_info['id'])
            category = int(category)
            if category < 0 or category > 3:
                raise ValueError('分类错误!')
            cursor.execute(save_statement,
                           (today, title, file_save_url, category, user_id))
            db_connection.commit()
        except Exception as e:
            db_connection.rollback()
            os.remove(file_path)
            db_connection.close()
            current_app.logger.error("上传交易通知错误:{}".format(e))
            return jsonify({'message': "上传交易通知错误"}), 400
        else:
            db_connection.close()
            return jsonify({"message": "上传交易通知成功!"}), 201
Exemplo n.º 3
0
    def post(self):
        body_json = request.form
        utoken = body_json.get('utoken')
        user_info = verify_json_web_token(utoken)
        if user_info['role_num'] > 3:
            return jsonify({"message": "登录已过期或不能进行这个操作."}), 400
        bulletin_title = body_json.get('bulletin_title')
        bulletin_file = request.files.get('bulletin_file', None)
        if not all([bulletin_title, bulletin_file]):
            return jsonify({"message": "参数错误!NOT FOUND NAME OR FILE."}), 400
        # hash文件名
        bulletin_filename = bulletin_file.filename
        file_hash_name = hash_filename(bulletin_filename)
        today = datetime.datetime.today()
        year = today.year
        month = "%.2d" % today.month
        day = "%.2d" % today.day
        bulletin_folder = os.path.join(
            BASE_DIR,
            "fileStorage/homepage/bulletin/{}/{}/".format(year, month))
        if not os.path.exists(bulletin_folder):
            os.makedirs(bulletin_folder)
        prefix_filename = "{}_".format(day)
        file_path = os.path.join(bulletin_folder,
                                 prefix_filename + file_hash_name)
        file_save_url = "homepage/bulletin/{}/{}/".format(
            year, month) + prefix_filename + file_hash_name
        bulletin_file.save(file_path)
        # 保存到数据库
        save_bulletin_statement = "INSERT INTO `info_bulletin` (`create_time`,`title`,`file_url`) " \
                                  "VALUES (%s, %s,%s);"
        db_connection = MySQLConnection()
        cursor = db_connection.get_cursor()
        try:

            cursor.execute(save_bulletin_statement,
                           (today, bulletin_title, file_save_url))
            db_connection.commit()
        except Exception as e:
            current_app.logger.error("保存公告错误:{}".format(e)), 400
            db_connection.rollback()
            db_connection.close()
            return jsonify({"message": "上传公告失败!"}), 400
        else:
            db_connection.close()
            return jsonify({"message": "上传公告成功!"}), 201
Exemplo n.º 4
0
    def post(self):
        body_form = request.form
        utoken = body_form.get('utoken')
        user_info = verify_json_web_token(utoken)
        if not user_info or user_info['role_num'] > enums.RESEARCH:
            return jsonify({"message": "登录已过期或不能进行操作。"}), 400
        title = body_form.get('title', None)
        investment_file = request.files.get('invest_file')
        if not all([title, investment_file]):
            return jsonify({"message": "参数错误!"}), 400
        db_connection = MySQLConnection()
        cursor = db_connection.get_cursor()
        invest_filename = investment_file.filename
        file_hash_name = hash_filename(invest_filename)
        today = datetime.datetime.today()
        year = today.year
        month = "%.2d" % today.month
        day = "%.2d" % today.day
        file_folder = os.path.join(
            BASE_DIR,
            "fileStorage/pserver/investmentplan/{}/{}/".format(year, month))
        if not os.path.exists(file_folder):
            os.makedirs(file_folder)
        prefix_filename = "{}_".format(day)
        file_path = os.path.join(file_folder, prefix_filename + file_hash_name)
        file_save_url = "pserver/investmentplan/{}/{}/".format(
            year, month) + prefix_filename + file_hash_name
        investment_file.save(file_path)

        save_statement = "INSERT INTO `info_investmentplan` (`title`,`file_url`,`author_id`) " \
                         "VALUES (%s,%s,%s);"
        try:
            user_id = int(user_info['id'])
            cursor.execute(save_statement, (title, file_save_url, user_id))
            db_connection.commit()
        except Exception as e:
            db_connection.rollback()
            os.remove(file_path)
            db_connection.close()
            current_app.logger.error("添加投资方案错误:{}".format(e))
            return jsonify({'message': "添加方案错误"}), 400
        else:
            db_connection.close()
            return jsonify({"message": "添加成功!"}), 201
Exemplo n.º 5
0
 def post(self):
     body_form = request.form
     utoken = body_form.get('utoken')
     user_info = verify_json_web_token(utoken)
     if user_info['role_num'] > 2:
         return jsonify({"message": "登录已过期或不能进行这个操作."}), 400
     ad_title = body_form.get('ad_title')
     ad_file = request.files.get('ad_file', None)
     ad_image = request.files.get('ad_image', None)
     if not all([ad_title, ad_file, ad_image]):
         return jsonify({"message": "参数不足!"}), 400
     # hash文件名
     ad_filename = ad_file.filename
     ad_imagename = ad_image.filename
     adfile_hash_name = hash_filename(ad_filename)
     adimage_hash_name = adfile_hash_name.rsplit('.', 1)[0] + '.' + ad_imagename.rsplit('.', 1)[1]
     ad_folder = os.path.join(BASE_DIR, "fileStorage/homepage/ad/")
     if not os.path.exists(ad_folder):
         os.makedirs(ad_folder)
     adfile_path = os.path.join(ad_folder + adfile_hash_name)
     adimage_path = os.path.join(ad_folder + adimage_hash_name)
     adfile_save_url = "homepage/ad/" + adfile_hash_name
     adimage_save_url = "homepage/ad/" + adimage_hash_name
     ad_file.save(adfile_path)
     ad_image.save(adimage_path)
     today = datetime.datetime.today()
     # 保存到数据库
     save_ad_statement = "INSERT INTO `info_advertisement` (`create_time`,`title`,`file_url`,`image_url`) " \
                         "VALUES (%s,%s,%s,%s);"
     db_connection = MySQLConnection()
     cursor = db_connection.get_cursor()
     try:
         cursor.execute(save_ad_statement, (today, ad_title, adfile_save_url, adimage_save_url))
         db_connection.commit()
     except Exception as e:
         current_app.logger.error("保存广告错误:{}".format(e)), 400
         db_connection.rollback()
         db_connection.close()
         os.remove(adfile_path)
         os.remove(adimage_path)
         return jsonify({"message": "新建广告失败!"}), 400
     else:
         db_connection.close()
         return jsonify({"message": "新建广告成功!"}), 201
Exemplo n.º 6
0
 def post(self):
     body_json = request.json
     user_info = verify_json_web_token(body_json.get('utoken', None))
     if not user_info:
         return jsonify({"message": "登录过期了,重新登录后再保存..."}), 400
     user_id = user_info['id']
     chart_options = body_json.get('chart_options', None)
     title = body_json.get('title', None)  # 图表标题
     variety_id = body_json.get('variety_id', None)
     table_id = body_json.get('table_id', None)
     decipherment = body_json.get('decipherment', '')
     if not all([chart_options, title, variety_id, table_id]):
         print(chart_options, title, variety_id)
         return jsonify({"message": "参数错误..."}), 400
     # configs file path
     configs_filename = hash_filename(str(user_id) + '.json')
     now = datetime.now()
     local_folder = 'fileStorage/chartconfigs/{}/{}/{}/'.format(
         user_id, now.strftime("%Y"), now.strftime("%m"))
     save_folder = os.path.join(BASE_DIR, local_folder)
     if not os.path.exists(save_folder):
         os.makedirs(save_folder)
     save_path = os.path.join(save_folder, configs_filename)
     config_sql_path = local_folder + configs_filename  # 保存到sql的地址
     with open(save_path, 'w', encoding='utf-8') as f:
         json.dump(chart_options, f, indent=4)
     # print(config_sql_path, len(config_sql_path))
     # 保存到sql中
     insert_statement = "INSERT `info_trend_echart` " \
                        "(`author_id`,`title`,`table_id`,`variety_id`,`options_file`,`decipherment`) " \
                        "VALUES (%s,%s,%s,%s,%s,%s);"
     db_connection = MySQLConnection()
     cursor = db_connection.get_cursor()
     # 保存表信息
     cursor.execute(insert_statement, (user_id, title, table_id, variety_id,
                                       config_sql_path, decipherment))
     db_connection.commit()
     db_connection.close()
     return jsonify({"message": "保存成功!"}), 201