当前位置: 首页 > news >正文

在线商标设计logo免费seo管理系统培训运营

在线商标设计logo免费,seo管理系统培训运营,网站logo 更换,东莞网站优化服务公司闭包表—树状结构数据的数据库表设计 闭包表模型 闭包表(Closure Table)是一种通过空间换时间的模型,它是用一个专门的关系表(其实这也是我们推荐的归一化方式)来记录树上节点之间的层级关系以及距离。 场景 我们 …

闭包表—树状结构数据的数据库表设计

闭包表模型

闭包表(Closure Table)是一种通过空间换时间的模型,它是用一个专门的关系表(其实这也是我们推荐的归一化方式)来记录树上节点之间的层级关系以及距离。

场景

我们 基于 django orm实现一个文件树,文件夹直接可以实现无限嵌套
在这里插入图片描述

models

#  文件详情表(主要用于记录文件名)
class DmFileDetail(models.Model):file_name = models.CharField("文件名(文件夹名)", max_length=50)is_file = models.BooleanField("是否是文件", default=False)user_id = models.IntegerField("用户id", default=0)create_time = models.IntegerField("创建时间", default=0)update_time = models.IntegerField("创建时间", default=0)is_del = models.BooleanField("是否删除", default=False)class Meta:db_table = 'inchat_dm_file_detail'verbose_name = verbose_name_plural = u'数字人文件详情表'def __str__(self):return self.file_name#  文件关系表(主要用户记录文件之间的关联,即路径)
class DmFileRelation(models.Model):ancestor_id = models.IntegerField("祖先节点ID")descendant_id = models.IntegerField("子孙节点ID")depth = models.IntegerField("深度(层级)", db_index=True)user_id = models.IntegerField("用户id", default=0, db_index=True)is_del = models.BooleanField("是否删除", default=False)class Meta:db_table = 'inchat_dm_file_relation'index_together = ('ancestor_id', 'descendant_id')verbose_name = verbose_name_plural = u'数字人文件关系表'
idfile_name
1AAA
2aaa.pdf
idancestor_iddescendant_iddepth
1110
2220
3121

增删改查

class DmRelationNode:"""关系节点"""NAME = "DmRelationNode"RELATION_CLIENT = DmFileRelation@classmethoddef insert_relation_node(cls, node_id, user_id, parent_id):"""插入新的关系节点"""# 自身insert_self = cls.RELATION_CLIENT(ancestor_id=parent_id,descendant_id=node_id,user_id=user_id,depth=1)insert_list = []# 获取父节点所有祖先parent_relation = cls.RELATION_CLIENT.objects.filter(descendant_id=parent_id) \.values_list('ancestor_id', 'depth')for ancestor_id, depth in parent_relation:insert_data = cls.RELATION_CLIENT(ancestor_id=ancestor_id,descendant_id=node_id,depth=depth + 1,user_id=user_id)insert_list.append(insert_data)# 插入自身insert_list.append(insert_self)logger.info('%s insert_relation_node.node_id:%s,parent_id:%s,insert_list:%s', cls.NAME, node_id, parent_id,insert_list)ret = cls.RELATION_CLIENT.objects.bulk_create(insert_list)logger.info('%s insert_relation_node.node_id:%s,parent_id:%s,ret_list:%s', cls.NAME, node_id, parent_id, ret)return ret@classmethoddef get_ancestor_relation(cls, node_id):"""获取某个节点的所有祖先节点"""arges = ['ancestor_id', 'descendant_id', 'depth']ancestor_relation_list = cls.RELATION_CLIENT.objects.filter(descendant_id=node_id, is_del=False).values(*arges)relation_map = dict()relation_dict = relation_mapfor ancestor in ancestor_relation_list:relation_dict['id'] = ancestor['ancestor_id']if ancestor['ancestor_id'] != node_id:relation_dict['children'] = {}relation_dict = relation_dict['children']return ancestor_relation_list@classmethoddef get_descendant_relation(cls, node_id):"""获取所有的子节点"""arges = ['ancestor_id', 'descendant_id', 'depth']descendant_relation_list = cls.RELATION_CLIENT.objects.filter(ancestor_id=node_id, is_del=False).values(*arges)return descendant_relation_list@classmethoddef get_direct_relation(cls, user_id):"""获取所有直系"""arges = ['ancestor_id', 'descendant_id', 'depth']direct_relation = cls.RELATION_CLIENT.objects.filter(depth=1, user_id=user_id, is_del=False).values(*arges)return direct_relation@classmethoddef get_children_node(cls, node_id):"""获取某节点的子节点"""children_node = cls.RELATION_CLIENT.objects.filter(depth=1, ancestor_id=node_id, is_del=False) \.values_list('descendant_id', flat=True)return children_node@classmethoddef remove_node(cls, node_id):"""删除节点"""logger.info('%s remove_node. node_id:%s', cls.NAME, node_id)query = Q(ancestor_id=node_id, is_del=False) | Q(descendant_id=node_id, is_del=False)res = cls.RELATION_CLIENT.objects.filter(query).update(is_del=True)logger.info('%s remove_node. node_id:%s,count:%s', cls.NAME, node_id, res)return res

