middleware.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: middleware.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-11-12 13:29:17 (CST)
  9. # Last Update:星期三 2017-3-29 13:29:17 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import g, request
  14. from flask_login import current_user
  15. from forums.api.forms import SortForm, SearchForm
  16. def set_form(form):
  17. within = request.args.get('within', 0, type=int)
  18. orderby = request.args.get('orderby', 0, type=int)
  19. desc = request.args.get('desc', 0, type=int)
  20. form.within.data = within
  21. form.orderby.data = orderby
  22. form.desc.data = desc
  23. return form
  24. class GlobalMiddleware(object):
  25. def preprocess_request(self):
  26. g.user = current_user
  27. g.sort_form = SortForm()
  28. g.sort_form = set_form(g.sort_form)
  29. g.search_form = SearchForm()
  30. request.user = current_user._get_current_object()
  31. if request.method == 'GET':
  32. request.data = request.args.to_dict()
  33. else:
  34. request.data = request.json
  35. if request.data is None:
  36. request.data = request.form.to_dict()