blob: 971b0bcf170f00071f21a75431c5cbfba922f24d [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"""
43 import re
Rushabh Mehtaaf1cb632012-02-06 07:29:52 +010044 name = title.lower()
45 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053046 return '-'.join(name.split()[:4])
47
48def add_page(title):
49 """add a custom page with title"""
50 name = page_name(title)
51 if webnotes.conn.sql("""select name from tabPage where name=%s""", name):
52 p = Document('Page', name)
53 else:
54 p = Document('Page')
55
56 p.title = title
57 p.name = p.page_name = name
58 p.module = 'Website'
59 p.standard = 'No'
60
61 return p
62
63def add_guest_access_to_page(page):
64 """add Guest in Page Role"""
65 if not webnotes.conn.sql("""select parent from `tabPage Role`
66 where role='Guest' and parent=%s""", page):
67 d = Document('Page Role')
68 d.parent = page
69 d.role = 'Guest'
Rushabh Mehtaaf1cb632012-02-06 07:29:52 +010070 d.save()