@@ -151,6 +151,15 @@ function cstr(s) { | |||
if(s==null)return ''; | |||
return s+''; | |||
} | |||
function nth(number) { | |||
number = cint(number); | |||
var s = 'th'; | |||
if((number+'').substr(-1)=='1') s = 'st'; | |||
if((number+'').substr(-1)=='2') s = 'nd'; | |||
if((number+'').substr(-1)=='3') s = 'rd'; | |||
return number+s; | |||
} | |||
function flt(v,decimals) { | |||
if(v==null || v=='')return 0; | |||
v=(v+'').replace(/,/g,''); | |||
@@ -198,18 +207,9 @@ var rstrip = function(s, chars) { | |||
} | |||
return s; | |||
} | |||
function repl_all(s, s1, s2) { | |||
var idx = s.indexOf(s1); | |||
while (idx != -1){ | |||
s = s.replace(s1, s2); | |||
idx = s.indexOf(s1); | |||
} | |||
return s; | |||
} | |||
function repl(s, dict) { | |||
if(s==null)return ''; | |||
for(key in dict) s = repl_all(s, '%('+key+')s', dict[key]); | |||
return s; | |||
return sprintf(s, dict); | |||
} | |||
///// dict type | |||
@@ -247,19 +247,7 @@ function add_lists(l1, l2) { | |||
} | |||
function docstring(obj) { | |||
var l = []; | |||
for(key in obj) { | |||
var v = obj[key]; | |||
if(v!=null) { | |||
if(typeof(v)==typeof(1)) { | |||
l[l.length] = "'"+ key + "':" + (v + ''); | |||
} else { | |||
v = v+''; // convert to string | |||
l[l.length] = "'"+ key + "':'" + v.replace(/'/g, "\\'").replace(/\n/g, "\\n") + "'"; | |||
} | |||
} | |||
} | |||
return "{" + l.join(',') + '}'; | |||
return JSON.stringify(obj); | |||
} | |||
function DocLink(p, doctype, name, onload) { | |||
@@ -174,7 +174,7 @@ wn.datetime = { | |||
global_date_format: function(d) { | |||
if(d.substr) d = this.str_to_obj(d); | |||
return d.getDate() + ' ' + month_list_full[d.getMonth()] + ' ' + d.getFullYear(); | |||
return nth(d.getDate()) + ' ' + month_list_full[d.getMonth()] + ' ' + d.getFullYear(); | |||
}, | |||
get_today: function() { | |||
@@ -252,9 +252,12 @@ wn.datetime.time_to_hhmm = function(hh,mm,am) { | |||
// long ago the date represents. | |||
function prettyDate(time){ | |||
if(!time) return '' | |||
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, "")), | |||
diff = (((new Date()).getTime() - date.getTime()) / 1000), | |||
day_diff = Math.floor(diff / 86400); | |||
var date = time; | |||
if(typeof(time)=="string") | |||
date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, "")); | |||
var diff = (((new Date()).getTime() - date.getTime()) / 1000), | |||
day_diff = Math.floor(diff / 86400); | |||
if ( isNaN(day_diff) || day_diff < 0 ) | |||
return ''; | |||
@@ -152,7 +152,7 @@ wn.widgets.Listing = function(opts) { | |||
this.start += m; | |||
// refreh more button | |||
if(r.values.length > this.page_length) $ds(this.more_button_area); | |||
if(r.values.length >= this.page_length) $ds(this.more_button_area); | |||
} else { | |||
if(this.start==0) { | |||
@@ -34,34 +34,17 @@ class DocType: | |||
""" | |||
from webnotes.utils import cint | |||
if (self.doc.name and self.doc.name.startswith('New Page')) or not self.doc.name: | |||
self.doc.name = self.doc.page_name.lower().replace('"','').replace("'",'').replace(' ', '-')[:20] | |||
self.doc.name = self.doc.page_name.lower().replace('"','').replace("'",'').\ | |||
replace(' ', '-')[:20] | |||
if webnotes.conn.exists('Page',self.doc.name): | |||
cnt = webnotes.conn.sql('select name from tabPage where name like "%s-%%" order by name desc limit 1' % self.doc.name) | |||
cnt = webnotes.conn.sql("""select name from tabPage | |||
where name like "%s-%%" order by name desc limit 1""" % self.doc.name) | |||
if cnt: | |||
cnt = cint(cnt[0][0].split('-')[-1]) + 1 | |||
else: | |||
cnt = 1 | |||
self.doc.name += '-' + str(cnt) | |||
def onload(self): | |||
""" | |||
loads html from file before passing | |||
""" | |||
import os | |||
from webnotes.modules import get_module_path, scrub | |||
# load content | |||
if not self.doc.module: | |||
return | |||
try: | |||
file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name) + '.html'), 'r') | |||
self.doc.content = file.read() or '' | |||
file.close() | |||
except IOError, e: # no file / permission | |||
if e.args[0]!=2: | |||
raise e | |||
def validate(self): | |||
""" | |||
Update $image tags | |||
@@ -90,8 +73,29 @@ class DocType: | |||
import os | |||
export_to_files(record_list=[['Page', self.doc.name]]) | |||
if self.doc.write_content and self.doc.content: | |||
file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name) + '.html'), 'w') | |||
file.write(self.doc.content) | |||
file.close() | |||
# write files | |||
path = os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name)) | |||
# html | |||
if not os.path.exists(path + '.html'): | |||
with open(path + '.html', 'w') as f: | |||
f.write("""<div class="layout-wrapper"> | |||
<a class="close" onclick="window.history.back();">×</a> | |||
<h1>Activity</h1> | |||
</div>""" % self.doc.title) | |||
# js | |||
if not os.path.exists(path + '.js'): | |||
with open(path + '.js', 'w') as f: | |||
f.write("""wn.pages['%s'].onload = function(wrapper) { }""" % self.doc.name) | |||
# py | |||
if not os.path.exists(path + '.py'): | |||
with open(path + '.py', 'w') as f: | |||
f.write("""import webnotes""") | |||
# css | |||
if not os.path.exists(path + '.css'): | |||
with open(path + '.css', 'w') as f: | |||
pass | |||
@@ -212,7 +212,17 @@ def whitelist(allow_guest=False): | |||
return innerfn | |||
def clear_cache(): | |||
def clear_cache(user=None): | |||
"""clear boot cache""" | |||
from webnotes.session_cache import clear | |||
clear() | |||
clear(user) | |||
def get_roles(): | |||
"""get roles of current user""" | |||
if session['user']=='Guest': | |||
return ['Guest'] | |||
roles = [r[0] for r in conn.sql("""select distinct role from tabUserRole | |||
where parent=%s and role!='All'""", session['user'])] | |||
return roles + ['All'] |
@@ -383,7 +383,7 @@ class CSVImport: | |||
def convert_csv_data_into_list(self,csv_data): | |||
st_list = [] | |||
for s in csv_data: | |||
st_list.append([unicode(d, 'utf-8').strip() for d in s]) | |||
st_list.append([d.strip() for d in s]) | |||
return st_list | |||
# Get Template method | |||