views.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #*************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: index.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2015-11-25 02:21:04
  7. #*************************************************************************
  8. #!/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from flask import (render_template, Blueprint)
  11. from maple.question.models import Tags, Questions
  12. from maple import app, login_serializer, login_manager, db
  13. from maple.user.models import User
  14. from maple.board.models import Board_F
  15. from maple.group.models import Group
  16. site = Blueprint('forums', __name__)
  17. @login_manager.token_loader
  18. def load_token(token):
  19. max_age = app.config["REMEMBER_COOKIE_DURATION"].total_seconds()
  20. data = login_serializer.loads(token, max_age=max_age)
  21. user = User.load_by_name(data[0])
  22. if user and data[1] == user.password:
  23. return user
  24. return None
  25. @login_manager.user_loader
  26. def user_loader(id):
  27. user = User.query.get(int(id))
  28. return user
  29. @site.route('/', methods=['GET'])
  30. def index():
  31. from sqlalchemy import or_
  32. clubs = Group.query.limit(15)
  33. schools = Questions.query.join(Questions.tags).filter(
  34. Tags.name == '新闻').offset(0).limit(5)
  35. wulwxys = Questions.query.join(Questions.tags).filter(
  36. Tags.name == '新闻').offset(5).limit(5)
  37. jidians = Questions.query.join(Questions.tags).filter(
  38. Tags.name == '新闻').offset(10).limit(5)
  39. bss = Questions.query.join(Questions.tags).filter(
  40. Tags.name == '新闻').offset(15).limit(5)
  41. masters = Questions.query.join(Questions.tags).filter(or_(
  42. Tags.name == '考研',Tags.name == '研究生新闻')).limit(5)
  43. questions = Questions.query.limit(16)
  44. return render_template('index/index.html',
  45. questions=questions,
  46. schools=schools,
  47. wulwxys=wulwxys,
  48. bss=bss,
  49. jidians=jidians,
  50. masters=masters,
  51. clubs=clubs)
  52. @site.route('/index', methods=['GET'])
  53. def forums():
  54. boards = Board_F.query.all()
  55. return render_template('index/forums.html', boards=boards)
  56. @site.route('/t', methods=['GET'], defaults={'tag': None})
  57. @site.route('/t/<tag>', methods=['GET'])
  58. def tag(tag):
  59. if tag is not None:
  60. questions = Questions.query.join(Questions.tags).\
  61. filter(Tags.name == tag).all()
  62. return render_template('index/tags.html', questions=questions, tag=tag)
  63. else:
  64. tags = db.session.query(Tags.name).group_by(Tags.name).all()
  65. return render_template('index/all_tags.html', tags=tags)
  66. @site.route('/about', methods=['GET'])
  67. def about():
  68. return render_template('index/about.html')