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

add get_by_id to Organization #3712

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions redash/models/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __str__(self):
def get_by_slug(cls, slug):
return cls.query.filter(cls.slug == slug).first()

@classmethod
def get_by_id(cls, _id):
return cls.query.filter(cls.id == _id).one()

Copy link
Member

@jezdez jezdez Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we add a custom base model class that has this method (and maybe be other duplicates)?

http://flask-sqlalchemy.pocoo.org/2.3/customizing/#model-class

# in redash/models/base.py
from flask_sqlalchemy import Model

class RedashModel(Model):
    @classmethod
    def get_by_id(cls, _id):
        return cls.query.filter(cls.id == _id).one()

# ...

db = RedashSQLAlchemy(
    model_class=RedashModel,
    session_options={
        'expire_on_commit': False
    },
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, but a bit out of scope for this PR. Let's keep it in mind for future refactors.

Copy link
Member

@jezdez jezdez Apr 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me 😀

@property
def default_group(self):
return self.groups.filter(Group.name == 'default', Group.type == Group.BUILTIN_GROUP).first()
Expand Down