views.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from django.db import connection
  2. from django.http import HttpResponseRedirect, JsonResponse
  3. from django.shortcuts import render
  4. from django.views.decorators.clickjacking import xframe_options_sameorigin
  5. from blog import models
  6. from blog.controller import article
  7. json_str = '''{
  8. "results": {
  9. "category1": {
  10. "name": "Category 1",
  11. "results": [
  12. {
  13. "title": "Result Title",
  14. "url": "/optional/url/on/click",
  15. "image": "optional-image.jpg",
  16. "price": "Optional Price",
  17. "description": "Optional Description"
  18. },
  19. {
  20. "title": "Result Title",
  21. "url": "/optional/url/on/click",
  22. "image": "optional-image.jpg",
  23. "price": "Optional Price",
  24. "description": "Optional Description"
  25. }
  26. ]
  27. },
  28. "category2": {
  29. "name": "Category 2",
  30. "results": [
  31. {
  32. "title": "Result Title",
  33. "url": "/optional/url/on/click",
  34. "image": "optional-image.jpg",
  35. "price": "Optional Price",
  36. "description": "Optional Description"
  37. }
  38. ]
  39. }
  40. },
  41. // optional action below results
  42. "action": {
  43. "url": '/path/to/results',
  44. "text": "View all 202 results"
  45. }
  46. }'''
  47. def search(request):
  48. if request.method == 'GET':
  49. keywords = request.GET.get('keywords')
  50. if keywords is not None:
  51. search_dict = dict()
  52. search_dict['title__contains'] = keywords
  53. search_dict['markdown_text__contains'] = keywords
  54. search_dict['status'] = 1
  55. result = models.Article.objects.filter(**search_dict).values('id', 'intro', 'title', 'category__name')
  56. result_list = {}
  57. for item in result:
  58. if result_list.get(item['category__name']) == None:
  59. result_list[item['category__name']] = {}
  60. result_list[item['category__name']]['title'] = item['category__name']
  61. result_list[item['category__name']]['results'] = [
  62. {
  63. 'title': item['title'],
  64. 'description': item['intro'],
  65. 'url': '/article/{0}.html'.format(item['id'])
  66. }
  67. ]
  68. else:
  69. result_list[item['category__name']]['results'].append({
  70. 'title': item['title'],
  71. 'description': item['intro'],
  72. 'url': '/article/{0}.html'.format(item['id'])
  73. })
  74. return JsonResponse({'results':result_list}, safe=False)
  75. else:
  76. return JsonResponse({'results': []}, safe=False)
  77. else:
  78. return JsonResponse({"success": False, "message": "FUCK YOU"}, safe=False)
  79. def current_record(request, date):
  80. page = request.GET.get('page')
  81. if page is None:
  82. page = 0
  83. result = get_record_and_tags()
  84. context = {'current_category': date,
  85. 'articles': article.get_article(is_paginator=True, page=int(page), date_record=date),
  86. 'records': result['records'],
  87. 'tags': result['tags']
  88. }
  89. return render(request, 'category.html', context=context)
  90. def avatar(request, pk):
  91. sql = '''select avatar from auth_user au left join avatar_avatar aa on au.id = aa.user_id where au.id = {0}'''.format(
  92. pk)
  93. cursor = connection.cursor()
  94. cursor.execute(sql)
  95. fetchall = cursor.fetchall()
  96. return HttpResponseRedirect('/' + fetchall[0][0]) # 跳转到主界面
  97. # Create your views here.
  98. @xframe_options_sameorigin
  99. def index(request):
  100. article_sql = '''SELECT count(*) as count,strftime('%Y-%m',created_time) as datetime FROM blog_article group by strftime('%Y,%m',created_time)'''
  101. tag_sql = '''SELECT bt.id,bt.name,count() as count FROM blog_tags bt left join blog_article_tags bat on bt.id = bat.tags_id group by bt.name
  102. order by count desc limit 10'''
  103. cursor = connection.cursor()
  104. cursor.execute(article_sql)
  105. fetchall = cursor.fetchall()
  106. records = []
  107. for obj in fetchall:
  108. dic = {}
  109. dic['count'] = obj[0]
  110. dic['datetime'] = obj[1]
  111. records.append(dic)
  112. cursor.execute(tag_sql)
  113. fetchall = cursor.fetchall()
  114. tags = []
  115. for obj in fetchall:
  116. dic = {}
  117. dic['id'] = obj[0]
  118. dic['name'] = obj[1]
  119. dic['count'] = obj[2]
  120. tags.append(dic)
  121. context = {
  122. 'articles_new': article.get_article(20),
  123. 'records': records,
  124. 'tags': tags
  125. }
  126. return render(request, 'index.html', context=context)
  127. def get_record_and_tags():
  128. article_sql = '''SELECT count(*) as count,strftime('%Y-%m',created_time) as datetime FROM blog_article group by strftime('%Y,%m',created_time) order by created_time desc'''
  129. tag_sql = '''SELECT bt.id,bt.name,count() as count FROM blog_tags bt left join blog_article_tags bat on bt.id = bat.tags_id group by bt.name
  130. order by count desc limit 10'''
  131. cursor = connection.cursor()
  132. cursor.execute(article_sql)
  133. fetchall = cursor.fetchall()
  134. records = []
  135. for obj in fetchall:
  136. dic = {}
  137. dic['count'] = obj[0]
  138. dic['datetime'] = obj[1]
  139. records.append(dic)
  140. cursor.execute(tag_sql)
  141. fetchall = cursor.fetchall()
  142. tags = []
  143. for obj in fetchall:
  144. dic = {}
  145. dic['id'] = obj[0]
  146. dic['name'] = obj[1]
  147. dic['count'] = obj[2]
  148. tags.append(dic)
  149. context = {
  150. 'records': records,
  151. 'tags': tags
  152. }
  153. return context