def get_post_list_html(group, view, limit_start=0, limit_length=20): access = get_access(group) if isinstance(view, basestring): view = get_views()[view] view = webnotes._dict(view) # verify permission for paging if webnotes.local.form_dict.cmd == "get_post_list_html": if not access.get("read"): return webnotes.PermissionError if view.name == "feed": order_by = "p.creation desc" else: now = get_datetime_str(now_datetime()) order_by = """(p.upvotes + post_reply_count - (timestampdiff(hour, p.creation, \"{}\") / 2)) desc, p.creation desc""".format(now) posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name, (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count from `tabPost` p, `tabProfile` pr where p.website_group = %s and pr.name = p.owner and ifnull(p.parent_post, '')='' order by {order_by} limit %s, %s""".format(order_by=order_by), (group, int(limit_start), int(limit_length)), as_dict=True) context = {"posts": posts, "limit_start": limit_start, "view": view} return webnotes.get_template("templates/includes/post_list.html").render(context)
def get_parent_post_html(post, access): profile = webnotes.bean("Profile", post.owner).doc for fieldname in ("first_name", "last_name", "fb_username", "fb_hometown", "fb_location"): post.fields[fieldname] = profile.fields[fieldname] return webnotes.get_template("templates/includes/inline_post.html", filters={"scrub_url": scrub_url})\ .render({"post": post.fields, "write": access.get("write")})
def get_post_list_html(group, view, limit_start=0, limit_length=20): access = get_access(group) if isinstance(view, basestring): view = get_views()[view] view = webnotes._dict(view) # verify permission for paging if webnotes.local.form_dict.cmd == "get_post_list_html": if not access.get("read"): return webnotes.PermissionError if view.name=="upcoming": condition = "and p.event_datetime >= %s" order_by = "p.event_datetime asc" else: condition = "and p.event_datetime < %s" order_by = "p.event_datetime desc" # should show based on time upto precision of hour # because the current hour should also be in upcoming now = now_datetime().replace(minute=0, second=0, microsecond=0) posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name, (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count from `tabPost` p, `tabProfile` pr where p.website_group = %s and pr.name = p.owner and ifnull(p.parent_post, '')='' and p.is_event=1 {condition} order by {order_by} limit %s, %s""".format(condition=condition, order_by=order_by), (group, now, int(limit_start), int(limit_length)), as_dict=True) context = {"posts": posts, "limit_start": limit_start, "view": view} return webnotes.get_template("templates/includes/post_list.html").render(context)
def prepare_daily_summary(user, unit_post_map, render_opts=None): allowed_units = [] event_post_map = { "posts": [], "lft": 0, "unit_title": "Events on {}".format(render_opts.get("formatted_event_date")), "public": 1 } for unit in sorted(unit_post_map, key=lambda u: unit_post_map.get(u, {}).get("lft")): if get_access(unit, profile=user).get("read"): allowed_units.append(unit) event_post_map["posts"] += unit_post_map[unit].get("events") or [] if event_post_map["posts"]: allowed_units = ["Events"] + allowed_units if allowed_units: if not render_opts: render_opts = {} render_opts.update({ "allowed_units": allowed_units, "unit_post_map": unit_post_map, "event_post_map": event_post_map }) return webnotes.get_template("templates/emails/daily_summary.html").render(render_opts) else: return ""
def add_post(unit, content, picture, picture_name, parent_post=None): access = get_access(unit) if not access.get("write"): raise webnotes.PermissionError post = webnotes.bean({ "doctype":"Post", "content": markdown2.markdown(content), "unit": unit, "parent_post": parent_post or None }) post.ignore_permissions = True post.insert() if picture_name and picture: file_data = save_file(picture_name, picture, "Post", post.doc.name, decode=True) post.doc.picture_url = file_data.file_name or file_data.file_url webnotes.conn.set_value("Post", post.doc.name, "picture_url", post.doc.picture_url) post.doc.fields.update(webnotes.conn.get_value("Profile", webnotes.session.user, ["first_name", "last_name", "fb_username"], as_dict=True)) webnotes.cache().delete_value("unit_html:" + unit) return webnotes.get_template("templates/includes/inline_post.html").render({"post":post.doc.fields, "write": access.get("write")})
def get_user_details(unit, fb_access_token=None): if fb_access_token: fb_userid = get_fb_userid(fb_access_token) profile = webnotes.conn.get_value("Profile", {"fb_userid": fb_userid}) if not profile: raise webnotes.AuthenticationError # login webnotes.local.login_manager.user = profile webnotes.local.login_manager.post_login() out = { "fb_username": get_fb_username(), "task_count": get_task_count() } access = None if unit != "tasks": access = get_access(unit) out["access"] = access if access and access.get("read"): out["private_units"] = webnotes.get_template("templates/includes/unit_list.html")\ .render({"children": get_child_unit_items(unit, public=0)}) return out
def get_parent_post_html(post, view): profile = webnotes.bean("Profile", post.owner).doc for fieldname in ("first_name", "last_name", "user_image", "fb_hometown", "fb_location"): post.fields[fieldname] = profile.fields[fieldname] return webnotes.get_template("templates/includes/inline_post.html")\ .render({"post": post.fields, "view": view})
def send_login_mail(self, subject, template, add_args): """send mail with login details""" from webnotes.profile import get_user_fullname from webnotes.utils import get_url mail_titles = webnotes.get_hooks().get("login_mail_title", []) title = webnotes.conn.get_default('company') or (mail_titles and mail_titles[0]) or "" full_name = get_user_fullname(webnotes.session['user']) if full_name == "Guest": full_name = "Administrator" args = { 'first_name': self.doc.first_name or self.doc.last_name or "user", 'user': self.doc.name, 'title': title, 'login_url': get_url(), 'user_fullname': full_name } args.update(add_args) sender = webnotes.session.user not in ("Administrator", "Guest") and webnotes.session.user or None webnotes.sendmail(recipients=self.doc.email, sender=sender, subject=subject, message=webnotes.get_template(template).render(args))
def get_formatted_html(subject, message, footer=None): message = scrub_urls(message) return inline_css(webnotes.get_template("templates/emails/standard.html").render({ "content": message, "footer": get_footer(footer), "title": subject }))
def build_page(page_name): context = get_context(page_name) html = webnotes.get_template(context.base_template_path).render(context) if can_cache(context.no_cache): webnotes.cache().set_value("page:" + page_name, html) return html
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ if not args: args = webnotes.local.form_dict args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) # notify commentors commentors = [ d[0] for d in webnotes.conn.sql( """select comment_by from tabComment where comment_doctype=%s and comment_docname=%s and ifnull(unsubscribed, 0)=0""", (comment.doc.comment_doctype, comment.doc.comment_docname)) ] owner = webnotes.conn.get_value(comment.doc.comment_doctype, comment.doc.comment_docname, "owner") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [owner])), doctype='Comment', email_field='comment_by', subject='New Comment on %s: %s' % (comment.doc.comment_doctype, comment.doc.title or comment.doc.comment_docname), message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args, ref_doctype=comment.doc.comment_doctype, ref_docname=comment.doc.comment_docname) template = webnotes.get_template( "lib/website/templates/includes/comment.html") return template.render({"comment": comment.doc.fields})
def get_task_list_html(tasks=None, limit_start=0, limit_length=20): if not tasks: tasks = get_task_list(limit_start, limit_length) return webnotes.get_template("templates/includes/post_list.html", filters={"scrub_url": scrub_url})\ .render({ "posts": tasks, "limit_start":limit_start, "with_unit": True, "unit_map": get_unit_map(tasks), "write": 1 })
def get_child_posts_html(post, view): posts = webnotes.conn.sql("""select p.*, pr.user_image, pr.first_name, pr.last_name from tabPost p, tabProfile pr where p.parent_post=%s and pr.name = p.owner order by p.creation asc""", (post.name,), as_dict=True) return webnotes.get_template("templates/includes/post_list.html")\ .render({ "posts": posts, "parent_post": post.name, "view": view })
def get_unit_settings_html(unit): if not get_access(unit).get("admin"): raise webnotes.PermissionError unit_profiles = webnotes.conn.sql("""select pr.first_name, pr.last_name, pr.fb_username, pr.fb_location, pr.fb_hometown, up.profile, up.`read`, up.`write`, up.`admin` from tabProfile pr, `tabUnit Profile` up where up.profile = pr.name and up.parent=%s""", (unit,), as_dict=1) return webnotes.get_template("templates/includes/unit_settings.html").render({ "public": webnotes.conn.get_value("Unit", unit, "public"), "unit_profiles": unit_profiles })
def notify(arg=None): from webnotes.utils import cstr, get_fullname, get_url webnotes.sendmail(\ recipients=[webnotes.conn.get_value("Profile", arg["contact"], "email") or arg["contact"]], sender= webnotes.conn.get_value("Profile", webnotes.session.user, "email"), subject="New Message from " + get_fullname(webnotes.user.name), message=webnotes.get_template("templates/emails/new_message.html").render({ "from": get_fullname(webnotes.user.name), "message": arg['txt'], "link": get_url() }) )
def add_comment(args=None): """ args = { 'comment': '', 'comment_by': '', 'comment_by_fullname': '', 'comment_doctype': '', 'comment_docname': '', 'page_name': '', } """ if not args: args = webnotes.local.form_dict args['doctype'] = "Comment" page_name = args.get("page_name") if "page_name" in args: del args["page_name"] if "cmd" in args: del args["cmd"] comment = webnotes.bean(args) comment.ignore_permissions = True comment.insert() # since comments are embedded in the page, clear the web cache webnotes.webutils.clear_cache(page_name) # notify commentors commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where comment_doctype=%s and comment_docname=%s and ifnull(unsubscribed, 0)=0""", (comment.doc.comment_doctype, comment.doc.comment_docname))] owner = webnotes.conn.get_value(comment.doc.comment_doctype, comment.doc.comment_docname, "owner") from webnotes.utils.email_lib.bulk import send send(recipients=list(set(commentors + [owner])), doctype='Comment', email_field='comment_by', subject='New Comment on %s: %s' % (comment.doc.comment_doctype, comment.doc.title or comment.doc.comment_docname), message='%(comment)s<p>By %(comment_by_fullname)s</p>' % args, ref_doctype=comment.doc.comment_doctype, ref_docname=comment.doc.comment_docname) template = webnotes.get_template("lib/website/templates/includes/comment.html") return template.render({"comment": comment.doc.fields})
def render_blocks(context): """returns a dict of block name and its rendered content""" from jinja2.utils import concat out = {} template = webnotes.get_template(context["template_path"]) # required as per low level API context = template.new_context(context) # render each block individually for block, render in template.blocks.items(): out[block] = concat(render(context)) return out
def suggest_user(term, group): profiles = webnotes.conn.sql("""select pr.name, pr.first_name, pr.last_name, pr.user_image, pr.fb_location, pr.fb_hometown from `tabProfile` pr where (pr.first_name like %(term)s or pr.last_name like %(term)s) and pr.user_image is not null and pr.enabled=1 and not exists(select wsp.name from `tabWebsite Sitemap Permission` wsp where wsp.website_sitemap=%(group)s and wsp.profile=pr.name)""", {"term": "%{}%".format(term), "group": group}, as_dict=True) template = webnotes.get_template("templates/includes/profile_display.html") return [{ "value": "{} {}".format(pr.first_name, pr.last_name), "profile_html": template.render({"profile": pr}), "profile": pr.name } for pr in profiles]
def suggest_user(group, term): """suggest a user that has read permission in this group tree""" profiles = webnotes.conn.sql("""select pr.name, pr.first_name, pr.last_name, pr.user_image, pr.fb_location, pr.fb_hometown from `tabProfile` pr where (pr.first_name like %(term)s or pr.last_name like %(term)s) and pr.name not in ("Guest", "Administrator") is not null and pr.enabled=1""", {"term": "%{}%".format(term), "group": group}, as_dict=True) template = webnotes.get_template("templates/includes/profile_display.html") return [{ "value": "{} {}".format(pr.first_name or "", pr.last_name or "").strip(), "profile_html": template.render({"profile": pr}), "profile": pr.name } for pr in profiles]
def suggest_user(term, unit): profiles = webnotes.conn.sql("""select pr.name, pr.first_name, pr.last_name, pr.fb_username, pr.fb_location, pr.fb_hometown from `tabProfile` pr where (pr.first_name like %(term)s or pr.last_name like %(term)s) and pr.fb_username is not null and not exists(select up.parent from `tabUnit Profile` up where up.parent=%(unit)s and up.profile=pr.name)""", {"term": "%{}%".format(term), "unit": unit}, as_dict=True) template = webnotes.get_template("templates/includes/profile_display.html") return [{ "value": "{} {}".format(pr.first_name, pr.last_name), "profile_html": template.render({"unit_profile": pr}), "profile": pr.name } for pr in profiles]
def _get_unit_html(unit): unit = webnotes.doc("Unit", unit) parents = webnotes.conn.sql("""select name, unit_title from tabUnit where lft < %s and rgt > %s order by lft asc""", (unit.lft, unit.rgt), as_dict=1) context = { "name": unit.name, "public": unit.public, "forum": unit.forum, "title": unit.unit_title, "parents": parents, "children": get_child_unit_items(unit.name, public=1), "post_list_html": get_post_list_html(unit.name) } return webnotes.get_template("templates/includes/unit.html").render(context)
def write_docs(data, build_sitemap=None, domain=None): from webnotes.utils import global_date_format if webnotes.session.user != "Administrator": raise webnotes.PermissionError if isinstance(data, basestring): data = json.loads(data) template = webnotes.get_template("app/docs/templates/docs.html") data["index"] = data["docs"] data["docs"] = None for name, d in data.items(): if d: if not d.get("title"): d["title"] = d["_label"] if d.get("_parent_page") == "docs.html": d["_parent_page"] = "index.html" if not d.get("_icon"): d["_icon"] = "icon-file-alt" if not d["_icon"].startswith("icon-"): d["_icon"] = "icon-" + d["_icon"] if d.get("_modified"): d["_modified"] = global_date_format(d["_modified"]) with open(get_path("public", "docs", name + ".html"), "w") as docfile: if not d.get("description"): d["description"] = "Help pages for " + d["title"] html = template.render(d) docfile.write(html.encode("utf-8", errors="ignore")) if build_sitemap and domain: if not domain.endswith("/"): domain = domain + "/" content = "" for fname in os.listdir(get_path("public", "docs")): fname = cstr(fname) if fname.endswith(".html"): content += sitemap_link_xml % ( domain + fname, get_timestamp(get_path("public", "docs", fname))) with open(get_path("public", "docs", "sitemap.xml"), "w") as sitemap: sitemap.write(sitemap_frame_xml % content)
def suggest_user(unit, term): """suggest a user that has read permission in this unit tree""" profiles = webnotes.conn.sql("""select pr.name, pr.first_name, pr.last_name, pr.fb_username, pr.fb_location, pr.fb_hometown from `tabProfile` pr where (pr.first_name like %(term)s or pr.last_name like %(term)s) and pr.fb_username is not null""", {"term": "%{}%".format(term), "unit": unit}, as_dict=True) template = webnotes.get_template("templates/includes/profile_display.html") return [{ "value": "{} {}".format(pr.first_name, pr.last_name), "profile_html": template.render({"unit_profile": pr}), "profile": pr.name } for pr in profiles]
def get_post_list_html(unit, limit_start=0, limit_length=20): access = get_access(unit) if webnotes.local.form_dict.cmd=="get_post_list_html": # for paging if not access.get("read"): raise webnotes.PermissionError posts = webnotes.conn.sql("""select p.name, p.unit, p.status, p.assigned_to, p.event_datetime, p.assigned_to_fullname, p.picture_url, p.creation, p.content, pr.fb_username, pr.first_name, pr.last_name, (select count(pc.name) from `tabPost` pc where pc.parent_post=p.name) as post_reply_count from tabPost p, tabProfile pr where p.unit=%s and pr.name = p.owner and ifnull(p.parent_post, '')='' order by p.creation desc limit %s, %s""", (unit, limit_start, limit_length), as_dict=True) return webnotes.get_template("templates/includes/post_list.html", filters={"scrub_url": scrub_url})\ .render({"posts": posts, "limit_start":limit_start, "write": access.get("write")})
def get_post_settings(unit, post_name): if not get_access(unit).get("write"): raise webnotes.PermissionError post = webnotes.bean("Post", post_name).doc if post.unit != unit: raise webnotes.ValidationError("Post does not belong to unit.") profile = None if post.assigned_to: profile = webnotes.conn.get_value("Profile", post.assigned_to, ["first_name", "last_name", "fb_username", "fb_location", "fb_hometown"], as_dict=True) return webnotes.get_template("templates/includes/post_settings.html").render({ "post": post, "unit_profile": profile, "status_options": (webnotes.get_doctype("Post").get_options("status") or "").split("\n") })
def write_docs(data, build_sitemap=None, domain=None): from webnotes.utils import global_date_format if webnotes.session.user != "Administrator": raise webnotes.PermissionError if isinstance(data, basestring): data = json.loads(data) template = webnotes.get_template("app/docs/templates/docs.html") data["index"] = data["docs"] data["docs"] = None for name, d in data.items(): if d: if not d.get("title"): d["title"] = d["_label"] if d.get("_parent_page") == "docs.html": d["_parent_page"] = "index.html" if not d.get("_icon"): d["_icon"] = "icon-file-alt" if not d["_icon"].startswith("icon-"): d["_icon"] = "icon-" + d["_icon"] if d.get("_modified"): d["_modified"] = global_date_format(d["_modified"]) with open(get_path("public", "docs", name + ".html"), "w") as docfile: if not d.get("description"): d["description"] = "Help pages for " + d["title"] html = template.render(d) docfile.write(html.encode("utf-8", errors="ignore")) if build_sitemap and domain: if not domain.endswith("/"): domain = domain + "/" content = "" for fname in os.listdir(get_path("public", "docs")): fname = cstr(fname) if fname.endswith(".html"): content += sitemap_link_xml % (domain + fname, get_timestamp(get_path("public", "docs", fname))) with open(get_path("public", "docs", "sitemap.xml"), "w") as sitemap: sitemap.write(sitemap_frame_xml % content)
def add_sitemap_permission(sitemap_page, profile): if not get_access(sitemap_page).get("admin"): raise webnotes.PermissionError permission = webnotes.bean({ "doctype": "Website Sitemap Permission", "website_sitemap": sitemap_page, "profile": profile, "read": 1 }) permission.insert(ignore_permissions=True) profile = permission.doc.fields profile.update(webnotes.conn.get_value("Profile", profile.profile, ["name", "first_name", "last_name", "user_image", "fb_location", "fb_hometown"], as_dict=True)) return webnotes.get_template("templates/includes/sitemap_permission.html").render({ "profile": profile })
def get_child_posts_html(post, limit_start=0, limit_length=20): if isinstance(post, basestring): post = webnotes.bean("Post", post).doc access = get_access(post.unit) if webnotes.local.form_dict.cmd=="get_child_posts_html": # for paging if not access.get("read"): raise webnotes.PermissionError posts = webnotes.conn.sql("""select p.name, p.unit, p.status, p.is_task, p.assigned_to, p.event_datetime, p.assigned_to_fullname, p.picture_url, p.creation, p.content, p.parent_post, pr.fb_username, pr.first_name, pr.last_name from tabPost p, tabProfile pr where p.parent_post=%s and pr.name = p.owner order by p.creation desc limit %s, %s""", (post.name, limit_start, limit_length), as_dict=True) return webnotes.get_template("templates/includes/post_list.html", filters={"scrub_url": scrub_url})\ .render({"posts": posts, "limit_start":limit_start, "write": access.get("write")})
def add_unit_profile(unit, profile): if not get_access(unit).get("admin"): raise webnotes.PermissionError unit = webnotes.bean("Unit", unit) unit.doclist.append({ "doctype": "Unit Profile", "parentfield": "unit_profiles", "profile": profile, "read": 1 }) unit.ignore_permissions = True unit.save() unit_profile = unit.doclist[-1].fields unit_profile.update(webnotes.conn.get_value("Profile", unit_profile.profile, ["first_name", "last_name", "fb_username", "fb_location", "fb_hometown"], as_dict=True)) return webnotes.get_template("templates/includes/unit_profile.html").render({ "unit_profile": unit_profile })
def _get_unit_html(unit, view=None): unit = webnotes.doc("Unit", unit) parents = webnotes.conn.sql("""select name, unit_title from tabUnit where lft < %s and rgt > %s order by lft asc""", (unit.lft, unit.rgt), as_dict=1) title = unit.unit_title if view: title += ": {}".format(view.title()) parents += [{"name": unit.name, "unit_title": unit.unit_title}] context = { "name": unit.name, "public": unit.public, "title": title, "parents": parents, "children": get_child_unit_items(unit.name, public=1), "post_list_html": get_post_list_html(unit.name, view=view), "view": view } return webnotes.get_template("templates/includes/unit.html").render(context)
def get_item_for_list_in_html(context): return webnotes.get_template("templates/includes/product_in_grid.html").render(context)