blob: ae6bff4f7ebf358b17813cd02e9e09c8e07cb442 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# 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 Mehtaab1148c2012-01-31 18:01:16 +053017import webnotes
18from webnotes.model.doc import Document
19
Rushabh Mehta949496c2012-01-25 18:48:46 +053020def make_template(doc, path, convert_fields = ['main_section', 'side_section']):
21 """make template"""
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053022 import os, jinja2
Rushabh Mehta949496c2012-01-25 18:48:46 +053023
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053024 markdown(doc, convert_fields)
Rushabh Mehta949496c2012-01-25 18:48:46 +053025
26 # write template
27 with open(path, 'r') as f:
28 temp = jinja2.Template(f.read())
29
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053030 return temp.render(doc = doc.fields)
31
32def 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
41def page_name(title):
42 """make page name from title, and check that there is no duplicate"""
Rushabh Mehtaa9209432012-05-07 18:00:57 +053043 import webnotes.cms
44 return webnotes.cms.page_name(title)
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053045
46def 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 Mehtaa9209432012-05-07 18:00:57 +053060
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053061def 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 Mehtaaf1cb632012-02-06 07:29:52 +010068 d.save()
Rushabh Mehtaa9209432012-05-07 18:00:57 +053069
Rushabh Mehta458ca8e2012-05-09 10:17:08 +053070def get_header(page_name):
Rushabh Mehtaa9209432012-05-07 18:00:57 +053071 """get page header"""
72
73 from webnotes.model.doc import Document
74 from jinja2 import Template
75
76 def get_item(l, label):
77 for i in l:
78 if i['label']==label:
79 return i
80
81 top_bar_items = webnotes.conn.sql("""select * from `tabTop Bar Item`
82 where parent='Website Settings' and parentfield='top_bar_items'
83 order by idx asc""", as_dict=1)
84
85 # build child items
86 for t in top_bar_items:
87 if t.get('parent_label'):
88 pi = get_item(t['parent_label'])
89 if not pi['child_items']:
90 pi['child_items'] = []
91 pi['child_items'].append(t)
92
93 website_settings = Document('Website Settings', 'Website Settings')
94
95 return Template("""<div class="navbar navbar-fixed-top">
96 <div class="navbar-inner">
97 <div class="container">
Rushabh Mehtaa933b632012-05-08 16:06:44 +053098 <a class="brand" href="index.html">{{ brand }}</a>
Rushabh Mehtaa9209432012-05-07 18:00:57 +053099 <ul class="nav">
100 {% for page in top_bar_items %}
101 {% if not page.parent_label %}
102 <li data-label="{{ page.label }}">
103 <a href="{{ page.url }}" {{ page.target }}>
104 {{ page.label }}
105 {% if page.child_items %}
106 <ul class="dropdown-menu">
107 {% for child in page.child_items %}
108 <li data-label="{{ child.label }}">
109 <a href="{{ child.url }}" {{ child.target }}>
110 {% endfor %}
111 </ul>
112 {% endif %}
113 </a></li>
114 {% endif %}
115 {% endfor %}
116 </ul>
117 <img src="images/lib/ui/spinner.gif" id="spinner"/>
118 <ul class="nav pull-right">
Rushabh Mehta5ab81632012-05-08 12:58:32 +0530119 <li id="login-topbar-item"><a href="login-page.html">Login</a></li>
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530120 </ul>
121 </div>
122 </div>
123 </div>""").render(top_bar_items = top_bar_items,
124 brand=website_settings.brand_html or webnotes.get_default('company') or 'ERPNext')
125
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530126def get_footer(page_name):
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530127 """get page footer"""
128
129 from webnotes.model.doc import Document
130 from jinja2 import Template
131
132 website_settings = Document('Website Settings', 'Website Settings')
133
134 website_settings.footer_items = webnotes.conn.sql("""select * from `tabTop Bar Item`
135 where parent='Website Settings' and parentfield='footer_items'
136 order by idx asc""", as_dict=1)
137
138 return Template("""<div class="web-footer">
139 <div class="web-footer-menu"><ul>
140 {% for item in footer_items %}
141 <li><a href="{{ item.url }}" {{ item.target }}
142 data-label="{{ item.label }}">{{ item.label }}</a></li>
143 {% endfor %}
144 </ul></div>
145 <div class="web-footer-copyright">&copy; {{ copyright }}
146 </div>""").render(website_settings.fields)