views.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2017 jianglin
  5. # File Name: views.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2017-03-13 13:29:37 (CST)
  9. # Last Update: Sunday 2018-03-04 22:37:00 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (request, flash, redirect, url_for, render_template)
  14. from flask_login import login_required, current_user
  15. from flask_maple.views import MethodView
  16. from forums.permission import confirm_permission
  17. from forums.extension import cache
  18. def cache_key():
  19. if current_user.is_authenticated:
  20. return 'view:{}:{}'.format(current_user.id, request.url)
  21. return 'view:{}'.format(request.url)
  22. def is_confirmed(func):
  23. def _is_confirmed(*args, **kwargs):
  24. if confirm_permission.can():
  25. ret = func(*args, **kwargs)
  26. return ret
  27. flash('请验证你的帐号', 'warning')
  28. return redirect(url_for('user.user', username=current_user.username))
  29. return _is_confirmed
  30. class BaseMethodView(MethodView):
  31. @cache.cached(timeout=180, key_prefix=cache_key)
  32. def dispatch_request(self, *args, **kwargs):
  33. return super(BaseMethodView, self).dispatch_request(*args, **kwargs)
  34. def render_template(self, template, **kwargs):
  35. return render_template(template, **kwargs)
  36. class IsAuthMethodView(BaseMethodView):
  37. decorators = [login_required]
  38. class IsConfirmedMethodView(BaseMethodView):
  39. decorators = [is_confirmed, login_required]