Exemplo n.º 1
0
 def update_one(self, table, id_item, **kwargs):
     """
     对execute_rowcount的一个封装
     :param table:
     :param id_item:
     :param kwargs:
     :return:
     """
     if len(kwargs) > 0:
         try:
             str_key = ''
             params = []
             for key, value in kwargs.iteritems():
                 str_key = str_key + key + '=%s, '
                 params.append(value)
             str_key = str_key[:-2]
             # sql = 'update ' + table + ' set ' + str_key + ' where id=' + id_item
             # 为了安全,如果id_item是字符串,去除 =
             if isinstance(id_item, type('')):
                 id_item = id_item.replace('=', '')
             elif isinstance(id_item, type(u'')):
                 id_item = id_item.replace(u'=', u'')
             sql = StringUtils.connect('update ', table, ' set ', str_key,
                                       ' where id=', id_item)
             result = self.execute_rowcount(sql, *params)
             self.commit()
             return result
         except Exception as e:
             print __name__, '更新条目出错:', e
             self.rollback()
             return -1
     else:
         return -1
Exemplo n.º 2
0
 def insert_one(self, table, **kwargs):
     """
     插入一条到数据库(先判断 id 是否存在,存在为更新,不在为插入)
     :param table: 要保存的表
     :param kwargs:
     :return:
     """
     if len(kwargs) > 0:
         keys = ''
         parameters = ''
         params = []
         for key, value in kwargs.iteritems():
             keys = keys + key + ', '
             params.append(value)
             parameters = parameters + '%s' + ', '
         keys = keys[:-2]  # 最后一个逗号不要
         parameters = parameters[:-2]
         # sql_insert = 'insert into ' + table + ' ( ' + keys + ' )' + ' values ' + '( ' + parameters + ' )'
         sql_insert = StringUtils.connect('insert into ', table, ' ( ',
                                          keys, ' )', ' values ', '( ',
                                          parameters, ' )')
         try:
             result = self.execute_rowcount(sql_insert, *params)
             self.commit()
             return result
         except Exception as e:
             print __name__, '插入条目到数据库出错: ', e
             self.rollback()
             return -1
     else:
         return -1
Exemplo n.º 3
0
    def get_total_pages(self, table, category, items_per_page):
        """

        :param table:
        :param category:
        :param items_per_page:
        :return:
        """
        try:
            sql_total = StringUtils.connect("select count(*) from ", table,
                                            " where category=%s")
            all_items = self.db.count(sql_total, category)
            if all_items % items_per_page != 0:
                page_total = all_items / items_per_page + 1
            else:
                page_total = all_items / items_per_page
            return page_total
        except Exception as e:
            print __name__, 'total_pages except: ', e
            return 0
Exemplo n.º 4
0
PATH_STATIC = os.path.join(PATH_BASE, NAME_STATIC)
# 模板路径
PATH_TEMPLATE = os.path.join(PATH_BASE, NAME_TEMPLATE)


# 以下路径都是不含静态文件夹名的相对路径
# 默认使用文件路径保存的路径(files/default/)(该文件夹下放:默认logo、默认缩略图、验证码图片)
PATH_DEFAULT_FILE = os.path.join(NAME_FILE_ROOT, 'default')
# 上传文件的路径(files/upload/)
PATH_UPLOAD = os.path.join(NAME_FILE_ROOT, 'upload')    # 这样写系统相关,使用时要通过os.path.join转为系统无关
# 临时文件存放夹(该文件夹可随意删)(files/temp/)
# PATH_TEMP = os.path.join(NAME_FILE_ROOT, 'temp')
PATH_TEMP = os.path.join(PATH_STATIC, NAME_FILE_ROOT, 'temp')
# 上传的 logo 保存的路径(保存在 upload 文件夹下)
PATH_LOGO = 'logo'

# 默认缩略图路径(不含静态文件夹名,url 形式)
PATH_THUMB_DEFAULT = 'files/default/default_thumb.jpg'
# 默认logo文件的路径(数据库没有内容时用)
PATH_LOGO_DEFAULT = 'files/default/logo.png'


