25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

156 lines
5.0 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 get_fullname
  6. from webnotes.webutils import get_access
  7. from webnotes.utils.file_manager import save_file
  8. from webnotes.templates.generators.website_group import get_pathname
  9. def get_post_context(context):
  10. post = webnotes.doc("Post", webnotes.form_dict.name)
  11. if post.parent_post:
  12. raise webnotes.PermissionError
  13. def _get_post_context():
  14. fullname = get_fullname(post.owner)
  15. return {
  16. "title": "{} by {}".format(post.title, fullname),
  17. "parent_post_html": get_parent_post_html(post, context),
  18. "post_list_html": get_child_posts_html(post, context),
  19. "parent_post": post.name
  20. }
  21. cache_key = "website_group_post:{}".format(post.name)
  22. return webnotes.cache().get_value(cache_key, lambda: _get_post_context())
  23. def get_parent_post_html(post, context):
  24. profile = webnotes.bean("Profile", post.owner).doc
  25. for fieldname in ("first_name", "last_name", "user_image", "location"):
  26. post.fields[fieldname] = profile.fields[fieldname]
  27. return webnotes.get_template("templates/includes/inline_post.html")\
  28. .render({"post": post.fields, "view": context.view})
  29. def get_child_posts_html(post, context):
  30. posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name
  31. from tabPost p, tabProfile pr
  32. where p.parent_post=%s and pr.name = p.owner
  33. order by p.creation asc""", (post.name,), as_dict=True)
  34. return webnotes.get_template("templates/includes/post_list.html")\
  35. .render({
  36. "posts": posts,
  37. "parent_post": post.name,
  38. "view": context.view
  39. })
  40. def clear_post_cache(post=None):
  41. cache = webnotes.cache()
  42. posts = [post] if post else webnotes.conn.sql_list("select name from `tabPost`")
  43. for post in posts:
  44. cache.delete_value("website_group_post:{}".format(post))
  45. @webnotes.whitelist(allow_guest=True)
  46. def add_post(group, content, picture, picture_name, title=None, parent_post=None,
  47. assigned_to=None, status=None, event_datetime=None):
  48. access = get_access(get_pathname(group))
  49. if not access.get("write"):
  50. raise webnotes.PermissionError
  51. if parent_post:
  52. if webnotes.conn.get_value("Post", parent_post, "parent_post"):
  53. webnotes.throw("Cannot reply to a reply")
  54. group = webnotes.doc("Website Group", group)
  55. post = webnotes.bean({
  56. "doctype":"Post",
  57. "title": (title or "").title(),
  58. "content": content,
  59. "website_group": group.name,
  60. "parent_post": parent_post or None
  61. })
  62. if not parent_post:
  63. if group.group_type == "Tasks":
  64. post.doc.is_task = 1
  65. post.doc.assigned_to = assigned_to
  66. elif group.group_type == "Events":
  67. post.doc.is_event = 1
  68. post.doc.event_datetime = event_datetime
  69. post.ignore_permissions = True
  70. post.insert()
  71. if picture_name and picture:
  72. process_picture(post, picture_name, picture)
  73. # send email
  74. if parent_post:
  75. post.run_method("send_email_on_reply")
  76. return post.doc.parent_post or post.doc.name
  77. @webnotes.whitelist(allow_guest=True)
  78. def save_post(post, content, picture=None, picture_name=None, title=None,
  79. assigned_to=None, status=None, event_datetime=None):
  80. post = webnotes.bean("Post", post)
  81. access = get_access(get_pathname(post.doc.website_group))
  82. if not access.get("write"):
  83. raise webnotes.PermissionError
  84. # TODO improve error message
  85. if webnotes.session.user != post.doc.owner:
  86. for fieldname in ("title", "content"):
  87. if post.doc.fields.get(fieldname) != locals().get(fieldname):
  88. webnotes.throw("You cannot change: {}".format(fieldname.title()))
  89. if picture and picture_name:
  90. webnotes.throw("You cannot change: Picture")
  91. post.doc.fields.update({
  92. "title": (title or "").title(),
  93. "content": content,
  94. "assigned_to": assigned_to,
  95. "status": status,
  96. "event_datetime": event_datetime
  97. })
  98. post.ignore_permissions = True
  99. post.save()
  100. if picture_name and picture:
  101. process_picture(post, picture_name, picture)
  102. return post.doc.parent_post or post.doc.name
  103. def process_picture(post, picture_name, picture):
  104. from webnotes.templates.generators.website_group import clear_cache
  105. file_data = save_file(picture_name, picture, "Post", post.doc.name, decode=True)
  106. post.doc.picture_url = file_data.file_name or file_data.file_url
  107. webnotes.conn.set_value("Post", post.doc.name, "picture_url", post.doc.picture_url)
  108. clear_cache(website_group=post.doc.website_group)
  109. @webnotes.whitelist()
  110. def suggest_user(group, term):
  111. """suggest a user that has read permission in this group tree"""
  112. profiles = webnotes.conn.sql("""select
  113. pr.name, pr.first_name, pr.last_name,
  114. pr.user_image, pr.location
  115. from `tabProfile` pr
  116. where (pr.first_name like %(term)s or pr.last_name like %(term)s)
  117. and pr.user_type = 'Website User' and pr.enabled=1""",
  118. {"term": "%{}%".format(term), "group": group}, as_dict=True)
  119. template = webnotes.get_template("templates/includes/profile_display.html")
  120. return [{
  121. "value": "{} {}".format(pr.first_name or "", pr.last_name or "").strip(),
  122. "profile_html": template.render({"profile": pr}),
  123. "profile": pr.name
  124. } for pr in profiles]