From 3ad19e78a6efb8dcd19b657148bfda501e3b2994 Mon Sep 17 00:00:00 2001 From: André Bernardes Date: Mon, 29 Jun 2015 15:48:51 -0300 Subject: [PATCH] Removing specific resources of task --- lib/noosfero/api/helpers.rb | 33 ++++++++++++++++++++++++++++++++- lib/noosfero/api/v1/tasks.rb | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------- 2 files changed, 140 insertions(+), 84 deletions(-) diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 0838a9a..9030f6c 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -45,7 +45,10 @@ module Noosfero end end - ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} + Rails.application.eager_load! + ARTICLE_TYPES = ['Article'] + Article.descendants.map{|a| a.to_s} + TASK_TYPES = ['Task'] + Task.descendants.map{|a| a.to_s} + def find_article(articles, id) article = articles.find(id) @@ -85,6 +88,34 @@ module Noosfero task.display_to?(current_user.person) ? task : forbidden! end + def post_task(asset, params) + return forbidden! unless current_person.has_permission?(:perform_task, asset) + + klass_type= params[:content_type].nil? ? 'Task' : params[:content_type] + return forbidden! unless TASK_TYPES.include?(klass_type) + + task = klass_type.constantize.new(params[:task]) + task.requestor_id = current_person.id + task.target_id = asset.id + task.target_type = 'Profile' + + if !task.save + render_api_errors!(task.errors.full_messages) + end + present task, :with => Entities::Task, :fields => params[:fields] + end + + def present_task(asset) + task = find_task(asset.tasks, params[:id]) + present task, :with => Entities::Task, :fields => params[:fields] + end + + def present_tasks(asset) + tasks = select_filtered_collection_of(asset, 'tasks', params) + tasks = tasks.select {|t| t.display_to?(current_user.person)} + present tasks, :with => Entities::Task, :fields => params[:fields] + end + def make_conditions_with_parameter(params = {}) parsed_params = parser_params(params) conditions = {} diff --git a/lib/noosfero/api/v1/tasks.rb b/lib/noosfero/api/v1/tasks.rb index 727617f..7e581b7 100644 --- a/lib/noosfero/api/v1/tasks.rb +++ b/lib/noosfero/api/v1/tasks.rb @@ -32,6 +32,31 @@ module Noosfero end + kinds = %w[community person enterprise] + kinds.each do |kind| + resource kind.pluralize.to_sym do + segment "/:#{kind}_id" do + resource :tasks do + get do + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) + present_tasks(profile) + end + + get ':id' do + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) + present_task(profile) + end + + post do + profile = environment.send(kind.pluralize).find(params["#{kind}_id"]) + post_task(profile, params) + end + end + end + end + end + + resource :communities do segment '/:community_id' do resource :tasks do @@ -73,89 +98,89 @@ module Noosfero end - resource :people do - segment '/:person_id' do - resource :tasks do - get do -# person = environment.people.find(params[:person_id]) -# articles = select_filtered_collection_of(person, 'articles', params) -# articles = articles.display_filter(current_person, person) -tasks = Task.all - present tasks, :with => Entities::Task, :fields => params[:fields] - end - - get ':id' do -# person = environment.people.find(params[:person_id]) -# article = find_article(person.articles, params[:id]) -task = Task.first - present task, :with => Entities::Task, :fields => params[:fields] - end - - post do -# person = environment.people.find(params[:person_id]) -# return forbidden! unless current_person.can_post_content?(person) -# -# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] -# return forbidden! unless ARTICLE_TYPES.include?(klass_type) -# -# article = klass_type.constantize.new(params[:article]) -# article.last_changed_by = current_person -# article.created_by= current_person -# article.profile = person -# -# if !article.save -# render_api_errors!(article.errors.full_messages) -# end -task = Task.first - present task, :with => Entities::Task, :fields => params[:fields] - end - - end - end - - end - - resource :enterprises do - segment '/:enterprise_id' do - resource :tasks do - get do -# enterprise = environment.enterprises.find(params[:enterprise_id]) -# articles = select_filtered_collection_of(enterprise, 'articles', params) -# articles = articles.display_filter(current_person, enterprise) -tasks = Task.all - present tasks, :with => Entities::Task, :fields => params[:fields] - end - - get ':id' do -# enterprise = environment.enterprises.find(params[:enterprise_id]) -# article = find_article(enterprise.articles, params[:id]) -task = Task.first - present task, :with => Entities::Task, :fields => params[:fields] - end - - post do -# enterprise = environment.enterprises.find(params[:enterprise_id]) -# return forbidden! unless current_person.can_post_content?(enterprise) -# -# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] -# return forbidden! unless ARTICLE_TYPES.include?(klass_type) -# -# article = klass_type.constantize.new(params[:article]) -# article.last_changed_by = current_person -# article.created_by= current_person -# article.profile = enterprise -# -# if !article.save -# render_api_errors!(article.errors.full_messages) -# end -task = Task.first - present task, :with => Entities::Task, :fields => params[:fields] - end - - end - end - - end +# resource :people do +# segment '/:person_id' do +# resource :tasks do +# get do +# # person = environment.people.find(params[:person_id]) +# # articles = select_filtered_collection_of(person, 'articles', params) +# # articles = articles.display_filter(current_person, person) +# tasks = Task.all +# present tasks, :with => Entities::Task, :fields => params[:fields] +# end + +# get ':id' do +# # person = environment.people.find(params[:person_id]) +# # article = find_article(person.articles, params[:id]) +# task = Task.first +# present task, :with => Entities::Task, :fields => params[:fields] +# end + +# post do +# # person = environment.people.find(params[:person_id]) +# # return forbidden! unless current_person.can_post_content?(person) +# # +# # klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] +# # return forbidden! unless ARTICLE_TYPES.include?(klass_type) +# # +# # article = klass_type.constantize.new(params[:article]) +# # article.last_changed_by = current_person +# # article.created_by= current_person +# # article.profile = person +# # +# # if !article.save +# # render_api_errors!(article.errors.full_messages) +# # end +# task = Task.first +# present task, :with => Entities::Task, :fields => params[:fields] +# end + +# end +# end + +# end + +# resource :enterprises do +# segment '/:enterprise_id' do +# resource :tasks do +# get do +# # enterprise = environment.enterprises.find(params[:enterprise_id]) +# # articles = select_filtered_collection_of(enterprise, 'articles', params) +# # articles = articles.display_filter(current_person, enterprise) +# tasks = Task.all +# present tasks, :with => Entities::Task, :fields => params[:fields] +# end + +# get ':id' do +# # enterprise = environment.enterprises.find(params[:enterprise_id]) +# # article = find_article(enterprise.articles, params[:id]) +# task = Task.first +# present task, :with => Entities::Task, :fields => params[:fields] +# end + +# post do +# # enterprise = environment.enterprises.find(params[:enterprise_id]) +# # return forbidden! unless current_person.can_post_content?(enterprise) +# # +# # klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type] +# # return forbidden! unless ARTICLE_TYPES.include?(klass_type) +# # +# # article = klass_type.constantize.new(params[:article]) +# # article.last_changed_by = current_person +# # article.created_by= current_person +# # article.profile = enterprise +# # +# # if !article.save +# # render_api_errors!(article.errors.full_messages) +# # end +# task = Task.first +# present task, :with => Entities::Task, :fields => params[:fields] +# end + +# end +# end + +# end end -- libgit2 0.21.2