utils.py 882 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. """Helper utilities and decorators."""
  3. import sys
  4. import time
  5. from datetime import datetime
  6. from flask import flash
  7. def flash_errors(form, category='warning'):
  8. """Flash all errors for a form."""
  9. for field, errors in form.errors.items():
  10. for error in errors:
  11. flash('{0} - {1}'.format(getattr(form, field).label.text, error), category)
  12. def date_str_to_obj(ymd):
  13. return time.strptime(ymd, '%Y-%m-%d')
  14. def datetime_str_to_obj(ymd_his):
  15. return datetime.strptime(ymd_his, "%Y-%m-%d %H:%i:%s")
  16. PY2 = int(sys.version[0]) == 2
  17. if PY2:
  18. text_type = unicode # noqa
  19. binary_type = str
  20. string_types = (str, unicode) # noqa
  21. unicode = unicode # noqa
  22. basestring = basestring # noqa
  23. else:
  24. text_type = str
  25. binary_type = bytes
  26. string_types = (str,)
  27. unicode = str
  28. basestring = (str, bytes)