views.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.db import connection
  2. from django.http import HttpResponseRedirect
  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. def current_record(request, date):
  8. page = request.GET.get('page')
  9. if page is None:
  10. page = 0
  11. result = get_record_and_tags()
  12. context = {'current_record': date,
  13. 'articles': article.get_article(is_paginator=True, page=int(page), date_record=date),
  14. 'records': result['records'],
  15. 'tags': result['tags']
  16. }
  17. return render(request, 'category.html', context=context)
  18. def avatar(request, pk):
  19. sql = '''select avatar from auth_user au left join avatar_avatar aa on au.id = aa.user_id where au.id = {0}'''.format(
  20. pk)
  21. cursor = connection.cursor()
  22. cursor.execute(sql)
  23. fetchall = cursor.fetchall()
  24. return HttpResponseRedirect('/' + fetchall[0][0]) # 跳转到主界面
  25. # Create your views here.
  26. @xframe_options_sameorigin
  27. def index(request):
  28. article_sql = '''SELECT count(*) as count,strftime('%Y-%m',created_time) as datetime FROM blog_article group by strftime('%Y,%m',created_time)'''
  29. 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
  30. order by count desc limit 10'''
  31. cursor = connection.cursor()
  32. cursor.execute(article_sql)
  33. fetchall = cursor.fetchall()
  34. records = []
  35. for obj in fetchall:
  36. dic = {}
  37. dic['count'] = obj[0]
  38. dic['datetime'] = obj[1]
  39. records.append(dic)
  40. cursor.execute(tag_sql)
  41. fetchall = cursor.fetchall()
  42. tags = []
  43. for obj in fetchall:
  44. dic = {}
  45. dic['id'] = obj[0]
  46. dic['name'] = obj[1]
  47. dic['count'] = obj[2]
  48. tags.append(dic)
  49. context = {
  50. 'articles_new': article.get_article(20),
  51. 'records': records,
  52. 'tags': tags
  53. }
  54. return render(request, 'index.html', context=context)
  55. def get_record_and_tags():
  56. article_sql = '''SELECT count(*) as count,strftime('%Y-%m',created_time) as datetime FROM blog_article group by strftime('%Y,%m',created_time)'''
  57. 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
  58. order by count desc limit 10'''
  59. cursor = connection.cursor()
  60. cursor.execute(article_sql)
  61. fetchall = cursor.fetchall()
  62. records = []
  63. for obj in fetchall:
  64. dic = {}
  65. dic['count'] = obj[0]
  66. dic['datetime'] = obj[1]
  67. records.append(dic)
  68. cursor.execute(tag_sql)
  69. fetchall = cursor.fetchall()
  70. tags = []
  71. for obj in fetchall:
  72. dic = {}
  73. dic['id'] = obj[0]
  74. dic['name'] = obj[1]
  75. dic['count'] = obj[2]
  76. tags.append(dic)
  77. context = {
  78. 'records': records,
  79. 'tags': tags
  80. }
  81. return context