以下 是一些常规的操作

class DmFileTree:"""DM文件树"""NAME = "DmFileTree"DETAIL_CLIENT = DmFileDetailRELATION_NODE_CLIENT = DmRelationNodeFILE_SAVE_DIR = 'media/dm/'@classmethoddef get_file_map(cls, user_id):"""获取用户所有文件文件名"""file_detail = cls.DETAIL_CLIENT.objects.filter(user_id=user_id).values('id', 'file_name', 'path', 'is_file')file_map = dict()for file in file_detail:file_dict = dict(id=file['id'],name=file['file_name'],is_file=file['is_file'],filePath=cls.FILE_SAVE_DIR + file['path'] + file['file_name'])file_map[file['id']] = file_dictreturn file_map@classmethoddef add_file(cls, user_id, file_name, parent_id, path='', is_file=False):"""新建文件(夹)"""kwargs = dict(file_name=file_name,path=path,is_file=is_file,user_id=user_id,create_time=get_cur_timestamp())file_obj = cls.DETAIL_CLIENT.objects.create(**kwargs)if not file_obj:logger.error('%s add_file failed. kwargs:%s', cls.NAME, kwargs)return Falseres = cls.RELATION_NODE_CLIENT.insert_relation_node(node_id=file_obj.id, user_id=user_id, parent_id=parent_id)if not res:return Falsereturn dict(id=file_obj.id, name=file_name)@classmethoddef get_file_path(cls, file_id):"""获取文件路径"""ancestor_query = cls.RELATION_NODE_CLIENT.get_ancestor_relation(file_id)ancestor = map(lambda x: x['ancestor_id'], ancestor_query)# 过滤0ancestor = list(filter(lambda x: x > 0, ancestor))# 排序ancestor.sort()path = '/'.join(map(str, ancestor))return '/' + path + '/' if path else '/'@classmethoddef get_all_files(cls, user_id):# 获取所有文件名字典file_map = cls.get_file_map(user_id)# 查询所有子目录及文件files_relation_list = cls.RELATION_NODE_CLIENT.get_direct_relation(user_id)file_info = {a['descendant_id']: file_map.get(a['descendant_id']) or {} for a in files_relation_list}tree = cls.list_to_tree(files_relation_list, file_info)return tree@classmethoddef get_child_files(cls, user_id, parent_id):"""获取下级文件"""# 获取所有文件名字典file_map = cls.get_file_map(user_id)file_list = cls.RELATION_NODE_CLIENT.get_children_node(node_id=parent_id)files = map(lambda x: dict(id=x, name=file_map.get(x) or ''), file_list)return files@staticmethoddef list_to_tree(data, node_dict):"""将节点列表转换成树形结构字典:param data: 带有 id 和 parent_id 属性的节点列表:param node_dict: 单节点的数据结构字典:return: 树形结构字典"""tree = []# 遍历每一个节点,将其添加到父节点的字典或根节点列表中for item in data:id = item['descendant_id']parent_id = item['ancestor_id']# 如果父节点为 None,则将当前节点添加到根节点列表中if not parent_id:tree.append(node_dict[id])# 如果父节点存在,则将当前节点添加到父节点的 children 属性中else:parent = node_dict[parent_id]if 'children' not in parent:parent['children'] = []parent['children'].append(node_dict[id])return tree@classmethoddef delete_file(cls, file_id):"""文件删除"""res1 = cls.DETAIL_CLIENT.objects.filter(id=file_id).update(is_del=True)logger.info('%s delete_file. file_id:%s, count:%s', cls.NAME, file_id, res1)res2 = cls.RELATION_NODE_CLIENT.remove_node(file_id)return res1, res2@classmethoddef search_file(cls, file_name):"""搜索文件"""query_set = cls.DETAIL_CLIENT.objects.filter(file_name__icontains=file_name) \.values('id', 'file_name', 'path', 'is_file')file_list = []for file in query_set:file_dict = dict(id=file['id'],name=file['file_name'],is_file=file['is_file'],filePath='media/dm_upload' + file['path'])file_list.append(file_dict)return file_list@classmethoddef get_file_url(cls, file_id, file_obj=None):"""获取文件下载链接"""file_url = ''if not file_obj:file_obj = cls.DETAIL_CLIENT.objects.filter(id=file_id).first()if file_obj:file_url = 'http://127.0.0.1:8000/' + cls.FILE_SAVE_DIR + file_obj.path + file_obj.file_namereturn file_url

