GithubHelp home page GithubHelp logo

Comments (5)

danielnaab avatar danielnaab commented on May 19, 2024

I see how Predicate._combine works and how it would be difficult to do any optimizations here, especially since the native bitwise OR will evaluate both sides.

I'll probably just stop "abusing" the library and work around this - but I would be curious if it's possible to handle this on the Predicate level in some manner.

from django-rules.

dfunckt avatar dfunckt commented on May 19, 2024

Your assumption that the right-hand side of the expression should not be evaluated is correct, and this used to be the case until dc8b561, which introduced this regression. It's unfortunate that it went unnoticed for so long -- thanks for finding this out!

I just released a new version (v1.1.0) that you may install from PyPi or master that restores short-circuiting for evaluation (among a couple of other changes -- have a look at changelog).

BTW, what's your (ab)use case? Have you checked whether you may accomplish the same thing by storing values in the invocation context?

from django-rules.

danielnaab avatar danielnaab commented on May 19, 2024

Awesome @dfunckt, thank you!

For my (ab)use case: I considered using the invocation context, but it was cleaner to modify the model instance being evaluated. Basically, I've got a large collection of predicates (one for each object type) that get combined into one master predicate that is used to determine if the context of an object is legal. Each object is a node in a tree, and certain node types can only exist in certain circumstances. In addition, rules are applied that do things like zero out certain fields if a user doesn't have permission to view them, or inherit attributes from parent nodes when creating new nodes, but they should only be run when evaluating the appropriate type. So the master predicate looks something like:

(is_type_1 & (type_1_rule & [...]) | (is_type_2 & (type_2_rule & [...]) | [...]

These model instances are then fed through django-rest-framework's model viewsets/serializers, which would either return them or save them to the db. So while I could store some data in the invocation context and mutate the model instance after rule evaluation, it's a bit simpler to just do it directly.

from django-rules.

dfunckt avatar dfunckt commented on May 19, 2024

Cool, sounds super-interesting! I've been looking for ways to better integrate rules with rest-framework, but haven't worked with it for a while to come up with anything useful.

Let me know if you think there's any way to improve rules that would help you out with this ;)

from django-rules.

danielnaab avatar danielnaab commented on May 19, 2024

Thanks for your help and the excellent work on this library! I don't have any suggestions at this point, since I'm not actually using the Django integration at all, but I'm still working through the use cases and refactoring as I go along. Everything works great so far. But I'll keep in touch if I have any ideas. :)

from django-rules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.