def get_context(self, context): # this is for double precaution. usually it wont reach this code if not published if not cint(self.published): raise Exception, "This blog has not been published yet!" # temp fields context.full_name = get_fullname(self.owner) context.updated = global_date_format(self.published_on) if self.blogger: context.blogger_info = frappe.get_doc("Blogger", self.blogger).as_dict() context.description = self.blog_intro or self.content[:140] context.metatags = { "name": self.title, "description": context.description, } if "<!-- markdown -->" in context.content: context.content = markdown(context.content) image = find_first_image(self.content) if image: context.metatags["image"] = image context.comment_list = get_comment_list(self.doctype, self.name) context.children = get_children() category = frappe.db.get_value("Blog Category", context.doc.blog_category, ["title", "page_name"], as_dict=1) context.parents = [{"title": category.title, "name": "blog/{0}".format(category.page_name)}]
def setup_app_context(self): self.docs_config = frappe.get_module(self.app + ".config.docs") version = self.hooks.get("app_version")[0] self.app_context = { "app": frappe._dict({ "name": self.app, "title": self.app_title, "description": self.hooks.get("app_description")[0], "version": version, "publisher": self.hooks.get("app_publisher")[0], "icon": self.hooks.get("app_icon")[0], "email": self.hooks.get("app_email")[0], "headline": self.docs_config.headline, "brand_html": getattr(self.docs_config, 'brand_html', None), "sub_heading": self.docs_config.sub_heading, "source_link": self.docs_config.source_link, "hide_install": getattr(self.docs_config, "hide_install", False), "docs_base_url": self.docs_config.docs_base_url, "long_description": markdown(getattr(self.docs_config, "long_description", "")), "license": self.hooks.get("app_license")[0], "branch": getattr(self.docs_config, "branch", None) or "develop", "style": getattr(self.docs_config, "style", ""), "google_analytics_id": getattr(self.docs_config, "google_analytics_id", "") }), "metatags": { "description": self.hooks.get("app_description")[0], }, "get_doctype_app": frappe.get_doctype_app }
def show_attached_email_headers_in_content(self, part): # get the multipart/alternative message try: from html import escape # python 3.x except ImportError: from cgi import escape # python 2.x message = list(part.walk())[1] headers = [] for key in ('From', 'To', 'Subject', 'Date'): value = cstr(message.get(key)) if value: headers.append('{label}: {value}'.format(label=_(key), value=escape(value))) self.text_content += '\n'.join(headers) self.html_content += '<hr>' + '\n'.join('<p>{0}</p>'.format(h) for h in headers) if not message.is_multipart() and message.get_content_type( ) == 'text/plain': # email.parser didn't parse it! text_content = self.get_payload(message) self.text_content += text_content self.html_content += markdown(text_content)
def get_static_content(self, context): with open(self.template_path, "r") as contentfile: context.main_section = unicode(contentfile.read(), 'utf-8') self.check_for_redirect(context) if not context.title: context.title = self.name.replace("-", " ").replace("_", " ").title() self.render_dynamic(context) if self.template_path.endswith(".md"): if context.main_section: lines = context.main_section.splitlines() first_line = lines[0].strip() if first_line.startswith("# "): context.title = first_line[2:] context.main_section = "\n".join(lines[1:]) context.main_section = markdown(context.main_section, sanitize=False) for extn in ("js", "css"): fpath = self.template_path.rsplit(".", 1)[0] + "." + extn if os.path.exists(fpath): with open(fpath, "r") as f: context["style" if extn=="css" else "script"] = f.read()
def build_user_docs(self): """Build templates for user docs pages, if missing.""" #user_docs_path = os.path.join(self.docs_path, "user") # license with open(os.path.join(self.app_path, "..", "license.txt"), "r") as license_file: self.app_context["license_text"] = markdown(license_file.read()) html = frappe.render_template("templates/autodoc/license.html", context = self.app_context) with open(os.path.join(self.docs_path, "license.html"), "w") as license_file: license_file.write(html.encode("utf-8")) # contents shutil.copy(os.path.join(frappe.get_app_path("frappe", "templates", "autodoc", "contents.html")), os.path.join(self.docs_path, "contents.html")) shutil.copy(os.path.join(frappe.get_app_path("frappe", "templates", "autodoc", "contents.py")), os.path.join(self.docs_path, "contents.py")) # install html = frappe.render_template("templates/autodoc/install.md", context = self.app_context) with open(os.path.join(self.docs_path, "install.md"), "w") as f: f.write(html) self.update_index_txt(self.docs_path)
def get_message(self): if self.content_type == "HTML": return frappe.render_template(self.message_html, {"doc": self.as_dict()}) return { 'Rich Text': self.message, 'Markdown': markdown(self.message_md) }[self.content_type or 'Rich Text']
def get_read_time(blog): content = blog.content or blog.content_html if blog.content_type == "Markdown": content = markdown(blog.content_md) total_words = len(strip_html_tags(content or "").split()) return ceil(total_words / 250)
def get_static_content(self, context): with open(self.template_path, "r") as contentfile: context.main_section = unicode(contentfile.read(), 'utf-8') self.check_for_redirect(context) if not context.title: context.title = self.name.replace("-", " ").replace("_", " ").title() self.render_dynamic(context) if self.template_path.endswith(".md"): if context.main_section: lines = context.main_section.splitlines() first_line = lines[0].strip() if first_line.startswith("# "): context.title = first_line[2:] context.main_section = "\n".join(lines[1:]) context.main_section = markdown(context.main_section, sanitize=False) for extn in ("js", "css"): fpath = self.template_path.rsplit(".", 1)[0] + "." + extn if os.path.exists(fpath): with open(fpath, "r") as f: context["style" if extn == "css" else "script"] = f.read()
def set_read_time(self): content = self.content or self.content_html or '' if self.content_type == "Markdown": content = markdown(self.content_md) total_words = len(strip_html_tags(content).split()) self.read_time = ceil(total_words / 250)
def get_email(recipients, sender='', msg='', subject='[No Subject]', text_content = None, footer=None, print_html=None, formatted=None, attachments=None, content=None, reply_to=None, cc=[], email_account=None, expose_recipients=None, inline_images=[], header=False): """ Prepare an email with the following format: - multipart/mixed - multipart/alternative - text/plain - multipart/related - text/html - inline image - attachment """ content = content or msg emailobj = EMail(sender, recipients, subject, reply_to=reply_to, cc=cc, email_account=email_account, expose_recipients=expose_recipients) if not content.strip().startswith("<"): content = markdown(content) emailobj.set_html(content, text_content, footer=footer, header=header, print_html=print_html, formatted=formatted, inline_images=inline_images) if isinstance(attachments, dict): attachments = [attachments] for attach in (attachments or []): # cannot attach if no filecontent if attach.get('fcontent') is None: continue emailobj.add_attachment(**attach) return emailobj
def get_message(self): return { 'Rich Text': self.message, 'Markdown': markdown(self.message_md), 'HTML': self.message_html }[self.content_type or 'Rich Text']
def get_email(recipients, sender='', msg='', subject='[No Subject]', text_content = None, footer=None, print_html=None, formatted=None, attachments=None, content=None, reply_to=None, cc=[], bcc=[], email_account=None, expose_recipients=None, inline_images=[], header=None): """ Prepare an email with the following format: - multipart/mixed - multipart/alternative - text/plain - multipart/related - text/html - inline image - attachment """ content = content or msg emailobj = EMail(sender, recipients, subject, reply_to=reply_to, cc=cc, bcc=bcc, email_account=email_account, expose_recipients=expose_recipients) if not content.strip().startswith("<"): content = markdown(content) emailobj.set_html(content, text_content, footer=footer, header=header, print_html=print_html, formatted=formatted, inline_images=inline_images) if isinstance(attachments, dict): attachments = [attachments] for attach in (attachments or []): # cannot attach if no filecontent if attach.get('fcontent') is None: continue emailobj.add_attachment(**attach) return emailobj
def get_context(self, context): if is_markdown(context.content): context.content = markdown(context.content) context.login_required = True context.level_class = get_level_class(self.level) context.comment_list = get_comment_list(self.doctype, self.name) context.children = get_category_sidebar() context.parents = self.get_parents(context)
def get_context(self, context): # this is for double precaution. usually it wont reach this code if not published if not cint(self.published): raise Exception("This blog has not been published yet!") # temp fields context.full_name = get_fullname(self.owner) context.updated = global_date_format(self.published_on) if self.blogger: context.blogger_info = frappe.get_doc("Blogger", self.blogger).as_dict() context.description = self.blog_intro or self.content[:140] context.metatags = { "name": self.title, "description": context.description, } if "<!-- markdown -->" in context.content: context.content = markdown(context.content) blog_settings = frappe.get_doc('Blog Settings', 'Blog Settings') context.enable_comments = blog_settings.enable_comments context.enable_blogger_info = blog_settings.enable_blogger_info image = find_first_image(self.content) if image: context.metatags["image"] = image context.comment_list = get_comment_list(self.doctype, self.name) if not context.comment_list: context.comment_text = _('No comments yet') else: if (len(context.comment_list)) == 1: context.comment_text = _('1 comment') else: context.comment_text = _('{0} comments').format( len(context.comment_list)) context.category = frappe.db.get_value("Blog Category", context.doc.blog_category, ["title", "route"], as_dict=1) context.parents = [{ "name": _("Home"), "route": "/" }, { "name": "Blog", "route": "/blog" }, { "label": context.category.title, "route": context.category.route }]
def get_context(self, context): if is_markdown(context.content): context.content = markdown(context.content) context.login_required = True context.category = frappe.get_doc("Help Category", self.category) context.level_class = get_level_class(self.level) context.comment_list = get_comment_list(self.doctype, self.name) context.show_sidebar = True context.sidebar_items = get_sidebar_items() context.parents = self.get_parents(context)
def get_context(self, context): if is_markdown(context.content): context.content = markdown(context.content) context.login_required = True context.category = frappe.get_doc('Help Category', self.category) context.level_class = get_level_class(self.level) context.comment_list = get_comment_list(self.doctype, self.name) context.show_sidebar = True context.sidebar_items = get_sidebar_items() context.parents = self.get_parents(context)
def after_insert(self): all_providers = frappe.get_all("Frappe Partner", fields=["email"], filters={"show_in_website": 1}) params = self.as_dict() params['job_detail'] = markdown(params['job_detail']) frappe.sendmail( subject = "New Job " + self.job_title, sender = "*****@*****.**", recipients = [p.email for p in all_providers] + ["*****@*****.**"], content = new_job_template.format(**params), as_bulk = True, reference_doctype = self.doctype, reference_name = self.name )
def after_insert(self): all_providers = frappe.get_all("Frappe Partner", fields=["email"], filters={"show_in_website": 1}) params = self.as_dict() params['job_detail'] = markdown(params['job_detail']) frappe.sendmail(subject="New Job " + self.job_title, sender="*****@*****.**", recipients=[p.email for p in all_providers] + ["*****@*****.**"], content=new_job_template.format(**params), delayed=True, reference_doctype=self.doctype, reference_name=self.name)
def get_html_content_based_on_type(doc, fieldname, content_type): ''' Set content based on content_type ''' content = doc.get(fieldname) if content_type == 'Markdown': content = markdown(doc.get(fieldname + '_md')) elif content_type == 'HTML': content = doc.get(fieldname + '_html') if content == None: content = '' return content
def show_attached_email_headers_in_content(self, part): # get the multipart/alternative message message = list(part.walk())[1] headers = [] for key in ('From', 'To', 'Subject', 'Date'): value = cstr(message.get(key)) if value: headers.append('{label}: {value}'.format(label=_(key), value=value)) self.text_content += '\n'.join(headers) self.html_content += '<hr>' + '\n'.join('<p>{0}</p>'.format(h) for h in headers) if not message.is_multipart() and message.get_content_type()=='text/plain': # email.parser didn't parse it! text_content = self.get_payload(message) self.text_content += text_content self.html_content += markdown(text_content)
def sendmail_md(recipients, sender=None, msg=None, subject=None, attachments=None, content=None, reply_to=None, cc=(), message_id=None, in_reply_to=None): """send markdown email""" sendmail(recipients, sender, markdown(content or msg), subject, attachments, reply_to=reply_to, cc=cc)
def get_email(recipients, sender='', msg='', subject='[No Subject]', text_content = None, footer=None, print_html=None, formatted=None, attachments=None, content=None, reply_to=None, cc=[], email_account=None, expose_recipients=None): """send an html email as multipart with attachments and all""" content = content or msg emailobj = EMail(sender, recipients, subject, reply_to=reply_to, cc=cc, email_account=email_account, expose_recipients=expose_recipients) if not content.strip().startswith("<"): content = markdown(content) emailobj.set_html(content, text_content, footer=footer, print_html=print_html, formatted=formatted) if isinstance(attachments, dict): attachments = [attachments] for attach in (attachments or []): emailobj.add_attachment(**attach) return emailobj
def get_email(recipients, sender='', msg='', subject='[No Subject]', text_content = None, footer=None, print_html=None, formatted=None, attachments=None, content=None, reply_to=None, cc=(), email_account=None): """send an html email as multipart with attachments and all""" content = content or msg emailobj = EMail(sender, recipients, subject, reply_to=reply_to, cc=cc, email_account=email_account) if not content.strip().startswith("<"): content = markdown(content) emailobj.set_html(content, text_content, footer=footer, print_html=print_html, formatted=formatted) if isinstance(attachments, dict): attachments = [attachments] for attach in (attachments or []): emailobj.add_attachment(**attach) return emailobj
def get_context(self, context): # this is for double precaution. usually it wont reach this code if not published if not cint(self.published): raise Exception("This blog has not been published yet!") # temp fields context.full_name = get_fullname(self.owner) context.updated = global_date_format(self.published_on) if self.blogger: context.blogger_info = frappe.get_doc("Blogger", self.blogger).as_dict() context.description = self.blog_intro or self.content[:140] context.metatags = { "name": self.title, "description": context.description, } if "<!-- markdown -->" in context.content: context.content = markdown(context.content) image = find_first_image(self.content) if image: context.metatags["image"] = image context.comment_list = get_comment_list(self.doctype, self.name) if not context.comment_list: context.comment_text = _('No comments yet') else: if(len(context.comment_list)) == 1: context.comment_text = _('1 comment') else: context.comment_text = _('{0} comments').format(len(context.comment_list)) context.category = frappe.db.get_value("Blog Category", context.doc.blog_category, ["title", "route"], as_dict=1) context.parents = [{"name": _("Home"), "route":"/"}, {"name": "Blog", "route": "/blog"}, {"label": context.category.title, "route":context.category.route}]
def set_calendar_invite(self, part): from icalendar import Calendar, Event cal = Calendar.from_ical(self.get_payload(part)) start_format = "%a %b %d %H:%M" end_format = "%H:%M" # only hour needed for end time text_content = "" for component in cal.walk('vevent'): event = component.get('summary') description = component.get('description') location = component.get('location') end = component.get('dtend') from frappe.utils.dateutils import datetime_in_user_format start = datetime_in_user_format(component.get('dtstart').dt) total_time = "%s-%s" % (start, end.dt.strftime(end_format)) text_content += "Calendar Invite\n\n Summary: %s \n\nDescription: %s \n\nLocation: %s \n\nTime: %s \n\n------\n\n " % (event, description, location, total_time) self.text_content += text_content self.html_content += markdown(text_content)
def get_context(self, context): # this is for double precaution. usually it wont reach this code if not published if not cint(self.published): raise Exception, "This blog has not been published yet!" # temp fields context.full_name = get_fullname(self.owner) context.updated = global_date_format(self.published_on) if self.blogger: context.blogger_info = frappe.get_doc("Blogger", self.blogger).as_dict() context.description = self.blog_intro or self.content[:140] context.metatags = { "name": self.title, "description": context.description, } if "<!-- markdown -->" in context.content: context.content = markdown(context.content) image = find_first_image(self.content) if image: context.metatags["image"] = image context.comment_list = get_comment_list(self.doctype, self.name) context.children = get_children() category = frappe.db.get_value("Blog Category", context.doc.blog_category, ["title", "page_name"], as_dict=1) context.parents = [{ "title": category.title, "name": "blog/{0}".format(category.page_name) }]
def send_billing_reminder(confirmation_url): system_manager = get_system_managers()[0] usage_info = get_usage_info() data = { 'site': frappe.local.site, 'full_name': frappe.db.get_value( 'User', system_manager, 'concat(ifnull(first_name, ""), ifnull(last_name, ""))'), 'support_email': '*****@*****.**', 'confirmation_url': confirmation_url, 'expires_on': usage_info.expires_on } stats = frappe.render_template( 'erpnext_shopify/templates/emails/billing.md', data, is_path=True) frappe.sendmail( recipients=[system_manager], subject='Your Shopify-ERPNext subscription is about to expire', message=markdown(stats))
def send_billing_reminder(confirmation_url): system_manager = get_system_managers()[0] usage_info = get_usage_info() data = { 'site': frappe.local.site, 'full_name': frappe.db.get_value('User', system_manager, 'concat(ifnull(first_name, ""), ifnull(last_name, ""))'), 'support_email': '*****@*****.**', 'confirmation_url': confirmation_url, 'expires_on': usage_info.expires_on } stats = frappe.render_template('erpnext_shopify/templates/emails/billing.md', data, is_path=True) frappe.sendmail(recipients=[system_manager], subject='Your Shopify-ERPNext subscription is about to expire', message=markdown(stats))