Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 1 | import webnotes |
| 2 | from webnotes.model.doc import Document |
| 3 | |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 4 | def make_template(doc, path, convert_fields = ['main_section', 'side_section']): |
| 5 | """make template""" |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 6 | import os, jinja2 |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 7 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 8 | markdown(doc, convert_fields) |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 9 | |
| 10 | # write template |
| 11 | with open(path, 'r') as f: |
| 12 | temp = jinja2.Template(f.read()) |
| 13 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 14 | return temp.render(doc = doc.fields) |
| 15 | |
| 16 | def markdown(doc, fields): |
| 17 | """convert fields to markdown""" |
| 18 | import markdown2 |
| 19 | # markdown |
| 20 | for f in fields: |
| 21 | doc.fields[f + '_html'] = markdown2.markdown(doc.fields[f] or '', \ |
| 22 | extras=["wiki-tables"]) |
| 23 | |
| 24 | |
| 25 | def page_name(title): |
| 26 | """make page name from title, and check that there is no duplicate""" |
| 27 | import re |
Rushabh Mehta | af1cb63 | 2012-02-06 07:29:52 +0100 | [diff] [blame] | 28 | name = title.lower() |
| 29 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 30 | return '-'.join(name.split()[:4]) |
| 31 | |
| 32 | def add_page(title): |
| 33 | """add a custom page with title""" |
| 34 | name = page_name(title) |
| 35 | if webnotes.conn.sql("""select name from tabPage where name=%s""", name): |
| 36 | p = Document('Page', name) |
| 37 | else: |
| 38 | p = Document('Page') |
| 39 | |
| 40 | p.title = title |
| 41 | p.name = p.page_name = name |
| 42 | p.module = 'Website' |
| 43 | p.standard = 'No' |
| 44 | |
| 45 | return p |
| 46 | |
| 47 | def add_guest_access_to_page(page): |
| 48 | """add Guest in Page Role""" |
| 49 | if not webnotes.conn.sql("""select parent from `tabPage Role` |
| 50 | where role='Guest' and parent=%s""", page): |
| 51 | d = Document('Page Role') |
| 52 | d.parent = page |
| 53 | d.role = 'Guest' |
Rushabh Mehta | af1cb63 | 2012-02-06 07:29:52 +0100 | [diff] [blame] | 54 | d.save() |