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.
 
 
 
 
 
 

82 lines
2.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 frappe
  5. from frappe.utils import global_date_format, get_fullname, cint
  6. from frappe.website.utils import find_first_image
  7. doctype = "Blog Post"
  8. condition_field = "published"
  9. sort_by = "published_on"
  10. sort_order = "desc"
  11. def get_context(context):
  12. blog_post = context.doc
  13. # this is for double precaution. usually it wont reach this code if not published
  14. if not cint(blog_post.published):
  15. raise Exception, "This blog has not been published yet!"
  16. # temp fields
  17. blog_post.full_name = get_fullname(blog_post.owner)
  18. blog_post.updated = global_date_format(blog_post.published_on)
  19. if blog_post.blogger:
  20. blog_post.blogger_info = frappe.get_doc("Blogger", blog_post.blogger).as_dict()
  21. blog_post.description = blog_post.blog_intro or blog_post.content[:140]
  22. blog_post.metatags = {
  23. "name": blog_post.title,
  24. "description": blog_post.description,
  25. }
  26. image = find_first_image(blog_post.content)
  27. if image:
  28. blog_post.metatags["image"] = image
  29. blog_post.categories = frappe.db.sql_list("select name from `tabBlog Category` order by name")
  30. blog_post.comment_list = frappe.db.sql("""\
  31. select comment, comment_by_fullname, creation
  32. from `tabComment` where comment_doctype="Blog Post"
  33. and comment_docname=%s order by creation""", (blog_post.name,), as_dict=1) or []
  34. return blog_post.__dict__
  35. @frappe.whitelist(allow_guest=True)
  36. def get_blog_list(start=0, by=None, category=None):
  37. condition = ""
  38. if by:
  39. condition = " and t1.blogger='%s'" % by.replace("'", "\'")
  40. if category:
  41. condition += " and t1.blog_category='%s'" % category.replace("'", "\'")
  42. query = """\
  43. select
  44. t1.title, t1.name, t3.name as page_name, t1.published_on as creation,
  45. day(t1.published_on) as day, monthname(t1.published_on) as month,
  46. year(t1.published_on) as year,
  47. ifnull(t1.blog_intro, t1.content) as content,
  48. t2.full_name, t2.avatar, t1.blogger,
  49. (select count(name) from `tabComment` where
  50. comment_doctype='Blog Post' and comment_docname=t1.name) as comments
  51. from `tabBlog Post` t1, `tabBlogger` t2, `tabWebsite Route` t3
  52. where ifnull(t1.published,0)=1
  53. and t1.blogger = t2.name
  54. and t3.docname = t1.name
  55. and t3.ref_doctype = "Blog Post"
  56. %(condition)s
  57. order by published_on desc, name asc
  58. limit %(start)s, 20""" % {"start": start, "condition": condition}
  59. result = frappe.db.sql(query, as_dict=1)
  60. # strip html tags from content
  61. for res in result:
  62. res['published'] = global_date_format(res['creation'])
  63. res['content'] = res['content'][:140]
  64. return result