-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #486 from resilient-tech/mergify/bp/version-14-hot…
…fix/pr-485 refactor: separate modules for uninstall and custom field utils (backport #485)
- Loading branch information
Showing
16 changed files
with
152 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import frappe | ||
|
||
from india_compliance.gst_india.setup import ( | ||
ITEM_VARIANT_FIELDNAMES, | ||
get_all_custom_fields, | ||
get_property_setters, | ||
) | ||
from india_compliance.gst_india.utils.custom_fields import delete_custom_fields | ||
|
||
|
||
def before_uninstall(): | ||
delete_custom_fields(get_all_custom_fields()) | ||
delete_property_setters() | ||
remove_fields_from_item_variant_settings() | ||
|
||
|
||
def delete_property_setters(): | ||
field_map = { | ||
"doctype": "doc_type", | ||
"fieldname": "field_name", | ||
} | ||
|
||
for property_setter in get_property_setters(): | ||
for key, fieldname in field_map.items(): | ||
if key in property_setter: | ||
property_setter[fieldname] = property_setter.pop(key) | ||
|
||
frappe.db.delete("Property Setter", property_setter) | ||
|
||
|
||
def remove_fields_from_item_variant_settings(): | ||
settings = frappe.get_doc("Item Variant Settings") | ||
settings.fields = [ | ||
row for row in settings.fields if row.field_name not in ITEM_VARIANT_FIELDNAMES | ||
] | ||
settings.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import frappe | ||
|
||
|
||
def toggle_custom_fields(custom_fields, show): | ||
""" | ||
Show / hide custom fields | ||
:param custom_fields: a dict like `{'Sales Invoice': [{fieldname: 'test', ...}]}` | ||
:param show: True to show fields, False to hide | ||
""" | ||
|
||
for doctypes, fields in custom_fields.items(): | ||
if isinstance(fields, dict): | ||
# only one field | ||
fields = [fields] | ||
|
||
if isinstance(doctypes, str): | ||
# only one doctype | ||
doctypes = (doctypes,) | ||
|
||
for doctype in doctypes: | ||
frappe.db.set_value( | ||
"Custom Field", | ||
{ | ||
"dt": doctype, | ||
"fieldname": ["in", [field["fieldname"] for field in fields]], | ||
}, | ||
"hidden", | ||
int(not show), | ||
) | ||
|
||
frappe.clear_cache(doctype=doctype) | ||
|
||
|
||
def delete_old_fields(fieldnames, doctypes): | ||
if isinstance(fieldnames, str): | ||
fields = (fieldnames,) | ||
|
||
if isinstance(doctypes, str): | ||
doctypes = (doctypes,) | ||
|
||
frappe.db.delete( | ||
"Custom Field", | ||
{ | ||
"fieldname": ("in", fields), | ||
"dt": ("in", doctypes), | ||
}, | ||
) | ||
|
||
|
||
def delete_custom_fields(custom_fields): | ||
""" | ||
:param custom_fields: a dict like `{'Sales Invoice': [{fieldname: 'test', ...}]}` | ||
""" | ||
|
||
for doctypes, fields in custom_fields.items(): | ||
if isinstance(fields, dict): | ||
# only one field | ||
fields = [fields] | ||
|
||
if isinstance(doctypes, str): | ||
# only one doctype | ||
doctypes = (doctypes,) | ||
|
||
for doctype in doctypes: | ||
frappe.db.delete( | ||
"Custom Field", | ||
{ | ||
"fieldname": ("in", [field["fieldname"] for field in fields]), | ||
"dt": doctype, | ||
}, | ||
) | ||
|
||
frappe.clear_cache(doctype=doctype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields | ||
|
||
from india_compliance.income_tax_india.constants.custom_fields import CUSTOM_FIELDS | ||
from india_compliance.patches.post_install.update_e_invoice_fields_and_logs import ( | ||
delete_custom_fields, | ||
) | ||
|
||
|
||
def after_install(): | ||
create_custom_fields(CUSTOM_FIELDS, update=True) | ||
|
||
|
||
def before_uninstall(): | ||
delete_custom_fields(CUSTOM_FIELDS) | ||
create_custom_fields(CUSTOM_FIELDS, ignore_validate=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from india_compliance.gst_india.utils.custom_fields import delete_custom_fields | ||
from india_compliance.income_tax_india.constants.custom_fields import CUSTOM_FIELDS | ||
|
||
|
||
def before_uninstall(): | ||
delete_custom_fields(CUSTOM_FIELDS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
india_compliance/patches/post_install/setup_custom_fields_for_gst.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.