helpers.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: helpers.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-05-20 13:56:43 (CST)
  9. # Last Update:星期四 2016-7-7 19:44:42 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import abort, current_app
  14. from flask_login import current_user
  15. from time import time
  16. from random import randint
  17. from maple import redis_data
  18. def is_num(num):
  19. if num is not None:
  20. try:
  21. num = int(num)
  22. if num > 0:
  23. return num
  24. else:
  25. abort(404)
  26. except ValueError:
  27. abort(404)
  28. def replies_page(topicId):
  29. app = current_app._get_current_object()
  30. replies = redis_data.hget('topic:%s' % str(topicId), 'replies')
  31. if not replies:
  32. replies = 0
  33. else:
  34. replies = int(replies)
  35. p = app.config['PER_PAGE']
  36. if replies % p == 0:
  37. q = replies // p
  38. else:
  39. q = replies // p + 1
  40. return q
  41. def register_api(site, view, endpoint, url, pk='uid', pk_type='int'):
  42. view_func = view.as_view(endpoint)
  43. site.add_url_rule(url,
  44. defaults={pk: None},
  45. view_func=view_func,
  46. methods=['GET', ])
  47. site.add_url_rule(url, view_func=view_func, methods=['POST', ])
  48. site.add_url_rule('%s<%s:%s>' % (url, pk_type, pk),
  49. view_func=view_func,
  50. methods=['GET', 'PUT', 'DELETE'])
  51. def make_uid():
  52. a = str(int(time()))
  53. b = str(current_user.id).zfill(6)
  54. c = str(randint(10, 99))
  55. return a + b + c