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

Question: how to implement multiple (possibly virtual) bool attributes for global scope manipulation #539

Closed
circulon opened this issue Dec 14, 2021 · 3 comments
Labels
enhancement A feature that exists, works as intended but needs to be improved

Comments

@circulon
Copy link
Contributor

Describe the feature as you'd like to see it
So in many of my tables I currently have 2 (sometimes more) boolean flags that indicate a given rows state (eg "active", "verified")

I am currently implementing these as separate mixins with their own global scopes.
The problem lies in combining these global scopes.
As the scopes have no knowledge of each other combining them for various AND, OR, NOT etc boolean logical operations.

Then I considered combining them all into a single mixin with a special active_flags attribute that defined which columns are active on that model.
The mixin contains logic for combining these flags in various ways and building the where, but its still seems overly complicated.

Then I thought hey lets use bit flags on a single column INT as this gives maximum flexibility and I can use bit AND, OR, NOT, XOR combinations in both python and the db engine.

Then I tried and failed to implement this (bit)flags scenario with the py-flags module.
More specifically I could not work out how to make the virtual attributes contained inside the Flags Class be visible to the Model as callable attributes.

So my question is really how could I implement some kind of dynamic boolean flag implementation?
Am I on the right track? or is there a different way I should be looking at this problem?
This is not an uncommon scenario and offers a lot of flexibility around per row settings.

Feedback and discussion or ideas for implementation would be greatly appreciated

@circulon circulon added the enhancement A feature that exists, works as intended but needs to be improved label Dec 14, 2021
@Marlysson
Copy link
Contributor

How are you currently implementing these scopes in separated logics?

@josephmancuso
Copy link
Member

@circulon If I understand the issue correctly this this could only really be done using normal scopes:

class User(Model):

    def active(self, query):
        return query.where('active', 1)
        
    def verified(self, query):
        return query.where('verified', 1)
        
    def verified_active(self, query):
        return self.active().verified()

And then used like this:

user = User.active().get()
user = User.verified_active().get()

@josephmancuso
Copy link
Member

For dynamic you could also put in a parameter into the scope:

class User(Model):

    def active(self, query, value):
        return query.where('active', value)
        
    def verified(self, query, value):
        return query.where('verified', value)
        
    def verified_active(self, query, active=1, verified=1):
        return self.active(active).verified(verified)
user = User.verified_active(1, 0).get()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved
Projects
None yet
Development

No branches or pull requests

3 participants