Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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