"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/models/time_entry_import.rb" (6 Apr 2020, 3608 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "time_entry_import.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 class TimeEntryImport < Import
   21   def self.menu_item
   22     :time_entries
   23   end
   24 
   25   def self.authorized?(user)
   26     user.allowed_to?(:import_time_entries, nil, :global => true)
   27   end
   28 
   29   # Returns the objects that were imported
   30   def saved_objects
   31     TimeEntry.where(:id => saved_items.pluck(:obj_id)).order(:id).preload(:activity, :project, :issue => [:tracker, :priority, :status])
   32   end
   33 
   34   def mappable_custom_fields
   35     TimeEntryCustomField.all
   36   end
   37 
   38   def allowed_target_projects
   39     Project.allowed_to(user, :log_time).order(:lft)
   40   end
   41 
   42   def allowed_target_activities
   43     project.activities
   44   end
   45 
   46   def allowed_target_users
   47     users = []
   48     if project
   49       users = project.members.active.preload(:user)
   50       users = users.map(&:user).select{ |u| u.allowed_to?(:log_time, project) }
   51     end
   52     users << User.current if User.current.logged? && !users.include?(User.current)
   53     users
   54   end
   55 
   56   def project
   57     project_id = mapping['project_id'].to_i
   58     allowed_target_projects.find_by_id(project_id) || allowed_target_projects.first
   59   end
   60 
   61   def activity
   62     if mapping['activity'].to_s =~ /\Avalue:(\d+)\z/
   63       activity_id = $1.to_i
   64       allowed_target_activities.find_by_id(activity_id)
   65     end
   66   end
   67 
   68   def user_value
   69     if mapping['user_id'].to_s =~ /\Avalue:(\d+)\z/
   70       $1.to_i
   71     end
   72   end
   73 
   74   private
   75 
   76   def build_object(row, item)
   77     object = TimeEntry.new
   78     object.author = user
   79 
   80     activity_id = nil
   81     if activity
   82       activity_id = activity.id
   83     elsif activity_name = row_value(row, 'activity')
   84       activity_id = allowed_target_activities.named(activity_name).first.try(:id)
   85     end
   86 
   87     user_id = nil
   88     if user.allowed_to?(:log_time_for_other_users, project)
   89       if user_value
   90         user_id = user_value
   91       elsif user_name = row_value(row, 'user_id')
   92         user_id = Principal.detect_by_keyword(allowed_target_users, user_name).try(:id)
   93       end
   94     else
   95       user_id = user.id
   96     end
   97 
   98     attributes = {
   99       :project_id  => project.id,
  100       :activity_id => activity_id,
  101       :author_id   => user.id,
  102       :user_id     => user_id,
  103 
  104       :issue_id    => row_value(row, 'issue_id'),
  105       :spent_on    => row_date(row, 'spent_on'),
  106       :hours       => row_value(row, 'hours'),
  107       :comments    => row_value(row, 'comments')
  108     }
  109 
  110     attributes['custom_field_values'] = object.custom_field_values.inject({}) do |h, v|
  111       value =
  112         case v.custom_field.field_format
  113         when 'date'
  114           row_date(row, "cf_#{v.custom_field.id}")
  115         else
  116           row_value(row, "cf_#{v.custom_field.id}")
  117         end
  118       if value
  119         h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, object)
  120       end
  121       h
  122     end
  123 
  124     object.send(:safe_attributes=, attributes, user)
  125     object
  126   end
  127 end