blob: 1058d7fa028f3af8aa1bbb3941caeded00da1e72 [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.
Anand Doshi3d53e862012-07-12 20:12:40 +053055 Please change the title of %s and save again."""
56 % (self.doctype, self.doc.name), raise_exception=1)
Anand Doshi72c945b2012-06-22 20:01:07 +053057
58 def clear_web_cache(self):
59 """
60 if web cache entry doesn't exist, it creates one
61 if duplicate entry exists for another doctype, it raises exception
62 """
Anand Doshi10bcf5e2012-06-26 18:54:10 +053063 # delete web cache entry of old name
64 if hasattr(self, 'old_page_name') and self.old_page_name and \
65 self.old_page_name != self.doc.page_name:
66 self.delete_web_cache(self.old_page_name)
67
Anand Doshi51146c02012-07-12 18:41:12 +053068 website.web_cache.create_cache(self.doc.page_name, self.doc.doctype, self.doc.name)
69 website.web_cache.clear_cache(self.doc.page_name, self.doc.doctype, self.doc.name)
Anand Doshi72c945b2012-06-22 20:01:07 +053070
71 def delete_web_cache(self, page_name):
72 """delete entry of page name from Web Cache"""
Anand Doshi51146c02012-07-12 18:41:12 +053073 website.web_cache.delete_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053074
75 def markdown_to_html(self, fields_list):
76 """convert fields from markdown to html"""
77 import markdown2
78 for f in fields_list:
79 field_name = "%s_html" % f
80 self.doc.fields[field_name] = markdown2.markdown(self.doc.fields.get(f) or '', \
81 extras=["wiki-tables"])