From f864c1139caef513de2a06a45337bff34d214dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cam=20Sa=C3=BCl?= <cammsaul@gmail.com> Date: Tue, 10 Jan 2017 16:20:33 -0800 Subject: [PATCH] Backported cleanup from public cards & dashboards branch :shower: --- src/metabase/api/card.clj | 1 + src/metabase/api/common.clj | 9 +++++++++ src/metabase/db.clj | 18 +++++++++++++----- src/metabase/models/dashboard.clj | 10 ++++++++-- test/metabase/test/util.clj | 11 +++++++---- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/metabase/api/card.clj b/src/metabase/api/card.clj index 2d3c1582a9c..356f1227711 100644 --- a/src/metabase/api/card.clj +++ b/src/metabase/api/card.clj @@ -335,6 +335,7 @@ :constraints constraints) options {:executed-by *current-user-id* :card-id card-id}] + (check-not-archived card) (qp/dataset-query query options))) (defendpoint POST "/:card-id/query" diff --git a/src/metabase/api/common.clj b/src/metabase/api/common.clj index 9ece0a03712..dd083ee41b0 100644 --- a/src/metabase/api/common.clj +++ b/src/metabase/api/common.clj @@ -292,3 +292,12 @@ (write-check (entity id))) ([entity id & other-conditions] (write-check (apply db/select-one entity :id id other-conditions)))) + + +;;; ------------------------------------------------------------ OTHER HELPER FNS ------------------------------------------------------------ + +(defn check-not-archived + "Check that the OBJECT is not `:archived`, or throw a `404`. Returns OBJECT as-is if check passes." + [object] + (u/prog1 object + (check-404 (not (:archived object))))) diff --git a/src/metabase/db.clj b/src/metabase/db.clj index b0888124874..a82e3a43e89 100644 --- a/src/metabase/db.clj +++ b/src/metabase/db.clj @@ -564,6 +564,15 @@ (models/default-fields (resolve-entity entity)))) +(defn do-post-select + "Perform post-processing for objects fetched from the DB. + Convert results OBJECTS to ENTITY record types and call the entity's `post-select` method on them." + {:style/indent 1} + [entity objects] + (let [entity (resolve-entity entity)] + (vec (for [object objects] + (models/do-post-select entity object))))) + (defn simple-select "Select objects from the database. Like `select`, but doesn't offer as many conveniences, so you should use that instead. This calls `post-select` on the results. @@ -572,11 +581,10 @@ {:style/indent 1} [entity honeysql-form] (let [entity (resolve-entity entity)] - (vec (for [object (query (merge {:select (or (models/default-fields entity) - [:*]) - :from [entity]} - honeysql-form))] - (models/do-post-select entity object))))) + (do-post-select entity (query (merge {:select (or (models/default-fields entity) + [:*]) + :from [entity]} + honeysql-form))))) (defn simple-select-one "Select a single object from the database. Like `select-one`, but doesn't offer as many conveniences, so prefer that instead. diff --git a/src/metabase/models/dashboard.clj b/src/metabase/models/dashboard.clj index c08a9ce83c7..5c2144937f3 100644 --- a/src/metabase/models/dashboard.clj +++ b/src/metabase/models/dashboard.clj @@ -2,7 +2,7 @@ (:require [clojure.data :refer [diff]] (metabase [db :as db] [events :as events]) - (metabase.models [card :as card] + (metabase.models [card :refer [Card], :as card] [dashboard-card :refer [DashboardCard], :as dashboard-card] [hydrate :refer [hydrate]] [interface :as i] @@ -59,7 +59,13 @@ "Return the `DashboardCards` associated with DASHBOARD, in the order they were created." {:hydrate :ordered_cards} [dashboard] - (db/select DashboardCard, :dashboard_id (u/get-id dashboard), {:order-by [[:created_at :asc]]})) + (db/do-post-select DashboardCard + (db/query {:select [:dashcard.*] + :from [[DashboardCard :dashcard]] + :join [[Card :card] [:= :dashcard.card_id :card.id]] + :where [:and [:= :dashcard.dashboard_id (u/get-id dashboard)] + [:= :card.archived false]] + :order-by [[:dashcard.created_at :asc]]}))) ;;; ## ---------------------------------------- PERSISTENCE FUNCTIONS ---------------------------------------- diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj index 9ac8a136563..c7a4de3d270 100644 --- a/test/metabase/test/util.clj +++ b/test/metabase/test/util.clj @@ -89,6 +89,13 @@ [k (f v)]))))) +(defn- user-id [username] + (require 'metabase.test.data.users) + ((resolve 'metabase.test.data.users/user->id) username)) + +(defn- rasta-id [] (user-id :rasta)) + + (defprotocol ^:private WithTempDefaults (^:private with-temp-defaults [this])) @@ -96,10 +103,6 @@ WithTempDefaults {:with-temp-defaults (constantly {})}) -(defn- rasta-id [] - (require 'metabase.test.data.users) - ((resolve 'metabase.test.data.users/user->id) :rasta)) - (u/strict-extend (class Card) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id (rasta-id) -- GitLab