# 验证码存放相对路径
PATH_VERIFY_SAVE_RELATIVE = PathUtils.connect(PATH_STATIC, 'files/default')
# 验证码存放绝对路径
PATH_VERIFY_SAVE_ABSOLUTE = PathUtils.connect(PATH_STATIC, 'files/default/verify.jpg')
# 验证码网页使用路径
PATH_VERIFY_URL = StringUtils.connect('/', NAME_STATIC, '/', 'files/default/verify.jpg')
# 验证码字体文件路径
PATH_VERIFY_FONT = PathUtils.connect(PATH_STATIC, 'files/default/Monaco.ttf')
Exemplo n.º 5
0
    def save_upload_file(self, post_streamer, user_id, user_category_info,
                         file_title):
        """
        返回文件路径、文件 id
        :param post_streamer:
        :param user_id: 文件使用者id
        :param user_category_info: 文件使用者的分类
        :param file_title: 文件名称
        :return:
        """
        def clean_file_name(file_name):
            """
            去除文件名中不规范的项目(利用正则表达式)
            """
            re_str = r"[\/\\\:\*\?\"\<\>\| _]"  # '/\:*?"<>|'
            return re.sub(re_str, "", file_name)

        # 获取文件信息
        file_info = {}
        for part in post_streamer.parts:
            """
            [
                { headers:[ {params, name, value}, {params, name, value} ], tempile, size },    # part

                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
                { headers:[ {params, name, value}, ], tempile, size },  # part
            ]
            0、poststreamer.parts,为一个 list 对象,其总共有六个 dict(headers、tempfile、size) 元素
            1、size 文件大小
            2、tempfile 值是一个临时文件对象 (转存要用到)
            4、headers 值是一个 list [ {params, name, value}, ]
            5、六个 dict 中,第一个为需要的

            """
            try:
                file_args = {}
                part["tmpfile"].close()
                # 获取文件后缀
                params = part["headers"][0].get("params", None)
                filename = params[
                    'filename']  # 只有上传文件对应的part才有该键,通过抛出异常来获得有效文件
                fill_suffix = PathUtils.get_file_suffix(filename)
                file_args['id'] = TimeUtils.time_id()
                file_args['user_id'] = user_id
                file_args['user_category'] = user_category_info['category']
                file_args['file_category'] = 'attachment'
                file_args['created'] = TimeUtils.datetime_date_simple()
                file_args['size'] = part["size"]
                file_args['title'] = clean_file_name(file_title)
                if len(file_args['title']) == 0:
                    file_args['title'] = str(file_args['id'])
                # 文件类型
                full_file_type = part["headers"][1].get("value", "text/plain")
                if '/' in full_file_type:
                    file_args['type'] = full_file_type.split('/')[0]
                elif '\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\')[0]
                elif '\\\\' in full_file_type:
                    file_args['type'] = full_file_type.split('\\\\')[0]
                # 文件名:id_user file_title suffix
                name_file = StringUtils.connect(file_args['user_category'],
                                                '_', file_args['user_id'], '-',
                                                file_args['id'], '-',
                                                file_args['title'],
                                                fill_suffix)
                path_folder_relative = self.make_path_folder_relative_no_static(
                )
                path_folder_absolute = PathUtils.connect(
                    path.PATH_STATIC, path_folder_relative)
                file_args['path'] = PathUtils.to_url(
                    PathUtils.connect(path_folder_relative, name_file))
                path_save = PathUtils.connect(path_folder_absolute, name_file)
                if isinstance(path_save, type('')):
                    path_save = path_save.decode('utf-8')
                # 创建文件夹路径
                if not os.path.exists(path_folder_absolute):
                    os.makedirs(path_folder_absolute)
                # 转存文件(这步将临时文件保存为正式文件, 关键)
                os.rename(part["tmpfile"].name, path_save)
                if not os.path.exists(path_save):  # 判断是否转存成功
                    file_info = {}
                    continue
                file_info = file_args
                # break # 不能终止循环,要通过异常,来删除临时文件
            except Exception as e:
                # traceback.print_exc()
                part["tmpfile"].close()
                os.unlink(part["tmpfile"].name)  # 删除临时文件(有多个,只有一个是上传文件)
        # 将信息写入数据库
        if len(file_info) > 0:
            result = self.insert_one_item(user_category_info['file_table'],
                                          **file_info)
            if result != -1:
                return {
                    '': file_info['id'],
                    'file_path': file_info['path'],
                    'file_title': file_info['title']
                }
            else:
                return {}
        else:
            return {}