Dossier Filtering
Hint
Dossier Filtering is a feature that allows you to filter the list of dossiers displayed in the Dossier List View. You can use it to filter the list of dossiers by the dossier’s attributes, by the dossier’s state or with more complex filters.
Basic filtering : Retrieving a specific dossier
The function get_dossiers allows you to retrieve all dossiers of a demarche. You can pass a filter argument to this function to filter the list of dossiers. Let’s retrieve a dossier by its number :
1from demarches_simpy import Profile, Dossier, Demarche, DossierState
2
3# Create a profile
4profile = Profile('API_KEY',verbose=True)
5
6# Create a dossier
7demarche = Demarche(000000, profile, verbose=False)
8
9
10specific_dossier = demarche.get_dossiers(-1, lambda dossier : dossier.number == 1234567)
11
12print(len(specific_dossier)) # 1
13
Note
The ‘-1’ argument passed to the function indicate that we don’t want a limit on the number of dossiers returned. If you want to retrieve only one dossier, you can pass 1 as the limit argument.
Background fetching
The id or the number of a dossier is part of the Dossier object. We don’t need to fetch the dossier to retrieve its id or number. That’s why it will be fast. But if we want to apply a filter on a property (which will be fetched) we need to enable bakcground_fetching option, which will fetch the dossier in background. It will increase the speed of the research. If you don’t enable this option it will work but it will take longer.
1## Background fetching
2
3# Define a filter function
4def filter(dossier : Dossier):
5 return dossier.state == DossierState.INSTRUCTION
6
7
8# Get all dossiers in Instruction State
9filtered_dossier = demarche.get_dossiers(-1,dossier_filter=filter, background_fetching=True)
10
11
Advanced usage
We saw that background_fetching will fetch property in background. But if the property is conditionnal it won’t be fetched. For instance ‘fields’ of a dossier is conditionnal. If you want to filter on a conditionnal property you need to enable them at default by using default_variables dict.
1## Advanced usage
2
3def filter(dossier : Dossier):
4 for field in dossier.get_fields():
5 if field.stringValue == 'foo':
6 return True
7 return False
8
9advanced_filtered_dossier = demarche.get_dossiers(-1,dossier_filter=filter, background_fetching=True, default_variables={"includeFields": True})
10
Full example
1from demarches_simpy import Profile, Dossier, AnnotationModifier
2
3# Create a profile
4profile = Profile('API_KEY',verbose=True)
5
6# Create a dossier
7dossier = Dossier(000000, profile)
8
9
10# Assign an instructeur_id
11
12# First method
13profile.set_instructeur_id('instructeur_id')
14
15# Second method
16## Only works if the dossier is not in CONSTRUCTION state
17instructeurs = dossier.get_attached_instructeurs_info()
18instructeur = instructeurs[0]
19
20# print(instructeur)
21# > {'id': 'instructeur_id', 'email': 'mail@mail.com'}
22
23profile.set_instructeur_id(instructeur['id'])
24
25# Create the annotation action
26
27action = AnnotationModifier(profile, dossier)
28
29# Get an annotation
30
31annotation = dossier.get_annotations()['annotation-label']
32
33# print(annotation)
34# > {'id': 'annotation_id', 'stringValue' : 'value'}
35
36# Modify the annotation
37
38annotation['stringValue'] = 'new_value'
39
40# Perform the action
41
42result = action.perform(annotation)
43
44if result == AnnotationModifier.NETWORK_ERROR:
45 print("Network error")
46elif result == AnnotationModifier.SUCCESS:
47 print("Success")
48
49