# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import api
from lxml import etree
from openerp.addons.base.res.res_config import \
    res_config_settings


class ResConfigSettings(res_config_settings):

    @api.model
    def fields_view_get(self, view_id=None, view_type='form',
                        context=None, toolbar=False, submenu=False):

        ret_val = super(ResConfigSettings, self).fields_view_get(
            view_id=view_id,
            view_type=view_type,
            context=context,
            toolbar=toolbar,
            submenu=submenu,
        )

        page_name = ret_val['name']
        doc = etree.XML(ret_val['arch'])

        if page_name == 'account settings':
            doc = self._remove_enterprise_fields(
                i=1,
                doc=doc,
                custom_queries=[
                    "//div[@name='bank_statement_import_options'] \
                        /preceding-sibling::label[1]",
                    "//div[@name='bank_statement_import_options']",
                    "//div[div[field[@name='module_account_sepa']]] \
                        /preceding-sibling::label[1]",
                    "//div[div[field[@name='module_account_sepa']]]",
                ],
            )
        elif page_name == 'sale settings':
            doc = self._remove_enterprise_fields(
                i=2,
                doc=doc,
                custom_queries=[
                    "//field[@name='module_crm_voip']",
                ],
            )
        else:
            doc = self._remove_enterprise_fields(i=2, doc=doc)

        ret_val['arch'] = etree.tostring(doc)
        return ret_val

    @api.model
    def _remove_enterprise_fields(self, i, doc, i_end=0, custom_queries=None):

        if custom_queries:
            for query in custom_queries:
                items = doc.xpath(query)
                for item in items:
                    item.getparent().remove(item)

        while i >= i_end:
            items = doc.xpath(
                "//" +
                "div[" * i +
                "field[@widget='upgrade_boolean']" +
                "]" * i +
                "/preceding-sibling::label[1]"
            )
            items += doc.xpath(
                "//" +
                "div[" * i +
                "field[@widget='upgrade_boolean']" +
                "]" * i
            )
            for item in items:
                item.getparent().remove(item)
            i -= 1

        return doc

@@ -737,16 +737,37 @@
 rch'%5D)%0A%0A
+        queries = %5B%5D%0A
         
@@ -818,123 +818,19 @@
     
-doc = self._remove_enterprise_fields(%0A                i=1,%0A                doc=doc,%0A                custom_
 queries
-=
+ += 
 %5B%0A  
@@ -847,57 +847,55 @@
     
-    
 %22//div%5B
+field%5B
 @name='
-bank_statement_im
+module_account_re
 port
-_options'%5D
+s' and
  %5C%0A 
@@ -917,107 +917,34 @@
     
-    /preceding-sibling::label%5B1%5D%22,%0A                    %22//div%5B@name='bank_statement_import_options'
+@widget='upgrade_boolean'%5D
 %5D%22,%0A
@@ -955,35 +955,27 @@
             
-    %22//div%5B
+%22//
 div%5Bfield%5B@n
@@ -994,24 +994,37 @@
 account_
-sepa'%5D%5D%5D
+reports_followup' and
  %5C%0A     
@@ -1042,39 +1042,34 @@
     
-    /preceding-sibling::label%5B1
+@widget='upgrade_boolean'%5D
 %5D%22,%0A
@@ -1084,27 +1084,19 @@
         
-    %22//div%5B
+%22//
 div%5Bfiel
@@ -1123,37 +1123,28 @@
 unt_
-sepa'%5D%5D%5D%22,%0A                %5D,
+batch_deposit' and %5C
 %0A   
@@ -1152,26 +1152,24 @@
         
-)%0A
         
 elif pag
@@ -1164,178 +1164,74 @@
     
-elif page_name == 'sale settings':%0A            doc = self._remove_enterprise_fields(%0A                i=2,%0A                doc=doc,%0A                custom_queries=%5B%0A      
+@widget='upgrade_boolean'%5D%5D%22,%0A            %5D%0A%0A        queries += %5B%0A
     
@@ -1242,78 +1242,55 @@
     
-  
 %22//
-field%5B@name='module_crm_voip'%5D%22,%0A                %5D,%0A            )
+div%5Bdiv%5Bfield%5B@widget='upgrade_boolean'%5D%5D%5D %5C
 %0A   
@@ -1298,22 +1298,16 @@
     
-else:%0A
         
     
@@ -1306,550 +1306,118 @@
     
-    doc = self._remove_enterprise_fields(i=2, doc=doc)%0A%0A        ret_val%5B'arch'%5D = etree.tostring(doc)%0A        return ret_val%0A%0A    @api.model%0A    def _remove_enterprise_fields(self, i, doc, i_end=0, custom_queries=None):%0A%0A        if custom_queries:%0A            for query in custom_queries:%0A                items = doc.xpath(query)%0A                for item in items:%0A                    item.getparent().remove(item)%0A%0A        while i %3E= i_end:%0A            items = doc.xpath(%0A                %22//%22 +%0A                %22div%5B%22 * i +%0A                %22
+/preceding-sibling::label%5B1%5D%22,%0A            %22//div%5Bdiv%5Bfield%5B@widget='upgrade_boolean'%5D%5D%5D%22,%0A            %22//div%5B
 fiel
@@ -1448,37 +1448,11 @@
 an'%5D
-%22 +%0A                %22%5D%22 * i +
+%5D %5C
 %0A   
@@ -1464,17 +1464,16 @@
         
-%22
 /precedi
@@ -1497,106 +1497,64 @@
 %5B1%5D%22
-%0A            )%0A            items += doc.xpath(%0A                %22//%22 +%0A                %22div%5B%22 * i +
+,%0A            %22//div%5Bfield%5B@widget='upgrade_boolean'%5D%5D%22,
 %0A   
@@ -1562,21 +1562,19 @@
         
-    %22
+%22//
 field%5B@w
@@ -1602,48 +1602,50 @@
 n'%5D%22
- +%0A                %22%5D%22 * i%0A            )
+,%0A        %5D%0A%0A        for query in queries:
 %0A   
@@ -1669,13 +1669,24 @@
  in 
-items
+doc.xpath(query)
 :%0A  
@@ -1729,24 +1729,25 @@
 e(item)%0A
+%0A
         
     i -=
@@ -1742,19 +1742,45 @@
     
-    i -= 1%0A
+ret_val%5B'arch'%5D = etree.tostring(doc)
 %0A   
@@ -1791,12 +1791,16 @@
  return 
-doc
+ret_val
 %0A

Refactor field removal to reduce loops, complexity, and eliminate any risky xpath queries.