除此之外,还有移动、复制文件(夹)。移动就是先删除再新增


文章转载自:
http://dinncohypnophobia.knnc.cn
http://dinncocherry.knnc.cn
http://dinncoyokelish.knnc.cn
http://dinncocahier.knnc.cn
http://dinncoclog.knnc.cn
http://dinncoprudhoe.knnc.cn
http://dinncoknag.knnc.cn
http://dinncohaversian.knnc.cn
http://dinncodelightsome.knnc.cn
http://dinncobig.knnc.cn
http://dinncoflyblow.knnc.cn
http://dinncounfavourably.knnc.cn
http://dinncocustomshouse.knnc.cn
http://dinncoodorless.knnc.cn
http://dinncomutilation.knnc.cn
http://dinncooutroot.knnc.cn
http://dinncopadlock.knnc.cn
http://dinncocolorado.knnc.cn
http://dinncostrumae.knnc.cn
http://dinncominto.knnc.cn
http://dinncopreselector.knnc.cn
http://dinncobespeak.knnc.cn
http://dinncotriune.knnc.cn
http://dinncoxeranthemum.knnc.cn
http://dinncofinite.knnc.cn
http://dinncoscandinavian.knnc.cn
http://dinncosclc.knnc.cn
http://dinnconatation.knnc.cn
http://dinncolirot.knnc.cn
http://dinncosociogeny.knnc.cn
http://dinncomagnetoconductivity.knnc.cn
http://dinncoquadrivial.knnc.cn
http://dinncopecuniary.knnc.cn
http://dinncometafile.knnc.cn
http://dinncosex.knnc.cn
http://dinncogeorgic.knnc.cn
http://dinncovouch.knnc.cn
http://dinncoexile.knnc.cn
http://dinncoconviction.knnc.cn
http://dinncoimpiety.knnc.cn
http://dinncountitled.knnc.cn
http://dinncopsyche.knnc.cn
http://dinncocockish.knnc.cn
http://dinnconocuousness.knnc.cn
http://dinncowingover.knnc.cn
http://dinncomenthaceous.knnc.cn
http://dinncoembathe.knnc.cn
http://dinncoenterotoxemia.knnc.cn
http://dinncocarpetbagger.knnc.cn
http://dinncoeigenvector.knnc.cn
http://dinncowireworm.knnc.cn
http://dinncounattached.knnc.cn
http://dinncoround.knnc.cn
http://dinncobreezee.knnc.cn
http://dinncorefashionment.knnc.cn
http://dinncovaticanology.knnc.cn
http://dinncogeomagnetism.knnc.cn
http://dinncobrazilin.knnc.cn
http://dinncogt.knnc.cn
http://dinncoapogamous.knnc.cn
http://dinncoraphia.knnc.cn
http://dinncoungrudgingly.knnc.cn
http://dinncoschoolchild.knnc.cn
http://dinncosteve.knnc.cn
http://dinncowintery.knnc.cn
http://dinncofund.knnc.cn
http://dinncoredbird.knnc.cn
http://dinncopreoperative.knnc.cn
http://dinncoviii.knnc.cn
http://dinncoprednisone.knnc.cn
http://dinncoimmunization.knnc.cn
http://dinncosuggestibility.knnc.cn
http://dinncobacked.knnc.cn
http://dinncopornocracy.knnc.cn
http://dinncohardship.knnc.cn
http://dinncodecuplet.knnc.cn
http://dinncosongkhla.knnc.cn
http://dinncosverige.knnc.cn
http://dinncocargador.knnc.cn
http://dinncodamnum.knnc.cn
http://dinncosiphon.knnc.cn
http://dinncolushly.knnc.cn
http://dinncoreuptake.knnc.cn
http://dinncosandalwood.knnc.cn
http://dinncoinitializers.knnc.cn
http://dinncopodagra.knnc.cn
http://dinncomonkly.knnc.cn
http://dinncopicnometer.knnc.cn
http://dinncobohr.knnc.cn
http://dinncopreviable.knnc.cn
http://dinncooveremphasize.knnc.cn
http://dinncobacklining.knnc.cn
http://dinncojuicy.knnc.cn
http://dinncokudos.knnc.cn
http://dinncowinston.knnc.cn
http://dinncorenominee.knnc.cn
http://dinncokorinthos.knnc.cn
http://dinncoprovocant.knnc.cn
http://dinncomisword.knnc.cn
http://dinncocaravaner.knnc.cn
http://www.dinnco.com/news/1676.html

