blob: 57e940a755f95ab8a6374f6b352a569db1f09d71 [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
Nabin Haita15740a2012-05-10 12:56:40 +053075 import webnotes.utils
Rushabh Mehtaa9209432012-05-07 18:00:57 +053076
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 Mehtaceb2ab32012-05-21 15:56:50 +020089 pi = get_item(top_bar_items, t['parent_label'])
90 if 'child_items' not in pi:
Rushabh Mehtaa9209432012-05-07 18:00:57 +053091 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 Mehtaa933b632012-05-08 16:06:44 +053099 <a class="brand" href="index.html">{{ brand }}</a>
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530100 <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 Mehta5ab81632012-05-08 12:58:32 +0530120 <li id="login-topbar-item"><a href="login-page.html">Login</a></li>
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530121 </ul>
122 </div>
123 </div>
124 </div>""").render(top_bar_items = top_bar_items,
Nabin Haita15740a2012-05-10 12:56:40 +0530125 brand=website_settings.brand_html or webnotes.utils.get_defaults('company') or 'ERPNext')
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530126
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530127def get_footer(page_name):
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530128 """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">&copy; {{ copyright }}
Nabin Haita15740a2012-05-10 12:56:40 +0530147 </div>""").render(website_settings.fields)