def index(request): """ 博客首页,展示全部博文 :param request: 请求信息 :param article_type: 文章类型 :return: """ if request.method == "GET": username = request.session.get("username", None) # 获取session中的用户名 user_obj = models.UserInfo.objects.filter( username=username).select_related("blog").first() # 获取用户对象 article_type_list = models.ArticleType.objects.all() # 获取所有的文章类型 current_page = int(request.GET.get("p", "1")) # 当前页码,默认是第一页 article_type = request.GET.get( "at", request.session.get("article_type")) # 获取用户类型,获取不到则从session中获取 request.session["article_type"] = article_type # 将本次查询的文章类型存入session if article_type and article_type != "0": # 有类型查询条件 article_type = int(article_type) # 先变成整数 article_list = models.Article.objects.filter( article_type=article_type).all().select_related( "blog", "blog__user") # 依据类型获取所有的文章 else: article_list = models.Article.objects.all().select_related( "blog", "blog__user") # 没有类型查询条件则获取所有文章 data_count = len(article_list) # 文章总个数 page_obj = Page(current_page, data_count) # 生成分页对象 article_list = article_list[page_obj.start:page_obj.end] # 获取当前页的所有文章 page_str = page_obj.page_str("/") # 获取分页html # 回复最多的文章 hot_recommend = models.Comment.objects.exclude( reply_id__isnull=True).values('article_id').annotate( c=Count('reply_id')) hot_recommend = sorted(list(hot_recommend), key=lambda i: i['c'], reverse=True)[:8] hot_recommend = models.Article.objects.filter( nid__in=[item["article_id"] for item in hot_recommend]).all() # 评论最多的文章 hot_comment = models.Comment.objects.values('article_id').annotate( c=Count('article_id')) hot_comment = sorted(list(hot_comment), key=lambda i: i['c'], reverse=True)[:8] hot_comment = models.Article.objects.filter( nid__in=[item["article_id"] for item in hot_comment]).all() return render( request, 'index.html', { "article_type_list": article_type_list, # 文章类型 'article_list': article_list, # 文章列表 "page_str": page_str, # 分页HTML "article_type": article_type, # 文章类型ID "user_obj": user_obj, # 用户对象 "hot_recommend": hot_recommend, # 吐血推荐文章 "hot_comment": hot_comment, # 评论最多文章 })
def article(request, *args, **kwargs): """ 博主个人文章管理 :param request: :return: """ condition = {} # 筛选条件 for k, v in kwargs.items(): kwargs[k] = int(v) # 将字符串改为数字 if v != "0": condition[k] = v username = request.session.get("username", None) # 获取session中的用户名 user_obj = models.UserInfo.objects.filter( username=username).select_related("blog").first() # 获取用户对象 category_list = models.Category.objects.filter( blog_id=user_obj.blog.nid).all() # 获取用户博客的所有分类 article_type_list = models.ArticleType.objects.all() # 获取文章类型 article_list = models.Article.objects.filter( blog_id=user_obj.blog.nid).filter(**condition).all() # 获取用户的文章 if request.method == "GET": current_page = int(request.GET.get("p", 1)) # 获取用户选择的页码,默认为第一页 page_obj = Page(current_page, len(article_list)) # 获取分页对象 page_str = page_obj.page_str("/backend/article.html") # 获取分页HTML article_list = article_list[page_obj.start:page_obj.end] # 获取当前页数据 return render( request, 'backend_article.html', { "user_obj": user_obj, # 用户对象 "category_list": category_list, # 博客分类列表 "article_type_list": article_type_list, # 文章分类 "article_list": article_list, # 文章列表 "page_str": page_str, # 分页对象 "kwargs": kwargs, # 用户文章分类和博客分类 })
def index(request, **kwargs): if request.method == 'GET': article_type_list = models.Article.type_choices c = {} for k, v in kwargs.items(): kwargs[k] = int(v) if v != '0': c[k] = v all_articles = models.Article.objects.filter( **c).order_by('-create_time') hot_articles = models.Article.objects.filter().order_by( '-up_count')[:5] read_articles = models.Article.objects.filter().order_by( '-read_count')[:5] # 分页 current_page = request.GET.get('p', 1) # 获取对应get数据p: href="/user_list/?p=%s current_page = int(current_page) page_num = 10 page = Page(current_page, len(all_articles), page_num) data = all_articles[page.start:page.end] # 列表切片 page_str = page.page_str(request.path_info) return render( request, 'index.html', { 'all_articles': data, 'page_str': page_str, 'hot': hot_articles, 'read': read_articles, 'article_type_list': article_type_list, 'arg_dict': kwargs, })
def index(request, user_dict): ''' index页面函数 ''' form = CreatAssetForm() search_str = request.GET.get("search_string", "") #得到按主机名过滤条件,默认为"" print(search_str) if request.method == "GET" and user_dict[ 'user'] == 'admin': #如果登录用户是admin,则显示所有主机条目信息 all_asset_list = Asset.objects.all().filter( hostname__icontains=search_str) elif request.method == "GET" and user_dict[ 'user'] != 'admin': #如果不是,则显示当前登录用户所属业务线的主机条目信息 all_asset_list = Asset.objects.filter( bussiness__userinfo__username=user_dict['user'], hostname__icontains=search_str) print(all_asset_list.count()) # all_users = UserInfo.objects.values_list("username") #所有用户名称 # all_env = Env.objects.values_list("name") #所有主机环境名称 username = user_dict['user'] #当前登录用户 # page_num=request.GET.get("page_num") #获取前端点击页码 try: current_page = int(request.GET.get('page_num')) except TypeError: current_page = 1 all_count = all_asset_list.count() # page_obj = Page(current_page,all_count,'/hosts.html') page_obj = Page(current_page, all_count, request.path_info, search_str) all_asset_list = all_asset_list[page_obj.start:page_obj.end] page_str = page_obj.page_html() # p=Paginator(all_asset_list,10) #分页器对象 # # try: # all_asset_list=p.page(page_num) #当前页码的主机条目对象 # now_page_range = p.page_range[int(page_num)-1:int(page_num)+8] #最多显示9条记录 # except PageNotAnInteger: # all_asset_list=p.page(1) #访问/bms/index/页面时page_num=""传入p.page()会报错,所以让显示的主机条目为第一页 # now_page_range=p.page_range[0:9] #最多显示9条记录 # except EmptyPage: # all_asset_list=p.page(p.num_pages) #超过页码总数,则显示最后一页 # print(p.page_range) # # print(all_asset_list) # print(request.GET, type(request.GET)) # print(request.GET.urlencode()) from django.http.request import QueryDict url_obj = QueryDict(mutable=True) url_obj['_getarg'] = request.GET.urlencode() # print(url_obj.urlencode()) url_param = url_obj.urlencode() return render(request, 'index.html', locals())
def chart(request): """图表视图""" current_page = request.GET.get("p", 1) current_page = int(current_page) chart_obj_list = models.Chart.objects.all() chart_obj_count = chart_obj_list.count() page_obj = Page(current_page, chart_obj_count) chart_obj_list = chart_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager('chart.html') return render(request, 'chart.html', {'chart_obj_list': chart_obj_list, 'page_str': page_str})
def wdty_jk(request): """网贷天眼接口""" username = request.session.get("username") # 获取用户名 userobj = models.User.objects.using("default").filter( username=username).first() # 获取用户对象 time_from = "" # 定义起始日期 time_to = "" # 定义终止日期 num_show = 10 # 定义每页显示行数 current_page = 1 # 定义当前页 if request.method == "GET": today = datetime.strftime(datetime.now() + timedelta(-1), "%Y-%m-%d") # 获取昨天日期 default_wdty_jk_dict = { "time_from": today, "time_to": today } # 默认日期为昨天 time_from = request.session.get("wdty_jk_dict", default_wdty_jk_dict).get("time_from") time_to = request.session.get("wdty_jk_dict", default_wdty_jk_dict).get("time_to") current_page = int(request.GET.get("p", "1")) # 获取当前页 elif request.method == "POST": time_from = request.POST.get("time_from") # 获取起始日期 time_to = request.POST.get("time_to") # 获取终止日期 request.session["wdty_jk_dict"] = { "time_from": time_from, "time_to": time_to, } # 将用户此次输入日期存入session current_page = 1 # POST请求则为第一页 url = "https://last.51rz.com/api/ip2peye.php/Ip2peye/blist?token=acb1415727bc2b1375d8f3a221816c1b&time_from={time_from}&time_to={time_to}%2023:59:59&status=1&page_size={num_show}&page_index={current_page}".format( time_from=time_from, time_to=time_to, num_show=num_show, current_page=current_page, ) response = requests.get( url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}) data = response.json() if response.status_code == 200 else { "page_count": 0, "loans": [] } # 获取接口返回的数据 data_count = int(data.get("page_count", 0)) * num_show # 获取总个数 info_list = data.get("loans") # 获取当前页内容 page_obj = Page(current_page, data_count, per_page_count=num_show) # 生成分页对象 page_str = page_obj.page_str("/backend/wdty_jk/") # 获取分页html return render( request, "backend_wdty_jk.html", { "username": username, "userobj": userobj, "info_list": info_list, "page_str": page_str, "time_from": time_from, "time_to": time_to })
def get(self): title = self.get_argument('title', None) current_page = self.get_argument("p", 1) per_page_count = self.get_argument("pre", 10) try: current_page = int(current_page) per_page_count = int(per_page_count) except ValueError as e: Logger().log(e, True) self.redirect('/index') if title: try: data_count = Article.select().where( Article.title.contains(title)).count() if data_count <= 0: return self.redirect('/404') page_obj = Page(current_page=current_page, data_count=data_count, per_page_count=per_page_count) page_html = page_obj.page_str( base_url="search?title={_kw}".format(_kw=title)) search_list = [] if current_page == 1: search_objs = Article.select().where( Article.title.contains(title))[-page_obj.end:] else: search_objs = Article.select().where( Article.title.contains( title))[-page_obj.end:-page_obj.start] for search_obj in search_objs: search_list.append({ 'id': search_obj.id, 'title': search_obj.title, 'summary': search_obj.summary, 'read_count': search_obj.read_count, 'created_date': search_obj.created_date, 'article_type': search_obj.article_type.article_type }) search_list.reverse() if current_page == 1 and len(search_list) < per_page_count: page_html = "" self.render('index/search.html', search_list=search_list, page_html=page_html) except Exception as e: Logger().log(e, True) return self.render('index/500.html') self.redirect('/index')
def get(self, tag_id=None): current_page = self.get_argument("p", 1) per_page_count = self.get_argument("pre", 10) try: current_page = int(current_page) per_page_count = int(per_page_count) except ValueError as e: Logger().log(e, True) return self.redirect('/index') if tag_id: try: data_count = Article.select().where( Article.article_type_id == tag_id).count() if data_count <= 0: return self.redirect('/404') page_obj = Page(current_page=current_page, data_count=data_count, per_page_count=per_page_count) page_html = page_obj.page_str( base_url="/tag/{_tid}?".format(_tid=tag_id, )) tag_list = [] if current_page == 1: tag_objs = Article.select().where( Article.article_type_id == tag_id)[-page_obj.end:] else: tag_objs = Article.select().where( Article.article_type_id == tag_id)[-page_obj.end:-page_obj.start] for search_obj in tag_objs: tag_list.append({ 'id': search_obj.id, 'title': search_obj.title, 'summary': search_obj.summary, 'read_count': search_obj.read_count, 'created_date': search_obj.created_date, 'article_type': search_obj.article_type.article_type }) tag_list.reverse() if current_page == 1 and len(tag_list) < per_page_count: page_html = "" return self.render('index/tag.html', tag_list=tag_list, page_html=page_html) except Exception as e: Logger().log(e, True) return self.render('index/500.html') self.redirect('/index')
def get(self, request): cp = int(request.GET.get("p", 1)) total = len(LIST) npp = request.COOKIES.get("npp", 10) if npp: obj = Page(cp, total, int(npp)) else: obj = Page(cp, total) data = LIST[obj.start:obj.end] pages = obj.pagination("/cms/user_list/") return render(request, 'user_list.html', {"li": data, "pages": pages})
def action(request): """报警策略视图""" current_page = request.GET.get("p", 1) current_page = int(current_page) action_obj_list = models.Action.objects.all() action_obj_count = action_obj_list.count() page_obj = Page(current_page, action_obj_count) action_obj_list = action_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager('action.html') return render(request, 'action.html', { 'action_obj_list': action_obj_list, 'page_str': page_str })
def hosts(request): current_page = int(request.GET.get('page')) all_count = models.Host.objects.all().count() # page_obj=Page(current_page,all_count,'/hosts.html') page_obj = Page(current_page, all_count, request.path_info) #path_info包含当前url # host_list=models.Host.objects.all()[page_obj.start(),page_obj.end()] 添加property装饰器后不用再加()调用 host_list = models.Host.objects.all()[page_obj.start:page_obj.end] page_str = page_obj.page_html() return render(request, 'hosts.html', { 'host_list': host_list, 'page_str': page_str })
def host_group(request): """主机组视图""" current_page = request.GET.get("p", 1) current_page = int(current_page) host_group_obj_list = models.HostGroup.objects.all() host_group_obj_count = host_group_obj_list.count() page_obj = Page(current_page, host_group_obj_count) host_group_obj_list = host_group_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager('host_group.html') return render(request, 'host_group.html', { 'host_group_obj_list': host_group_obj_list, 'page_str': page_str })
def user(request): """用户视图""" current_page = request.GET.get("p", 1) current_page = int(current_page) user_obj_list = models.UserProfile.objects.all() user_obj_count = user_obj_list.count() page_obj = Page(current_page, user_obj_count) user_obj_list = user_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager('user.html') return render(request, 'user.html', { 'user_obj_list': user_obj_list, 'page_str': page_str })
def blog(request, site, **kwargs): blog = models.Blog.objects.filter(site=site).select_related('user').first() UserInfo = models.UserInfo.objects.filter(blog=blog).first() articles_count = models.Article.objects.filter( blog=blog).order_by('-create_time').count() read_articles = models.Article.objects.filter( blog=blog).order_by('-read_count')[:5] hot_articles = models.Article.objects.filter( blog=blog).order_by('-up_count')[:5] #组合搜索 article_type_list = models.Article.type_choices category_list = models.Category.objects.filter(blog=blog) blog = models.Blog.objects.filter(site=site).select_related('user').first() c = {} for k, v in kwargs.items(): kwargs[k] = int(v) if v != '0': c[k] = v # 指定查找所有属于该博主的博文 c['blog'] = blog data_count = models.Article.objects.filter(**c).count() articles = models.Article.objects.filter( **c).order_by('-create_time').select_related('blog') # 分页 current_page = request.GET.get('p', 1) # 获取对应get数据p: href="/user_list/?p=%s current_page = int(current_page) page_num = 10 page = Page(current_page, len(articles), page_num) data = articles[page.start:page.end] # 列表切片 page_str = page.page_str(request.path_info) page_start = page.start return render( request, 'blog.html', { 'userinfo': UserInfo, 'blog': blog, 'articles': data, 'articles_count': articles_count, 'page_str': page_str, 'read': read_articles, 'hot': hot_articles, 'article_type_list': article_type_list, 'category_list': category_list, 'arg_dic': kwargs, 'data_count': data_count, 'page_start': page_start, })
def filter(request, site, condition, val): """ 分类显示 :param request: :param site: 博客站点 :param condition: 筛选条件 :param val: 值 :return: """ blog_obj = models.Blog.objects.filter( site=site).select_related('user').first() if not blog_obj: return redirect('/') tag_list = models.Tag.objects.filter(blog=blog_obj) # 获取博主所有的标签 category_list = models.Category.objects.filter(blog=blog_obj) # 获取博主所有的分类 # date_format(create_time,"%Y-%m") date_list_sql = """select nid, count(nid) as num,strftime("%%Y-%%m",create_time) as ctime from repository_article where blog_id = %s group by strftime("%%Y-%%m",create_time) """ % blog_obj.nid date_list = models.Article.objects.raw(date_list_sql) # 获取博主文章所揽阔的月份 template_name = "home_summary_list.html" if condition == 'tag': template_name = "home_title_list.html" article_list = models.Article.objects.filter(tags__title=val, blog=blog_obj).all() elif condition == 'category': article_list = models.Article.objects.filter(category__title=val, blog=blog_obj).all() elif condition == 'date': article_list = models.Article.objects.filter(blog=blog_obj).extra( where=['strftime("%%Y-%%m",create_time)=%s'], params=[ val, ]).all() else: article_list = [] current_page = int(request.GET.get("p", 1)) # 获取用户选择的页码,默认为第一页 page_obj = Page(current_page, len(article_list)) # 获取分页对象 page_str = page_obj.page_str("/%s/%s/%s.html" % (site, condition, val)) # 获取分页HTML article_list = article_list[page_obj.start:page_obj.end] # 获取当前页数据 return render( request, template_name, { 'blog_obj': blog_obj, 'tag_list': tag_list, 'category_list': category_list, 'date_list': date_list, 'article_list': article_list, 'page_str': page_str, })
def articles(request, *args, **kwargs): # print(kwargs) #获取当前url # print(request.path_info) # from django.urls import reverse # url = reverse('articles',kwargs=kwargs) # print(url) # article_type_list = models.ArticleType.objects.all() # 组合搜索 article_type_list = models.Article.type_choices category_list = models.Category.objects.filter( blog_id=request.session['user_info']['blog__nid']) blog = models.Blog.objects.filter( site=request.session['user_info']['blog__site']).select_related( 'user').first() #组合搜索 c = {} for k, v in kwargs.items(): kwargs[k] = int(v) if v != '0': c[k] = v # 指定查找所有属于该博主的博文 c['blog'] = blog data_count = models.Article.objects.filter(**c).count() articles = models.Article.objects.filter( **c).order_by('-create_time').select_related('blog') #分页 current_page = request.GET.get('p', 1) # 获取对应get数据p: href="/user_list/?p=%s current_page = int(current_page) page = Page(current_page, len(articles), 7) data = articles[page.start:page.end] # 列表切片 page_str = page.page_str(reverse('articles', kwargs=kwargs)) #显示序号 page_start = page.start return render( request, 'BM_articles.html', { 'arts': data, 'page_str': page_str, 'article_type_list': article_type_list, 'category_list': category_list, 'arg_dic': kwargs, 'data_count': data_count, 'page_start': page_start })
def user_list(request): """ cookies 获取测试 """ # 第二个参数为取不到cookies的默认值 page_of_count = int(request.COOKIES.get('page_of_count', 10)) print('cookies', page_of_count) """ 分页测试 """ current_page = request.GET.get('p', 1) current_page = int(current_page) page_obj = Page(current_page, len(LIST), page_of_count, 11) # 获取应当给前端显示的item列表 li = LIST[page_obj.start: page_obj.end] page_str = page_obj.generate_page_str('/user_list?p=') return render(request, 'user_list.html', {'user_list': li, 'page_str': page_str, 'page_of_count': page_of_count})
def users(request, user_dict, id): search_str = request.GET.get("search_string", "") username = user_dict['user'] if request.method == "GET" and user_dict[ 'user'] == 'admin': #admin用户登录(get)时 user_form = UserForm() all_user_list = UserInfo.objects.all().filter( username__icontains=search_str) # 分页功能: try: current_page = int(request.GET.get('page_num')) except TypeError: current_page = 1 all_count = all_user_list.count() # page_obj = Page(current_page,all_count,'/hosts.html') page_obj = Page(current_page, all_count, request.path_info, search_str) all_user_list = all_user_list[page_obj.start:page_obj.end] page_str = page_obj.page_html() return render(request, 'users.html', locals()) elif request.method == "POST": response = {'status': True, 'data': None, 'msg': None} user_form = UserForm(data=request.POST) if user_form.is_valid(): #如果POST过来的数据有效 print("得到有效字段:", user_form.cleaned_data) username = user_form.cleaned_data.pop("username") # 去掉 用户名字段 bid_list = user_form.cleaned_data.pop( "bussinesslines") #得到用户对应的业务线id UserInfo.objects.filter( id=id).update(**user_form.cleaned_data) # 更改除了用户名,业务线其他字段 if user_dict['user'] == 'admin': print("要更新的业务线id:", bid_list) obj = UserInfo.objects.filter(id=id).first() # 要修改的用户obj obj.bussinesslines.set(bid_list) # 只有admin能修改用户对应的业务线 else: #无效则返回False和错误的msg response['status'] = False response['msg'] = user_form.errors return HttpResponse(json.dumps(response)) else: #其他用户get时 user_form = UserForm() all_user_list = UserInfo.objects.filter(username=username).filter( username__icontains=search_str) return render(request, 'users.html', locals())
def wdzj_jk(request): """网贷之家接口""" username = request.session.get("username") # 获取用户名 userobj = models.User.objects.using("default").filter( username=username).first() # 获取用户对象 qdate = "" # 定义日期 num_show = 10 # 定义每页显示行数 current_page = 1 # 定义当前页 if request.method == "GET": today = datetime.strftime(datetime.now() + timedelta(-1), "%Y-%m-%d") # 获取昨天日期 default_wdzj_jk_dict = {"qdate": today} # 默认日期为昨天 qdate = request.session.get("wdzj_jk_dict", default_wdzj_jk_dict).get("qdate") current_page = int(request.GET.get("p", "1")) # 获取当前页 elif request.method == "POST": qdate = request.POST.get("qdate") # 获取起始日期 request.session["wdzj_jk_dict"] = { "qdate": qdate } # 将用户此次输入日期存入session current_page = 1 # POST请求则为第一页 url = "https://last.51rz.com/api/iwdzj.php/IwdzjnewV2/GetNowProjects?token=2e7c3ff493e716d0680d175513b0dff4&date={qdate}&page={current_page}&pageSize={num_show}".format( qdate=qdate, num_show=num_show, current_page=current_page, ) response = requests.get( url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}) data = response.json() if response.status_code == 200 else { "totalCount": 0, "borrowList": [] } # 获取接口返回的数据 data_count = int(data.get("totalCount", 0)) # 获取总个数 info_list = data.get("borrowList") # 获取当前页内容 page_obj = Page(current_page, data_count, per_page_count=num_show) # 生成分页对象 page_str = page_obj.page_str("/backend/wdzj_jk/") # 获取分页html return render( request, "backend_wdzj_jk.html", { "username": username, "userobj": userobj, "info_list": info_list, "page_str": page_str, "qdate": qdate })
def get(self, request, *agrs, **kwargs): LIST = [] msg = Show.objects.order_by("-create_time") for i in msg: LIST.append(i) current_page = request.GET.get('p', 1) current_page = int(current_page) page_obj = Page(current_page, len(LIST)) comments = 1 data = LIST[page_obj.start():page_obj.end()] context = { 'msg': msg, 'content_all': data, 'page_str': page_obj.page_str(reverse('file:inform')), 'comments': comments } return render(request, 'inform.html', context)
def myblog(request, site, nid): blog = models.Blog.objects.filter(site=site).select_related('user').first() UserInfo = models.UserInfo.objects.filter(blog=blog).first() articles_count = models.Article.objects.filter( blog=blog).order_by('-create_time').count() new_articles = models.Article.objects.filter( blog=blog).order_by('-create_time')[:5] obj = models.Article.objects.filter(blog=blog, nid=nid).first() obj_content = models.ArticleDetail.objects.filter(article=obj).first() hot_articles = models.Article.objects.filter( blog=blog).order_by('-up_count')[:5] comment = models.Comment.objects.filter( article_id=nid).order_by('-create_time') #每次请求,阅读数加一 # print(obj.read_count) n = obj.read_count n += 1 models.Article.objects.filter(blog=blog, nid=nid).update(read_count=n) # 分页 current_page = request.GET.get('p', 1) # 获取对应get数据p: href="/user_list/?p=%s current_page = int(current_page) page = Page(current_page, len(comment), 10) data = comment[page.start:page.end] # 列表切片 k = 'content_comment' page_str = page.page_str(request.path_info, k) comment_count = len(comment) page_start = page.start return render( request, 'myblog.html', { 'userinfo': UserInfo, 'blog': blog, 'articles_count': articles_count, 'new': new_articles, 'hot': hot_articles, 'obj': obj, 'obj_content': obj_content, 'comment': data, 'page_str': page_str, 'page_start': page_start, 'comment_count': comment_count, })
def get(self): current_page = self.get_argument("p", 1) per_page_count = self.get_argument("pre", 10) try: current_page = int(current_page) per_page_count = int(per_page_count) except ValueError as e: Logger().log(e, True) self.redirect('/index') try: data_count = Article.select().count() page_obj = Page(current_page=current_page, data_count=data_count, per_page_count=per_page_count) page_html = page_obj.page_str(base_url="index?") at_list = [] if current_page == 1: article_objs = Article.select()[-page_obj.end:] else: article_objs = Article.select()[-page_obj.end:-page_obj.start] for article_obj in article_objs: at_list.append({ 'id': article_obj.id, 'title': article_obj.title, 'summary': article_obj.summary, 'read_count': article_obj.read_count, 'created_date': article_obj.created_date, 'article_type': article_obj.article_type.article_type }) at_list.reverse() except Exception as e: Logger().log(e, True) return self.render('index/500.html') if current_page == 1 and len(at_list) < per_page_count: page_html = "" self.render('index/index.html', at_list=at_list, page_html=page_html)
def detail(request, site, nid): """ 博文详细页 :param request: :param site: :param nid: :return: """ blog_obj = models.Blog.objects.filter( site=site).select_related('user').first() if not blog_obj: return redirect('/') tag_list = models.Tag.objects.filter(blog=blog_obj) # 获取博主所有的标签 category_list = models.Category.objects.filter(blog=blog_obj) # 获取博主所有的分类 # date_format(create_time,"%Y-%m") date_list_sql = """select nid, count(nid) as num,strftime("%%Y-%%m",create_time) as ctime from repository_article where blog_id = %s group by strftime("%%Y-%%m",create_time) """ % blog_obj.nid date_list = models.Article.objects.raw(date_list_sql) # 获取博主文章所揽阔的月份 # 获取文章对象 article = models.Article.objects.filter(blog=blog_obj, nid=nid).select_related( 'category', 'article_detail').first() article.read_count += 1 # 文章阅读数加一 article.save() # 保存文章对象 comment_list = models.Comment.objects.filter( article=article).select_related('reply') # 获取评论列表 current_page = int(request.GET.get("p", 1)) # 获取用户选择的页码,默认为第一页 page_obj = Page(current_page, len(comment_list)) # 获取分页对象 page_str = page_obj.page_str("/%s/%s.html" % (site, nid)) # 获取分页HTML comment_list = comment_list[page_obj.start:page_obj.end] # 获取当前页数据 return render( request, 'home_detail.html', { 'blog_obj': blog_obj, 'tag_list': tag_list, 'category_list': category_list, 'date_list': date_list, 'article': article, 'comment_list': comment_list, 'page_str': page_str, })
def authorization(request, user_dict, id): search_str = request.GET.get("search_string", "") username = user_dict['user'] if request.method == "GET" and user_dict[ 'user'] == 'admin': # admin用户登录(get)时 b_form = BussinessForm() all_bussiness_list = BussinessLine.objects.all().filter( name__icontains=search_str) # 分页功能: print(all_bussiness_list) try: current_page = int(request.GET.get('page_num')) except TypeError: current_page = 1 all_count = all_bussiness_list.count() # page_obj = Page(current_page,all_count,'/hosts.html') page_obj = Page(current_page, all_count, request.path_info, search_str) all_user_list = all_bussiness_list[page_obj.start:page_obj.end] page_str = page_obj.page_html() return render(request, 'authorization.html', locals()) elif request.method == "POST": response = {'status': True, 'data': None, 'msg': None} b_form = BussinessForm(data=request.POST) if b_form.is_valid(): print("得到有效字段:", b_form.cleaned_data) name = b_form.cleaned_data.pop("name") # 去掉业务线名字段 BussinessLine.objects.filter( id=id).update(**b_form.cleaned_data) # 更改除了业务线名,其他字段 if user_dict['user'] == 'admin': print("要更新的业务线名:", name) BussinessLine.objects.filter(id=id).update( name=name) # 只有admin能修改用户对应的业务线 else: # 无效则返回False和错误的msg response['status'] = False response['msg'] = b_form.errors return HttpResponse(json.dumps(response)) else: return render(request, 'users.html', locals())
def application(request): """应用集视图""" template_id = request.GET.get('templateid') if template_id: template_id = int(template_id) else: template_id = 0 template_obj_list = models.Template.objects.all() template_obj = models.Template.objects.filter(id=template_id).first() current_page = request.GET.get("p", 1) current_page = int(current_page) if template_obj: application_obj_list = template_obj.applications.all() application_obj_count = application_obj_list.count() page_obj = Page(current_page, application_obj_count) application_obj_list = application_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='application.html', template_id=template_id) else: application_obj_list = models.Application.objects.all() application_obj_count = application_obj_list.count() page_obj = Page(current_page, application_obj_count) application_obj_list = application_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='application.html', template_id=template_id) return render(request, 'application.html', {'application_obj_list': application_obj_list, 'page_str': page_str, 'template_obj_list': template_obj_list, 'template_id': template_id})
def host(request): """主机视图""" host_group_id = request.GET.get('groupid') if host_group_id: host_group_id = int(host_group_id) else: host_group_id = 0 host_group_obj_list = models.HostGroup.objects.all() host_group_obj = models.HostGroup.objects.filter(id=host_group_id).first() current_page = request.GET.get("p", 1) current_page = int(current_page) if host_group_obj: host_obj_list = host_group_obj.host_set.all() host_obj_count = host_obj_list.count() page_obj = Page(current_page, host_obj_count) host_obj_list = host_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='host.html', host_group_id=host_group_id) else: # 没找到相关主机组 host_obj_list = models.Host.objects.all() host_obj_count = host_obj_list.count() page_obj = Page(current_page, host_obj_count) host_obj_list = host_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='host.html', host_group_id=host_group_id) return render( request, 'host.html', { 'host_obj_list': host_obj_list, 'page_str': page_str, 'host_group_obj_list': host_group_obj_list, 'host_group_id': host_group_id })
def trigger(request): """触发器视图""" template_id = request.GET.get('templateid') if template_id: template_id = int(template_id) else: template_id = 0 template_obj_list = models.Template.objects.all() template_obj = models.Template.objects.filter(id=template_id).first() current_page = request.GET.get("p", 1) current_page = int(current_page) if template_obj: trigger_obj_list = template_obj.trigger_set.all() trigger_obj_count = trigger_obj_list.count() page_obj = Page(current_page, trigger_obj_count) trigger_obj_list = trigger_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='trigger.html', template_id=template_id) else: trigger_obj_list = models.Trigger.objects.all() trigger_obj_count = trigger_obj_list.count() page_obj = Page(current_page, trigger_obj_count) trigger_obj_list = trigger_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='trigger.html', template_id=template_id) return render( request, 'trigger.html', { 'trigger_obj_list': trigger_obj_list, 'page_str': page_str, 'template_obj_list': template_obj_list, 'template_id': template_id })
def template(request): """模板视图""" host_group_id = request.GET.get('groupid') if host_group_id: host_group_id = int(host_group_id) else: host_group_id = 0 host_group_obj_list = models.HostGroup.objects.all() host_group_obj = models.HostGroup.objects.filter(id=host_group_id).first() current_page = request.GET.get("p", 1) current_page = int(current_page) if host_group_obj: template_obj_list = host_group_obj.templates.all() template_obj_count = template_obj_list.count() page_obj = Page(current_page, template_obj_count) template_obj_list = template_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='template.html', host_group_id=host_group_id) else: template_obj_list = models.Template.objects.all() template_obj_count = template_obj_list.count() page_obj = Page(current_page, template_obj_count) template_obj_list = template_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='template.html', host_group_id=host_group_id) return render( request, 'template.html', { 'template_obj_list': template_obj_list, 'page_str': page_str, 'host_group_obj_list': host_group_obj_list, 'host_group_id': host_group_id })
def item(request): """监控项视图""" application_id = request.GET.get('applicationid') if application_id: application_id = int(application_id) else: application_id = 0 application_obj_list = models.Application.objects.all() application_obj = models.Application.objects.filter( id=application_id).first() current_page = request.GET.get("p", 1) current_page = int(current_page) if application_obj: item_obj_list = application_obj.items.all() item_obj_count = item_obj_list.count() page_obj = Page(current_page, item_obj_count) item_obj_list = item_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='item.html', application_id=application_id) else: item_obj_list = models.Item.objects.all() item_obj_count = item_obj_list.count() page_obj = Page(current_page, item_obj_count) item_obj_list = item_obj_list[page_obj.start:page_obj.end] page_str = page_obj.pager(base_url='item.html', application_id=application_id) return render( request, 'item.html', { 'item_obj_list': item_obj_list, 'page_str': page_str, 'application_obj_list': application_obj_list, 'application_id': application_id })
def index(request,*args,**kwargs): ''' 主页面函数 :param request: :param args: :param kwargs: {new_type_id:} :return: ''' if request.method=="GET": register_form = RegisterForm() login_form = LoginForm() publish_form = PublishForm() search_str = request.GET.get("search_string", "") current_type_id = kwargs.get('new_type_id') if current_type_id:current_type_id=int(current_type_id) news_type_list = NewsType.objects.all() news_list = News.objects.filter(**kwargs).order_by('-ctime') # **kwargs <==> new_type_id = kwargs.get('new_type_id') try: current_page = int(request.GET.get('page_num')) except TypeError: current_page = 1 all_count = news_list.count() # page_obj = Page(current_page,all_count,'/hosts.html') page_obj = Page(current_page, all_count, request.path_info, search_str) news_list = news_list[page_obj.start:page_obj.end] page_str = page_obj.page_html() print(news_list) user_dict = request.session.get('is_login', None) print(user_dict) if user_dict: username = user_dict['user'] user_id = user_dict['user_id'] return render(request,"index.html",locals())
def home(request, site): """ 博主个人首页 :param request: :param site: 博主的网站后缀如:http://xxx.com/wupeiqi.html :return: """ blog_obj = models.Blog.objects.filter( site=site).select_related('user').first() if not blog_obj: return redirect('/') tag_list = models.Tag.objects.filter(blog=blog_obj) # 获取博主所有的标签 category_list = models.Category.objects.filter(blog=blog_obj) # 获取博主所有的分类 # date_format(create_time,"%Y-%m") date_list_sql = """select nid, count(nid) as num,strftime("%%Y-%%m",create_time) as ctime from repository_article where blog_id = %s group by strftime("%%Y-%%m",create_time) """ % blog_obj.nid date_list = models.Article.objects.raw(date_list_sql) # 获取博主文章所揽阔的月份 article_list = models.Article.objects.filter( blog=blog_obj).order_by('-nid').all() # 获取博主所有文章 top_article_list = models.Article.objects.filter(blog=blog_obj, top=1).all() # 获取博主置顶文章 current_page = int(request.GET.get("p", 1)) # 获取用户选择的页码,默认为第一页 page_obj = Page(current_page, len(article_list)) # 获取分页对象 page_str = page_obj.page_str("/%s.html" % site) # 获取分页HTML article_list = article_list[page_obj.start:page_obj.end] # 获取当前页数据 return render( request, 'home.html', { 'blog_obj': blog_obj, 'tag_list': tag_list, 'category_list': category_list, 'date_list': date_list, 'article_list': article_list, 'page_str': page_str, 'top_article_list': top_article_list, })