Adds REST API for TimeEntries (#6823).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4461 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2010-12-04 10:13:15 +00:00
parent 9284a32c9a
commit f7cf8aa878
5 changed files with 228 additions and 18 deletions

View File

@ -1,5 +1,5 @@
# redMine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang
# Redmine - project management software
# Copyright (C) 2006-2010 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
@ -18,10 +18,11 @@
class TimelogController < ApplicationController
menu_item :issues
before_filter :find_project, :only => [:new, :create]
before_filter :find_time_entry, :only => [:edit, :update, :destroy]
before_filter :find_time_entry, :only => [:show, :edit, :update, :destroy]
before_filter :authorize, :except => [:index]
before_filter :find_optional_project, :only => [:index]
accept_key_auth :index, :show, :create, :update, :destroy
helper :sort
include SortHelper
helper :issues
@ -66,6 +67,18 @@ class TimelogController < ApplicationController
render :layout => !request.xhr?
}
format.api {
@entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
@entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
@entries = TimeEntry.find(:all,
:include => [:project, :activity, :user, {:issue => :tracker}],
:conditions => cond.conditions,
:order => sort_clause,
:limit => @entry_pages.items_per_page,
:offset => @entry_pages.current.offset)
render :template => 'timelog/index.apit'
}
format.atom {
entries = TimeEntry.find(:all,
:include => [:project, :activity, :user, {:issue => :tracker}],
@ -85,6 +98,14 @@ class TimelogController < ApplicationController
end
end
end
def show
respond_to do |format|
# TODO: Implement html response
format.html { render :nothing => true, :status => 406 }
format.api { render :template => 'timelog/show.apit' }
end
end
def new
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
@ -102,10 +123,18 @@ class TimelogController < ApplicationController
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
if @time_entry.save
flash[:notice] = l(:notice_successful_update)
redirect_back_or_default :action => 'index', :project_id => @time_entry.project
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_back_or_default :action => 'index', :project_id => @time_entry.project
}
format.api { render :template => 'timelog/show.apit', :status => :created, :location => time_entry_url(@time_entry) }
end
else
render :action => 'edit'
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@time_entry) }
end
end
end
@ -122,21 +151,40 @@ class TimelogController < ApplicationController
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
if @time_entry.save
flash[:notice] = l(:notice_successful_update)
redirect_back_or_default :action => 'index', :project_id => @time_entry.project
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_back_or_default :action => 'index', :project_id => @time_entry.project
}
format.api { head :ok }
end
else
render :action => 'edit'
respond_to do |format|
format.html { render :action => 'edit' }
format.api { render_validation_errors(@time_entry) }
end
end
end
verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
def destroy
if @time_entry.destroy && @time_entry.destroyed?
flash[:notice] = l(:notice_successful_delete)
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_delete)
redirect_to :back
}
format.api { head :ok }
end
else
flash[:error] = l(:notice_unable_delete_time_entry)
respond_to do |format|
format.html {
flash[:notice] = l(:notice_unable_delete_time_entry)
redirect_to :back
}
format.api { render_validation_errors(@time_entry) }
end
end
redirect_to :back
rescue ::ActionController::RedirectBackError
redirect_to :action => 'index', :project_id => @time_entry.project
end
@ -154,11 +202,11 @@ private
end
def find_project
if params[:issue_id]
@issue = Issue.find(params[:issue_id])
if issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])
@issue = Issue.find(issue_id)
@project = @issue.project
elsif params[:project_id]
@project = Project.find(params[:project_id])
elsif project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])
@project = Project.find(project_id)
else
render_404
return false

View File

@ -0,0 +1,16 @@
api.array :time_entries do
@entries.each do |time_entry|
api.time_entry do
api.id time_entry.id
api.project(:id => time_entry.project_id, :name => time_entry.project.name) unless time_entry.project.nil?
api.issue(:id => time_entry.issue_id) unless time_entry.issue.nil?
api.user(:id => time_entry.user_id, :name => time_entry.user.name) unless time_entry.user.nil?
api.activity(:id => time_entry.activity_id, :name => time_entry.activity.name) unless time_entry.activity.nil?
api.hours time_entry.hours
api.comments time_entry.comments
api.spent_on time_entry.spent_on
api.created_on time_entry.created_on
api.updated_on time_entry.updated_on
end
end
end

