Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Chubatiuk committed Dec 23, 2023
1 parent a9079bf commit d3391c3
Show file tree
Hide file tree
Showing 54 changed files with 443 additions and 817 deletions.
1 change: 1 addition & 0 deletions redash/alerts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum


class Alerts(str, Enum):
UNKNOWN_STATE = "unknown"
OK_STATE = "ok"
Expand Down
18 changes: 7 additions & 11 deletions redash/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@ def __init__(self, *args, **kwargs):


def create_app():
from redash import (
authentication,
handlers,
security,
tasks
)
from . import (
limiter,
mail,
migrate,
)
from redash import authentication, handlers, security, tasks
from redash.handlers.webpack import configure_webpack
from redash.metrics import request as request_metrics
from redash.models import db, users
from redash.utils import sentry
from redash.version_check import reset_new_version_status

from . import (
limiter,
mail,
migrate,
)

sentry.init()
app = Redash()

Expand Down
4 changes: 1 addition & 3 deletions redash/authentication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def hmac_load_user_from_request(request):
return user

if query_id:
query = models.db.session.scalars(
select(models.Query)
.where(models.Query.id == query_id)).one()
query = models.db.session.scalars(select(models.Query).where(models.Query.id == query_id)).one()
calculated_signature = sign(query.api_key, request.path, expires)

if query.api_key and signature == calculated_signature:
Expand Down
3 changes: 2 additions & 1 deletion redash/authentication/jwt_auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import json
import logging

import jwt
import requests

Expand Down
3 changes: 2 additions & 1 deletion redash/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
import json

import click
from flask import current_app
from flask.cli import FlaskGroup, run_command, with_appcontext
from rq import Connection
Expand Down
26 changes: 8 additions & 18 deletions redash/cli/data_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ def list_command(organization=None):
"""List currently configured data sources."""
if organization:
org = models.Organization.get_by_slug(organization)
data_sources = (
select(models.DataSource)
.where(models.DataSource.org == org))
data_sources = select(models.DataSource).where(models.DataSource.org == org)
else:
data_sources = select(models.DataSource)
sources = (
models.db.session
.scalars(data_sources.order_by(models.DataSource.name)))
sources = models.db.session.scalars(data_sources.order_by(models.DataSource.name))
for i, ds in enumerate(sources):
if i > 0:
print("-" * 20)
Expand Down Expand Up @@ -75,10 +71,8 @@ def test(name, organization="default"):
try:
org = models.Organization.get_by_slug(organization)
data_source = models.db.session.scalars(
select(models.DataSource)
.where(
models.DataSource.name == name,
models.DataSource.org == org)).one()
select(models.DataSource).where(models.DataSource.name == name, models.DataSource.org == org)
).one()
print("Testing connection to data source: {} (id={})".format(name, data_source.id))
try:
data_source.query_runner.test_connection()
Expand Down Expand Up @@ -191,10 +185,8 @@ def delete(name, organization="default"):
try:
org = models.Organization.get_by_slug(organization)
data_source = models.db.session.scalars(
select(models.DataSource)
.where(
models.DataSource.name == name,
models.DataSource.org == org)).one()
select(models.DataSource).where(models.DataSource.name == name, models.DataSource.org == org)
).one()
print("Deleting data source: {} (id={})".format(name, data_source.id))
models.db.session.delete(data_source)
models.db.session.commit()
Expand Down Expand Up @@ -228,10 +220,8 @@ def edit(name, new_name=None, options=None, type=None, organization="default"):
validate_data_source_type(type)
org = models.Organization.get_by_slug(organization)
data_source = models.db.session.scalars(
select(models.DataSource)
.where(
models.DataSource.name == name,
models.DataSource.org == org)).one()
select(models.DataSource).where(models.DataSource.name == name, models.DataSource.org == org)
).one()
update_attr(data_source, "name", new_name)
update_attr(data_source, "type", type)

Expand Down
6 changes: 2 additions & 4 deletions redash/cli/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def list_command(organization=None):
else:
qgroups = select(models.Group)

