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