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.
 
 
 
 
 
 

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