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 | |
Anand Doshi | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 18 | import webnotes |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 19 | |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 20 | def render(page_name): |
| 21 | """render html page""" |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 22 | try: |
| 23 | if page_name: |
| 24 | html = get_html(page_name) |
| 25 | else: |
| 26 | html = get_html('index') |
| 27 | except Exception, e: |
| 28 | html = get_html('404') |
| 29 | |
| 30 | from webnotes.handler import eprint, print_zip |
| 31 | eprint("Content-Type: text/html") |
| 32 | print_zip(html) |
| 33 | |
| 34 | def get_html(page_name): |
| 35 | """get page html""" |
| 36 | page_name = scrub_page_name(page_name) |
| 37 | comments = get_comments(page_name) |
| 38 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 39 | from website.web_cache import get_page_html |
| 40 | html = get_page_html(page_name, comments) |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 41 | return html |
| 42 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 43 | def get_comments(page_name): |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 44 | if page_name == '404': |
| 45 | comments = """error: %s""" % webnotes.getTraceback() |
| 46 | else: |
| 47 | comments = """page: %s""" % page_name |
| 48 | |
| 49 | return comments |
| 50 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 51 | def scrub_page_name(page_name): |
| 52 | if page_name.endswith('.html'): |
| 53 | page_name = page_name[:-5] |
| 54 | |
| 55 | return page_name |
| 56 | |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 57 | def make_template(doc, path, convert_fields = ['main_section', 'side_section']): |
| 58 | """make template""" |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 59 | import os, jinja2 |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | 949496c | 2012-01-25 18:48:46 +0530 | [diff] [blame] | 61 | # write template |
| 62 | with open(path, 'r') as f: |
| 63 | temp = jinja2.Template(f.read()) |
| 64 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 65 | return temp.render(doc = doc.fields) |
| 66 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 67 | def page_name(title): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 68 | """make page name from title""" |
| 69 | import re |
| 70 | name = title.lower() |
| 71 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
| 72 | return '-'.join(name.split()[:4]) |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 73 | |
| 74 | def update_page_name(doc, title): |
| 75 | """set page_name and check if it is unique""" |
Rushabh Mehta | a2e4cb0 | 2012-12-06 17:09:38 +0530 | [diff] [blame] | 76 | webnotes.conn.set(doc, "page_name", page_name(title)) |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 77 | |
| 78 | res = webnotes.conn.sql("""\ |
| 79 | select count(*) from `tab%s` |
| 80 | where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'), |
| 81 | (doc.page_name, doc.name)) |
| 82 | if res and res[0][0] > 0: |
| 83 | webnotes.msgprint("""A %s with the same title already exists. |
| 84 | Please change the title of %s and save again.""" |
| 85 | % (doc.doctype, doc.name), raise_exception=1) |