Commit eb5146b3398c39c80879ba03ce73c43698ddebd4
Exists in
staging
and in
4 other branches
Merge branch 'api' of gitlab.com:participa/noosfero into api
Showing
8 changed files
with
137 additions
and
17 deletions
Show diff stats
lib/noosfero/api/entities.rb
@@ -64,6 +64,7 @@ module Noosfero | @@ -64,6 +64,7 @@ module Noosfero | ||
64 | expose :profile, :using => Profile | 64 | expose :profile, :using => Profile |
65 | expose :categories, :using => Category | 65 | expose :categories, :using => Category |
66 | expose :image, :using => Image | 66 | expose :image, :using => Image |
67 | + #TODO Apply vote stuff in core and make this test | ||
67 | expose :votes_for | 68 | expose :votes_for |
68 | expose :votes_against | 69 | expose :votes_against |
69 | expose :setting | 70 | expose :setting |
lib/noosfero/api/helpers.rb
@@ -2,10 +2,10 @@ module Noosfero | @@ -2,10 +2,10 @@ module Noosfero | ||
2 | module API | 2 | module API |
3 | module APIHelpers | 3 | module APIHelpers |
4 | PRIVATE_TOKEN_PARAM = :private_token | 4 | PRIVATE_TOKEN_PARAM = :private_token |
5 | - ALLOWED_PARAMETERS = ['parent_id', 'from', 'until', 'content_type'] | 5 | + ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type] |
6 | 6 | ||
7 | def current_user | 7 | def current_user |
8 | - private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token'] || cookies['_noosfero_api_session']).to_s if params | 8 | + private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s |
9 | @current_user ||= User.find_by_private_token(private_token) | 9 | @current_user ||= User.find_by_private_token(private_token) |
10 | @current_user = nil if !@current_user.nil? && @current_user.private_token_expired? | 10 | @current_user = nil if !@current_user.nil? && @current_user.private_token_expired? |
11 | @current_user | 11 | @current_user |
@@ -53,10 +53,10 @@ module Noosfero | @@ -53,10 +53,10 @@ module Noosfero | ||
53 | def make_conditions_with_parameter(params = {}) | 53 | def make_conditions_with_parameter(params = {}) |
54 | parsed_params = parser_params(params) | 54 | parsed_params = parser_params(params) |
55 | conditions = {} | 55 | conditions = {} |
56 | - from_date = DateTime.parse(parsed_params.delete('from')) if parsed_params['from'] | ||
57 | - until_date = DateTime.parse(parsed_params.delete('until')) if parsed_params['until'] | 56 | + from_date = DateTime.parse(parsed_params.delete(:from)) if parsed_params[:from] |
57 | + until_date = DateTime.parse(parsed_params.delete(:until)) if parsed_params[:until] | ||
58 | 58 | ||
59 | - conditions[:type] = parse_content_type(parsed_params.delete('content_type')) unless parsed_params['content_type'].nil? | 59 | + conditions[:type] = parse_content_type(parsed_params.delete(:content_type)) unless parsed_params[:content_type].nil? |
60 | 60 | ||
61 | conditions[:created_at] = period(from_date, until_date) if from_date || until_date | 61 | conditions[:created_at] = period(from_date, until_date) if from_date || until_date |
62 | conditions.merge!(parsed_params) | 62 | conditions.merge!(parsed_params) |
@@ -167,7 +167,11 @@ module Noosfero | @@ -167,7 +167,11 @@ module Noosfero | ||
167 | private | 167 | private |
168 | 168 | ||
169 | def parser_params(params) | 169 | def parser_params(params) |
170 | - params.select{|k,v| ALLOWED_PARAMETERS.include?(k)} | 170 | + parsed_params = {} |
171 | + params.map do |k,v| | ||
172 | + parsed_params[k.to_sym] = v if ALLOWED_PARAMETERS.include?(k.to_sym) | ||
173 | + end | ||
174 | + parsed_params | ||
171 | end | 175 | end |
172 | 176 | ||
173 | def default_limit | 177 | def default_limit |
lib/noosfero/api/v1/articles.rb
@@ -32,10 +32,13 @@ module Noosfero | @@ -32,10 +32,13 @@ module Noosfero | ||
32 | get ':id/children' do | 32 | get ':id/children' do |
33 | article = find_article(environment.articles, params[:id]) | 33 | article = find_article(environment.articles, params[:id]) |
34 | 34 | ||
35 | + #TODO make tests for this situation | ||
35 | votes_order = params.delete(:order) if params[:order]=='votes_score' | 36 | votes_order = params.delete(:order) if params[:order]=='votes_score' |
36 | articles = select_filtered_collection_of(article, 'children', params) | 37 | articles = select_filtered_collection_of(article, 'children', params) |
37 | articles = articles.display_filter(current_person, nil) | 38 | articles = articles.display_filter(current_person, nil) |
38 | 39 | ||
40 | + | ||
41 | + #TODO make tests for this situation | ||
39 | if votes_order | 42 | if votes_order |
40 | articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') | 43 | articles = articles.joins('left join votes on articles.id=votes.voteable_id').group('articles.id').reorder('sum(coalesce(votes.vote, 0)) DESC') |
41 | end | 44 | end |
lib/tasks/backup.rake
1 | -desc "Creates a backup of the user files stored in public/" | ||
2 | -task :backup do | ||
3 | - dirs = Dir.glob('public/images/[0-9][0-9][0-9][0-9]') + ['public/articles', 'public/thumbnails', 'public/user_themes'].select { |d| File.exists?(d) } | ||
4 | - tarball = 'backups/files-' + Time.now.strftime('%Y-%m-%d-%R') + '.tar' | 1 | +task :load_backup_config do |
2 | + $config = YAML.load_file('config/database.yml') | ||
3 | +end | ||
4 | + | ||
5 | +task :check_backup_support => :load_backup_config do | ||
6 | + if $config['production']['adapter'] != 'postgresql' | ||
7 | + fail("Only PostgreSQL is supported for backups at the moment") | ||
8 | + end | ||
9 | +end | ||
10 | + | ||
11 | +backup_dirs = [ | ||
12 | + 'public/image_uploads', | ||
13 | + 'public/articles', | ||
14 | + 'public/thumbnails', | ||
15 | + 'public/user_themes', | ||
16 | +] | ||
17 | + | ||
18 | +desc "Creates a backup of the database and uploaded files" | ||
19 | +task :backup => :check_backup_support do | ||
20 | + dirs = backup_dirs.select { |d| File.exists?(d) } | ||
21 | + | ||
22 | + backup_name = Time.now.strftime('%Y-%m-%d-%R') | ||
23 | + backup_file = File.join('tmp/backup', backup_name) + '.tar.gz' | ||
24 | + mkdir_p 'tmp/backup' | ||
25 | + dump = File.join('tmp/backup', backup_name) + '.sql' | ||
26 | + | ||
27 | + database = $config['production']['database'] | ||
28 | + host = $config['production']['host'] | ||
29 | + sh "pg_dump -h #{host} #{database} > #{dump}" | ||
30 | + | ||
31 | + sh 'tar', 'chaf', backup_file, dump, *dirs | ||
32 | + rm_f dump | ||
33 | + | ||
34 | + puts "****************************************************" | ||
35 | + puts "Backup in #{backup_file} !" | ||
36 | + puts | ||
37 | + puts "To restore, use:" | ||
38 | + puts "$ rake restore BACKUP=#{backup_file}" | ||
39 | + puts "****************************************************" | ||
40 | +end | ||
41 | + | ||
42 | +def invalid_backup!(message, items=[]) | ||
43 | + puts "E: #{message}" | ||
44 | + items.each do |i| | ||
45 | + puts "E: - #{i}" | ||
46 | + end | ||
47 | + puts "E: Is this a backup archive created by Noosfero with \`rake backup\`?" | ||
48 | + exit 1 | ||
49 | +end | ||
50 | + | ||
51 | +desc "Restores a backup created previousy with \`rake backup\`" | ||
52 | +task :restore => :check_backup_support do | ||
53 | + backup = ENV["BACKUP"] | ||
54 | + unless backup | ||
55 | + puts "usage: rake restore BACKUP=/path/to/backup" | ||
56 | + exit 1 | ||
57 | + end | ||
58 | + | ||
59 | + files = `tar taf #{backup}`.split | ||
60 | + | ||
61 | + # validate files in the backup | ||
62 | + invalid_files = [] | ||
63 | + files.each do |f| | ||
64 | + if f !~ /tmp\/backup\// && (backup_dirs.none? { |d| f =~ /^#{d}\// }) | ||
65 | + invalid_files << f | ||
66 | + end | ||
67 | + end | ||
68 | + if invalid_files.size > 0 | ||
69 | + invalid_backup!("Invalid files found in the backup archive", invalid_files) | ||
70 | + end | ||
71 | + | ||
72 | + # find database dump in the archive | ||
73 | + dumps = files.select do |f| | ||
74 | + File.dirname(f) == 'tmp/backup' && f =~ /\.sql$/ | ||
75 | + end | ||
76 | + if dumps.size == 0 | ||
77 | + invalid_backup!("Could not find a database dump in the archive.") | ||
78 | + elsif dumps.size > 1 | ||
79 | + invalid_backup!("Multiple database dumps found in the archive:", dumps) | ||
80 | + end | ||
81 | + dump = dumps.first | ||
82 | + | ||
83 | + database = $config['production']['database'] | ||
84 | + username = $config['production']['username'] | ||
85 | + host = $config['production']['host'] | ||
86 | + | ||
87 | + puts "WARNING: backups should be restored to an empty database, otherwise" | ||
88 | + puts "data from the backup may not be loaded properly." | ||
89 | + puts | ||
90 | + puts 'You can remove the existing database and create a new one with:' | ||
91 | + puts | ||
92 | + puts "$ sudo -u postgres dropdb -h #{host} #{database}" | ||
93 | + puts "$ sudo -u postgres createdb -h #{host} #{database} --owner #{username}" | ||
94 | + puts | ||
95 | + print "Are you sure you want to continue (y/N)? " | ||
96 | + response = $stdin.gets.strip | ||
97 | + unless ['y', 'yes'].include?(response.downcase) | ||
98 | + puts "*** ABORTED." | ||
99 | + exit 1 | ||
100 | + end | ||
101 | + | ||
102 | + sh 'tar', 'xaf', backup | ||
103 | + sh "rails dbconsole production < #{dump}" | ||
104 | + rm_f dump | ||
5 | 105 | ||
6 | - mkdir_p(File.dirname(tarball)) | ||
7 | - sh('tar', 'cf', tarball, *dirs) | 106 | + puts "****************************************************" |
107 | + puts "Backup restored!" | ||
108 | + puts "****************************************************" | ||
8 | end | 109 | end |
plugins/sub_organizations/db/migrate/20150508153119_add_timestamp_to_relation.rb
0 → 100644
plugins/sub_organizations/lib/sub_organizations_plugin/relation.rb
1 | class SubOrganizationsPlugin::Relation < Noosfero::Plugin::ActiveRecord | 1 | class SubOrganizationsPlugin::Relation < Noosfero::Plugin::ActiveRecord |
2 | - record_timestamps = false | ||
3 | - | ||
4 | belongs_to :parent, :polymorphic => true | 2 | belongs_to :parent, :polymorphic => true |
5 | belongs_to :child, :polymorphic => true | 3 | belongs_to :child, :polymorphic => true |
6 | 4 |
test/unit/api/helpers_test.rb
1 | require File.dirname(__FILE__) + '/test_helper' | 1 | require File.dirname(__FILE__) + '/test_helper' |
2 | require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") | 2 | require File.expand_path(File.dirname(__FILE__) + "/../../../lib/noosfero/api/helpers") |
3 | 3 | ||
4 | -class APITest < ActiveSupport::TestCase | 4 | +class APIHelpersTest < ActiveSupport::TestCase |
5 | 5 | ||
6 | include Noosfero::API::APIHelpers | 6 | include Noosfero::API::APIHelpers |
7 | 7 | ||
@@ -126,11 +126,18 @@ class APITest < ActiveSupport::TestCase | @@ -126,11 +126,18 @@ class APITest < ActiveSupport::TestCase | ||
126 | assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at] | 126 | assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at] |
127 | end | 127 | end |
128 | 128 | ||
129 | + should 'make_conditions_with_parameter return created_at parameter if from period is defined as string' do | ||
130 | + assert_not_nil make_conditions_with_parameter('from' => '2010-10-10')[:created_at] | ||
131 | + end | ||
132 | + | ||
129 | should 'make_conditions_with_parameter return created_at parameter if until period is defined' do | 133 | should 'make_conditions_with_parameter return created_at parameter if until period is defined' do |
130 | assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at] | 134 | assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at] |
131 | end | 135 | end |
132 | 136 | ||
133 | -# should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do | 137 | + should 'make_conditions_with_parameter return created_at parameter if until period is defined as string' do |
138 | + assert_not_nil make_conditions_with_parameter('until' => '2010-10-10')[:created_at] | ||
139 | + end | ||
140 | + | ||
134 | should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do | 141 | should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do |
135 | assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min | 142 | assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min |
136 | end | 143 | end |
test/unit/api/session_test.rb