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 forbid_act(request): cid = int(get(request, 'cid')) act = get(request, 'act') dictAct = {'active': 99, 'forbid': 1, 'del': '0'} try: Cate.objects.filter(id=cid).update( status=dictAct[act.replace(' ', '')]) except: tpl, ctx = 'admin/msg.html', {'title': '错误', 'content': '操作失败!'} return render_to_response(tpl, ctx) return redirect('admin:category_act', action='list')
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 forbid_act(request): cid = int(get(request, 'cid')) act = get(request, 'act') dictAct = {'active':99, 'forbid':1, 'del':'0'} try: Cate.objects.filter(id=cid).update(status=dictAct[act.replace(' ', '')]) except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'操作失败!'} return render_to_response(tpl, ctx) return redirect('admin:category_act', action='list')
def list(self, request): # 分页操作 pn = get(request, 'pn') p_size = 20 p_total = math.ceil(Permission.objects.count()/p_size) if not pn or pn == '0': pn = 1 pn = abs(int(pn)) p_next = pn + 1 if pn > p_total: pn = p_total if p_next > p_total: p_next = p_total start = p_size*(pn - 1) + 1 end = start + p_size if pn - 1: p_prev = pn - 1 else: p_prev = 1 p_list = list(range(p_total + 1))[1:] print(p_list) rs = Permission.objects.all()[start:end] ctx = {'pn':pn, 'p_list':p_list, 'p_next':p_next, 'p_prev':p_prev,'p_total':p_total, 'rs':rs} return render_to_response('admin/permission.html', ctx)
def list(self, request): # 分页操作 pn = get(request, 'pn') p_size = 20 p_total = math.ceil(Permission.objects.count() / p_size) if not pn or pn == '0': pn = 1 pn = abs(int(pn)) p_next = pn + 1 if pn > p_total: pn = p_total if p_next > p_total: p_next = p_total start = p_size * (pn - 1) + 1 end = start + p_size if pn - 1: p_prev = pn - 1 else: p_prev = 1 p_list = list(range(p_total + 1))[1:] print(p_list) rs = Permission.objects.all()[start:end] ctx = { 'pn': pn, 'p_list': p_list, 'p_next': p_next, 'p_prev': p_prev, 'p_total': p_total, 'rs': rs } return render_to_response('admin/permission.html', ctx)
def list_act(request): """ 列表 """ # 分页获取内容 # 显示 cn = abs(int(get(request, 'cn'))) or 1 # 分页参数 c_size = 20 item_total = Cate.objects.count() c_total = math.ceil(item_total / c_size) cn = min(c_total, cn) c_next = min(c_total, cn + 1) c_prev = max(1, cn - 1) # 当页内容 start = c_size * (cn - 1) end = c_size + start rs = Cate.objects.order_by('id')[start:end] if not rs: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '系统内还没有分类!' } else: c_list = [c for c in range(1, c_total + 1)] ctx = { 'cn': cn, 'c_next': c_next, 'c_prev': c_prev, 'c_total': c_total, 'c_list': c_list, 'rs': rs } return render_to_response('admin/category.html', ctx)
def list_act(request): """ 列表 """ # 分页获取内容 # 显示 cn = abs(int(get(request, 'cn'))) or 1 # 分页参数 c_size = 20 item_total = Cate.objects.count() c_total = math.ceil(item_total/c_size) cn = min(c_total, cn) c_next = min(c_total, cn + 1) c_prev = max(1, cn - 1) # 当页内容 start = c_size * (cn - 1) end = c_size + start rs = Cate.objects.order_by('id')[start:end] if not rs: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'系统内还没有分类!'} else: c_list = [c for c in range(1, c_total + 1)] ctx = {'cn': cn, 'c_next':c_next, 'c_prev':c_prev, 'c_total':c_total, 'c_list':c_list,'rs': rs } return render_to_response('admin/category.html', ctx)
def list_act(request): """ 列表 """ # 分页获取内容 # 显示 pn = abs(int(get(request, 'pn'))) or 1 # 分页参数 p_size = 20 item_total = Archive.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 = Archive.objects.all()[start:end] cate_list = list(Category.get_all_as_dict()) if not rs: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'系统内还没有文章!'} else: p_list = [p for p in range(1, p_total + 1)] ctx = {'pn': pn, 'p_next':p_next, 'p_prev':p_prev, 'p_total':p_total, 'p_list':p_list,'rs': rs, 'cate_list': cate_list} return render_to_response('admin/archive.html', ctx)
def user_info(self, request): try: uid = int(get(request, 'uid')) oUser = User.objects.get(id=uid) oGroups = oUser.groups.all() #print(dir(oUser)) #print(oGroups) return 'admin/user-info.html', {'groupList':oGroups,'oUser':oUser} except: #raise return 'admin/msg.html', {'title':'错误', 'content':'用户不存在!'}
def user_active(self, request): uid = max(int(get(request, 'uid')), 0) if not uid: rtn = 'admin/msg.html', {'title':'错误', 'content':'操作失败[id 非法]!'} else: # 修改字段 if User.objects.filter(id=uid).update(is_staff=1): rtn = 'admin/msg.html', {'title':'提示', 'content':'操作成功!'} else: rtn = 'admin/msg.html', {'title':'错误', 'content':'操作失败[无法操作数据库]!'} return rtn
def user_info(self, request): try: uid = int(get(request, 'uid')) oUser = User.objects.get(id=uid) oGroups = oUser.groups.all() #print(dir(oUser)) #print(oGroups) return 'admin/user-info.html', { 'groupList': oGroups, 'oUser': oUser } except: #raise return 'admin/msg.html', {'title': '错误', 'content': '用户不存在!'}
def user_active(self, request): uid = max(int(get(request, 'uid')), 0) if not uid: rtn = 'admin/msg.html', {'title': '错误', 'content': '操作失败[id 非法]!'} else: # 修改字段 if User.objects.filter(id=uid).update(is_staff=1): rtn = 'admin/msg.html', {'title': '提示', 'content': '操作成功!'} else: rtn = 'admin/msg.html', { 'title': '错误', 'content': '操作失败[无法操作数据库]!' } return rtn
def main(req): """ 读取分类/文章 cid -->category id aid -->archive id """ # 首页: 无cid 亦无aid # 分类列表: 只有cid # 内容页: 既有cid亦有aid cid = int(get(req, 'cid')) aid = int(get(req, 'aid')) oCmsPages = CmsPages(cid, aid, req) if aid: #print(__file__) return oCmsPages.content_page() elif cid: #print(__file__) return oCmsPages.cate_page() elif not cid and not aid: page = 'index' else: page = 'Error' return HttpResponse(page)
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_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_del(self, request): # TODO: 删除前需要 js 弹出确认对话框 # 要删除引用group的其他表中的条目 gid = max(int(get(request, 'gid')), 0) print(gid) try: o_group = Group.objects.filter(id=gid) o_group.delete() # 下一步删除auth_group_permissions auth_user_groups 中对应的groupid del_premission_group = Group.objects.raw('delete from `auth_group_premissions` where `group_id` = gid') del_auth_user_groups = Group.objects.raw('delete from `auth_user_groups` where `group_id` = gid') rtn = 'admin/msg.html', {'title': '提示', 'content':'权限组删除成功'} except: rtn = 'admin/msg.html', {'title': '错误', 'content':'权限组删除出错!'} raise return rtn
def group_del(self, request): # TODO: 删除前需要 js 弹出确认对话框 # 要删除引用group的其他表中的条目 gid = max(int(get(request, 'gid')), 0) print(gid) try: o_group = Group.objects.filter(id=gid) o_group.delete() # 下一步删除auth_group_permissions auth_user_groups 中对应的groupid del_premission_group = Group.objects.raw( 'delete from `auth_group_premissions` where `group_id` = gid') del_auth_user_groups = Group.objects.raw( 'delete from `auth_user_groups` where `group_id` = gid') rtn = 'admin/msg.html', {'title': '提示', 'content': '权限组删除成功'} except: rtn = 'admin/msg.html', {'title': '错误', 'content': '权限组删除出错!'} raise return rtn
def content_page(self): """ 取page content 类型: 1/ normal --> cate_type == NORMAL 2/ single --> cate_type == SINGLE """ #try: # 取内容 o_arch = Archive.objects.get(id=self.aid) # get cate cid = o_arch.cate_id o_cate = Category.objects.get(id=cid) oPCate = _get_parent_cates(o_cate) arch_list_of_cate = Archive.objects.filter(cate_id=cid) crntMenu = get(self.request, 'm') #print(crntMenu) menu_list = _get_menus(self.request, crntMenu) #print(menu_list) ctx = { 'o_arch': o_arch, 'arch_list': arch_list_of_cate, 'menuJSON': json.dumps(menu_list['dict']), 'm_lower': crntMenu, 'm': crntMenu.upper(), 'menus': menu_list['list'], } if o_cate.cate_type == 'NORMAL': tpl = 'cms/page_normal.html' else: #o_cate.cate_type == 'SINGLE': if o_arch.tpl: tpl = get_tpl_path('cms') + o_arch.tpl else: tpl = 'cms/page_single.html' #except: # print(__file__) #raise Http404 return render_to_response(tpl, ctx)
def content_page(self): """ 取page content 类型: 1/ normal --> cate_type == NORMAL 2/ single --> cate_type == SINGLE """ #try: # 取内容 o_arch = Archive.objects.get(id=self.aid) # get cate cid = o_arch.cate_id o_cate = Category.objects.get(id=cid) oPCate = _get_parent_cates(o_cate) arch_list_of_cate = Archive.objects.filter(cate_id=cid) crntMenu = get(self.request, 'm') #print(crntMenu) menu_list = _get_menus(self.request, crntMenu) #print(menu_list) ctx = {'o_arch': o_arch, 'arch_list':arch_list_of_cate, 'menuJSON': json.dumps(menu_list['dict']), 'm_lower' : crntMenu, 'm' : crntMenu.upper(), 'menus' : menu_list['list'],} if o_cate.cate_type == 'NORMAL': tpl = 'cms/page_normal.html' else: #o_cate.cate_type == 'SINGLE': if o_arch.tpl: tpl = get_tpl_path('cms') + o_arch.tpl else: tpl = 'cms/page_single.html' #except: # print(__file__) #raise Http404 return render_to_response(tpl, ctx)
def list_act(request): """ 列表 """ # 分页获取内容 # 显示 pn = abs(int(get(request, 'pn'))) or 1 # 分页参数 p_size = 20 item_total = Archive.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 = Archive.objects.all()[start:end] cate_list = list(Category.get_all_as_dict()) if not rs: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '系统内还没有文章!' } else: p_list = [p for p in range(1, p_total + 1)] ctx = { 'pn': pn, 'p_next': p_next, 'p_prev': p_prev, 'p_total': p_total, 'p_list': p_list, 'rs': rs, 'cate_list': cate_list } return render_to_response('admin/archive.html', 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 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 edit_act(request): """ 添加 """ # 检查是否显示表单 if not post(request, 'do_submit'): # 获取分类列表 cate_list = Category.get_tree_as_options() # 获取广告位列表 position_list = Position.get_all_as_checkbox() # 取得本文章内容 a_id = get(request, 'aid') o_arch = Archive.objects.filter(id=a_id).values()[0] #print(type(o_arch['author'])) # 获取cate_name if not o_arch: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'该文章不存在!'} else: # 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) #print(_tpl_list) # 显示表单 user_list = UserGet.all_as_option() tpl, ctx = 'admin/archive_edit.html', {'cates': cate_list, 'user_list': user_list, 'tpl_list': tpl_list, 'pos_list': position_list, 'archive': o_arch} 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 o_archive.filter(id=post(request, 'aid')).update( 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, tpl = post(request, 'tpl'), position_id = form.cleaned_data['position_id'] ) tpl, ctx = 'admin/msg.html', {'title':'提示', 'content':'修改文章成功!'} except: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'无法修改文章!'} raise else: tpl, ctx = 'admin/msg.html', {'title':'错误', 'content':'无法修改文章!', 'err_msg': form.errors} return render_to_response(tpl, ctx)
def edit_act(request): # 检查是否显示表单 if not request.method == 'POST': # 获取分类列表 # 显示表单 mid = get(request, 'mid') menu_list = Menu.objects.all().values() o_tree = CateTree(menu_list) menus = o_tree.tree(seprator=' . ', wrapper='<option value="{2}">{0}</option>', wrapper_all=True) o_menu = Menu.objects.get(id=mid) tpl, ctx = 'admin/menu_edit.html', {'o_menu':o_menu, '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 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.filter(id=get(request, 'mid')) try: menu.update(path=path, name=name, m_type=m_type, code=code, list_order=list_order, url=url, depth=depth, pid=pid, has_child=0 ) # 若父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 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 edit_act(request): """ 添加 """ # 检查是否显示表单 if not post(request, 'do_submit'): # 获取分类列表 cate_list = Category.get_tree_as_options() # 获取广告位列表 position_list = Position.get_all_as_checkbox() # 取得本文章内容 a_id = get(request, 'aid') o_arch = Archive.objects.filter(id=a_id).values()[0] #print(type(o_arch['author'])) # 获取cate_name if not o_arch: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '该文章不存在!' } else: # 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) #print(_tpl_list) # 显示表单 user_list = UserGet.all_as_option() tpl, ctx = 'admin/archive_edit.html', { 'cates': cate_list, 'user_list': user_list, 'tpl_list': tpl_list, 'pos_list': position_list, 'archive': o_arch } 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 o_archive.filter(id=post(request, 'aid')).update( 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, tpl=post(request, 'tpl'), position_id=form.cleaned_data['position_id']) tpl, ctx = 'admin/msg.html', { 'title': '提示', 'content': '修改文章成功!' } except: tpl, ctx = 'admin/msg.html', { 'title': '错误', 'content': '无法修改文章!' } raise else: 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 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)