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.
 
 
 
 
 
 

123 regels
3.5 KiB

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