def _group_permission(self, request): """ group 授权 添加permissions到group """ # 取得gid gid = int(post(request, 'gid')) # 取得permissions 类似 1,2,3,4,5 逗号隔开的数字字符串 pms = post(request, 'pms') if not gid or not pms: return False # 添加 gid:group_id pms:premission_id 到表: auth_group_permissions # 加工 permissions 字符串 # insert into `auth_group_permissions` (`group_id`, `permission_id`) # values (gid, pms_id) list_pms = pms.split(',') query_str = [ 'insert into `auth_group_permissions` (`group_id`, `permission_id`) ' ] for pms_id in list_pms: if int(pms_id): query_str.append(' values(' + str(gid) + ', ' + str(pms_id) + ') ') return Group.objects.raw(query_str.join)
def group_edit(self, request): """ 编辑权限 """ gid = max(int(get(request, 'gid')), 0) if not gid: rtn = 'admin/msg.html', {'title': '错误', 'content': '该权限组不存在!'} return rtn # 显示表单 if not post(request, 'do_submit'): try: rs = Group.objects.get(id=gid) g_data = {'name': rs.name} print(rs.permissions.all()) g_form = GroupForm(g_data) perm_dict = AuthActions.get_perm_list_tree() back_url = request.META['HTTP_REFERER'] rtn = 'admin/group_edit.html', { 'perm_dict': perm_dict, 'group_perms': [perm.id for perm in rs.permissions.all()], 'form': g_form, 'gid': gid } except: raise else: # 入库 name = post(request, 'name') g_form = GroupForm({'name': name, 'id': gid}) # 无论是否修改名称都要重新授权 o_group = Group.objects.get(id=gid) o_group.permissions.clear() # o_group.permissions = [] # o_group.permissions.add() 这两句同上面的clear() 等效 o_group.permissions = request.POST.getlist('perm') o_group.permissions.add() # 先清空权限 # 再update 组 # 再添加权限 g_form.is_valid() v_data = g_form.cleaned_data if v_data and 'name' in v_data: Group.objects.filter(id=gid).update(name=name) rtn = 'admin/msg.html', {'title': '提示', 'content': '修改权限组成功'} else: rtn = 'admin/msg.html', {'title': '提示', 'content': '用户授权成功!'} return rtn
def group_list(self, request): """ group 列表展示 参数: pn 当前page number """ pn = abs(int(post(request, 'pn'))) or 1 #rs = Group.objects.raw('drop table cmsadmin_archive') #print(rs) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = Group.objects.count() p_total = math.ceil(item_total / p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = Group.objects.all()[start:end] p_list = [p for p in range(1, p_total + 1)] ctx = { 'pn': pn, 'ppn': ppn, 'p_next': p_next, 'p_prev': p_prev, 'p_total': p_total, 'p_list': p_list, 'rs': rs } return ('admin/group.html', ctx)
def group_add(self, request): """ """ # 显示表单 if request.method != 'POST': g_form = GroupForm() perm_dict = AuthActions.get_perm_list_tree() #print(perm_dict) rtn = 'admin/group_add.html', { 'perm_dict': perm_dict, 'form': g_form } return rtn name = post(request, 'name') # TODO: forms.py 中验证name合法性 if not name: rtn = 'admin/msg.html', {'title': '错误', 'content': '组名不能为空'} else: # 入库 try: o_group = Group.objects.create(name=name) o_group.save() o_group.permissions = request.POST.getlist('perm') o_group.permissions.add() rtn = 'admin/msg.html', {'title': '提示', 'content': '添加权限组成功'} except IntegrityError: rtn = 'admin/msg.html', { 'title': '错误', 'content': '权限组名称已经存在!' } return rtn
def group_list(self, request): """ group 列表展示 参数: pn 当前page number """ pn = abs(int(post(request, 'pn'))) or 1 #rs = Group.objects.raw('drop table cmsadmin_archive') #print(rs) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = Group.objects.count() p_total = math.ceil(item_total/p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = Group.objects.all()[start:end] p_list = [p for p in range(1, p_total + 1)] ctx = {'pn': pn, 'ppn':ppn, 'p_next':p_next, 'p_prev':p_prev, 'p_total':p_total, 'p_list':p_list,'rs': rs} return ('admin/group.html', ctx)
def group_add(self, request): """ """ # 显示表单 if request.method != 'POST': g_form = GroupForm() perm_dict= AuthActions.get_perm_list_tree() #print(perm_dict) rtn = 'admin/group_add.html', {'perm_dict': perm_dict, 'form': g_form} return rtn name = post(request, 'name') # TODO: forms.py 中验证name合法性 if not name: rtn = 'admin/msg.html', {'title':'错误', 'content':'组名不能为空'} else: # 入库 try: o_group = Group.objects.create(name=name) o_group.save() o_group.permissions = request.POST.getlist('perm') o_group.permissions.add() rtn = 'admin/msg.html', {'title':'提示', 'content':'添加权限组成功'} except IntegrityError: rtn = 'admin/msg.html', {'title':'错误', 'content':'权限组名称已经存在!'} return rtn
def user_list(slef, request): """ user 列表展示 参数: pn 当前page number """ pn = abs(int(post(request, 'pn'))) or 1 #print(pn) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = User.objects.count() p_total = math.ceil(item_total / p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = User.objects.all()[start:end] p_list = [p for p in range(1, p_total + 1)] ctx = { 'pn': pn, 'ppn': ppn, 'p_next': p_next, 'p_prev': p_prev, 'p_total': p_total, 'p_list': p_list, 'rs': rs } return ('admin/user.html', ctx)
def login(request): if request.method == 'POST': # 根据输入的用户名获取user user_name = post(request, 'user_name').replace(' ', '').replace("'", '') pwd = post(request, 'pwd') # 将user传入 login ouser = auth.authenticate(username=user_name, password=pwd) if ouser and ouser.is_active: auth.login(request, ouser) else: return render_to_response('admin/msg.html', {'title':'错误', 'content':'用户名或密码错误!'}) return redirect('/manage/') else: # 登录表单 return render_to_response('admin/login.html')
def cate_page(self): """ 栏目分两种: # 单网页 # 多文章 """ # 取 cate内容 try: spc = get(self.request, 'spc') o_cate = Category.objects.get(id=self.cid) #print(o_cate.cate_type) if o_cate.cate_type == 'NORMAL': # get archive list and the content of which on top_pos pn = abs(int(post(request, 'pn'))) or 1 #rs = Group.objects.raw('drop table cmsadmin_archive') #print(rs) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = Group.objects.count() p_total = math.ceil(item_total/p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = arch_list = Archive.objects.all() if rs : rs = rs[start:end] p_list = [p for p in range(1, p_total + 1)] menu_list = _get_menus(self.request, get(self.request, 'm')) ctx = {'pn': pn, 'ppn':ppn, 'p_next':p_next, 'p_prev':p_prev, 'p_total':p_total, 'p_list':p_list,'rs': rs,'menuJSON': json.dumps(menu_list), 'menus' : menu_list,} tpl, ctx = ('cms/cate_normal.html', ctx) else: #print('sigle') menu_list = _get_menus(self.request, get(self.request, 'm')) # FIXIT: Error if spc: tpl = ''.join(['cms/cate_spc_', o_cate.alias, '_single.html']) #print(tpl) else: tpl = 'cms/cate_single.html' ctx = {'menuJSON': json.dumps(menu_list), 'menus' : menu_list,} except: #print('Error') raise Http404 return render_to_response(tpl, ctx)
def add(self, request): """ 需要添加到对应的 content_type上面 """ # 检查是否提交内容 if not post(request, 'do_submit'): # 获取django_content_type # TODO: 根据app->model(表)生成二级级联菜单 rs_content_type = ContentType.objects.raw( ' select id, app_label as appname, model as modelname from django_content_type' ) print(rs_content_type[1].model) tpl = 'admin/permission_add.html' ctx = {'content_type': rs_content_type} return render_to_response(tpl, ctx) else: # 入库 # 检查contenttype是否存在 codename = post(request, 'codename') name = post(request, 'name') content_type_id = post(request, 'content_type_id') if not (codename and name and content_type_id): tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '数据提交不完整' } else: if ContentType.objects.get(id=content_type_id): o_permission = Permission.objects.create( content_type_id=content_type_id, codename=codename, name=name) o_permission.save() tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '添加权限成功!' } else: # 出错 tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '暂时无法新建权限!' } return render_to_response(tpl, ctx)
def group_edit(self, request): """ 编辑权限 """ gid = max(int(get(request, 'gid')), 0) if not gid : rtn = 'admin/msg.html', {'title':'错误', 'content':'该权限组不存在!'} return rtn # 显示表单 if not post(request, 'do_submit'): try: rs = Group.objects.get(id=gid) g_data = {'name': rs.name} print(rs.permissions.all()) g_form = GroupForm(g_data) perm_dict= AuthActions.get_perm_list_tree() back_url = request.META['HTTP_REFERER'] rtn = 'admin/group_edit.html', {'perm_dict':perm_dict, 'group_perms': [perm.id for perm in rs.permissions.all()], 'form': g_form, 'gid':gid} except : raise else: # 入库 name = post(request, 'name') g_form = GroupForm({'name': name, 'id':gid}) # 无论是否修改名称都要重新授权 o_group = Group.objects.get(id=gid) o_group.permissions.clear() # o_group.permissions = [] # o_group.permissions.add() 这两句同上面的clear() 等效 o_group.permissions = request.POST.getlist('perm') o_group.permissions.add() # 先清空权限 # 再update 组 # 再添加权限 g_form.is_valid() v_data = g_form.cleaned_data if v_data and 'name' in v_data: Group.objects.filter(id=gid).update(name=name) rtn = 'admin/msg.html', {'title':'提示', 'content':'修改权限组成功'} else: rtn = 'admin/msg.html', {'title':'提示', 'content':'用户授权成功!'} return rtn
def add_act(request): """ 添加 """ if request.method != 'POST': key = get(request, 'key') dictTpl = {'siteInfo':'admin/sys-site-info.html', 'siteSeo': 'admin/sys-site-seo.html',} ctx = {} try: try: oSysInfo = SysInfo.objects.get(key=key) val = oSysInfo.value ctx = {'keyId':oSysInfo.id, 'oval':json.loads(val)} except: pass #print(type(json.loads(val))) return render_to_response(dictTpl[key], ctx) except: raise raise Http404('内部错误:网页没找到') else: # print(dir(request.POST)) # print([i for i in request.post.lists()]) dictList = request.POST.lists() val = {} for i in dictList: item = val[i[0]] = i[1][0] # print(json.dumps(val)) val = json.dumps(val) key = get(request, 'key') keyId = post(request, 'keyId') value = json.dumps(val) try: if(keyId): oSysInfo = SysInfo.objects.filter(id=keyId).update(key=key, value=value) tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'修改成功!'} else: oSysInfo = SysInfo.objects.create(key=key, value=value) oSysInfo.save() tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'新建成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'新建失败!'} raise return render_to_response(tpl, ctx)
def add_act(self, request): """ 添加 """ if not post(request, 'do_submit'): # 显示表单 tpl, ctx = 'admin/position_add.html', {} else: name = post(request, 'name') if re.match(re.compile(r'^[\u4e00-\u9fa5\w-]{2,30}$'), name): # 入库 o_pos = Position.objects.create(name=name) try: o_pos.save() tpl, ctx = 'admin/msg.html', {'title':'提示','content':'添加成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误','content':'无法添加推荐位!'} else: tpl, ctx = 'admin/msg.html', {'title':'错误','content':'无法添加推荐位{0}'.format(name)} return render_to_response(tpl, ctx)
def _group_permission(self, request): """ group 授权 添加permissions到group """ # 取得gid gid = int(post(request, 'gid')) # 取得permissions 类似 1,2,3,4,5 逗号隔开的数字字符串 pms = post(request, 'pms') if not gid or not pms: return False # 添加 gid:group_id pms:premission_id 到表: auth_group_permissions # 加工 permissions 字符串 # insert into `auth_group_permissions` (`group_id`, `permission_id`) # values (gid, pms_id) list_pms = pms.split(',') query_str = ['insert into `auth_group_permissions` (`group_id`, `permission_id`) '] for pms_id in list_pms: if int(pms_id): query_str.append(' values(' + str(gid) + ', ' + str(pms_id) + ') ') return Group.objects.raw(query_str.join)
def add(self, request): """ 需要添加到对应的 content_type上面 """ # 检查是否提交内容 if not post(request, 'do_submit'): # 获取django_content_type # TODO: 根据app->model(表)生成二级级联菜单 rs_content_type = ContentType.objects.raw(' select id, app_label as appname, model as modelname from django_content_type') print(rs_content_type[1].model) tpl = 'admin/permission_add.html' ctx = {'content_type': rs_content_type} return render_to_response(tpl, ctx) else: # 入库 # 检查contenttype是否存在 codename = post(request, 'codename') name = post(request, 'name') content_type_id = post(request, 'content_type_id') if not (codename and name and content_type_id): tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'数据提交不完整'} else: if ContentType.objects.get(id=content_type_id): o_permission = Permission.objects.create(content_type_id=content_type_id, codename=codename, name=name) o_permission.save() tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'添加权限成功!'} else: # 出错 tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'暂时无法新建权限!'} return render_to_response(tpl, ctx)
def user_list(slef, request): """ user 列表展示 参数: pn 当前page number """ pn = abs(int(post(request, 'pn'))) or 1 #print(pn) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = User.objects.count() p_total = math.ceil(item_total/p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = User.objects.all()[start:end] p_list = [p for p in range(1, p_total + 1)] ctx = {'pn': pn, 'ppn':ppn, 'p_next':p_next, 'p_prev':p_prev, 'p_total':p_total, 'p_list':p_list,'rs': rs} return ('admin/user.html', ctx)
def add_act(request): """ 添加 """ # 检查是否显示表单 if not post(request, 'do_submit'): # 获取分类列表 cate_list = Category.get_tree_as_options() # 获取广告位列表 position_list = Position.get_all_as_checkbox() # 显示表单 user_list = UserGet.all_as_option() # tpl list of content _tpl_list = get_tpl_list('cms') tpl_list = [] if _tpl_list: for tpl in _tpl_list: if re.search('^page', tpl): tpl_list.append(tpl) tpl, ctx = 'admin/archive_add.html', {'cates': cate_list, 'user_list': user_list, 'tpl_list': tpl_list, 'pos_list': position_list} else: # 手工获取post数据 pos_ids = request.POST.getlist('position_id') pos_id = [] if pos_ids: for p_id in pos_ids: pos_id.append(p_id + ',') pos_id = ''.join(pos_id) pos_id = pos_id[:len(pos_id) - 1] now = dt.datetime.now() data = {'title': post(request, 'title'), 'summary': post(request, 'summary'), 'content': post(request, 'content'), 'keywords': post(request, 'keywords'), 'description': post(request, 'description'), 'cate_id': post(request, 'cate_id'), 'author': post(request, 'author'), 'referer': post(request, 'referer'), 'create_time': now, 'last_edit_time': now, 'position_id': pos_id } form = ArchiveForm(data) if form.is_valid(): # 入库 try: o_archive = Archive.objects.create( title = form.cleaned_data['title'], summary = form.cleaned_data['summary'], content = form.cleaned_data['content'], keywords = form.cleaned_data['keywords'], description = form.cleaned_data['description'], cate_id = form.cleaned_data['cate_id'], author = form.cleaned_data['author'], referer = form.cleaned_data['referer'], create_time = form.cleaned_data['create_time'], last_edit_time = now, position_id = form.cleaned_data['position_id'] , tpl=post(request, 'tpl') ) o_archive.save() tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'添加新文章成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'.无法添加新文章!'} else: print(form.errors) tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'无法添加新文章!', 'err_msg': form.errors} return render_to_response(tpl, ctx)
def add_act(request): """ 添加 """ # 检查是否显示表单 if not request.method == 'POST': # 获取分类列表 # 显示表单 menu_list = Menu.objects.all().values() #print(menu_list) o_tree = CateTree(menu_list) menus = o_tree.tree(seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) #print(menus) tpl, ctx = 'admin/menu_add.html', {'menu_tree_select': menus} return render_to_response(tpl, ctx) # 处理输入并入库 # TODO: menu 模型没有定义form类进行数据验证 else: # pid / path / has_child / depth pid = int(post(request, 'pid')) name=post(request, 'name') m_type = post(request, 'm_type') or 'back' url = post(request, 'url') code = post(request, 'code') list_order = post(request, 'list_order') # 检测pid是否为-1 若是则返回错误提示选择父目录 if pid < 0: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'选择一个上级类别或作为”顶级类别“!'} elif pid > 0: # 获取父cate print(pid) p_menu = Menu.objects.filter(id=pid).get() if not p_menu: # 父cate不存在 tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'父菜单不存在!'} else: # 父目录存在 则检测兄弟由无重名 若有则提示出错 same_name_siblings = Menu.objects.filter(name=name, pid=pid).count() if same_name_siblings: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'相同父类别下已经有该名称分类!'} return render_to_response(tpl, ctx) else: # 没有同名兄弟 可以提交(pid/name在上面已经赋值) path = p_menu.path + ',' + str(pid) depth = int(p_menu.depth) + 1 p_has_child = p_menu.has_child elif pid <= 0: path = 0 depth = 0 # 本cate入库 menu = Menu.objects.create(path=path, name=name, m_type=m_type, code=code, url=url, depth=depth, list_order = list_order, pid=pid, has_child=0 ) try: menu.save() # 若父cate的has_child 为0 则修改为1 否则不操作 if pid and not p_has_child: Menu.objects.filter(id=pid).update(has_child=1) #return redirect('admin:category_act', action='list') tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'添加分类成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'添加分类出错!'} return render_to_response(tpl, ctx)
def add_act(request): """ 添加 """ # 检查是否显示表单 if request.method != 'POST': # 获取分类列表 # 显示表单 cate_list = Cate.objects.all().values() o_form = CateForm() o_tree = CateTree(cate_list) cates = o_tree.tree(seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) tpl, ctx = 'admin/cate_add.html', { 'form': o_form, 'cate_tree_select': cates } return render_to_response(tpl, ctx) # 处理输入并入库 # TODO: category 模型没有定义form类进行数据验证 else: # pid / path / has_child / depth pid = int(post(request, 'pid')) name = post(request, 'name') alias = post(request, 'alias') cate_type = post(request, 'cate_type') #print(cate_type) # 检测pid是否为-1 若是则返回错误提示选择父目录 if pid < 0: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '选择一个上级分类或作为”顶级分类“!' } return render_to_response(tpl, ctx) elif pid > 0: # 获取父cate print(pid) p_cate = Cate.objects.filter(id=pid).get() if not p_cate: # 父cate不存在 tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '父分类不存在!' } else: # 父目录存在 则检测兄弟由无重名 若有则提示出错 same_name_siblings = Cate.objects.filter(name=name, pid=pid).count() if same_name_siblings: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '相同父分类下已经有该名称分类!' } return render_to_response(tpl, ctx) else: # 没有同名兄弟 可以提交(pid/name在上面已经赋值) path = p_cate.path + ',' + str(pid) depth = int(p_cate.depth) + 1 p_has_child = p_cate.has_child elif pid == 0: path = 0 depth = 0 # 本cate入库 alias = alias.strip(' ') o_form = CateForm({ 'name': name, 'cate_type': cate_type, 'alias': alias, 'path': path }) if (o_form.is_valid()): alias, cnt = re.subn(r' +', '-', alias) #print(alias) cate = Cate.objects.create(path=path, name=name, depth=depth, pid=pid, alias=alias, has_child=0) try: cate.save() # 若父cate的has_child 为0 则修改为1 否则不操作 if pid and not p_has_child: Cate.objects.filter(id=pid).update(has_child=1) #return redirect('admin:category_act', action='list') tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '.添加分类成功!' } except: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '..添加分类出错!' } else: # FIXIT: 无端提示 cate_type 为必填项 print(o_form.errors) tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '添加分类出错!' } return render_to_response(tpl, ctx)
def edit_act(request): cid = get(request, 'cid') if request.method == 'GET': if not cid: tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '.分类不存在!' } else: try: oCate = Cate.objects.get(id=cid) cate_list = Cate.objects.all().values() o_form = CateForm() o_tree = CateTree(cate_list) cates = o_tree.tree( seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) tpl, ctx = 'admin/cate_edit.html', { 'form': o_form, 'oCate': oCate, 'cate_tree_select': cates } print(oCate) except: tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '分类不存在!' } return render_to_response(tpl, ctx) else: pid = int(post(request, 'pid')) cid = int(post(request, 'cid')) name = post(request, 'name') alias = post(request, 'alias') cate_type = post(request, 'cate_type') #print(cate_type) # 检测pid是否为-1 若是则返回错误提示选择父目录 #print(cate_type) if pid < 0: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '选择一个上级分类或作为”顶级分类“!' } return render_to_response(tpl, ctx) elif pid > 0: # 获取父cate p_cate = Cate.objects.filter(id=pid).get() if not p_cate: # 父cate不存在 tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '父分类不存在!' } else: # 父目录存在 则检测兄弟由无重名 若有则提示出错 same_name_siblings = Cate.objects.filter(name=name, pid=pid) siblings_cnt = same_name_siblings.count() if siblings_cnt > 1: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '相同父分类下已经有该名称分类!' } return render_to_response(tpl, ctx) else: # 没有同名兄弟 可以提交(pid/name在上面已经赋值) path = p_cate.path + ',' + str(pid) depth = int(p_cate.depth) + 1 p_has_child = p_cate.has_child elif pid == 0: path = 0 depth = 0 # 本cate入库 alias = alias.strip(' ') o_form = CateForm({ 'name': name, 'cate_type': cate_type, 'alias': alias, 'path': path }) if (o_form.is_valid()): alias, cnt = re.subn(r' +', '-', alias) #print(alias) try: Cate.objects.filter(id=cid).update(path=path, name=name, cate_type=cate_type, depth=depth, pid=pid, alias=alias, has_child=0) # 若父cate的has_child 为0 则修改为1 否则不操作 if pid and not p_has_child: Cate.objects.filter(id=pid).update(has_child=1) #return redirect('admin:category_act', action='list') tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '编辑分类成功!' } except: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '..编辑分类出错!' } raise else: # FIXIT: 无端提示 cate_type 为必填项 tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '编辑分类出错!' } return render_to_response(tpl, ctx)
def user_edit(self, request): """ 编辑内容 """ group_list = Group.objects.all().values() uId = get(request, 'uid') if not int(uId): tpl, ctx = ('admin/msg.html', {'title':'错误', 'content':'用户不存在,或系统错误!'}) if request.method != 'POST': # 根据uid 获取用户信息 try: oUser = User.objects.get(id=uId) u_data = dict(username=oUser.username, first_name=oUser.first_name, is_staff=oUser.is_staff) form = UserForm(initial = u_data) # Fixit form.is_valid() # 取得当前grouplist import sqlite3 from Ycms.settings import BASE_DIR conn = sqlite3.connect(os.path.join(BASE_DIR,'db.sqlite3')) # print(os.path.join(BASE_DIR,'db.sqlite3')) csr = conn.cursor() rs = csr.execute('select group_id from auth_user_groups where user_id = {0}'.format(uId) ) conn.commit() #print('select group_id from auth_user_groups where user_id = {0}'.format(uId) ) c_g_list = [] g_id_all = csr.fetchall() for g_id in g_id_all: if g_id: c_g_list.append(g_id[0]) tpl, ctx = ('admin/user_edit.html', {'form': form, 'oUser': oUser, 'group_list': group_list, 'current_group_list': c_g_list, 'id':uId}) except : tpl, ctx = ('admin/msg.html', {'title':'错误', 'content':'用户不存在![id 错误]'}) else: # 处理修改 oUser = User() try: oUser.first_name = post(request, 'first_name') password = post(request, 'password') if not password: User.objects.filter(id=post(request,'uid')).update( first_name=post(request, 'first_name'), is_superuser = int(post(request, 'is_superuser')) ) elif not re.match(re.compile(r'^[^\u4e00-\u9fa5a]{6,16}$'), password): return 'admin/msg.html',{'title':'错误','content':'密码为非中文的6到16位字符'} else: pwd = make_password(password) User.objects.filter(id=post(request,'uid')).update( first_name=post(request, 'first_name'), password = pwd, is_superuser = int(post(request, 'is_superuser')) ) # 检查有无group_id post过来 if 'group_ids' in request.POST and request.POST['group_ids']: group_ids = request.POST.getlist('group_ids') if group_ids : # print(group_ids) # 构造sql str_sql = 'insert into auth_user_groups(user_id, group_id) values ' for g_id in group_ids: str_sql = str_sql + '(' + str(uId) + ',' + str(g_id) + '),' str_sql = str_sql.strip(',') # print(str_sql) # 入库 import sqlite3 from Ycms.settings import BASE_DIR conn = sqlite3.connect(os.path.join(BASE_DIR,'db.sqlite3')) # print(os.path.join(BASE_DIR,'db.sqlite3')) csr = conn.cursor() csr.execute('delete from auth_user_groups where user_id={0}'.format(uId)) conn.commit() rs = csr.execute(str_sql) conn.commit() # print(rs) tpl, ctx = ('admin/msg.html', {'title':'提示', 'content':'修改成功!'}) except: #pass tpl, ctx = ('admin/msg.html', {'title':'错误', 'content':'修改用户出错!'}) raise return (tpl, ctx)
def add_act(request): """ 添加 """ # 检查是否显示表单 if request.method != 'POST': # 获取分类列表 # 显示表单 cate_list = Cate.objects.all().values() o_form = CateForm() o_tree = CateTree(cate_list) cates = o_tree.tree(seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) tpl, ctx = 'admin/cate_add.html', {'form':o_form, 'cate_tree_select': cates} return render_to_response(tpl, ctx) # 处理输入并入库 # TODO: category 模型没有定义form类进行数据验证 else: # pid / path / has_child / depth pid = int(post(request, 'pid')) name=post(request, 'name') alias = post(request, 'alias') cate_type = post(request, 'cate_type') #print(cate_type) # 检测pid是否为-1 若是则返回错误提示选择父目录 if pid < 0: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'选择一个上级分类或作为”顶级分类“!'} return render_to_response(tpl, ctx) elif pid > 0: # 获取父cate print(pid) p_cate = Cate.objects.filter(id=pid).get() if not p_cate: # 父cate不存在 tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'父分类不存在!'} else: # 父目录存在 则检测兄弟由无重名 若有则提示出错 same_name_siblings = Cate.objects.filter(name=name, pid=pid).count() if same_name_siblings: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'相同父分类下已经有该名称分类!'} return render_to_response(tpl, ctx) else: # 没有同名兄弟 可以提交(pid/name在上面已经赋值) path = p_cate.path + ',' + str(pid) depth = int(p_cate.depth) + 1 p_has_child = p_cate.has_child elif pid == 0: path = 0 depth = 0 # 本cate入库 alias = alias.strip(' ') o_form = CateForm({'name':name, 'cate_type':cate_type, 'alias':alias, 'path':path}) if (o_form.is_valid()): alias, cnt = re.subn(r' +', '-', alias) #print(alias) cate = Cate.objects.create(path=path, name=name, depth=depth, pid=pid, alias=alias, has_child=0 ) try: cate.save() # 若父cate的has_child 为0 则修改为1 否则不操作 if pid and not p_has_child: Cate.objects.filter(id=pid).update(has_child=1) #return redirect('admin:category_act', action='list') tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'.添加分类成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'..添加分类出错!'} else: # FIXIT: 无端提示 cate_type 为必填项 print(o_form.errors) tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'添加分类出错!'} return render_to_response(tpl, ctx)
def user_edit(self, request): """ 编辑内容 """ group_list = Group.objects.all().values() uId = get(request, 'uid') if not int(uId): tpl, ctx = ('admin/msg.html', { 'title': '错误', 'content': '用户不存在,或系统错误!' }) if request.method != 'POST': # 根据uid 获取用户信息 try: oUser = User.objects.get(id=uId) u_data = dict(username=oUser.username, first_name=oUser.first_name, is_staff=oUser.is_staff) form = UserForm(initial=u_data) # Fixit form.is_valid() # 取得当前grouplist import sqlite3 from Ycms.settings import BASE_DIR conn = sqlite3.connect(os.path.join(BASE_DIR, 'db.sqlite3')) # print(os.path.join(BASE_DIR,'db.sqlite3')) csr = conn.cursor() rs = csr.execute( 'select group_id from auth_user_groups where user_id = {0}' .format(uId)) conn.commit() #print('select group_id from auth_user_groups where user_id = {0}'.format(uId) ) c_g_list = [] g_id_all = csr.fetchall() for g_id in g_id_all: if g_id: c_g_list.append(g_id[0]) tpl, ctx = ('admin/user_edit.html', { 'form': form, 'oUser': oUser, 'group_list': group_list, 'current_group_list': c_g_list, 'id': uId }) except: tpl, ctx = ('admin/msg.html', { 'title': '错误', 'content': '用户不存在![id 错误]' }) else: # 处理修改 oUser = User() try: oUser.first_name = post(request, 'first_name') password = post(request, 'password') if not password: User.objects.filter(id=post(request, 'uid')).update( first_name=post(request, 'first_name'), is_superuser=int(post(request, 'is_superuser'))) elif not re.match(re.compile(r'^[^\u4e00-\u9fa5a]{6,16}$'), password): return 'admin/msg.html', { 'title': '错误', 'content': '密码为非中文的6到16位字符' } else: pwd = make_password(password) User.objects.filter(id=post(request, 'uid')).update( first_name=post(request, 'first_name'), password=pwd, is_superuser=int(post(request, 'is_superuser'))) # 检查有无group_id post过来 if 'group_ids' in request.POST and request.POST['group_ids']: group_ids = request.POST.getlist('group_ids') if group_ids: # print(group_ids) # 构造sql str_sql = 'insert into auth_user_groups(user_id, group_id) values ' for g_id in group_ids: str_sql = str_sql + '(' + str(uId) + ',' + str( g_id) + '),' str_sql = str_sql.strip(',') # print(str_sql) # 入库 import sqlite3 from Ycms.settings import BASE_DIR conn = sqlite3.connect( os.path.join(BASE_DIR, 'db.sqlite3')) # print(os.path.join(BASE_DIR,'db.sqlite3')) csr = conn.cursor() csr.execute( 'delete from auth_user_groups where user_id={0}'. format(uId)) conn.commit() rs = csr.execute(str_sql) conn.commit() # print(rs) tpl, ctx = ('admin/msg.html', { 'title': '提示', 'content': '修改成功!' }) except: #pass tpl, ctx = ('admin/msg.html', { 'title': '错误', 'content': '修改用户出错!' }) raise return (tpl, ctx)
def add_act(request): """ 添加 """ # 检查是否显示表单 if not post(request, 'do_submit'): # 获取分类列表 cate_list = Category.get_tree_as_options() # 获取广告位列表 position_list = Position.get_all_as_checkbox() # 显示表单 user_list = UserGet.all_as_option() # tpl list of content _tpl_list = get_tpl_list('cms') tpl_list = [] if _tpl_list: for tpl in _tpl_list: if re.search('^page', tpl): tpl_list.append(tpl) tpl, ctx = 'admin/archive_add.html', { 'cates': cate_list, 'user_list': user_list, 'tpl_list': tpl_list, 'pos_list': position_list } else: # 手工获取post数据 pos_ids = request.POST.getlist('position_id') pos_id = [] if pos_ids: for p_id in pos_ids: pos_id.append(p_id + ',') pos_id = ''.join(pos_id) pos_id = pos_id[:len(pos_id) - 1] now = dt.datetime.now() data = { 'title': post(request, 'title'), 'summary': post(request, 'summary'), 'content': post(request, 'content'), 'keywords': post(request, 'keywords'), 'description': post(request, 'description'), 'cate_id': post(request, 'cate_id'), 'author': post(request, 'author'), 'referer': post(request, 'referer'), 'create_time': now, 'last_edit_time': now, 'position_id': pos_id } form = ArchiveForm(data) if form.is_valid(): # 入库 try: o_archive = Archive.objects.create( title=form.cleaned_data['title'], summary=form.cleaned_data['summary'], content=form.cleaned_data['content'], keywords=form.cleaned_data['keywords'], description=form.cleaned_data['description'], cate_id=form.cleaned_data['cate_id'], author=form.cleaned_data['author'], referer=form.cleaned_data['referer'], create_time=form.cleaned_data['create_time'], last_edit_time=now, position_id=form.cleaned_data['position_id'], tpl=post(request, 'tpl')) o_archive.save() tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '添加新文章成功!' } except: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '.无法添加新文章!' } else: print(form.errors) tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '无法添加新文章!', 'err_msg': form.errors } return render_to_response(tpl, ctx)
def edit_act(request): cid = get(request, 'cid') if request.method == 'GET': if not cid: tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'.分类不存在!'} else: try: oCate = Cate.objects.get(id=cid) cate_list = Cate.objects.all().values() o_form = CateForm() o_tree = CateTree(cate_list) cates = o_tree.tree(seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) tpl, ctx = 'admin/cate_edit.html', {'form':o_form, 'oCate': oCate, 'cate_tree_select':cates} print(oCate) except: tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'分类不存在!'} return render_to_response(tpl, ctx) else: pid = int(post(request, 'pid')) cid = int(post(request, 'cid')) name=post(request, 'name') alias = post(request, 'alias') cate_type = post(request, 'cate_type') #print(cate_type) # 检测pid是否为-1 若是则返回错误提示选择父目录 #print(cate_type) if pid < 0: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'选择一个上级分类或作为”顶级分类“!'} return render_to_response(tpl, ctx) elif pid > 0: # 获取父cate p_cate = Cate.objects.filter(id=pid).get() if not p_cate: # 父cate不存在 tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'父分类不存在!'} else: # 父目录存在 则检测兄弟由无重名 若有则提示出错 same_name_siblings = Cate.objects.filter(name=name, pid=pid) siblings_cnt = same_name_siblings.count() if siblings_cnt > 1: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'相同父分类下已经有该名称分类!'} return render_to_response(tpl, ctx) else: # 没有同名兄弟 可以提交(pid/name在上面已经赋值) path = p_cate.path + ',' + str(pid) depth = int(p_cate.depth) + 1 p_has_child = p_cate.has_child elif pid == 0: path = 0 depth = 0 # 本cate入库 alias = alias.strip(' ') o_form = CateForm({'name':name, 'cate_type':cate_type,'alias':alias, 'path':path}) if (o_form.is_valid()): alias, cnt = re.subn(r' +', '-', alias) #print(alias) try: Cate.objects.filter(id=cid).update(path=path, name=name, cate_type=cate_type, depth=depth, pid=pid, alias=alias, has_child=0) # 若父cate的has_child 为0 则修改为1 否则不操作 if pid and not p_has_child: Cate.objects.filter(id=pid).update(has_child=1) #return redirect('admin:category_act', action='list') tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'编辑分类成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'..编辑分类出错!'} raise else: # FIXIT: 无端提示 cate_type 为必填项 tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'编辑分类出错!'} return render_to_response(tpl, ctx)
def user_add(self, request): """ 添加user 表单/处理逻辑 TODO: 表单自动验证,获取cleandate """ # 显示录入表单(检测 dosubmit字段。若为空则显示录入表单) group_list = Group.objects.all().values() if 'do_submit' not in request.POST or not post(request, 'do_submit'): # 获取group form = UserForm({'is_active':1}) #print(dir(request.session)) tpl, ctx = ('admin/user_add.html', {'form': form, 'group_list': group_list, 'user':request.user, 'action_title':'添加用户'}) else: #处理提交后的数据 username = post(request, 'username') password = post(request,'password') isactive = post(request,'is_active') isstaff = post(request,'is_staff') firstname = post(request, 'first_name') if isactive: isactive = 1 if isstaff: isstaff = 1 try: # 验证数据 uInfo = {'username':username, 'password':password, 'first_name':firstname, 'is_staff':isstaff, 'is_active':isactive} uForm = UserForm(uInfo) if(uForm.is_valid()): u = User.objects.create_user(username=username, password=password) u.is_active = 1 u.is_staff = 1 u.is_superuser = int(post(request, 'is_superuser')) u.first_name= firstname u.save() # 若上面添加没有出错才会执行到这里 # raw 添加 user_group 相应条目 # 检查有无group_id post过来 if 'group_ids' in request.POST and request.POST['group_ids']: group_ids = request.POST.getlist('group_ids') if group_ids : u.groups = group_ids; u.groups.add() # print(rs) tpl, ctx = ('admin/msg.html', {'title':'提示', 'content':'添加用户成功!'}) else: tpl, ctx = ('admin/user_add.html', {'is_error':'error', 'form':uForm, 'group_list': group_list, 'content':uForm.errors.as_json()}) except IntegrityError: #XXX: forms.py 里面进行错误控制self.add_error()之后,这里可能就没用了。 tpl, ctx = ('admin/msg.html', {'title':'错误', 'content':'用户名已存在!请选择其他用户名'}) return (tpl, ctx)
def cate_page(self): """ 栏目分两种: # 单网页 # 多文章 """ # 取 cate内容 try: spc = get(self.request, 'spc') o_cate = Category.objects.get(id=self.cid) #print(o_cate.cate_type) if o_cate.cate_type == 'NORMAL': # get archive list and the content of which on top_pos pn = abs(int(post(request, 'pn'))) or 1 #rs = Group.objects.raw('drop table cmsadmin_archive') #print(rs) ppn = 'pn' in request.POST and request.POST['pn'] # 分页参数 p_size = 20 item_total = Group.objects.count() p_total = math.ceil(item_total / p_size) pn = min(p_total, pn) p_next = min(p_total, pn + 1) p_prev = max(1, pn - 1) # 当页内容 start = p_size * (pn - 1) end = p_size + start rs = arch_list = Archive.objects.all() if rs: rs = rs[start:end] p_list = [p for p in range(1, p_total + 1)] menu_list = _get_menus(self.request, get(self.request, 'm')) ctx = { 'pn': pn, 'ppn': ppn, 'p_next': p_next, 'p_prev': p_prev, 'p_total': p_total, 'p_list': p_list, 'rs': rs, 'menuJSON': json.dumps(menu_list), 'menus': menu_list, } tpl, ctx = ('cms/cate_normal.html', ctx) else: #print('sigle') menu_list = _get_menus(self.request, get(self.request, 'm')) # FIXIT: Error if spc: tpl = ''.join( ['cms/cate_spc_', o_cate.alias, '_single.html']) #print(tpl) else: tpl = 'cms/cate_single.html' ctx = { 'menuJSON': json.dumps(menu_list), 'menus': menu_list, } except: #print('Error') raise Http404 return render_to_response(tpl, ctx)
def user_add(self, request): """ 添加user 表单/处理逻辑 TODO: 表单自动验证,获取cleandate """ # 显示录入表单(检测 dosubmit字段。若为空则显示录入表单) group_list = Group.objects.all().values() if 'do_submit' not in request.POST or not post(request, 'do_submit'): # 获取group form = UserForm({'is_active': 1}) #print(dir(request.session)) tpl, ctx = ('admin/user_add.html', { 'form': form, 'group_list': group_list, 'user': request.user, 'action_title': '添加用户' }) else: #处理提交后的数据 username = post(request, 'username') password = post(request, 'password') isactive = post(request, 'is_active') isstaff = post(request, 'is_staff') firstname = post(request, 'first_name') if isactive: isactive = 1 if isstaff: isstaff = 1 try: # 验证数据 uInfo = { 'username': username, 'password': password, 'first_name': firstname, 'is_staff': isstaff, 'is_active': isactive } uForm = UserForm(uInfo) if (uForm.is_valid()): u = User.objects.create_user(username=username, password=password) u.is_active = 1 u.is_staff = 1 u.is_superuser = int(post(request, 'is_superuser')) u.first_name = firstname u.save() # 若上面添加没有出错才会执行到这里 # raw 添加 user_group 相应条目 # 检查有无group_id post过来 if 'group_ids' in request.POST and request.POST[ 'group_ids']: group_ids = request.POST.getlist('group_ids') if group_ids: u.groups = group_ids u.groups.add() # print(rs) tpl, ctx = ('admin/msg.html', { 'title': '提示', 'content': '添加用户成功!' }) else: tpl, ctx = ('admin/user_add.html', { 'is_error': 'error', 'form': uForm, 'group_list': group_list, 'content': uForm.errors.as_json() }) except IntegrityError: #XXX: forms.py 里面进行错误控制self.add_error()之后,这里可能就没用了。 tpl, ctx = ('admin/msg.html', { 'title': '错误', 'content': '用户名已存在!请选择其他用户名' }) return (tpl, ctx)