Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploading an entity with a select and multi-select attribute #3

Closed
tanner-ducharme opened this issue Dec 13, 2021 · 11 comments
Closed

Comments

@tanner-ducharme
Copy link

whenever I try to pass a string as the value for a select or multi-select field, I get an error reading:
Error uploading entity 422 {"error":"validation failed","validations":[{"keyword":"metadataMatchesTemplateProperties","schemaPath":"#/metadataMatchesTemplateProperties","params":{"keyword":"metadataMatchesTemplateProperties"},"dataPath":".metadata['source']","message":"related dictionary value/s does not exists"

However, I checked the dictionary for the field, and the value I was trying to upload was present in the dictionary.

For reference, I was trying to upload an entity of the type "Media Articles"
The field in question was "Source"
I was trying to upload a "Source" value of "Free Tibet", which is present in the source dictionary according to the "Thesauri" section

This is how I'm creating the entity:

entity = {'title': row['title_final'],
               'template': template_id,
               'metadata': {
                            'article_contents': [{'value': row['article_final']}],
                            'date_published': [{'value': row['date']}],
                            'date_scrapped': [{'value': row['date_accessed']}],
                            'source': [{'value': 'Free Tibet'}], # gives error
                            'link': [{'value': row['url']}],
               }
        }

If I comment out the "Source" field, the upload is successful.

Any advice would be greatly appreciated. Thanks

@natasha-todi
@pddocs

@txau
Copy link

txau commented Dec 14, 2021

@tanner-ducharme the correct format for thesauri items would be:

metadata: {
  'source': [
    { value: 'thesaurus_item_id', label: 'Free Tibet' }
  ]
}

When working with the API directly, when in doubt use your browser console to inspect a POST request, you'll get all the proper formats.

@txau txau closed this as completed Dec 14, 2021
@tanner-ducharme
Copy link
Author

A couple of things:

  1. 'thesaurus_item_id'
    would that be the string at the end of the url for the "Source" thesaurus? (in this case 5d7fa250abfb862c7663a2aa). I tried
'source': [
        {'value': '5d7fa250abfb862c7663a2aa', 
         'label': 'Free Tibet'}
        ],

and got the same error as above

  1. I'm having trouble inspecting the network activity.
    when monitoring the network at https://ral.uwazi.io/en/, I see no activity when I run my code. I know the monitor is working because I see activity when I interact with the webpage directly.

What url should I be using in order to inspect the failed POST?

Thanks for your help.

@txau
Copy link

txau commented Dec 15, 2021

@tanner-ducharme

  1. That may have been my mistake, try without the label, so just:
'source': [
        { 'value': '5d7fa250abfb862c7663a2aa' }
        ],
  1. The inspector will only show you browser traffic. The idea is that you perform a manual entity creation/edition and take a look to the request format in order to figure out if your script is using the proper format for the API call.

@tanner-ducharme
Copy link
Author

  1. Without the label I get the same error. Regardless, how is the value of "Free Tibet" included if it's not specified?
  2. I'm very lost on this point and googling the problem brings up many unrelated resources. How do I manually create an entity with chrome dev tools?
    Thanks

@txau
Copy link

txau commented Dec 15, 2021

  1. Because Uwazi will denormalize (copy) that label directly from the thesaurus.
  2. With devtools open and in the network tab you navigate with your browser as a user will do. You do not need to run any commands, just use Uwazi normally. You'll see a lot of requests related to different client-server communications. The one that you are looking for has the method POST. Take a look to the sample screenshot:

image

I can't really help you debug this without more debugging info such as an specific error or server response.

  • Maybe the id you are using in your request is invalid?
  • Maybe it is not that particular field that is failing? You may have some required fields that you are not sending in the request. Ie. title is always required.

In you initial error reporting it displays {"keyword":"metadataMatchesTemplateProperties"},"dataPath":".metadata['source']","message":"related dictionary value/s does not exists" . Looks like the thesauri that was associated with that template property (Source) does no longer exist?

@tanner-ducharme
Copy link
Author

  1. Are you saying that there is an ID associated with the individual values of the thesaurus? If so, how do I access it? I don't see any way to click on or select the individual options
  2. The thesaurus does still exist. However, I figured out how to inspect the request (thank you for your patience). I made a dummy entity and found the following

source: [{value: "67107c1f-66df-4338-82d0-4437463764bf", label: "Free Tibet"}]

The problem is, I have no idea where the string 67107c1f-66df-4338-82d0-4437463764bf comes from. Is there any way to ascertain the origin of this code?

Thanks

@txau
Copy link

txau commented Dec 15, 2021

Ok. We are starting to make sense now, you were sending the wrong id and Uwazi wouldn't pass validation.

You can get the whole list in JSON format from http://host/api/thesauris

@tanner-ducharme
Copy link
Author

Thanks, I found it.
How can I retrieve this data and utilize it in my code via the API? Having to find it in the browser is tedious

Thanks

@txau
Copy link

txau commented Dec 16, 2021

You'll find template definitions here: https://host/api/templates

The actual format of the value you need to send may vary depending on the type of data but it is always an array with objects. The object properties differ. Ie:

  • text, number and rich text: [{value: something}]
  • links: [{label: something, url: something}]
  • ...

The browser request comes handy in case you don't know the format.

@tanner-ducharme
Copy link
Author

Sorry, I wasn't clear. How can I retrieve the value's for the different thesauri options via the API as opposed to having to look in my browser at https://ral.uwazi.io/api/thesauris?

For example, say I wanted to upload a new entity with a 'Source' field value of "Global Times". Is there any way to retrieve it's id code (988283e0-e9a6-45d5-8334-50f4b43b89de) programatically?

I hope this makes sense.

Thanks again

@txau
Copy link

txau commented Dec 17, 2021

@tanner-ducharme yes there is. You send a request to that URL from your code and you parse the response as JSON. This library has some functionality already built-in here: https://github.com/huridocs/python_uwazi_API/blob/main/uwazi_api/Thesauris.py as explained by the end of the readme: https://github.com/huridocs/python_uwazi_API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants