Fixtures Support - filters

Hi Frappe team.

As you know i made two apps, Base Vat and Jasper Erpnext Report. The problem is when i export fixtures (Custom Field) the fixtures of Jasper Erpnext Report get mix with fixtures of Base Vat and the other way around. So when i update my apps people get Custom Fields that they do not want. If they install only Base Vat then they get also the Custom Fields of Jasper. If we all made apps with fixtures then the problem propagates.

So, the solution in my view is simple:

just change the code in fixtures.py like this:

1 def export_fixtures():
2 	"""Export fixtures as JSON to `[app]/fixtures`"""
3 	for app in frappe.get_installed_apps():
4 		for fixture in frappe.get_hooks("fixtures", app_name=app):
5 			filters = None
6 			if isinstance(fixture, dict):
7 				filters = fixture.get("filters")
8 				fixture = fixture.get("dt")
9 			print "Exporting {0} {1} {2}".format(fixture, app, filters)
10 			if not os.path.exists(frappe.get_app_path(app, "fixtures")):
11				os.mkdir(frappe.get_app_path(app, "fixtures"))

12 			export_json(fixture, frappe.get_app_path(app, "fixtures", frappe.scrub(fixture) + ".json"), filters=filters)

We only need to add lines 5, 6,7 and 8. And in line 12 in export_json function pass argument filters.

Then in hooks.py we only need to use the special filters if we want to.

If we want we only have to do in base_vat hooks.py:

    fixtures = [
	"Custom Script",
	{"dt":"Custom Field", "filters": [["dt", "in", ("Customer", "Company")]]}
    ]

or in hooks.py of jasper_erpnext_report:

fixtures = [
	{"dt":"Custom Field", "filters": {"dt": "File"}}
]

In this way we can export to fixtures of base_vat Customer and Company fixtures and for jasper fixtures of File.

Of course we can be more specific, like:
For Base Vat fixtures:

{"dt":"Custom Field", "filters": [["fieldname", "in", ("vies_vat_check", "validate_vat", "vat_or_nif ")]]}

For Jasper Erpnext:

{"dt":"Custom Field", "filters": {"fieldname": "attached_to_report_name"}}

Please considere my proposal.

Thanks.

12 Likes

@luisfmfernandes nice! Please send a pull-request, will be happy to merge :smile:

Done.

Pull request: Add filters to export fixtures #1332

1 Like

Bom dia.

A alteração que fiz foi apenas para extrair e limitar o que é exportado, o resto é feito pelo frappe.

Isto é, o frappe guarda sempre num ficheiro json, quando se faz: bench export-fixtures.

Qualquer coisa diz.

Luís Fernandes.

1 Like

I could not find if this pull request was completed. Nor could i find any documentation for filtering fixtures beyond these posts.

“Nor could i find any documentation for filtering fixtures beyond these posts.”

You may have a case here!?

But where docs may not be apparent or lacking fortunately there are search options that will get you somewhere, on the command line and of course this forum -

frappe@ubuntu:~/frappe-bench$ bench --help | grep export-fixtures
export-fixtures Export fixtures

hope this helps.

1 Like

The PR had been merged.
https://github.com/frappe/frappe/pull/1332

To use filters, check description of the PR.

1 Like

The data structure is as follows:
fixtures is a list of dictionary_objects:

fixtures = [object1, object2, objectN]

Each dictionary_object with filters contains:

*dictionary_object* = { "doctype":"*doctype_name*", "filters":"*list_of_filters*" }

doctype is self-explanatory, it is the DocType name you wish to export.

Each list_of_filters contains itself, a list_of_filter_criteria
The list_or_filter_criteria itself is a list containing:

*fieldname*, *operator*, (*value*)

Note that value is a Tuple!

*list_of_filter_criteria* = ["fieldname", "operator", ("Value1","Value2","ValueN", )]

So that:
“filters”: [list_of_filter_criteria]

For your convenience, these are the allowed operators:

Side note: I stumbled upon the operators during a traceback while running bench export-fixtures by using “not” and this was the message returned:

Operator must be one of =, !=, >, <, >=, <=, like, not like, in, not in, between, descendants of, ancestors of, not descendants of, not ancestors of, is```
6 Likes