template_filters.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from flask import current_app
  2. from flaskbb.helpers import time_diff, time_delta_format, check_perm
  3. def format_date(value, format='%Y-%m-%d'):
  4. """
  5. Returns a formatted time string
  6. """
  7. return value.strftime(format)
  8. def time_since(value):
  9. return time_delta_format(value)
  10. def is_online(user):
  11. return user.lastseen >= time_diff()
  12. def is_current_user(user, post):
  13. """
  14. Check if the post is written by the user
  15. """
  16. return post.user_id == user.id
  17. def edit_post(user, post, forum):
  18. """
  19. Check if the post can be edited by the user
  20. """
  21. return check_perm(user, 'editpost', forum, post.user_id)
  22. def delete_post(user, post, forum):
  23. """
  24. Check if the post can be edited by the user
  25. """
  26. return check_perm(user, 'deletepost', forum, post.user_id)
  27. def delete_topic(user, post, forum):
  28. """
  29. Check if the topic can be deleted by the user
  30. """
  31. return check_perm(user, 'deletetopic', forum, post.user_id)
  32. def post_reply(user, forum):
  33. """
  34. Check if the user is allowed to post in the forum
  35. """
  36. return check_perm(user, 'postreply', forum)
  37. def crop_title(title):
  38. """
  39. Crops the title to a specified length
  40. """
  41. length = current_app.config['TITLE_LENGTH']
  42. if len(title) > length:
  43. return title[:length] + "..."
  44. return title