def bedchamber_list(request, template=None): q = request.GET.get("q", "") page = request.GET.get("page", 1) filter = Q() if q: filter = filter & Q(uuid=q) bedchamber_list = Bedchamber.objects.filter(filter) years = Bedchamber.objects.dates("dtcreated", "month") query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") print query_part new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(bedchamber_list, 20) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return { "bedchamber_list": pages.cur_page.object_list, "years": years, "q": q, "pages": pages, }, template
def my_article(request): """我的文章""" q = request.GET.get("q", "") page = request.GET.get("page", 1) filter = Q(title__icontains=q) if q: articles = request.user.person.article_set.filter(filter).order_by("-dtcreated") else: articles = request.user.person.article_set.order_by("-dtcreated") query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(articles, 10) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("article/my_article.xhtml", { "articles": pages.cur_page.object_list, "pages": pages, "q": q, }, context_instance = RequestContext(request))
def view_category(request, category_pk=None, author_pk=None): """浏览用户某一日志分类文章""" page = request.GET.get("page", 1) if category_pk: category = get_object_or_404(ArticleCategory, pk=category_pk) author = category.author articles = category.article_set.filter(category=category_pk).order_by("-dtcreated") elif author_pk: author = get_object_or_404(User, pk=author_pk) articles = Article.objects.filter(category__isnull=True, author=author).order_by("-dtcreated") query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for q in query_part: if not q.startswith("page="): new_query_parts.append(q) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(articles, 10) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string if category_pk is not None: category_pk = int(category_pk) return render_to_response("article/view_article_category.xhtml", { "articles": pages.cur_page.object_list, "pages": pages, "author": author, "category_pk": category_pk, }, context_instance = RequestContext(request))
def my_attachment(request, template=None): """我的附件管理""" q = request.GET.get("q", "") page = request.GET.get("page", 1) filter = Q(user=request.user) if q: filter = Q(article__title__icontains=q) | Q(file__icontains=q) & filter attachment_list = Attachment.objects.filter(filter) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(attachment_list, 20) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return { "attachment_list": pages.cur_page.object_list, "pages": pages, "q": q, }, template
def follower(request, user_pk): user = get_object_or_404(User, pk=user_pk) profile = user.get_profile() profile.visit_count = F("visit_count") + 1 profile.save() follow_list = user.followers.all() if request.user.is_authenticated(): is_following = Follow.objects.filter(follower=request.user, followee=user).exists() else: is_following = False page = int(request.GET.get("page", 1)) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(follow_list, 30) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("person/follower.html", { "follow_list": follow_list, "show_user": user, "pages": pages, "is_following": is_following, }, context_instance=RequestContext(request))
def show_hot(request): """最热门的Show""" q = request.GET.get("q", "") page = int(request.GET.get("page", 1)) #show_list = Show.objects.all().order_by("-like_count", "-comment_count", "-dtcreated") #流行度的计算参考如下链接 #http://stackoverflow.com/questions/1965341/implementing-a-popularity-algorithm-in-django show_list = Show.objects.filter(is_valid=True).select_related().extra( select = { #"popularity": '(like_count - 1) / POW(TIMESTAMPDIFF(HOUR, dtcreated, CURRENT_TIMESTAMP()) + 2, 1.5)', #如果不对like_count 强制数据类型转换, 由int型转为decimal, 当0 -1 的时候,得到的結果不是 -1 而是2^64 -1 "popularity": '(CAST(like_count AS DECIMAL) - 1) / POW(TIMESTAMPDIFF(HOUR, dtcreated, CURRENT_TIMESTAMP()) + 2, 1.5)', }, order_by = ['-popularity', "-hits", "-dtcreated"] ) if q: show_list = show_list.filter(title__icontains=q) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(show_list, 10) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("show/index.xhtml", locals(), context_instance=RequestContext(request))
def show_tags(request): """显示Show tags """ page = int(request.GET.get("page", 1)) #show_list = Show.objects.all().order_by("-like_count", "-comment_count", "-dtcreated") #流行度的计算参考如下链接 #http://stackoverflow.com/questions/1965341/implementing-a-popularity-algorithm-in-django from tagging.models import Tag tag_list = Tag.objects.cloud_for_model(Show) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(tag_list, 200) pages.cur_page = pages.page(page) #增加tag count 分等级段, 以便以tag cloud 形式展示不同tag 显示大小 import math tag_level_num = 7 tag_max_count = max([t.count for t in pages.cur_page.object_list]) tag_step = math.ceil(float(tag_max_count) / tag_level_num) for t in pages.cur_page.object_list: tag_level = t.count / int(tag_step) setattr(t, "level", tag_level) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("show/tags.html", locals(), context_instance=RequestContext(request))
def search(request): """搜索页面""" if not request.user.is_authenticated(): messages.error(request, u"只有登陆的用户才有搜索用户") context = request.GET.get("context") q = request.GET.get("q","") page = request.GET.get("page", 1) filters = request.GET.get("filters", "").split(",") import time qstart = time.time() if context == "article": from article.views import search return search(request) if context == "person": filter = Q(name__icontains=q) | \ Q(username__icontains=q) |\ Q(abbr_name__icontains=q) |\ Q(uuid__icontains=q) |\ Q(spell_name__icontains=q) if "Staff" in filters and "Student" not in filters: filter = filter & Q(identity=Person.IDENTITY_STAFF) if "Student" in filters and "Staff" not in filters: filter = filter & Q(identity=Person.IDENTITY_STUDENT) if "Male" in filters and "Female" not in filters: filter = filter & Q(gender=Person.GENDER_MALE) if "Female" in filters and "Male" not in filters: filter = filter & Q(gender=Person.GENDER_FEMALE) #persons = Person.objects.filter(Q(name__icontains=p) | Q(spell_name__icontains=p)) persons = Person.objects.filter(filter) search_result = [] for r in persons: search_result.append(r) qend = time.time() qtime = qend - qstart query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") print query_part new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(search_result, 20) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("index/search.html", { "site": u"搜索", "search_result": pages.cur_page.object_list, "q": q, "qtime": qtime, "pages": pages, "filters": filters, }, context_instance=RequestContext(request))
def check_index(request, template=None): page = request.GET.get("page", 1) q = request.GET.get("q", "") if not q: filter = Q() else: filter = Q(klass__uuid=q) | \ Q(klass__name__icontains=q) | \ Q(klass__master__username__icontains=q) | \ Q(klass__master__name__icontains=q) | \ Q(klass__master__uuid__icontains=q) | \ Q(checker__name__icontains=q) | \ Q(checker__username__icontains=q) | \ Q(checker__uuid__icontains=q) #check_list = Check.objects.filter(filter).extra( #{"ranking": 'early_morning_ranking + morning_ranking + afternoon_ranking + evening_ranking + \ #student_attendance_ranking + school_uniform_ranking + \ #classroom_order_ranking + hairstyle_finery_ranking + smoke_drink_ranking + \ #game_ranking + classroom_discipline_ranking + home_visiting_ranking + other_ranking ' #} #).order_by("-dtchecked", "-ranking") check_list = Check.objects.filter(filter).order_by("-dtchecked", "-sum_ranking") query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") print query_part new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(check_list, 20) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return { "check_list": pages.cur_page.object_list, "pages": pages, "q": q, }, template
def check_list(request, template=None): bedchamber_pk = request.GET.get("bedchamber_pk", None) year = request.GET.get("year", None) month = request.GET.get("month", None) page = request.GET.get("page", 1) title = u"" filter = Q() if bedchamber_pk: filter = filter & Q(bedchamber__uuid=bedchamber_pk) bedchamber = get_object_or_404(Bedchamber, pk=bedchamber_pk) title += u" %s 寝室" % unicode(bedchamber) if year: filter = filter & Q(dtchecked__year=year) title += u" %s 年" % unicode(year) if month: filter = filter & Q(dtchecked__month=month) title += u" %s 月份" % unicode(month) check_list = Check.objects.filter(filter) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") print query_part new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(check_list, 20) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return { "check_list": pages.cur_page.object_list, "title": title, "bedchamber_pk": bedchamber_pk, "pages": pages, }, template
def hottest_article(request): """最热门的日志列表""" q = request.GET.get("q", "") page = request.GET.get("page", 1) filter = Q(title__icontains=q) filter = Q(status=Article.STATUS_PUBLISH) if q: filter = filter & Q(title__icontains=q) articles = Article.objects.filter(filter).order_by("-read_count") #作者排行榜 SHOW_HOTTEST_AUTHORS_NUMBER = 20 hottest_authors = Article.objects.values("author").annotate(article_count=Count("author")) hottest_persons = Person.objects.in_bulk(list(hottest_authors.values_list("author", flat=True))) hottest_authors = hottest_authors.order_by("-article_count")[:SHOW_HOTTEST_AUTHORS_NUMBER] # 注意, order_by 返回一个新的排序的結果,并不改变原結果 print hottest_authors for a in hottest_authors: a["author"] = hottest_persons.get(a["author"]) query_string = request.META.get("QUERY_STRING", u"") query_part = query_string.split("&") new_query_parts = [] for qp in query_part: if not qp.startswith("page="): new_query_parts.append(qp) no_page_query_string = u"&".join(new_query_parts) pages = Paginator(articles, 10) pages.cur_page = pages.page(page) pages.page_prefix_link = request.path + "?"+ no_page_query_string return render_to_response("article/hottest_article.html", { "articles": pages.cur_page.object_list, "pages": pages, "q": q, "hottest_authors": hottest_authors, }, context_instance = RequestContext(request))