groups = models.db.session.scalars(
qgroups.order_by(models.Group.name)).all()
groups = models.db.session.scalars(qgroups.order_by(models.Group.name)).all()
for i, group in enumerate(groups):
if i > 0:
print("-" * 20)
Expand All @@ -117,8 +116,7 @@ def list_command(organization=None):
)
)

members = models.db.session.scalars(
models.Group.members(group.id)).all()
members = models.db.session.scalars(models.Group.members(group.id)).all()
user_names = [m.name for m in members]
if user_names:
print("Users: {}".format(", ".join(user_names)))
Expand Down
6 changes: 2 additions & 4 deletions redash/cli/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def set_google_apps_domains(domains):
"""
Sets the allowable domains to the comma separated list DOMAINS.
"""
organization = models.db.session.scalar(
select(models.Organization))
organization = models.db.session.scalar(select(models.Organization))
k = models.Organization.SETTING_GOOGLE_APPS_DOMAINS
organization.settings[k] = domains.split(",")
models.db.session.add(organization)
Expand All @@ -24,8 +23,7 @@ def set_google_apps_domains(domains):

@manager.command(name="show_google_apps_domains")
def show_google_apps_domains():
organization = models.db.session.scalar(
select(models.Organization))
organization = models.db.session.scalar(select(models.Organization))
print("Current list of Google Apps domains: {}".format(", ".join(organization.google_apps_domains)))


Expand Down
36 changes: 12 additions & 24 deletions redash/cli/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from flask.cli import AppGroup
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql.expression import select, delete as sql_delete
from sqlalchemy.sql.expression import delete as sql_delete
from sqlalchemy.sql.expression import select

from redash import models
from redash.handlers.users import invite_user
Expand Down Expand Up @@ -146,15 +147,13 @@ def create_root(email, name, google_auth=False, password=None, organization="def
print("Creating root user (%s, %s) in organization %s..." % (email, name, organization))
print("Login with Google Auth: %r\n" % google_auth)

user = models.db.session.scalar(
select(models.User).where(models.User.email == email))
user = models.db.session.scalar(select(models.User).where(models.User.email == email))
if user is not None:
print("User [%s] is already exists." % email)
exit(1)

org_slug = organization
org = models.db.session.scalar(
select(models.Organization).where(models.Organization.slug == org_slug))
org = models.db.session.scalar(select(models.Organization).where(models.Organization.slug == org_slug))
if org is None:
org = models.Organization(name=org_slug, slug=org_slug, settings={})

Expand Down Expand Up @@ -203,18 +202,15 @@ def delete(email, organization=None):
"""
Delete user EMAIL.
"""
deleted_users_query = (
sql_delete(models.User)
.where(models.User.email == email))
deleted_users_query = sql_delete(models.User).where(models.User.email == email)
if organization:
org = models.Organization.get_by_slug(organization)
deleted_users_query = (deleted_users_query
.where(models.User.org == org))
deleted_users_query = deleted_users_query.where(models.User.org == org)
deleted_count = len(
models.db.session.scalars(
deleted_users_query
.returning(models.User.id)
.execution_options(synchronize_session=False)).all())
deleted_users_query.returning(models.User.id).execution_options(synchronize_session=False)
).all()
)
models.db.session.commit()
print("Deleted %d users." % deleted_count)