View File

@ -0,0 +1,12 @@
api.time_entry do
api.id @time_entry.id
api.project(:id => @time_entry.project_id, :name => @time_entry.project.name) unless @time_entry.project.nil?
api.issue(:id => @time_entry.issue_id) unless @time_entry.issue.nil?
api.user(:id => @time_entry.user_id, :name => @time_entry.user.name) unless @time_entry.user.nil?
api.activity(:id => @time_entry.activity_id, :name => @time_entry.activity.name) unless @time_entry.activity.nil?
api.hours @time_entry.hours
api.comments @time_entry.comments
api.spent_on @time_entry.spent_on
api.created_on @time_entry.created_on
api.updated_on @time_entry.updated_on
end

View File

@ -85,7 +85,7 @@ Redmine::AccessControl.map do |map|
map.project_module :time_tracking do |map|
map.permission :log_time, {:timelog => [:new, :create, :edit, :update]}, :require => :loggedin
map.permission :view_time_entries, :timelog => [:index], :time_entry_reports => [:report]
map.permission :view_time_entries, :timelog => [:index, :show], :time_entry_reports => [:report]
map.permission :edit_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :member
map.permission :edit_own_time_entries, {:timelog => [:new, :create, :edit, :update, :destroy]}, :require => :loggedin
map.permission :manage_project_activities, {:project_enumerations => [:update, :destroy]}, :require => :member

View File

@ -0,0 +1,134 @@
# Redmine - project management software
# Copyright (C) 2006-2010 Jean-Philippe Lang
#
# 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.
require "#{File.dirname(__FILE__)}/../../test_helper"
class ApiTest::TimeEntriesTest < ActionController::IntegrationTest
fixtures :all
def setup
Setting.rest_api_enabled = '1'
end
context "GET /time_entries.xml" do
should "return time entries" do
get '/time_entries.xml', {}, :authorization => credentials('jsmith')
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'time_entries',
:child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
end
end
context "GET /time_entries/2.xml" do
should "return requested time entry" do
get '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'time_entry',
:child => {:tag => 'id', :content => '2'}
end
end
context "POST /time_entries.xml" do
context "with issue_id" do
should "return create time entry" do
assert_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
end
assert_response :created
assert_equal 'application/xml', @response.content_type
entry = TimeEntry.first(:order => 'id DESC')
assert_equal 'jsmith', entry.user.login
assert_equal Issue.find(1), entry.issue
assert_equal Project.find(1), entry.project
assert_equal Date.parse('2010-12-02'), entry.spent_on
assert_equal 3.5, entry.hours
assert_equal TimeEntryActivity.find(11), entry.activity
end
end
context "with project_id" do
should "return create time entry" do
assert_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, :authorization => credentials('jsmith')
end
assert_response :created
assert_equal 'application/xml', @response.content_type
entry = TimeEntry.first(:order => 'id DESC')
assert_equal 'jsmith', entry.user.login
assert_nil entry.issue
assert_equal Project.find(1), entry.project
assert_equal Date.parse('2010-12-02'), entry.spent_on
assert_equal 3.5, entry.hours
assert_equal TimeEntryActivity.find(11), entry.activity
end
end
context "with invalid parameters" do
should "return errors" do
assert_no_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, :authorization => credentials('jsmith')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
end
end
end
context "PUT /time_entries/2.xml" do
context "with valid parameters" do
should "update time entry" do
assert_no_difference 'TimeEntry.count' do
put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, :authorization => credentials('jsmith')
end
assert_response :ok
assert_equal 'API Update', TimeEntry.find(2).comments
end
end
context "with invalid parameters" do
should "return errors" do
assert_no_difference 'TimeEntry.count' do
put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, :authorization => credentials('jsmith')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
end
end
end
context "DELETE /time_entries/2.xml" do
should "destroy time entry" do
assert_difference 'TimeEntry.count', -1 do
delete '/time_entries/2.xml', {}, :authorization => credentials('jsmith')
end
assert_response :ok
assert_nil TimeEntry.find_by_id(2)
end
end
def credentials(user, password=nil)
ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
end
end