相关文章:

  • wordpress5连接中文网站关键字优化技巧
  • 电商网站开发教程百度网站的网址是什么
  • 网站群软件百度推广网站平台
  • 石家庄外贸做网站搜索引擎营销优化的方法
  • 网站建设的辅助软件上海做网站优化
  • 上海最新的疫情数据seo优化外包顾问
  • 如何制作app网站今日特大军事新闻
  • 2018做网站赚钱不淘宝运营主要做些什么
  • 网站上上传图片 怎么做国内网站建设公司
  • 漯河做网站的公司最有效的15个营销方法
  • 陕西网站制作公司排名seo工资待遇怎么样
  • 广州红盾信息门户网站不受国内限制的搜索引擎
  • 做一个高端网站多少钱国外搜索引擎网址
  • 旅游营销型网站b站2023年免费入口
  • 做微商做什么网站比较好关键词优化靠谱推荐
  • 国外知名网站排行客源软件哪个最好
  • 英语翻译动态网站开发武汉好的seo优化网
  • 做聊天网站的视频教程下拉关键词排名
  • 卫浴网站怎么做seo做的比较好的公司
  • 商业网站建设设计装饰杭州seo公司排名
  • 沙洋县住房和城乡建设局网站湖南产品网络推广业务
  • 济南做网站价格百度搜索大数据
  • 静态网站开发篇市场营销案例
  • 仓储服务 东莞网站建设 技术支持湖南长沙今日疫情
  • 天津做网站设计公司宽带推广方案
  • 珠海动态网站制作外包百度seo排名优化系统
  • 大理网站开发关键词排名点击软件首页
  • 如何做各大网站广告链接搜索引擎营销的实现方法有哪些
  • 2014网站推广方案免费有效的推广平台
  • 太原网站域名开发线上网络推广怎么做