You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. from webnotes.utils import now_datetime, get_datetime_str
  6. from webnotes.webutils import get_access
  7. from webnotes.templates.generators.website_group import get_views
  8. def get_views():
  9. return views
  10. def get_context(group_context):
  11. return {
  12. "post_list_html": get_post_list_html(group_context["group"]["name"], group_context["view"])
  13. }
  14. @webnotes.whitelist(allow_guest=True)
  15. def get_post_list_html(group, view, limit_start=0, limit_length=20):
  16. access = get_access(group)
  17. if isinstance(view, basestring):
  18. view = get_views(group)["view"]
  19. view = webnotes._dict(view)
  20. # verify permission for paging
  21. if webnotes.local.form_dict.cmd == "get_post_list_html":
  22. if not access.get("read"):
  23. return webnotes.PermissionError
  24. if view.name == "feed":
  25. order_by = "p.creation desc"
  26. else:
  27. now = get_datetime_str(now_datetime())
  28. order_by = """(p.upvotes + post_reply_count - (timestampdiff(hour, p.creation, \"{}\") / 2)) desc,
  29. p.creation desc""".format(now)
  30. posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name,
  31. (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count
  32. from `tabPost` p, `tabProfile` pr
  33. where p.website_group = %s and pr.name = p.owner and ifnull(p.parent_post, '')=''
  34. order by {order_by} limit %s, %s""".format(order_by=order_by),
  35. (group, int(limit_start), int(limit_length)), as_dict=True)
  36. context = {"posts": posts, "limit_start": limit_start, "view": view}
  37. return webnotes.get_template("templates/includes/post_list.html").render(context)
  38. views = {
  39. "popular": {
  40. "name": "popular",
  41. "template_path": "templates/unit_templates/forum_list.html",
  42. "url": "/{group}",
  43. "label": "Popular",
  44. "icon": "icon-heart",
  45. "default": True,
  46. "upvote": True,
  47. "idx": 1
  48. },
  49. "feed": {
  50. "name": "feed",
  51. "template_path": "templates/unit_templates/forum_list.html",
  52. "url": "/{group}?view=feed",
  53. "label": "Feed",
  54. "icon": "icon-rss",
  55. "upvote": True,
  56. "idx": 2
  57. },
  58. "post": {
  59. "name": "post",
  60. "template_path": "templates/unit_templates/base_post.html",
  61. "url": "/{group}?view=post&name={post}",
  62. "label": "Post",
  63. "icon": "icon-comments",
  64. "upvote": True,
  65. "hidden": True,
  66. "no_cache": True,
  67. "idx": 3
  68. },
  69. "edit": {
  70. "name": "edit",
  71. "template_path": "templates/unit_templates/base_edit.html",
  72. "url": "/{group}?view=edit&name={post}",
  73. "label": "Edit Post",
  74. "icon": "icon-pencil",
  75. "hidden": True,
  76. "no_cache": True,
  77. "idx": 4
  78. },
  79. "add": {
  80. "name": "add",
  81. "template_path": "templates/unit_templates/base_edit.html",
  82. "url": "/{group}?view=add",
  83. "label": "Add Post",
  84. "icon": "icon-plus",
  85. "hidden": True,
  86. "idx": 5
  87. },
  88. "settings": {
  89. "name": "settings",
  90. "template_path": "templates/unit_templates/base_settings.html",
  91. "url": "/{group}&view=settings",
  92. "label": "Settings",
  93. "icon": "icon-cog",
  94. "hidden": True,
  95. "idx": 6
  96. }
  97. }