Commit 3ad19e78a6efb8dcd19b657148bfda501e3b2994

Authored by André Guedes
Committed by Rodrigo Souto
1 parent 30352374

Removing specific resources of task

lib/noosfero/api/helpers.rb
@@ -45,7 +45,10 @@ module Noosfero @@ -45,7 +45,10 @@ module Noosfero
45 end 45 end
46 end 46 end
47 47
48 - ARTICLE_TYPES = Article.descendants.map{|a| a.to_s} 48 + Rails.application.eager_load!
  49 + ARTICLE_TYPES = ['Article'] + Article.descendants.map{|a| a.to_s}
  50 + TASK_TYPES = ['Task'] + Task.descendants.map{|a| a.to_s}
  51 +
49 52
50 def find_article(articles, id) 53 def find_article(articles, id)
51 article = articles.find(id) 54 article = articles.find(id)
@@ -85,6 +88,34 @@ module Noosfero @@ -85,6 +88,34 @@ module Noosfero
85 task.display_to?(current_user.person) ? task : forbidden! 88 task.display_to?(current_user.person) ? task : forbidden!
86 end 89 end
87 90
  91 + def post_task(asset, params)
  92 + return forbidden! unless current_person.has_permission?(:perform_task, asset)
  93 +
  94 + klass_type= params[:content_type].nil? ? 'Task' : params[:content_type]
  95 + return forbidden! unless TASK_TYPES.include?(klass_type)
  96 +
  97 + task = klass_type.constantize.new(params[:task])
  98 + task.requestor_id = current_person.id
  99 + task.target_id = asset.id
  100 + task.target_type = 'Profile'
  101 +
  102 + if !task.save
  103 + render_api_errors!(task.errors.full_messages)
  104 + end
  105 + present task, :with => Entities::Task, :fields => params[:fields]
  106 + end
  107 +
  108 + def present_task(asset)
  109 + task = find_task(asset.tasks, params[:id])
  110 + present task, :with => Entities::Task, :fields => params[:fields]
  111 + end
  112 +
  113 + def present_tasks(asset)
  114 + tasks = select_filtered_collection_of(asset, 'tasks', params)
  115 + tasks = tasks.select {|t| t.display_to?(current_user.person)}
  116 + present tasks, :with => Entities::Task, :fields => params[:fields]
  117 + end
  118 +
88 def make_conditions_with_parameter(params = {}) 119 def make_conditions_with_parameter(params = {})
89 parsed_params = parser_params(params) 120 parsed_params = parser_params(params)
90 conditions = {} 121 conditions = {}
lib/noosfero/api/v1/tasks.rb
@@ -32,6 +32,31 @@ module Noosfero @@ -32,6 +32,31 @@ module Noosfero
32 32
33 end 33 end
34 34
  35 + kinds = %w[community person enterprise]
  36 + kinds.each do |kind|
  37 + resource kind.pluralize.to_sym do
  38 + segment "/:#{kind}_id" do
  39 + resource :tasks do
  40 + get do
  41 + profile = environment.send(kind.pluralize).find(params["#{kind}_id"])
  42 + present_tasks(profile)
  43 + end
  44 +
  45 + get ':id' do
  46 + profile = environment.send(kind.pluralize).find(params["#{kind}_id"])
  47 + present_task(profile)
  48 + end
  49 +
  50 + post do
  51 + profile = environment.send(kind.pluralize).find(params["#{kind}_id"])
  52 + post_task(profile, params)
  53 + end
  54 + end
  55 + end
  56 + end
  57 + end
  58 +
  59 +
