Exemple #1
0
def index(request):
	'''
'''
	if not request.user.is_authenticated:
		dic={}
		return render_to_response("login.html",dic)

	else:
		dic={}
		return render_to_response("index.html",dic)
Exemple #2
0
def loginout(request):
	'''
'''
	if request.user.is_authenticated():
		logout(request)
		return HttpResponseRedirect("/")

	else:
		dic={
			"not_matched_prompt":False,
			"user_locked_prompt":False,
		}
		if request.method=="POST":
			login_form=forms.login(request)
			if login_form==0:
				return HttpResponseRedirect("/")
			elif login_form==1:
				dic.update({
					"not_matched_prompt":True
				})
			elif login_form==2:
				dic.update({
					"user_locked_prompt":True
				})
		return render_to_response("login.html",dic,request=request)
Exemple #3
0
def tag(request, name):
    '''
    show the tag list
    '''
    config = models.config.objects.get(enabled=1)
    try:
        theTag = models.tag.objects.get(name=name)
    except:
        return Http404
    theArticles = Paginator(theTag.articles.all(), 20)
    try:
        page = request.GET["page"]
    except:
        page = 1

    dic = {
        "lists": [[theTag, True,
                   theArticles.page(page).object_list]],
        "listType": 2,
        "listDescription": "包含标签“%s”的博文" % name,
        "showPaginator": True,

        # pagination
        "url_template": "/tag/" + name + "/",
        "pages": theArticles.num_pages,
        "current": page,
    }

    dic.update(
        frame(navbarActive=-1, title="标签“%s” - " % name + config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #4
0
def index(request):
    '''
    for showing index page
    '''
    config=models.config.objects.get(enabled=1)

    try:
        page=int(request.GET["page"])
    except:
        page=1
    ls = models.article.objects.list(itemsPerResult=config.articlesPerPage)
    if page>ls.num_pages:
        return HttpResponseRedirect("/")
    dic = {
        # index
        "displayList": ls.page(page).object_list,

        # pagination
        "url_template": "?page=%s",
        "pages": ls.num_pages,
        "current": page,
    }
    if page==1:
        dic.update(stickyList=models.article.objects.listSticky())
    dic.update(frame(0,config.getBrand()))
    return render_to_response("index.html", dic)
Exemple #5
0
def index(request):
    '''
    for showing index page
    '''
    config = models.config.objects.get(enabled=1)

    try:
        page = int(request.GET["page"])
    except:
        page = 1
    ls = models.article.objects.list(itemsPerResult=config.articlesPerPage)
    if page > ls.num_pages:
        return HttpResponseRedirect("/")
    dic = {
        # index
        "displayList": ls.page(page).object_list,

        # pagination
        "url_template": "?page=%s",
        "pages": ls.num_pages,
        "current": page,
    }
    if page == 1:
        dic.update(stickyList=models.article.objects.listSticky())
    dic.update(frame(0, config.getBrand()))
    return render_to_response("index.html", dic)
Exemple #6
0
def tag(request, name):
    '''
    show the tag list
    '''
    config = models.config.objects.get(enabled=1)
    try:
        theTag=models.tag.objects.get(name=name)
    except:
        return Http404
    theArticles=Paginator(theTag.articles.all(),20)
    try:
        page=request.GET["page"]
    except:
        page=1

    dic = {
        "lists": [[theTag,True,theArticles.page(page).object_list]],
        "listType": 2,
        "listDescription": "包含标签“%s”的博文" % name,
        "showPaginator": True,

        # pagination
        "url_template": "/tag/" + name + "/",
        "pages": theArticles.num_pages,
        "current": page,
    }

    dic.update(frame(navbarActive=-1,title="标签“%s” - "%name+config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #7
0
def category(request, categoryId=None):
    '''
    get all categories and their articles
    '''
    config = models.config.objects.get(enabled=1)

    try:
        page = int(request.GET["page"])
    except:
        page = 1

    ar = []
    if not categoryId:
        # show category list only
        c = models.category.objects.annotate(
            article_count=Count("articles")).order_by(
                "-article_count"
            )  # sort by the count of articles combined to the category
        for i in c:
            ctgry = [i, False, []]
            for atcls in models.article.objects.list(
                    categoryId=i.id, itemsPerResult=3,
                    stickyIncluded=True).page(1):
                ctgry[2].append(atcls)
            ar.append(ctgry)
        dic = {
            "lists": ar,
            "listType": 1,
            "listDescription": "",
            "showPaginator": False,
        }

    else:
        # show articles behind the given category
        # show has a paginator
        try:
            c = models.category.objects.get(id=categoryId)
        except:
            return Http404
        categoryArticlePaginator = models.article.objects.list(
            categoryId=c.id, itemsPerResult=20, stickyIncluded=True)
        ctgry = [c, True, []]
        for atcls in categoryArticlePaginator.page(page):
            ctgry[2].append(atcls)
        ar.append(ctgry)
        dic = {
            "lists": ar,
            "listType": 1,
            "listDescription": "分类“%s”的博文" % c.name,
            "showPaginator": True,

            # pagination
            "url_template": "/category/" + str(categoryId) + "/%s/",
            "pages": categoryArticlePaginator.num_pages,
            "current": page,
        }

    dic.update(frame(navbarActive=1, title="分类 - " + config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #8
0
def about(request):
    '''
    show about page
    '''
    config=models.config.objects.get(enabled=1)
    dic = {
        # about
        "about": config.ownerSelfIntro
    }
    dic.update(frame(4,"关于 - "+config.getBrand()))
    return render_to_response("about.html",dic)
Exemple #9
0
def about(request):
    '''
    show about page
    '''
    config = models.config.objects.get(enabled=1)
    dic = {
        # about
        "about": config.ownerSelfIntro
    }
    dic.update(frame(4, "关于 - " + config.getBrand()))
    return render_to_response("about.html", dic)
Exemple #10
0
def article(request, slug):
    '''
    show an article
    '''
    config=models.config.objects.get(enabled=1)
    try:
        atcle = models.article.objects.get(slug=slug)
        if not atcle.shown:
            return render_to_response("404.html", status=404)
        atcle.accessFrequency = F("accessFrequency")+1
        atcle.save()
        # F函数使得原本的AccessFrequency过期且无法使用
        # 故须要重新读取
        atcle = models.article.objects.get(slug=slug)
    except:
        return render_to_response("404.html", status=404)
    dic = {
        "article": atcle,
    }
    dic.update(frame(navbarActive=-1,title=atcle.title+" - "+config.getBrand()))
    return render_to_response("article.html", dic, request=request)
Exemple #11
0
def article(request, slug):
    '''
    show an article
    '''
    config = models.config.objects.get(enabled=1)
    try:
        atcle = models.article.objects.get(slug=slug)
        if not atcle.shown:
            return render_to_response("404.html", status=404)
        atcle.accessFrequency = F("accessFrequency") + 1
        atcle.save()
        # F函数使得原本的AccessFrequency过期且无法使用
        # 故须要重新读取
        atcle = models.article.objects.get(slug=slug)
    except:
        return render_to_response("404.html", status=404)
    dic = {
        "article": atcle,
    }
    dic.update(
        frame(navbarActive=-1, title=atcle.title + " - " + config.getBrand()))
    return render_to_response("article.html", dic, request=request)
Exemple #12
0
def searchArticle(request):
    '''
    search the title and content of articles.
    '''
    config = models.config.objects.get(enabled=1)
    try:
        toSearch=request.GET["keywords"]
    except:
        return HttpResponseBadRequest("")

    dic = {
        "lists": [[None, False, models.article.objects.search(toSearch)]],
        "listDescription": "搜索包含“%s”的结果" % html.escape(toSearch),
        "listType": 3,
        "showPaginator": False
    }
    dic.update(frame(navbarActive=-1,title="搜索结果 - "+config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #13
0
def searchArticle(request):
    '''
    search the title and content of articles.
    '''
    config = models.config.objects.get(enabled=1)
    try:
        toSearch = request.GET["keywords"]
    except:
        return HttpResponseBadRequest("")

    dic = {
        "lists": [[None, False,
                   models.article.objects.search(toSearch)]],
        "listDescription": "搜索包含“%s”的结果" % html.escape(toSearch),
        "listType": 3,
        "showPaginator": False
    }
    dic.update(frame(navbarActive=-1, title="搜索结果 - " + config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #14
0
def flatPage(request, slug=""):
    '''
    展示静态页
    '''
    config = models.config.objects.get(enabled=1)
    try:
        page = int(request.GET["page"])
    except:
        page = 1

    print(slug)
    if slug:
        # show one flatpage
        showLinkToOriginal = False
        try:
            ls = Paginator([models.flatPage.objects.get(slug=slug)],
                           per_page=1)
            if not ls.object_list[0].shown: raise Exception()
        except:
            return HttpResponseNotFound()
    else:
        # display a flatpages list
        showLinkToOriginal = True
        ls = Paginator(models.flatPage.objects.filter(shown=True),
                       config.articlesPerPage, 0)
        if page > ls.num_pages:
            return HttpResponseRedirect("/flatpage/")
    dic = {
        # flatpage
        "displayList": ls.page(page).object_list,
        "showLinkToOriginal": showLinkToOriginal,

        # pagination
        "url_template": "?page=%s",
        "pages": ls.num_pages,
        "current": page,
    }
    dic.update(frame(3, "静态页 - " + config.getBrand()))
    return render_to_response("flatpage.html", dic)
Exemple #15
0
def reg(request):
	'''
'''
	if request.user.is_authenticated():
		return HttpResponseRedirect("/")

	else:
		dic={
				"username_taken_prompt":False,
				"password_not_matched_prompt":False,
				"captcha_not_matched_prompt":False
		}
		if request.method=="POST":
			reg_form=forms.register(request)
			if reg_form==0:
				return HttpResponseRedirect("/")
			elif reg_form==1:
				dic.update({"username_taken_prompt":True})
			elif reg_form==2:
				dic.update({"password_not_matched_prompt":True})
			elif reg_form==3:
				dic.update({"captcha_not_matched_prompt":True})
		return render_to_response("register.html",dic,request=request)
Exemple #16
0
def flatPage(request,slug=""):
    '''
    展示静态页
    '''
    config=models.config.objects.get(enabled=1)
    try:
        page=int(request.GET["page"])
    except:
        page=1

    print(slug)
    if slug:
        # show one flatpage
        showLinkToOriginal=False
        try:
            ls=Paginator([models.flatPage.objects.get(slug=slug)],per_page=1)
            if not ls.object_list[0].shown:raise Exception()
        except:
            return HttpResponseNotFound()
    else:
        # display a flatpages list
        showLinkToOriginal=True
        ls = Paginator(models.flatPage.objects.filter(shown=True),config.articlesPerPage,0)
        if page>ls.num_pages:
            return HttpResponseRedirect("/flatpage/")
    dic = {
        # flatpage
        "displayList": ls.page(page).object_list,
        "showLinkToOriginal":showLinkToOriginal,

        # pagination
        "url_template": "?page=%s",
        "pages": ls.num_pages,
        "current": page,
    }
    dic.update(frame(3,"静态页 - "+config.getBrand()))
    return render_to_response("flatpage.html",dic)
Exemple #17
0
def archive(request, year=None,month=None):
    '''
    show archives
    '''
    config = models.config.objects.get(enabled=1)
    ar = []

    if year is None and month is None:
        # show archives of this year
        # 如果未指定需要显示哪一段时间的归档,
        # 显示当年的归档以及他们前三篇博文,
        # 然后显示往年各个年份(并不显示其下的归档)
        for i in models.archive.objects.list(year=datetime.now().year):
            atcls = []
            for a in models.article.objects.list(
                    itemsPerResult=3,
                    archiveObj=i,
                    stickyIncluded=True
            ).page(1).object_list:
                atcls.append(a)
            ar.append([i, False, atcls])
        # legacy archives show legacy years
        legacyYears=models.archive.objects.getYears()
        legacyYears.remove(datetime.now().year)
        listDescription = ""

    elif year and not month:
        for i in models.archive.objects.list(year=int(year)):
            atcls = []
            for a in models.article.objects.list(
                    itemsPerResult=3,
                    archiveObj=i,
                    stickyIncluded=True
            ).page(1).object_list:
                atcls.append(a)
            ar.append([i, False, atcls])
        legacyYears=[]
        listDescription = "%s年的归档" % year

    elif int(year) >= 2012 and 1 <= int(month) <= 12:
        i = models.archive.objects.get(
            year=int(year),
            month=int(month)
        )
        atcls = []
        for a in models.article.objects.list(
                itemsPerResult=999,
                archiveObj=i,
                stickyIncluded=True
        ).page(1).object_list:
            atcls.append(a)
        ar.append([i, True, atcls])
        legacyYears=[]
        listDescription = "%s年%s月的归档" % (year,month)

    else:
        return Http404

    dic = {
        "config":config, # when creating legacy archives list, domain prefix is needed
        "lists": ar,
        "legacyYears":legacyYears, # for display older year, in archive page only
        "listType": 0,
        "listDescription": listDescription,
        "showPaginator": False,
    }
    dic.update(frame(navbarActive=2,title="分类 - "+config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #18
0
def category(request, categoryId=None):
    '''
    get all categories and their articles
    '''
    config = models.config.objects.get(enabled=1)

    try:
        page = int(request.GET["page"])
    except:
        page = 1

    ar = []
    if not categoryId:
        # show category list only
        c = models.category.objects.annotate(article_count=Count("articles")).order_by(
            "-article_count")  # sort by the count of articles combined to the category
        for i in c:
            ctgry = [i, False, []]
            for atcls in models.article.objects.list(
                    categoryId=i.id,
                    itemsPerResult=3,
                    stickyIncluded=True
            ).page(1):
                ctgry[2].append(atcls)
            ar.append(ctgry)
        dic = {
            "lists": ar,
            "listType": 1,
            "listDescription": "",
            "showPaginator": False,
        }

    else:
        # show articles behind the given category
        # show has a paginator
        try:
            c = models.category.objects.get(id=categoryId)
        except:
            return Http404
        categoryArticlePaginator = models.article.objects.list(
            categoryId=c.id,
            itemsPerResult=20,
            stickyIncluded=True
        )
        ctgry = [c, True, []]
        for atcls in categoryArticlePaginator.page(page):
            ctgry[2].append(atcls)
        ar.append(ctgry)
        dic = {
            "lists": ar,
            "listType": 1,
            "listDescription": "分类“%s”的博文" % c.name,
            "showPaginator": True,

            # pagination
            "url_template": "/category/" + str(categoryId) + "/%s/",
            "pages": categoryArticlePaginator.num_pages,
            "current":page,
        }

    dic.update(frame(navbarActive=1, title="分类 - " + config.getBrand()))
    return render_to_response("list.html", dic)
Exemple #19
0
def archive(request, year=None, month=None):
    '''
    show archives
    '''
    config = models.config.objects.get(enabled=1)
    ar = []

    if year is None and month is None:
        # show archives of this year
        # 如果未指定需要显示哪一段时间的归档,
        # 显示当年的归档以及他们前三篇博文,
        # 然后显示往年各个年份(并不显示其下的归档)
        for i in models.archive.objects.list(year=datetime.now().year):
            atcls = []
            for a in models.article.objects.list(
                    itemsPerResult=3, archiveObj=i,
                    stickyIncluded=True).page(1).object_list:
                atcls.append(a)
            ar.append([i, False, atcls])
        # legacy archives show legacy years
        legacyYears = models.archive.objects.getYears()
        legacyYears.remove(datetime.now().year)
        listDescription = ""

    elif year and not month:
        for i in models.archive.objects.list(year=int(year)):
            atcls = []
            for a in models.article.objects.list(
                    itemsPerResult=3, archiveObj=i,
                    stickyIncluded=True).page(1).object_list:
                atcls.append(a)
            ar.append([i, False, atcls])
        legacyYears = []
        listDescription = "%s年的归档" % year

    elif int(year) >= 2012 and 1 <= int(month) <= 12:
        i = models.archive.objects.get(year=int(year), month=int(month))
        atcls = []
        for a in models.article.objects.list(
                itemsPerResult=999, archiveObj=i,
                stickyIncluded=True).page(1).object_list:
            atcls.append(a)
        ar.append([i, True, atcls])
        legacyYears = []
        listDescription = "%s年%s月的归档" % (year, month)

    else:
        return Http404

    dic = {
        "config":
        config,  # when creating legacy archives list, domain prefix is needed
        "lists": ar,
        "legacyYears":
        legacyYears,  # for display older year, in archive page only
        "listType": 0,
        "listDescription": listDescription,
        "showPaginator": False,
    }
    dic.update(frame(navbarActive=2, title="分类 - " + config.getBrand()))
    return render_to_response("list.html", dic)