Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 17 | import webnotes |
| 18 | from webnotes.model.doc import Document |
| 19 | |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 20 | def make_template(doc, path, convert_fields = ['main_section', 'side_section']): |
| 21 | """make template""" |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 22 | import os, jinja2 |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 24 | markdown(doc, convert_fields) |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 25 | |
| 26 | # write template |
| 27 | with open(path, 'r') as f: |
| 28 | temp = jinja2.Template(f.read()) |
| 29 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 30 | return temp.render(doc = doc.fields) |
| 31 | |
| 32 | def markdown(doc, fields): |
| 33 | """convert fields to markdown""" |
| 34 | import markdown2 |
| 35 | # markdown |
| 36 | for f in fields: |
| 37 | doc.fields[f + '_html'] = markdown2.markdown(doc.fields[f] or '', \ |
| 38 | extras=["wiki-tables"]) |
| 39 | |
| 40 | |
| 41 | def page_name(title): |
| 42 | """make page name from title, and check that there is no duplicate""" |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 43 | import webnotes.cms |
| 44 | return webnotes.cms.page_name(title) |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 45 | |
| 46 | def add_page(title): |
| 47 | """add a custom page with title""" |
| 48 | name = page_name(title) |
| 49 | if webnotes.conn.sql("""select name from tabPage where name=%s""", name): |
| 50 | p = Document('Page', name) |
| 51 | else: |
| 52 | p = Document('Page') |
| 53 | |
| 54 | p.title = title |
| 55 | p.name = p.page_name = name |
| 56 | p.module = 'Website' |
| 57 | p.standard = 'No' |
| 58 | |
| 59 | return p |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 61 | def add_guest_access_to_page(page): |
| 62 | """add Guest in Page Role""" |
| 63 | if not webnotes.conn.sql("""select parent from `tabPage Role` |
| 64 | where role='Guest' and parent=%s""", page): |
| 65 | d = Document('Page Role') |
| 66 | d.parent = page |
| 67 | d.role = 'Guest' |
Rushabh Mehta | af1cb63 | 2012-02-06 07:29:52 +0100 | [diff] [blame] | 68 | d.save() |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 69 | |
Rushabh Mehta | 458ca8e | 2012-05-09 10:17:08 +0530 | [diff] [blame] | 70 | def get_header(page_name): |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 71 | """get page header""" |
| 72 | |
| 73 | from webnotes.model.doc import Document |
| 74 | from jinja2 import Template |
Nabin Hait | a15740a | 2012-05-10 12:56:40 +0530 | [diff] [blame] | 75 | import webnotes.utils |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 76 | |
| 77 | def get_item(l, label): |
| 78 | for i in l: |
| 79 | if i['label']==label: |
| 80 | return i |
| 81 | |
| 82 | top_bar_items = webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 83 | where parent='Website Settings' and parentfield='top_bar_items' |
| 84 | order by idx asc""", as_dict=1) |
| 85 | |
| 86 | # build child items |
| 87 | for t in top_bar_items: |
| 88 | if t.get('parent_label'): |
Rushabh Mehta | ceb2ab3 | 2012-05-21 15:56:50 +0200 | [diff] [blame] | 89 | pi = get_item(top_bar_items, t['parent_label']) |
| 90 | if 'child_items' not in pi: |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 91 | pi['child_items'] = [] |
| 92 | pi['child_items'].append(t) |
| 93 | |
| 94 | website_settings = Document('Website Settings', 'Website Settings') |
| 95 | |
| 96 | return Template("""<div class="navbar navbar-fixed-top"> |
| 97 | <div class="navbar-inner"> |
| 98 | <div class="container"> |
Rushabh Mehta | a933b63 | 2012-05-08 16:06:44 +0530 | [diff] [blame] | 99 | <a class="brand" href="index.html">{{ brand }}</a> |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 100 | <ul class="nav"> |
| 101 | {% for page in top_bar_items %} |
| 102 | {% if not page.parent_label %} |
| 103 | <li data-label="{{ page.label }}"> |
| 104 | <a href="{{ page.url }}" {{ page.target }}> |
| 105 | {{ page.label }} |
| 106 | {% if page.child_items %} |
| 107 | <ul class="dropdown-menu"> |
| 108 | {% for child in page.child_items %} |
| 109 | <li data-label="{{ child.label }}"> |
| 110 | <a href="{{ child.url }}" {{ child.target }}> |
| 111 | {% endfor %} |
| 112 | </ul> |
| 113 | {% endif %} |
| 114 | </a></li> |
| 115 | {% endif %} |
| 116 | {% endfor %} |
| 117 | </ul> |
| 118 | <img src="images/lib/ui/spinner.gif" id="spinner"/> |
| 119 | <ul class="nav pull-right"> |
Rushabh Mehta | 5ab8163 | 2012-05-08 12:58:32 +0530 | [diff] [blame] | 120 | <li id="login-topbar-item"><a href="login-page.html">Login</a></li> |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 121 | </ul> |
| 122 | </div> |
| 123 | </div> |
| 124 | </div>""").render(top_bar_items = top_bar_items, |
Nabin Hait | a15740a | 2012-05-10 12:56:40 +0530 | [diff] [blame] | 125 | brand=website_settings.brand_html or webnotes.utils.get_defaults('company') or 'ERPNext') |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 126 | |
Rushabh Mehta | 458ca8e | 2012-05-09 10:17:08 +0530 | [diff] [blame] | 127 | def get_footer(page_name): |
Rushabh Mehta | a920943 | 2012-05-07 18:00:57 +0530 | [diff] [blame] | 128 | """get page footer""" |
| 129 | |
| 130 | from webnotes.model.doc import Document |
| 131 | from jinja2 import Template |
| 132 | |
| 133 | website_settings = Document('Website Settings', 'Website Settings') |
| 134 | |
| 135 | website_settings.footer_items = webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 136 | where parent='Website Settings' and parentfield='footer_items' |
| 137 | order by idx asc""", as_dict=1) |
| 138 | |
| 139 | return Template("""<div class="web-footer"> |
| 140 | <div class="web-footer-menu"><ul> |
| 141 | {% for item in footer_items %} |
| 142 | <li><a href="{{ item.url }}" {{ item.target }} |
| 143 | data-label="{{ item.label }}">{{ item.label }}</a></li> |
| 144 | {% endfor %} |
| 145 | </ul></div> |
| 146 | <div class="web-footer-copyright">© {{ copyright }} |
Nabin Hait | a15740a | 2012-05-10 12:56:40 +0530 | [diff] [blame] | 147 | </div>""").render(website_settings.fields) |