From 0acf687e207fa56fc1b848120dc7b8a1d41bb650 Mon Sep 17 00:00:00 2001 From: Zarrar Date: Mon, 18 Dec 2017 14:01:07 +0530 Subject: [PATCH] Land unit tree (#12072) * improvise tree structure * remove set intro code * remove all land units dependency * test case corrected --- .../agriculture/doctype/land_unit/land_unit.js | 11 ----------- .../agriculture/doctype/land_unit/land_unit.py | 16 +++++++++++++++- .../doctype/land_unit/land_unit_tree.js | 17 ++++++++++++++++- .../doctype/land_unit/test_land_unit.js | 1 - .../doctype/land_unit/test_land_unit.py | 6 +++--- .../doctype/land_unit/test_records.json | 10 ++++++++-- erpnext/agriculture/setup.py | 7 +------ 7 files changed, 43 insertions(+), 25 deletions(-) diff --git a/erpnext/agriculture/doctype/land_unit/land_unit.js b/erpnext/agriculture/doctype/land_unit/land_unit.js index 5b40b241dc1..3bf1434bca0 100644 --- a/erpnext/agriculture/doctype/land_unit/land_unit.js +++ b/erpnext/agriculture/doctype/land_unit/land_unit.js @@ -27,15 +27,4 @@ frappe.ui.form.on('Land Unit', { frm.doc.longitude = frm.fields_dict.location.map.getCenter()['lng']; } }, - refresh: function(frm) { - if(!frm.doc.parent_land_unit) { - frm.set_read_only(); - frm.set_intro(__("This is a root land unit and cannot be edited.")); - } else { - frm.set_intro(null); - } - }, - parent_land_unit: function(frm) { - frm.set_intro(null); - }, }); diff --git a/erpnext/agriculture/doctype/land_unit/land_unit.py b/erpnext/agriculture/doctype/land_unit/land_unit.py index fe683bc28b1..42a9b33954c 100644 --- a/erpnext/agriculture/doctype/land_unit/land_unit.py +++ b/erpnext/agriculture/doctype/land_unit/land_unit.py @@ -32,6 +32,7 @@ class LandUnit(NestedSet): ancestor_features[index] = json.loads(feature) ancestor_doc.set_location_value(features = ancestor_features) ancestor_doc.db_set(fieldname='area', value=ancestor_doc.get('area')-self.get('area'),commit=True) + super(LandUnit, self).on_update() def validate(self): if not self.is_new(): @@ -79,7 +80,6 @@ class LandUnit(NestedSet): def on_update(self): super(LandUnit, self).on_update() - self.validate_one_root() def add_child_property(self): location = self.get('location') @@ -163,4 +163,18 @@ def ring_area(coords): area = area * RADIUS * RADIUS / 2 return area + +@frappe.whitelist() +def get_children(doctype, parent, is_root=False): + if is_root: + parent = '' + + land_units = frappe.db.sql("""select name as value, + is_group as expandable + from `tabLand Unit` + where ifnull(`parent_land_unit`,'') = %s + order by name""", (parent), as_dict=1) + + # return nodes + return land_units \ No newline at end of file diff --git a/erpnext/agriculture/doctype/land_unit/land_unit_tree.js b/erpnext/agriculture/doctype/land_unit/land_unit_tree.js index e0d647934e3..bf64126a2e6 100644 --- a/erpnext/agriculture/doctype/land_unit/land_unit_tree.js +++ b/erpnext/agriculture/doctype/land_unit/land_unit_tree.js @@ -1,11 +1,26 @@ frappe.treeview_settings["Land Unit"] = { + get_tree_nodes: "erpnext.agriculture.doctype.land_unit.land_unit.get_children", ignore_fields:["parent_land_unit"], + get_tree_root: false, disable_add_node: true, + root_label: "All Land Units", + onload: function(me) { + me.make_tree(); + }, toolbar: [ { toggle_btn: true }, { - label:__("Add Child"), + label:__("Edit"), + condition: function(node) { return (node.label!='All Land Units'); }, click: function(node) { + frappe.set_route('Form', 'Land Unit', node.data.value); + } + }, + { + label:__("Add Child"), + condition: function(node) { return node.expandable; }, + click: function(node) { + if(node.label=='All Land Units') node.label=''; var lu = frappe.new_doc("Land Unit", { "parent_land_unit": node.label }); diff --git a/erpnext/agriculture/doctype/land_unit/test_land_unit.js b/erpnext/agriculture/doctype/land_unit/test_land_unit.js index c23db7711a7..f9d3566ac0c 100644 --- a/erpnext/agriculture/doctype/land_unit/test_land_unit.js +++ b/erpnext/agriculture/doctype/land_unit/test_land_unit.js @@ -12,7 +12,6 @@ QUnit.test("test: Land Unit", function (assert) { // insert a new Land Unit () => frappe.tests.make('Land Unit', [ // values to be set - {parent_land_unit: 'All Land Units'}, {land_unit_name: 'Basil Farm'} ]), () => { diff --git a/erpnext/agriculture/doctype/land_unit/test_land_unit.py b/erpnext/agriculture/doctype/land_unit/test_land_unit.py index cf08f987eaf..c45ad5ec9b8 100644 --- a/erpnext/agriculture/doctype/land_unit/test_land_unit.py +++ b/erpnext/agriculture/doctype/land_unit/test_land_unit.py @@ -21,6 +21,6 @@ class TestLandUnit(unittest.TestCase): temp['features'][0]['properties']['feature_of'] = land_unit formatted_land_units.extend(temp['features']) formatted_land_unit_string = str(formatted_land_units) - all_land_units = frappe.get_doc('Land Unit', 'All Land Units') - self.assertEquals(formatted_land_unit_string, str(json.loads(all_land_units.get('location'))['features'])) - self.assertEquals(area, all_land_units.get('area')) + test_land = frappe.get_doc('Land Unit', 'Test Land') + self.assertEquals(formatted_land_unit_string, str(json.loads(test_land.get('location'))['features'])) + self.assertEquals(area, test_land.get('area')) diff --git a/erpnext/agriculture/doctype/land_unit/test_records.json b/erpnext/agriculture/doctype/land_unit/test_records.json index 6baaf2754ba..e7fed9f9464 100644 --- a/erpnext/agriculture/doctype/land_unit/test_records.json +++ b/erpnext/agriculture/doctype/land_unit/test_records.json @@ -1,10 +1,16 @@ [ + { + "doctype": "Land Unit", + "land_unit_name": "Test Land", + "is_group": 1, + "is_container": 1 + }, { "doctype": "Land Unit", "land_unit_name": "Basil Farm", "location": "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"point_type\":\"circle\",\"radius\":884.5625420736483},\"geometry\":{\"type\":\"Point\",\"coordinates\":[72.875834,19.100566]}}]}", - "parent_land_unit": "All Land Units", - "parent": "All Land Units", + "parent_land_unit": "Test Land", + "parent": "Test Land", "is_group": 1, "is_container": 1 }, diff --git a/erpnext/agriculture/setup.py b/erpnext/agriculture/setup.py index 9044e0971cb..ab91343d5d1 100644 --- a/erpnext/agriculture/setup.py +++ b/erpnext/agriculture/setup.py @@ -4,18 +4,13 @@ from frappe import _ from erpnext.setup.utils import insert_record def setup_agriculture(): - if frappe.db.exists('Land Unit', 'All Land Units'): + if frappe.get_all('Agriculture Analysis Criteria'): # already setup return create_agriculture_data() def create_agriculture_data(): records = [ - dict( - doctype="Land Unit", - land_unit_name="All Land Units", - is_group=1, - is_container=1), dict( doctype='Item Group', item_group_name='Fertilizer',