annotate .svn/pristine/4a/4ae302ddbfc2e165220960caea9f80efe7375d8b.svn-base @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class TimeEntryQuery < Query
Chris@1517 19
Chris@1517 20 self.queried_class = TimeEntry
Chris@1517 21
Chris@1517 22 self.available_columns = [
Chris@1517 23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1517 24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
Chris@1517 25 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1517 26 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
Chris@1517 27 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
Chris@1517 28 QueryColumn.new(:comments),
Chris@1517 29 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
Chris@1517 30 ]
Chris@1517 31
Chris@1517 32 def initialize(attributes=nil, *args)
Chris@1517 33 super attributes
Chris@1517 34 self.filters ||= {}
Chris@1517 35 add_filter('spent_on', '*') unless filters.present?
Chris@1517 36 end
Chris@1517 37
Chris@1517 38 def initialize_available_filters
Chris@1517 39 add_available_filter "spent_on", :type => :date_past
Chris@1517 40
Chris@1517 41 principals = []
Chris@1517 42 if project
Chris@1517 43 principals += project.principals.sort
Chris@1517 44 unless project.leaf?
Chris@1517 45 subprojects = project.descendants.visible.all
Chris@1517 46 if subprojects.any?
Chris@1517 47 add_available_filter "subproject_id",
Chris@1517 48 :type => :list_subprojects,
Chris@1517 49 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1517 50 principals += Principal.member_of(subprojects)
Chris@1517 51 end
Chris@1517 52 end
Chris@1517 53 else
Chris@1517 54 if all_projects.any?
Chris@1517 55 # members of visible projects
Chris@1517 56 principals += Principal.member_of(all_projects)
Chris@1517 57 # project filter
Chris@1517 58 project_values = []
Chris@1517 59 if User.current.logged? && User.current.memberships.any?
Chris@1517 60 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1517 61 end
Chris@1517 62 project_values += all_projects_values
Chris@1517 63 add_available_filter("project_id",
Chris@1517 64 :type => :list, :values => project_values
Chris@1517 65 ) unless project_values.empty?
Chris@1517 66 end
Chris@1517 67 end
Chris@1517 68 principals.uniq!
Chris@1517 69 principals.sort!
Chris@1517 70 users = principals.select {|p| p.is_a?(User)}
Chris@1517 71
Chris@1517 72 users_values = []
Chris@1517 73 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1517 74 users_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1517 75 add_available_filter("user_id",
Chris@1517 76 :type => :list_optional, :values => users_values
Chris@1517 77 ) unless users_values.empty?
Chris@1517 78
Chris@1517 79 activities = (project ? project.activities : TimeEntryActivity.shared.active)
Chris@1517 80 add_available_filter("activity_id",
Chris@1517 81 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
Chris@1517 82 ) unless activities.empty?
Chris@1517 83
Chris@1517 84 add_available_filter "comments", :type => :text
Chris@1517 85 add_available_filter "hours", :type => :float
Chris@1517 86
Chris@1517 87 add_custom_fields_filters(TimeEntryCustomField)
Chris@1517 88 add_associations_custom_fields_filters :project, :issue, :user
Chris@1517 89 end
Chris@1517 90
Chris@1517 91 def available_columns
Chris@1517 92 return @available_columns if @available_columns
Chris@1517 93 @available_columns = self.class.available_columns.dup
Chris@1517 94 @available_columns += TimeEntryCustomField.visible.
Chris@1517 95 map {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1517 96 @available_columns += IssueCustomField.visible.
Chris@1517 97 map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
Chris@1517 98 @available_columns
Chris@1517 99 end
Chris@1517 100
Chris@1517 101 def default_columns_names
Chris@1517 102 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
Chris@1517 103 end
Chris@1517 104
Chris@1517 105 def results_scope(options={})
Chris@1517 106 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1517 107
Chris@1517 108 TimeEntry.visible.
Chris@1517 109 where(statement).
Chris@1517 110 order(order_option).
Chris@1517 111 joins(joins_for_order_statement(order_option.join(','))).
Chris@1517 112 includes(:activity)
Chris@1517 113 end
Chris@1517 114
Chris@1517 115 def sql_for_activity_id_field(field, operator, value)
Chris@1517 116 condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
Chris@1517 117 condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
Chris@1517 118 ids = value.map(&:to_i).join(',')
Chris@1517 119 table_name = Enumeration.table_name
Chris@1517 120 if operator == '='
Chris@1517 121 "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))"
Chris@1517 122 else
Chris@1517 123 "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))"
Chris@1517 124 end
Chris@1517 125 end
Chris@1517 126
Chris@1517 127 # Accepts :from/:to params as shortcut filters
Chris@1517 128 def build_from_params(params)
Chris@1517 129 super
Chris@1517 130 if params[:from].present? && params[:to].present?
Chris@1517 131 add_filter('spent_on', '><', [params[:from], params[:to]])
Chris@1517 132 elsif params[:from].present?
Chris@1517 133 add_filter('spent_on', '>=', [params[:from]])
Chris@1517 134 elsif params[:to].present?
Chris@1517 135 add_filter('spent_on', '<=', [params[:to]])
Chris@1517 136 end
Chris@1517 137 self
Chris@1517 138 end
Chris@1517 139 end