Commit 3a430590622aaa7ba730e89202f00f73032d1d61
1 parent
06e7258c
Exists in
ratings_minor_fixes
and in
4 other branches
Add steps to upload image using people's endpoint
- Replace after_validation by a helper method
Showing
6 changed files
with
76 additions
and
10 deletions
Show diff stats
lib/noosfero/api/helpers.rb
1 | require 'grape' | 1 | require 'grape' |
2 | +require 'base64' | ||
3 | +require 'tempfile' | ||
2 | require_relative '../../find_by_contents' | 4 | require_relative '../../find_by_contents' |
3 | 5 | ||
4 | - module Noosfero; | ||
5 | - module API | ||
6 | - module APIHelpers | 6 | +module Noosfero; |
7 | + module API | ||
8 | + module APIHelpers | ||
7 | PRIVATE_TOKEN_PARAM = :private_token | 9 | PRIVATE_TOKEN_PARAM = :private_token |
8 | DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived] | 10 | DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived] |
9 | 11 | ||
@@ -231,7 +233,7 @@ require_relative '../../find_by_contents' | @@ -231,7 +233,7 @@ require_relative '../../find_by_contents' | ||
231 | else | 233 | else |
232 | created_at = scope.find(reference_id).created_at | 234 | created_at = scope.find(reference_id).created_at |
233 | scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) | 235 | scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) |
234 | - end | 236 | + end |
235 | end | 237 | end |
236 | 238 | ||
237 | def by_categories(scope, params) | 239 | def by_categories(scope, params) |
@@ -368,6 +370,30 @@ require_relative '../../find_by_contents' | @@ -368,6 +370,30 @@ require_relative '../../find_by_contents' | ||
368 | not_found! if Noosfero::API::API.endpoint_unavailable?(self, @environment) | 370 | not_found! if Noosfero::API::API.endpoint_unavailable?(self, @environment) |
369 | end | 371 | end |
370 | 372 | ||
373 | + def asset_with_image params | ||
374 | + if params.has_key? :image_builder | ||
375 | + asset_api_params = params | ||
376 | + asset_api_params[:image_builder] = base64_to_uploadedfile(asset_api_params[:image_builder]) | ||
377 | + return asset_api_params | ||
378 | + end | ||
379 | + params | ||
380 | + end | ||
381 | + | ||
382 | + def base64_to_uploadedfile(base64_image) | ||
383 | + tempfile = base64_to_tempfile base64_image | ||
384 | + converted_image = base64_image | ||
385 | + converted_image[:tempfile] = tempfile | ||
386 | + return {uploaded_data: ActionDispatch::Http::UploadedFile.new(converted_image)} | ||
387 | + end | ||
388 | + | ||
389 | + def base64_to_tempfile base64_image | ||
390 | + base64_img_str = base64_image[:tempfile] | ||
391 | + decoded_base64_str = Base64.decode64(base64_img_str) | ||
392 | + tempfile = Tempfile.new(base64_image[:filename]) | ||
393 | + tempfile.write(decoded_base64_str.encode("ascii-8bit").force_encoding("utf-8")) | ||
394 | + tempfile.rewind | ||
395 | + tempfile | ||
396 | + end | ||
371 | private | 397 | private |
372 | 398 | ||
373 | def parser_params(params) | 399 | def parser_params(params) |
@@ -394,7 +420,6 @@ require_relative '../../find_by_contents' | @@ -394,7 +420,6 @@ require_relative '../../find_by_contents' | ||
394 | end_period = until_date.nil? ? DateTime.now : until_date | 420 | end_period = until_date.nil? ? DateTime.now : until_date |
395 | begin_period..end_period | 421 | begin_period..end_period |
396 | end | 422 | end |
397 | - | ||
398 | end | 423 | end |
399 | end | 424 | end |
400 | end | 425 | end |
lib/noosfero/api/v1/articles.rb
@@ -51,7 +51,7 @@ module Noosfero | @@ -51,7 +51,7 @@ module Noosfero | ||
51 | post ':id' do | 51 | post ':id' do |
52 | article = environment.articles.find(params[:id]) | 52 | article = environment.articles.find(params[:id]) |
53 | return forbidden! unless article.allow_edit?(current_person) | 53 | return forbidden! unless article.allow_edit?(current_person) |
54 | - article.update_attributes!(params[:article]) | 54 | + article.update_attributes!(asset_with_image(params[:article])) |
55 | present_partial article, :with => Entities::Article | 55 | present_partial article, :with => Entities::Article |
56 | end | 56 | end |
57 | 57 |
lib/noosfero/api/v1/people.rb
@@ -55,11 +55,10 @@ module Noosfero | @@ -55,11 +55,10 @@ module Noosfero | ||
55 | post ':id' do | 55 | post ':id' do |
56 | authenticate! | 56 | authenticate! |
57 | return forbidden! if current_person.id.to_s != params[:id] | 57 | return forbidden! if current_person.id.to_s != params[:id] |
58 | - current_person.update_attributes!(params[:person]) | 58 | + current_person.update_attributes!(asset_with_image(params[:person])) |
59 | present current_person, :with => Entities::Person, :current_person => current_person | 59 | present current_person, :with => Entities::Person, :current_person => current_person |
60 | end | 60 | end |
61 | - | ||
62 | - # Example Request: | 61 | + |
63 | # POST api/v1/people?person[login]=some_login&person[password]=some_password&person[name]=Jack | 62 | # POST api/v1/people?person[login]=some_login&person[password]=some_password&person[name]=Jack |
64 | # for each custom field for person, add &person[field_name]=field_value to the request | 63 | # for each custom field for person, add &person[field_name]=field_value to the request |
65 | desc "Create person" | 64 | desc "Create person" |
@@ -76,7 +75,7 @@ module Noosfero | @@ -76,7 +75,7 @@ module Noosfero | ||
76 | params[:person][:custom_values][key]=params[:person].delete(key) if Person.custom_fields(environment).any?{|cf| cf.name==key} | 75 | params[:person][:custom_values][key]=params[:person].delete(key) if Person.custom_fields(environment).any?{|cf| cf.name==key} |
77 | end | 76 | end |
78 | 77 | ||
79 | - user = User.build(user_data, params[:person], environment) | 78 | + user = User.build(user_data, asset_with_image(params[:person]), environment) |
80 | 79 | ||
81 | begin | 80 | begin |
82 | user.signup! | 81 | user.signup! |
test/api/helpers_test.rb
1 | require_relative 'test_helper' | 1 | require_relative 'test_helper' |
2 | +require "base64" | ||
2 | require 'noosfero/api/helpers' | 3 | require 'noosfero/api/helpers' |
3 | 4 | ||
4 | class APIHelpersTest < ActiveSupport::TestCase | 5 | class APIHelpersTest < ActiveSupport::TestCase |
@@ -238,6 +239,24 @@ class APIHelpersTest < ActiveSupport::TestCase | @@ -238,6 +239,24 @@ class APIHelpersTest < ActiveSupport::TestCase | ||
238 | present_partial(model, {}) | 239 | present_partial(model, {}) |
239 | end | 240 | end |
240 | 241 | ||
242 | + should 'create a :uploaded_data hash, expected by image_builder ' do | ||
243 | + base64_image = create_base64_image | ||
244 | + uploadedfile = base64_to_uploadedfile base64_image | ||
245 | + assert uploadedfile.has_key? :uploaded_data | ||
246 | + assert_equal uploadedfile[:uploaded_data].original_filename, base64_image[:filename] | ||
247 | + assert_equal uploadedfile[:uploaded_data].content_type, base64_image[:type] | ||
248 | + assert uploadedfile[:uploaded_data].tempfile | ||
249 | + end | ||
250 | + | ||
251 | + should 'return a params copy with a UploadedFile object' do | ||
252 | + base64_image = create_base64_image | ||
253 | + params = {} | ||
254 | + params.merge!({image_builder: base64_image}) | ||
255 | + asset_params = asset_with_image params | ||
256 | + assert !asset_params[:image_builder][:uploaded_data].nil? | ||
257 | + assert asset_params[:image_builder][:uploaded_data].is_a? ActionDispatch::Http::UploadedFile | ||
258 | + end | ||
259 | + | ||
241 | protected | 260 | protected |
242 | 261 | ||
243 | def error!(info, status) | 262 | def error!(info, status) |
test/api/people_test.rb
@@ -386,4 +386,15 @@ class PeopleTest < ActiveSupport::TestCase | @@ -386,4 +386,15 @@ class PeopleTest < ActiveSupport::TestCase | ||
386 | assert_not_nil json['person'][attribute] | 386 | assert_not_nil json['person'][attribute] |
387 | end | 387 | end |
388 | end | 388 | end |
389 | + | ||
390 | + should 'update person image' do | ||
391 | + login_api | ||
392 | + base64_image = create_base64_image | ||
393 | + params.merge!({person: {image_builder: base64_image}}) | ||
394 | + assert_nil person.image | ||
395 | + post "/api/v1/people/#{person.id}?#{params.to_query}" | ||
396 | + person.reload | ||
397 | + assert_not_nil person.image | ||
398 | + assert_equal person.image.filename, base64_image[:filename] | ||
399 | + end | ||
389 | end | 400 | end |
test/api/test_helper.rb
@@ -33,6 +33,18 @@ class ActiveSupport::TestCase | @@ -33,6 +33,18 @@ class ActiveSupport::TestCase | ||
33 | 33 | ||
34 | attr_accessor :private_token, :user, :person, :params, :environment | 34 | attr_accessor :private_token, :user, :person, :params, :environment |
35 | 35 | ||
36 | + def create_base64_image | ||
37 | + image_path = File.absolute_path(Rails.root + 'public/images/noosfero-network.png') | ||
38 | + image_name = File.basename(image_path) | ||
39 | + image_type = "image/#{File.extname(image_name).delete "."}" | ||
40 | + encoded_base64_img = Base64.encode64(File.open(image_path) {|io| io.read }) | ||
41 | + base64_image = {} | ||
42 | + base64_image[:tempfile] = encoded_base64_img | ||
43 | + base64_image[:filename] = image_name | ||
44 | + base64_image[:type] = image_type | ||
45 | + base64_image | ||
46 | + end | ||
47 | + | ||
36 | private | 48 | private |
37 | 49 | ||
38 | def json_response_ids(kind) | 50 | def json_response_ids(kind) |