filters.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: filter.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-15 00:39:29 (CST)
  9. # Last Update:星期三 2016-6-15 18:44:9 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from maple import redis_data
  15. from maple.settings import setting
  16. from maple.topic.models import Reply, Topic
  17. from maple.user.models import User
  18. class Filters(object):
  19. def timesince(dt, default="just now"):
  20. now = datetime.now()
  21. diff = now - dt
  22. if diff.days > 10:
  23. return dt.strftime('%Y-%m-%d %H:%M')
  24. if diff.days <= 10 and diff.days > 0:
  25. periods = ((diff.days, "day", "days"), )
  26. if diff.days <= 0 and diff.seconds > 3600:
  27. periods = ((diff.seconds / 3600, "hour", "hours"), )
  28. if diff.seconds <= 3600 and diff.seconds > 90:
  29. periods = ((diff.seconds / 60, "minute", "minutes"), )
  30. if diff.seconds <= 90:
  31. return default
  32. # periods = ((diff.days / 365, "year", "years"),
  33. # (diff.days / 30, "month", "months"),
  34. # (diff.days / 7, "week", "weeks"),
  35. # (diff.days, "day", "days"),
  36. # (diff.seconds / 3600, "hour", "hours"),
  37. # (diff.seconds / 60, "minute", "minutes"),
  38. # (diff.seconds, "second", "seconds"), )
  39. for period, singular, plural in periods:
  40. if period:
  41. return "%d %s ago" % (period, singular if period == 1 else
  42. plural)
  43. return default
  44. def get_last_reply(uid):
  45. reply = Reply.query.join(Reply.topic).filter(Topic.id == uid).first()
  46. return reply
  47. def get_user_infor(name):
  48. user = User.query.filter(User.username == name).first()
  49. return user
  50. def get_read_count(id):
  51. read = redis_data.hget('topic:%s' % str(id), 'read')
  52. replies = redis_data.hget('topic:%s' % str(id), 'replies')
  53. if not read:
  54. read = 0
  55. else:
  56. read = int(read)
  57. if not replies:
  58. replies = 0
  59. else:
  60. replies = int(replies)
  61. return replies, read
  62. class Title(object):
  63. title = setting['title']
  64. picture = setting['picture']
  65. description = setting['description']