Anand Doshi | 72c945b | 2012-06-22 20:01:07 +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 |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 18 | import webnotes |
| 19 | import website.utils |
| 20 | import website.web_cache |
| 21 | |
| 22 | class Page(object): |
| 23 | def __init__(self, doctype): |
| 24 | self.doctype = doctype |
| 25 | |
| 26 | def autoname(self): |
| 27 | """name from title""" |
| 28 | self.doc.name = website.utils.page_name(self.doc.title) |
| 29 | |
| 30 | def validate(self): |
| 31 | if self.doc.name: |
| 32 | self.old_page_name = webnotes.conn.get_value(self.doctype, self.doc.name, 'page_name') |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 33 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 34 | def on_update(self): |
| 35 | # page name updates with the title |
| 36 | self.update_page_name() |
| 37 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 38 | self.clear_web_cache() |
| 39 | |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 40 | self.doc.save() |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 41 | |
| 42 | def on_trash(self): |
| 43 | """delete Web Cache entry""" |
| 44 | self.delete_web_cache(self.doc.page_name) |
| 45 | |
| 46 | def update_page_name(self): |
| 47 | """set page_name and check if it is unique""" |
| 48 | self.doc.page_name = website.utils.page_name(self.doc.title) |
| 49 | |
| 50 | res = webnotes.conn.sql("""\ |
| 51 | select count(*) from `tab%s` |
| 52 | where page_name=%s and name!=%s""" % (self.doctype, '%s', '%s'), |
| 53 | (self.doc.page_name, self.doc.name)) |
| 54 | if res and res[0][0] > 0: |
| 55 | webnotes.msgprint("""A %s with the same title already exists. |
Anand Doshi | 3d53e86 | 2012-07-12 20:12:40 +0530 | [diff] [blame] | 56 | Please change the title of %s and save again.""" |
| 57 | % (self.doctype, self.doc.name), raise_exception=1) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 58 | |
| 59 | def clear_web_cache(self): |
| 60 | """ |
| 61 | if web cache entry doesn't exist, it creates one |
| 62 | if duplicate entry exists for another doctype, it raises exception |
| 63 | """ |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 64 | # delete web cache entry of old name |
| 65 | if hasattr(self, 'old_page_name') and self.old_page_name and \ |
| 66 | self.old_page_name != self.doc.page_name: |
| 67 | self.delete_web_cache(self.old_page_name) |
| 68 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 69 | website.web_cache.create_cache(self.doc.page_name, self.doc.doctype, self.doc.name) |
| 70 | website.web_cache.clear_cache(self.doc.page_name, self.doc.doctype, self.doc.name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 71 | |
| 72 | def delete_web_cache(self, page_name): |
| 73 | """delete entry of page name from Web Cache""" |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 74 | website.web_cache.delete_cache(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 75 | |
| 76 | def markdown_to_html(self, fields_list): |
| 77 | """convert fields from markdown to html""" |
| 78 | import markdown2 |
| 79 | for f in fields_list: |
| 80 | field_name = "%s_html" % f |
| 81 | self.doc.fields[field_name] = markdown2.markdown(self.doc.fields.get(f) or '', \ |
| 82 | extras=["wiki-tables"]) |