seitime-frappe/frappe/model/utils/__init__.py
tundebabzy 6e29d9e925 xrange to range (#3237)
* introduces build status for local fork

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range

* converts xrange to range using six.moves.range

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* converts xrange to range using six.moves.range7

* Revert "introduces build status for local fork"

This reverts commit 61f40983d0b3c6725369171ab850a18e5c029cc4.
2017-05-11 10:55:34 +05:30

63 lines
1.8 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals, print_function
from six.moves import range
import frappe
from frappe.utils import cstr
from frappe.build import html_to_js_template
import re
"""
Model utilities, unclassified functions
"""
def set_default(doc, key):
"""Set is_default property of given doc and unset all others filtered by given key."""
if not doc.is_default:
frappe.db.set(doc, "is_default", 1)
frappe.db.sql("""update `tab%s` set `is_default`=0
where `%s`=%s and name!=%s""" % (doc.doctype, key, "%s", "%s"),
(doc.get(key), doc.name))
def set_field_property(filters, key, value):
'''utility set a property in all fields of a particular type'''
docs = [frappe.get_doc('DocType', d.parent) for d in \
frappe.get_all("DocField", fields=['parent'], filters=filters)]
for d in docs:
d.get('fields', filters)[0].set(key, value)
d.save()
print('Updated {0}'.format(d.name))
frappe.db.commit()
class InvalidIncludePath(frappe.ValidationError): pass
def render_include(content):
'''render {% raw %}{% include "app/path/filename" %}{% endraw %} in js file'''
content = cstr(content)
# try 5 levels of includes
for i in range(5):
if "{% include" in content:
paths = re.findall(r'''{% include\s['"](.*)['"]\s%}''', content)
if not paths:
frappe.throw('Invalid include path', InvalidIncludePath)
for path in paths:
app, app_path = path.split('/', 1)
with open(frappe.get_app_path(app, app_path), 'r') as f:
include = unicode(f.read(), 'utf-8')
if path.endswith('.html'):
include = html_to_js_template(path, include)
content = re.sub(r'''{{% include\s['"]{0}['"]\s%}}'''.format(path), include, content)
else:
break
return content