From 3f6a722bcb2f7b5b3b4cf6f54a443b1c1c0cbb77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Tue, 18 Feb 2020 15:28:34 +0100 Subject: [PATCH] #1080 Revision accesses --- app/controllers/dmsf_controller.rb | 4 +- app/controllers/dmsf_files_controller.rb | 12 ++- app/models/dmsf_file_revision.rb | 15 ---- app/models/dmsf_file_revision_access.rb | 4 + app/models/dmsf_file_revision_access_query.rb | 90 +++++++++++++++++++ .../dmsf_files/_revision_access.html.erb | 62 +++++++++---- app/views/dmsf_files/show.html.erb | 6 +- config/locales/en.yml | 3 + ...ndex_on_dmsf_file_revision_id_to_access.rb | 27 ++++++ 9 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 app/models/dmsf_file_revision_access_query.rb create mode 100644 db/migrate/20200218142414_add_index_on_dmsf_file_revision_id_to_access.rb diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index df67ed79..92231d52 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -95,7 +95,7 @@ class DmsfController < ApplicationController format.html { @dmsf_count = @query.dmsf_count @dmsf_pages = Paginator.new @dmsf_count, per_page_option, params['page'] - @dmsf_nodes = @query.dmsf_nodes(offset: @dmsf_pages.offset, limit: @dmsf_pages.per_page) + #@dmsf_nodes = @query.dmsf_nodes(offset: @dmsf_pages.offset, limit: @dmsf_pages.per_page) render layout: !request.xhr? } format.api @@ -141,7 +141,7 @@ class DmsfController < ApplicationController format.html { @dmsf_count = @query.dmsf_count @dmsf_pages = Paginator.new @dmsf_count, per_page_option, params['page'] - @dmsf_nodes = @query.dmsf_nodes(offset: @dmsf_pages.offset, limit: @dmsf_pages.per_page) + #@dmsf_nodes = @query.dmsf_nodes(offset: @dmsf_pages.offset, limit: @dmsf_pages.per_page) render layout: !request.xhr? } end diff --git a/app/controllers/dmsf_files_controller.rb b/app/controllers/dmsf_files_controller.rb index 922ccce2..b0103bd4 100644 --- a/app/controllers/dmsf_files_controller.rb +++ b/app/controllers/dmsf_files_controller.rb @@ -32,10 +32,13 @@ class DmsfFilesController < ApplicationController accept_api_auth :show, :view, :delete - helper :all + helper :all #TODO: Is it needed? helper :dmsf_workflows helper :dmsf + helper :queries + include QueriesHelper + def permissions if @file render_403 unless DmsfFolder.permissions?(@file.dmsf_folder) @@ -84,6 +87,13 @@ class DmsfFilesController < ApplicationController @file_delete_allowed = User.current.allowed_to?(:file_delete, @project) @file_manipulation_allowed = User.current.allowed_to?(:file_manipulation, @project) @revision_pages = Paginator.new @file.dmsf_file_revisions.visible.count, params['per_page'] ? params['per_page'].to_i : 25, params['page'] + @rlf = cookies[:dmsf_switch_rlf] == 'true' + if @rlf + @revision_access_query = retrieve_query(DmsfFileRevisionAccessQuery, true) + @revision_access_query.revision_id = @revision.id + @revision_access_count = @revision_access_query.access_count + @revision_access_pages = Paginator.new @revision_access_count, per_page_option, params['page'] + end respond_to do |format| format.html { diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb index 1cdd06fa..8f59dd22 100644 --- a/app/models/dmsf_file_revision.rb +++ b/app/models/dmsf_file_revision.rb @@ -142,21 +142,6 @@ class DmsfFileRevision < ActiveRecord::Base super end - # In a static call, we find the first matched record on base object type and - # then run the access_grouped call against it - def self.access_grouped(revision_id) - DmsfFileRevision.find(revision_id).first.access_grouped - end - - # Get grouped data from dmsf_file_revision_access about file interactions - # - 22-06-2012 - Rather than calling a static, we should use the access - # (has_many) to re-run a query - it makes more sense then executing - # custom SQL into a temporary object - # - def access_grouped - dmsf_file_revision_access.select('user_id, COUNT(*) AS count, MIN(created_at) AS first_at, MAX(created_at) AS last_at').group('user_id') - end - def version ver = DmsfUploadHelper::gui_version(major_version).to_s if -minor_version != ' '.ord diff --git a/app/models/dmsf_file_revision_access.rb b/app/models/dmsf_file_revision_access.rb index b2e4c74c..a2c61985 100644 --- a/app/models/dmsf_file_revision_access.rb +++ b/app/models/dmsf_file_revision_access.rb @@ -24,9 +24,13 @@ class DmsfFileRevisionAccess < ActiveRecord::Base belongs_to :dmsf_file_revision belongs_to :user + delegate :dmsf_file, :to => :dmsf_file_revision, :allow_nil => false delegate :project, :to => :dmsf_file, :allow_nil => false + # TODO: dmsf_file_revision_accesses.dmsf_file_revision_id should be a key + scope :access_grouped, -> { select('user_id, COUNT(*) AS count, MIN(created_at) AS first_at, MAX(created_at) AS last_at').group('user_id') } + validates :dmsf_file_revision, presence: true DownloadAction = 0 diff --git a/app/models/dmsf_file_revision_access_query.rb b/app/models/dmsf_file_revision_access_query.rb new file mode 100644 index 00000000..515ce693 --- /dev/null +++ b/app/models/dmsf_file_revision_access_query.rb @@ -0,0 +1,90 @@ +# encode: utf-8 +# frozen_string_literal: true +# +# Redmine plugin for Document Management System "Features" +# +# Copyright © 2011-20 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +class DmsfFileRevisionAccessQuery < Query + + attr_accessor :revision_id + + self.queried_class = DmsfFileRevisionAccess + self.view_permission = :view_dmsf_files + + # Standard columns + self.available_columns = [ + QueryColumn.new(:user, frozen: true), + QueryColumn.new(:count, frozen: true), + QueryColumn.new(:first_at, frozen: true), + QueryColumn.new(:last_at, frozen: true) + ] + + def initialize(attributes) + super attributes + self.sort_criteria = [] + self.filters = {} + end + + ###################################################################################################################### + # Inherited + # + + def base_scope + unless @scope + @scope = DmsfFileRevisionAccess. + where(dmsf_file_revision_id: revision_id) + end + @scope + end + + # Returns the issue count + def access_count + base_scope. + group(:user_id). + count.size + rescue ::ActiveRecord::StatementInvalid => e + raise StatementInvalid.new(e.message) + end + + def type + 'DmsfFileRevisionAccessQuery' + end + + def available_columns + unless @available_columns + @available_columns = self.class.available_columns.dup + end + @available_columns + end + + #alias default_columns_names available_columns + + ###################################################################################################################### + # New + + def accesses(options={}) + base_scope. + access_grouped. + joins(:user). + order('`count` DESC'). + limit(options[:limit]). + offset(options[:offset]) + end + +end diff --git a/app/views/dmsf_files/_revision_access.html.erb b/app/views/dmsf_files/_revision_access.html.erb index 16f3d29e..e225fb01 100644 --- a/app/views/dmsf_files/_revision_access.html.erb +++ b/app/views/dmsf_files/_revision_access.html.erb @@ -22,23 +22,49 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. %> - - - - - - - - - - - <% revision.access_grouped.each do |access| %> +<% if query %> + <% query_options = nil unless defined?(query_options) %> + <% query_options ||= {} %> +
+
<%= l(:field_user) %><%= l(:heading_access_downloads_emails) %><%= l(:heading_access_first) %><%= l(:heading_access_last) %>
+ - - - - + <% query.inline_columns.each do |column| %> + <%= column_header(query, column, query_options) %> + <% end %> - <% end %> - -
<%= link_to_user(access.user) %><%= access.count %><%= format_time(access.first_at) %><%= format_time(access.last_at) %>
+ + + <% query.accesses.each do |access| %> + + <% query.inline_columns.each do |column| %> + <%= content_tag('td', column_content(column, access), class: column.css_classes) %> + <% end %> + + <% end %> + + + + <%= pagination_links_full revision_access_pages, revision_access_count %> +<% else %> + + + + + + + + + + + <% DmsfFileRevisionAccess.access_grouped.where(dmsf_file_revision_id: revision.id).each do |access| %> + + + + + + + <% end %> + +
<%= l(:field_user) %><%= l(:heading_access_downloads_emails) %><%= l(:heading_access_first) %><%= l(:heading_access_last) %>
<%= link_to_user(access.user) %><%= access.count %><%= format_time(access.first_at) %><%= format_time(access.last_at) %>
+<% end %> diff --git a/app/views/dmsf_files/show.html.erb b/app/views/dmsf_files/show.html.erb index 45857274..a7db2f39 100644 --- a/app/views/dmsf_files/show.html.erb +++ b/app/views/dmsf_files/show.html.erb @@ -167,7 +167,11 @@
" style="display:none"> - <%= render(:partial => 'revision_access', locals: { revision: revision }) if @file_manipulation_allowed %> + <% if @file_manipulation_allowed %> + <%= render partial: 'revision_access', locals: { revision: revision, query: @query, + revision_access_count: @revision_access_count, + revision_access_pages: @revision_access_pages } %> + <% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 2358fcf0..6f8445f2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -418,6 +418,9 @@ en: field_modified: Updated field_extension: Ext. field_updated: Updated + field_count: Downloads/Email + field_first_at: First + field_last_at: Last easy_pages: modules: diff --git a/db/migrate/20200218142414_add_index_on_dmsf_file_revision_id_to_access.rb b/db/migrate/20200218142414_add_index_on_dmsf_file_revision_id_to_access.rb new file mode 100644 index 00000000..9ce95cc6 --- /dev/null +++ b/db/migrate/20200218142414_add_index_on_dmsf_file_revision_id_to_access.rb @@ -0,0 +1,27 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright © 2011-20 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class AddIndexOnDmsfFileRevisionIdToAccess < ActiveRecord::Migration[5.2] + + def change + add_index :dmsf_file_revision_accesses, :dmsf_file_revision_id + end + +end