35 resource :communities do 60 resource :communities do
36 segment '/:community_id' do 61 segment '/:community_id' do
37 resource :tasks do 62 resource :tasks do
@@ -73,89 +98,89 @@ module Noosfero @@ -73,89 +98,89 @@ module Noosfero
73 98
74 end 99 end
75 100
76 - resource :people do  
77 - segment '/:person_id' do  
78 - resource :tasks do  
79 - get do  
80 -# person = environment.people.find(params[:person_id])  
81 -# articles = select_filtered_collection_of(person, 'articles', params)  
82 -# articles = articles.display_filter(current_person, person)  
83 -tasks = Task.all  
84 - present tasks, :with => Entities::Task, :fields => params[:fields]  
85 - end  
86 -  
87 - get ':id' do  
88 -# person = environment.people.find(params[:person_id])  
89 -# article = find_article(person.articles, params[:id])  
90 -task = Task.first  
91 - present task, :with => Entities::Task, :fields => params[:fields]  
92 - end  
93 -  
94 - post do  
95 -# person = environment.people.find(params[:person_id])  
96 -# return forbidden! unless current_person.can_post_content?(person)  
97 -#  
98 -# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]  
99 -# return forbidden! unless ARTICLE_TYPES.include?(klass_type)  
100 -#  
101 -# article = klass_type.constantize.new(params[:article])  
102 -# article.last_changed_by = current_person  
103 -# article.created_by= current_person  
104 -# article.profile = person  
105 -#  
106 -# if !article.save  
107 -# render_api_errors!(article.errors.full_messages)  
108 -# end  
109 -task = Task.first  
110 - present task, :with => Entities::Task, :fields => params[:fields]  
111 - end  
112 -  
113 - end  
114 - end  
115 -  
116 - end  
117 -  
118 - resource :enterprises do  
119 - segment '/:enterprise_id' do  
120 - resource :tasks do  
121 - get do  
122 -# enterprise = environment.enterprises.find(params[:enterprise_id])  
123 -# articles = select_filtered_collection_of(enterprise, 'articles', params)  
124 -# articles = articles.display_filter(current_person, enterprise)  
125 -tasks = Task.all  
126 - present tasks, :with => Entities::Task, :fields => params[:fields]  
127 - end  
128 -  
129 - get ':id' do  
130 -# enterprise = environment.enterprises.find(params[:enterprise_id])  
131 -# article = find_article(enterprise.articles, params[:id])  
132 -task = Task.first  
133 - present task, :with => Entities::Task, :fields => params[:fields]  
134 - end  
135 -  
136 - post do  
137 -# enterprise = environment.enterprises.find(params[:enterprise_id])  
138 -# return forbidden! unless current_person.can_post_content?(enterprise)  
139 -#  
140 -# klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]  
141 -# return forbidden! unless ARTICLE_TYPES.include?(klass_type)  
142 -#  
143 -# article = klass_type.constantize.new(params[:article])  
144 -# article.last_changed_by = current_person  
145 -# article.created_by= current_person  
146 -# article.profile = enterprise  
147 -#  
148 -# if !article.save  
149 -# render_api_errors!(article.errors.full_messages)  
150 -# end  
151 -task = Task.first  
152 - present task, :with => Entities::Task, :fields => params[:fields]  
153 - end  
154 -  
155 - end  
156 - end  
157 -  
158 - end 101 +# resource :people do
  102 +# segment '/:person_id' do
  103 +# resource :tasks do
  104 +# get do
  105 +# # person = environment.people.find(params[:person_id])
  106 +# # articles = select_filtered_collection_of(person, 'articles', params)
  107 +# # articles = articles.display_filter(current_person, person)
  108 +# tasks = Task.all
  109 +# present tasks, :with => Entities::Task, :fields => params[:fields]
  110 +# end
  111 +
  112 +# get ':id' do
  113 +# # person = environment.people.find(params[:person_id])
  114 +# # article = find_article(person.articles, params[:id])
  115 +# task = Task.first
  116 +# present task, :with => Entities::Task, :fields => params[:fields]
  117 +# end
  118 +
  119 +# post do
  120 +# # person = environment.people.find(params[:person_id])
  121 +# # return forbidden! unless current_person.can_post_content?(person)
  122 +# #
  123 +# # klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
  124 +# # return forbidden! unless ARTICLE_TYPES.include?(klass_type)
  125 +# #
  126 +# # article = klass_type.constantize.new(params[:article])
  127 +# # article.last_changed_by = current_person
  128 +# # article.created_by= current_person
  129 +# # article.profile = person
  130 +# #
  131 +# # if !article.save
  132 +# # render_api_errors!(article.errors.full_messages)
  133 +# # end
  134 +# task = Task.first
  135 +# present task, :with => Entities::Task, :fields => params[:fields]
  136 +# end
  137 +
  138 +# end
  139 +# end
  140 +
  141 +# end
  142 +
  143 +# resource :enterprises do
  144 +# segment '/:enterprise_id' do
  145 +# resource :tasks do
  146 +# get do
  147 +# # enterprise = environment.enterprises.find(params[:enterprise_id])
  148 +# # articles = select_filtered_collection_of(enterprise, 'articles', params)
  149 +# # articles = articles.display_filter(current_person, enterprise)
  150 +# tasks = Task.all
  151 +# present tasks, :with => Entities::Task, :fields => params[:fields]
  152 +# end
  153 +
  154 +# get ':id' do
  155 +# # enterprise = environment.enterprises.find(params[:enterprise_id])
  156 +# # article = find_article(enterprise.articles, params[:id])
  157 +# task = Task.first
  158 +# present task, :with => Entities::Task, :fields => params[:fields]
  159 +# end
  160 +
  161 +# post do
  162 +# # enterprise = environment.enterprises.find(params[:enterprise_id])
  163 +# # return forbidden! unless current_person.can_post_content?(enterprise)
  164 +# #
  165 +# # klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
  166 +# # return forbidden! unless ARTICLE_TYPES.include?(klass_type)
  167 +# #
  168 +# # article = klass_type.constantize.new(params[:article])
  169 +# # article.last_changed_by = current_person
  170 +# # article.created_by= current_person
  171 +# # article.profile = enterprise
  172 +# #
  173 +# # if !article.save
  174 +# # render_api_errors!(article.errors.full_messages)
  175 +# # end
  176 +# task = Task.first
  177 +# present task, :with => Entities::Task, :fields => params[:fields]
  178 +# end
  179 +
  180 +# end
  181 +# end
  182 +
  183 +# end
159 184
160 185
161 end 186 end