refactor: Deduplication (#6347)

* refactor: Deduplicate setup_perms_for

`setup_perms_for` has two identical definitions in

frappe/utils/reset_doc.py and
frappe/patches/v7_2/setup_custom_perms.py

Remove definition from setup_custom_perms.py, use import instead

* refactor: Deduplicate get_parent_doc and set_timeline_doc

`get_parent_doc` and `set_timeline_doc` are accidentally defined twice in

frappe/core/utils.py and
frappe/core/doctype/communication/communication.py

Remove definition from communication.py, use import instead

* fix(python3): Replace urlopen import with six.moves.urllib.request.urlopen
This commit is contained in:
Aditya Hase 2018-11-05 11:01:59 +05:30 committed by Rushabh Mehta
parent 2521e306a4
commit e6b070e611
3 changed files with 5 additions and 57 deletions

View file

@ -10,6 +10,7 @@ from frappe.core.doctype.communication.comment import (notify_mentions,
update_comment_in_doc, on_trash)
from frappe.core.doctype.communication.email import (validate_email,
notify, _notify, update_parent_mins_to_first_response)
from frappe.core.utils import get_parent_doc, set_timeline_doc
from frappe.utils.bot import BotReply
from frappe.utils import parse_addr
@ -241,34 +242,6 @@ class Communication(Document):
if commit:
frappe.db.commit()
def get_parent_doc(doc):
"""Returns document of `reference_doctype`, `reference_doctype`"""
if not hasattr(doc, "parent_doc"):
if doc.reference_doctype and doc.reference_name:
doc.parent_doc = frappe.get_doc(doc.reference_doctype, doc.reference_name)
else:
doc.parent_doc = None
return doc.parent_doc
def set_timeline_doc(doc):
"""Set timeline_doctype and timeline_name"""
parent_doc = get_parent_doc(doc)
if (doc.timeline_doctype and doc.timeline_name) or not parent_doc:
return
timeline_field = parent_doc.meta.timeline_field
if not timeline_field:
return
doctype = parent_doc.meta.get_link_doctype(timeline_field)
name = parent_doc.get(timeline_field)
if doctype and name:
doc.timeline_doctype = doctype
doc.timeline_name = name
else:
return
def on_doctype_update():
"""Add indexes in `tabCommunication`"""

View file

@ -1,6 +1,7 @@
import frappe
from frappe.permissions import setup_custom_perms
from frappe.core.page.permission_manager.permission_manager import get_standard_permissions
from frappe.utils.reset_doc import setup_perms_for
'''
Copy DocPerm to Custom DocPerm where permissions are set differently
@ -9,30 +10,3 @@ Copy DocPerm to Custom DocPerm where permissions are set differently
def execute():
for d in frappe.db.get_all('DocType', dict(istable=0, issingle=0, custom=0)):
setup_perms_for(d.name)
def setup_perms_for(doctype):
perms = frappe.get_all('DocPerm', fields='*', filters=dict(parent=doctype), order_by='idx asc')
# get default perms
try:
standard_perms = get_standard_permissions(doctype)
except (IOError, KeyError):
# no json file, doctype no longer exists!
return
same = True
if len(standard_perms) != len(perms):
same = False
else:
for i, p in enumerate(perms):
standard = standard_perms[i]
for fieldname in frappe.get_meta('DocPerm').get_fieldnames_with_value():
if p.get(fieldname) != standard.get(fieldname):
same = False
break
if not same:
break
if not same:
setup_custom_perms(doctype)

View file

@ -1,5 +1,5 @@
import frappe
import json, urllib2, os
import json, os
from frappe.modules import scrub, get_module_path, load_doctype_module, utils
from frappe.custom.doctype.customize_form.customize_form import doctype_properties, docfield_properties
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
@ -7,6 +7,7 @@ from frappe.custom.doctype.custom_field.custom_field import create_custom_field
from frappe.modules.import_file import get_file_path, read_doc_from_file
from frappe.core.page.permission_manager.permission_manager import get_standard_permissions
from frappe.permissions import setup_custom_perms
from six.moves.urllib.request import urlopen
branch = 'develop'
@ -28,7 +29,7 @@ def reset_doc(doctype):
try:
git_link = '/'.join(['https://raw.githubusercontent.com/frappe',\
app, branch, doc_path.split('apps/'+app)[1]])
original_file = urllib2.urlopen(git_link).read()
original_file = urlopen(git_link).read()
except:
print('Did not find {0} in {1}'.format(doctype, app))
return