Expand All @@ -234,15 +230,9 @@ def password(email, password, organization=None):
"""
if organization:
org = models.Organization.get_by_slug(organization)
user = models.db.session.scalar(
select(models.User)
.where(
models.User.email == email,
models.User.org == org))
user = models.db.session.scalar(select(models.User).where(models.User.email == email, models.User.org == org))
else:
user = models.db.session.scalar(
select(models.User)
.where(models.User.email == email))
user = models.db.session.scalar(select(models.User).where(models.User.email == email))

if user is not None:
user.hash_password(password)
Expand Down Expand Up @@ -320,8 +310,6 @@ def list_command(organization=None):
)
)

groups = models.db.session.scalars(
select(models.Group)
.where(models.Group.id.in_(user.group_ids))).all()
groups = models.db.session.scalars(select(models.Group).where(models.Group.id.in_(user.group_ids))).all()
group_names = [group.name for group in groups]
print("Groups: {}".format(", ".join(group_names)))
2 changes: 1 addition & 1 deletion redash/destinations/asana.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import requests

from redash.destinations import BaseDestination, register
from redash.alerts import Alerts
from redash.destinations import BaseDestination, register


class Asana(BaseDestination):
Expand Down
2 changes: 1 addition & 1 deletion redash/destinations/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import requests

from redash.destinations import BaseDestination, register
from redash.alerts import Alerts
from redash.destinations import BaseDestination, register
from redash.utils import json_dumps

colors = {
Expand Down
2 changes: 1 addition & 1 deletion redash/destinations/webex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import requests

from redash.destinations import BaseDestination, register
from redash.alerts import Alerts
from redash.destinations import BaseDestination, register


class Webex(BaseDestination):
Expand Down
8 changes: 5 additions & 3 deletions redash/handlers/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask_login import current_user, login_required
from sqlalchemy.sql.expression import select

from redash import models, redis_connection
from redash.authentication import current_org
Expand All @@ -19,9 +20,10 @@ def outdated_queries():
if query_ids:
outdated_queries = models.db.session.scalars(
select(models.Query)
.outerjoin(models.QueryResult)
.where(models.Query.id.in_(query_ids))
.order_by(models.Query.created_at.desc())).all()
.outerjoin(models.QueryResult)
.where(models.Query.id.in_(query_ids))
.order_by(models.Query.created_at.desc())
).all()
else:
outdated_queries = []

Expand Down
9 changes: 3 additions & 6 deletions redash/handlers/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def post(self):
@require_permission("list_alerts")
def get(self):
self.record_event({"action": "list", "object_type": "alert"})
alerts = models.db.session.scalars(
models.Alert.all(group_ids=self.current_user.group_ids)).all()
alerts = models.db.session.scalars(models.Alert.all(group_ids=self.current_user.group_ids)).all()
return [serialize_alert(alert) for alert in alerts]


Expand Down Expand Up @@ -127,15 +126,13 @@ def get(self, alert_id):
alert = models.Alert.get_by_id_and_org(alert_id, self.current_org)
require_access(alert, self.current_user, view_only)

subscriptions = models.db.session.scalars(
models.AlertSubscription.all(alert_id)).all()
subscriptions = models.db.session.scalars(models.AlertSubscription.all(alert_id)).all()
return [s.to_dict() for s in subscriptions]


class AlertSubscriptionResource(BaseResource):
def delete(self, alert_id, subscriber_id):
subscription = get_object_or_404(
models.db.session.get, models.AlertSubscription, subscriber_id)
subscription = get_object_or_404(models.db.session.get, models.AlertSubscription, subscriber_id)
require_admin_or_owner(subscription.user.id)
models.db.session.delete(subscription)
models.db.session.commit()
Expand Down
18 changes: 6 additions & 12 deletions redash/handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import time
from inspect import isclass

from flask import Blueprint, current_app, request
from flask_login import current_user, login_required
from flask_restful import Resource, abort
from sqlalchemy import cast
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm.exc import NoResultFound
from inspect import isclass

from redash import settings
from redash.authentication import current_org
from redash.models import db
Expand Down Expand Up @@ -64,6 +65,7 @@ def require_fields(req, fields):
if f not in req:
abort(400)


def get_object_or_404(fn, *args, **kwargs):
try:
rv = fn(*args, **kwargs)
Expand All @@ -75,22 +77,14 @@ def get_object_or_404(fn, *args, **kwargs):


def paginate(query_set, page, per_page, serializer, **kwargs):
results = db.paginate(
query_set,
page=page,
per_page=per_page,
max_per_page=250)
results = db.paginate(query_set, page=page, per_page=per_page, max_per_page=250)

# support for old function based serializers
# support for old function based serializers
if isclass(serializer):
items = serializer(results.items, **kwargs).serialize()
else:
items = [serializer(result) for result in results.items]
return {
"count": results.total,
"page": page,
"per_page": per_page,
"results": items}
return {"count": results.total, "page": page, "per_page": per_page, "results": items}


def org_scoped_rule(rule):
Expand Down

0 comments on commit d3391c3

Please sign in to comment.