blob: 2e04982938850ce5acd5d8eaf00d27118117d0bd [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
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Anand Doshi72c945b2012-06-22 20:01:07 +053018import webnotes
19import website.utils
20import website.web_cache
21
22class 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 Doshi10bcf5e2012-06-26 18:54:10 +053033
Anand Doshi72c945b2012-06-22 20:01:07 +053034 def on_update(self):
35 # page name updates with the title
36 self.update_page_name()
37
Anand Doshi72c945b2012-06-22 20:01:07 +053038 self.clear_web_cache()
39
Anand Doshi10bcf5e2012-06-26 18:54:10 +053040 self.doc.save()
Anand Doshi72c945b2012-06-22 20:01:07 +053041
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 Doshi3d53e862012-07-12 20:12:40 +053056 Please change the title of %s and save again."""
57 % (self.doctype, self.doc.name), raise_exception=1)
Anand Doshi72c945b2012-06-22 20:01:07 +053058
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 Doshi10bcf5e2012-06-26 18:54:10 +053064 # 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 Doshi51146c02012-07-12 18:41:12 +053069 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 Doshi72c945b2012-06-22 20:01:07 +053071
72 def delete_web_cache(self, page_name):
73 """delete entry of page name from Web Cache"""
Anand Doshi51146c02012-07-12 18:41:12 +053074 website.web_cache.delete_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053075
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"])