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""" |
| 43 | import re |
Rushabh Mehta | af1cb63 | 2012-02-06 07:29:52 +0100 | [diff] [blame] | 44 | name = title.lower() |
| 45 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 46 | return '-'.join(name.split()[:4]) |
| 47 | |
| 48 | def 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 | |
| 63 | def 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 Mehta | af1cb63 | 2012-02-06 07:29:52 +0100 | [diff] [blame] | 70 | d.save() |