algolia / algoliasearch-django

Seamless integration of Algolia into your Django project.
https://www.algolia.com
MIT License
173 stars 65 forks source link

Version conflict with algoliasearch #283

Closed rashad closed 5 years ago

rashad commented 5 years ago

Description

I'm trying to create a secured API Key in my project and came to realize that the default import suggested in the doc that goes like

from algoliasearch.search_client import SearchClient

lead to an error when running the development server

ModuleNotFoundError: No module named 'algoliasearch.search_client'

So I tried to update my algoliasearch module but faced an error of compatibility

ERROR: algoliasearch-django 1.7.1 has requirement algoliasearch<2.0,>=1.0, but you'll have algoliasearch 2.0.4 which is incompatible.

It looks like the 1.7.1 is the latest available version of the algoliasearch-django package. So what should I do?

Steps To Reproduce

Thanks for your help ✌️

nunomaduro commented 5 years ago

The algoliasearch 2.x is not supported in the Django integration at the moment. But you can use the 1.x to generate a secured api key: https://github.com/algolia/algoliasearch-client-python/blob/277d653fc3907f50db439429822b4b7c7fc1a581/algoliasearch/client.py#L761.

Please tell me if you need me to prepare the snippet for you.

rashad commented 5 years ago

Thanks a lot for your help! 🙏

If you have some time yes I'd love a snippet I'm pretty sure I'm going to miss some best practice tips 😊

chloelbn commented 5 years ago

Hi @rashad,

I'm taking over from @nunomaduro. Here is an example code snippet to generate a secured api key and instantiate a client with it

securedKey = client.generate_secured_api_key(YOUR_ALGOLIA_SEARCH_API_KEY, {'restrictIndices': 'index1'})

securedClient = algoliasearch.Client(
    YOUR_ALGOLIA_APP_ID,
    securedKey
)

Don't hesitate and reach back to us if something is not clear!

Have a good day.

rashad commented 5 years ago

Hello @chloelbn

I would not want to bother you more than needed but I didn't manage to make things work correctly as per what you suggested.

I looked a bit in the old Algolia Python Client's PRs and it looks like the generate_secured_api_key method had been introduced in the v2 PR as shown here.

However, since I'm not a pro dev it's possible that the error comes from my own code. This is the error I get with the following code

from algoliasearch import client
def generate_algolia_secured_key(request):
    request_user = request.user
    secured_key = client.generate_secured_api_key(
        settings.ALGOLIA["API_KEY"],
        {
            'filters': 'viewable_by:'+request_user,
        }
    )
    print(secured_key)
    return secured_key

image

any idea what I could have missed? 🙏

Akinduko commented 4 years ago

@rashad how did you eventually resolve this issue?

Akinduko commented 4 years ago

Hi @nunomaduro @chloelbn ,

Can you please assist, it seems the algoliasearch module has no search_client method,

ERROR: algoliasearch-django 1.7.1 has requirement algoliasearch<2.0,>=1.0, but you'll have algoliasearch 2.1.0 which is incompatible.

The suggested option also does not work, I get below error.

AttributeError: module 'algoliasearch.client' has no attribute 'generate_secured_api_key'

nunomaduro commented 4 years ago

What version of algoliasearch you have installed?

rashad commented 4 years ago

@Akinduko I stood with the following config

algoliasearch==1.20.0
algoliasearch-django==1.7.1

Tbh I did not solved the issue and thus did not generate api keys (yet). The snippet provided by @chloelbn did not work for me so I kept the traditional config and ended up creating a viewable_by property to my models to handle permissions. I then filter by this viewable_by attribute for autocomplete search as per

        source: autocomplete.sources.hits(cases, { hitsPerPage: 5, facetFilters: 'viewable_by:'+user }),
        displayKey: 'name',
        templates: {
            header: '<div class="aa-suggestions-category"><i class="material-icons mr-1 small-icon">folder</i>Dossiers</div>',
            suggestion: function(suggestion){
                //console.log(suggestion)
                hr = suggestion._highlightResult;
                return `
                    <a href="${suggestion.get_absolute_url}" class="aa-result">
                        <span>${hr.name.value}</span>
                        <span>${hr.clients[0].value}</span>
                    </a>
                `;
            },
        }