biovision-base 0.5.170614
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +42 -0
- data/Rakefile +26 -0
- data/app/assets/config/biovision_base_manifest.js +0 -0
- data/app/assets/images/biovision/base/icons/alert.svg +15 -0
- data/app/assets/images/biovision/base/icons/back.svg +28 -0
- data/app/assets/images/biovision/base/icons/create.svg +1 -0
- data/app/assets/images/biovision/base/icons/destroy.svg +3 -0
- data/app/assets/images/biovision/base/icons/edit.svg +22 -0
- data/app/assets/images/biovision/base/icons/exit.svg +1 -0
- data/app/assets/images/biovision/base/icons/foreign/facebook.svg +1 -0
- data/app/assets/images/biovision/base/icons/gear.svg +4 -0
- data/app/assets/images/biovision/base/icons/lock-closed.svg +1 -0
- data/app/assets/images/biovision/base/icons/lock-open.svg +1 -0
- data/app/assets/images/biovision/base/icons/notice.svg +1 -0
- data/app/assets/images/biovision/base/icons/return.svg +1 -0
- data/app/assets/images/biovision/base/icons/warning.svg +43 -0
- data/app/assets/images/biovision/base/icons/world.svg +4 -0
- data/app/assets/images/biovision/base/placeholders/image.svg +19 -0
- data/app/assets/images/biovision/base/placeholders/photo.svg +23 -0
- data/app/assets/images/biovision/base/placeholders/user.svg +21 -0
- data/app/assets/javascripts/biovision/base/biovision.js +238 -0
- data/app/assets/stylesheets/biovision/base/admin.scss +58 -0
- data/app/assets/stylesheets/biovision/base/biovision.scss +472 -0
- data/app/assets/stylesheets/biovision/base/buttons.scss +90 -0
- data/app/assets/stylesheets/biovision/base/default.scss +45 -0
- data/app/assets/stylesheets/biovision/base/filters.scss +53 -0
- data/app/assets/stylesheets/biovision/base/fonts.scss +9 -0
- data/app/assets/stylesheets/biovision/base/message-box.scss +60 -0
- data/app/assets/stylesheets/biovision/base/pagination.scss +42 -0
- data/app/assets/stylesheets/biovision/base/tootik.scss +385 -0
- data/app/assets/stylesheets/biovision/base/track.scss +28 -0
- data/app/controllers/admin/agents_controller.rb +30 -0
- data/app/controllers/admin/browsers_controller.rb +30 -0
- data/app/controllers/admin/codes_controller.rb +25 -0
- data/app/controllers/admin/editable_pages_controller.rb +25 -0
- data/app/controllers/admin/index_controller.rb +5 -0
- data/app/controllers/admin/metrics_controller.rb +29 -0
- data/app/controllers/admin/privilege_groups_controller.rb +48 -0
- data/app/controllers/admin/privileges_controller.rb +5 -0
- data/app/controllers/admin/tokens_controller.rb +28 -0
- data/app/controllers/admin/users_controller.rb +87 -0
- data/app/controllers/admin_controller.rb +10 -0
- data/app/controllers/agents_controller.rb +67 -0
- data/app/controllers/authentication_controller.rb +70 -0
- data/app/controllers/browsers_controller.rb +63 -0
- data/app/controllers/codes_controller.rb +60 -0
- data/app/controllers/concerns/authentication.rb +19 -0
- data/app/controllers/concerns/biovision/admin/privileges.rb +34 -0
- data/app/controllers/concerns/entity_priority.rb +7 -0
- data/app/controllers/concerns/lockable_entity.rb +23 -0
- data/app/controllers/concerns/toggleable_entity.rb +7 -0
- data/app/controllers/editable_pages_controller.rb +60 -0
- data/app/controllers/index_controller.rb +5 -0
- data/app/controllers/metrics_controller.rb +33 -0
- data/app/controllers/my/confirmations_controller.rb +37 -0
- data/app/controllers/my/index_controller.rb +7 -0
- data/app/controllers/my/profiles_controller.rb +81 -0
- data/app/controllers/my/recoveries_controller.rb +68 -0
- data/app/controllers/privilege_groups_controller.rb +57 -0
- data/app/controllers/privileges_controller.rb +74 -0
- data/app/controllers/profiles_controller.rb +22 -0
- data/app/controllers/tokens_controller.rb +60 -0
- data/app/controllers/users_controller.rb +57 -0
- data/app/helpers/biovision_helper.rb +81 -0
- data/app/helpers/biovision_users_helper.rb +52 -0
- data/app/helpers/codes_helper.rb +10 -0
- data/app/helpers/editable_pages_helper.rb +20 -0
- data/app/helpers/privileges_helper.rb +11 -0
- data/app/helpers/tracking_helper.rb +17 -0
- data/app/jobs/application_job.rb +7 -0
- data/app/jobs/clean_tokens_job.rb +8 -0
- data/app/mailers/code_sender.rb +15 -0
- data/app/models/agent.rb +38 -0
- data/app/models/application_record.rb +3 -0
- data/app/models/browser.rb +22 -0
- data/app/models/code.rb +58 -0
- data/app/models/code_type.rb +11 -0
- data/app/models/concerns/biovision/privilege_base.rb +143 -0
- data/app/models/concerns/biovision/user_base.rb +124 -0
- data/app/models/concerns/biovision/user_privilege_base.rb +46 -0
- data/app/models/concerns/has_owner.rb +19 -0
- data/app/models/concerns/required_unique_name.rb +13 -0
- data/app/models/concerns/required_unique_slug.rb +12 -0
- data/app/models/concerns/toggleable.rb +29 -0
- data/app/models/editable_page.rb +30 -0
- data/app/models/foreign_site.rb +75 -0
- data/app/models/foreign_user.rb +26 -0
- data/app/models/metric.rb +67 -0
- data/app/models/metric_value.rb +15 -0
- data/app/models/privilege.rb +3 -0
- data/app/models/privilege_group.rb +50 -0
- data/app/models/privilege_group_privilege.rb +6 -0
- data/app/models/token.rb +75 -0
- data/app/models/user.rb +3 -0
- data/app/models/user_privilege.rb +3 -0
- data/app/services/canonizer.rb +29 -0
- data/app/services/code_manager.rb +16 -0
- data/app/services/code_manager/confirmation.rb +26 -0
- data/app/services/code_manager/invitation.rb +27 -0
- data/app/services/code_manager/recovery.rb +31 -0
- data/app/uploaders/avatar_uploader.rb +62 -0
- data/app/uploaders/editable_page_image_uploader.rb +38 -0
- data/app/views/admin/agents/_filter.html.erb +16 -0
- data/app/views/admin/agents/_list.html.erb +17 -0
- data/app/views/admin/agents/_nav_item.html.erb +6 -0
- data/app/views/admin/agents/_toggleable.html.erb +7 -0
- data/app/views/admin/agents/entity/_in_list.html.erb +13 -0
- data/app/views/admin/agents/index.html.erb +17 -0
- data/app/views/admin/agents/show.html.erb +28 -0
- data/app/views/admin/browsers/_list.html.erb +17 -0
- data/app/views/admin/browsers/_nav_item.html.erb +6 -0
- data/app/views/admin/browsers/_toggleable.html.erb +7 -0
- data/app/views/admin/browsers/agents/_add.html.erb +79 -0
- data/app/views/admin/browsers/entity/_in_list.html.erb +13 -0
- data/app/views/admin/browsers/index.html.erb +17 -0
- data/app/views/admin/browsers/show.html.erb +29 -0
- data/app/views/admin/codes/_list.html.erb +11 -0
- data/app/views/admin/codes/_nav_item.html.erb +6 -0
- data/app/views/admin/codes/entity/_in_list.html.erb +24 -0
- data/app/views/admin/codes/index.html.erb +16 -0
- data/app/views/admin/codes/show.html.erb +51 -0
- data/app/views/admin/editable_pages/_list.html.erb +11 -0
- data/app/views/admin/editable_pages/_nav_item.html.erb +6 -0
- data/app/views/admin/editable_pages/entity/_in_list.html.erb +13 -0
- data/app/views/admin/editable_pages/index.html.erb +14 -0
- data/app/views/admin/editable_pages/show.html.erb +47 -0
- data/app/views/admin/index/index.html.erb +25 -0
- data/app/views/admin/metrics/_list.html.erb +26 -0
- data/app/views/admin/metrics/_nav_item.html.erb +6 -0
- data/app/views/admin/metrics/data.jbuilder +6 -0
- data/app/views/admin/metrics/index.html.erb +14 -0
- data/app/views/admin/metrics/show.html.erb +52 -0
- data/app/views/admin/privilege_groups/_list.html.erb +11 -0
- data/app/views/admin/privilege_groups/_nav_item.html.erb +6 -0
- data/app/views/admin/privilege_groups/entity/_in_list.html.erb +10 -0
- data/app/views/admin/privilege_groups/index.html.erb +14 -0
- data/app/views/admin/privilege_groups/show.html.erb +31 -0
- data/app/views/admin/privileges/_list.html.erb +15 -0
- data/app/views/admin/privileges/_nav_item.html.erb +6 -0
- data/app/views/admin/privileges/entity/_groups.html.erb +34 -0
- data/app/views/admin/privileges/entity/_in_list.html.erb +25 -0
- data/app/views/admin/privileges/index.html.erb +20 -0
- data/app/views/admin/privileges/show.html.erb +66 -0
- data/app/views/admin/privileges/users.html.erb +24 -0
- data/app/views/admin/tokens/_filter.html.erb +17 -0
- data/app/views/admin/tokens/_list.html.erb +11 -0
- data/app/views/admin/tokens/_nav_item.html.erb +6 -0
- data/app/views/admin/tokens/_toggleable.html.erb +7 -0
- data/app/views/admin/tokens/entity/_in_list.html.erb +25 -0
- data/app/views/admin/tokens/index.html.erb +19 -0
- data/app/views/admin/tokens/show.html.erb +43 -0
- data/app/views/admin/users/_filter.html.erb +22 -0
- data/app/views/admin/users/_list.html.erb +17 -0
- data/app/views/admin/users/_nav_item.html.erb +6 -0
- data/app/views/admin/users/_search.html.erb +5 -0
- data/app/views/admin/users/_toggleable.html.erb +7 -0
- data/app/views/admin/users/codes.html.erb +24 -0
- data/app/views/admin/users/entity/_in_list.html.erb +24 -0
- data/app/views/admin/users/entity/_privilege.html.erb +10 -0
- data/app/views/admin/users/entity/_privilege_tree.html.erb +17 -0
- data/app/views/admin/users/index.html.erb +19 -0
- data/app/views/admin/users/privileges.html.erb +35 -0
- data/app/views/admin/users/search.jbuilder +10 -0
- data/app/views/admin/users/search/_results.html.erb +11 -0
- data/app/views/admin/users/show.html.erb +94 -0
- data/app/views/admin/users/tokens.html.erb +24 -0
- data/app/views/agents/_agent.jbuilder +20 -0
- data/app/views/agents/_filter.html.erb +53 -0
- data/app/views/agents/_form.html.erb +33 -0
- data/app/views/agents/edit.html.erb +17 -0
- data/app/views/agents/index.jbuilder +5 -0
- data/app/views/agents/new.html.erb +15 -0
- data/app/views/agents/show.jbuilder +3 -0
- data/app/views/application/not_found.html.erb +9 -0
- data/app/views/application/not_found.jbuilder +3 -0
- data/app/views/application/service_unavailable.html.erb +9 -0
- data/app/views/application/unauthorized.html.erb +11 -0
- data/app/views/application/unauthorized.jbuilder +3 -0
- data/app/views/authentication/_form.html.erb +16 -0
- data/app/views/authentication/_info.html.erb +8 -0
- data/app/views/authentication/new.html.erb +10 -0
- data/app/views/browsers/_form.html.erb +28 -0
- data/app/views/browsers/edit.html.erb +17 -0
- data/app/views/browsers/new.html.erb +15 -0
- data/app/views/code_sender/email.text.erb +9 -0
- data/app/views/code_sender/password.text.erb +10 -0
- data/app/views/codes/_form.html.erb +49 -0
- data/app/views/codes/edit.html.erb +17 -0
- data/app/views/codes/new.html.erb +15 -0
- data/app/views/editable_pages/_form.html.erb +63 -0
- data/app/views/editable_pages/edit.html.erb +17 -0
- data/app/views/editable_pages/form/_ckeditor.html.erb +11 -0
- data/app/views/editable_pages/new.html.erb +15 -0
- data/app/views/index/index.html.erb +5 -0
- data/app/views/layouts/admin.html.erb +23 -0
- data/app/views/layouts/admin/_footer.html.erb +0 -0
- data/app/views/layouts/admin/_header.html.erb +11 -0
- data/app/views/layouts/admin/header/_logo.html.erb +3 -0
- data/app/views/metrics/_form.html.erb +41 -0
- data/app/views/metrics/edit.html.erb +17 -0
- data/app/views/metrics/index.jbuilder +7 -0
- data/app/views/metrics/show.jbuilder +3 -0
- data/app/views/my/confirmations/show.html.erb +30 -0
- data/app/views/my/index/index.html.erb +17 -0
- data/app/views/my/profiles/edit.html.erb +10 -0
- data/app/views/my/profiles/edit/_form.html.erb +70 -0
- data/app/views/my/profiles/new.html.erb +7 -0
- data/app/views/my/profiles/new/_form.html.erb +34 -0
- data/app/views/my/profiles/show.html.erb +3 -0
- data/app/views/my/recoveries/show.html.erb +39 -0
- data/app/views/privilege_groups/_form.html.erb +25 -0
- data/app/views/privilege_groups/edit.html.erb +17 -0
- data/app/views/privilege_groups/new.html.erb +15 -0
- data/app/views/privileges/_form.html.erb +42 -0
- data/app/views/privileges/edit.html.erb +21 -0
- data/app/views/privileges/new.html.erb +14 -0
- data/app/views/profiles/_profile.html.erb +48 -0
- data/app/views/profiles/show.html.erb +4 -0
- data/app/views/shared/_flash_messages.html.erb +7 -0
- data/app/views/shared/_list_of_errors.html.erb +11 -0
- data/app/views/shared/_meta_texts.html.erb +17 -0
- data/app/views/shared/_nothing_found.html.erb +1 -0
- data/app/views/shared/_track.html.erb +10 -0
- data/app/views/shared/actions/_locks.html.erb +4 -0
- data/app/views/shared/actions/_priority_changer.html.erb +4 -0
- data/app/views/shared/admin/_breadcrumbs.html.erb +6 -0
- data/app/views/shared/editable_pages/_body.html.erb +12 -0
- data/app/views/shared/filter/_flag.html.erb +17 -0
- data/app/views/tokens/_form.html.erb +30 -0
- data/app/views/tokens/edit.html.erb +17 -0
- data/app/views/tokens/new.html.erb +15 -0
- data/app/views/users/_form.html.erb +78 -0
- data/app/views/users/edit.html.erb +18 -0
- data/app/views/users/new.html.erb +15 -0
- data/config/initializers/carrierwave.rb +22 -0
- data/config/initializers/pluralization.rb +2 -0
- data/config/locales/common-ru.yml +96 -0
- data/config/locales/editable-pages-ru.yml +43 -0
- data/config/locales/plurals.rb +17 -0
- data/config/locales/track-ru.yml +102 -0
- data/config/locales/users-ru.yml +324 -0
- data/config/routes.rb +92 -0
- data/db/migrate/20170301000001_create_metrics.rb +23 -0
- data/db/migrate/20170301000002_create_metric_values.rb +19 -0
- data/db/migrate/20170301000101_create_browsers.rb +22 -0
- data/db/migrate/20170301000102_create_agents.rb +22 -0
- data/db/migrate/20170302000001_create_users.rb +52 -0
- data/db/migrate/20170302000002_create_tokens.rb +23 -0
- data/db/migrate/20170302000003_create_code_types.rb +20 -0
- data/db/migrate/20170302000004_create_codes.rb +24 -0
- data/db/migrate/20170302000101_create_privileges.rb +34 -0
- data/db/migrate/20170302000102_create_user_privileges.rb +17 -0
- data/db/migrate/20170302000103_create_privilege_groups.rb +23 -0
- data/db/migrate/20170302000104_create_privilege_group_privileges.rb +24 -0
- data/db/migrate/20170320000000_create_editable_pages.rb +22 -0
- data/db/migrate/20170425000001_create_foreign_sites.rb +18 -0
- data/db/migrate/20170425000002_create_foreign_users.rb +23 -0
- data/lib/biovision/base.rb +9 -0
- data/lib/biovision/base/base_methods.rb +96 -0
- data/lib/biovision/base/engine.rb +17 -0
- data/lib/biovision/base/privilege_methods.rb +35 -0
- data/lib/biovision/base/version.rb +5 -0
- data/lib/tasks/biovision/agents.rake +38 -0
- data/lib/tasks/biovision/base_tasks.rake +4 -0
- data/lib/tasks/biovision/browsers.rake +38 -0
- data/lib/tasks/biovision/codes.rake +49 -0
- data/lib/tasks/biovision/tokens.rake +54 -0
- data/lib/tasks/biovision/users.rake +67 -0
- metadata +467 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
class CreateForeignUsers < ActiveRecord::Migration[5.0]
|
2
|
+
def up
|
3
|
+
unless ForeignUser.table_exists?
|
4
|
+
create_table :foreign_users do |t|
|
5
|
+
t.timestamps
|
6
|
+
t.references :foreign_site, foreign_key: true, null: false, on_update: :cascade, on_delete: :cascade
|
7
|
+
t.references :user, foreign_key: true, null: false, on_update: :cascade, on_delete: :cascade
|
8
|
+
t.references :agent, foreign_key: true, on_update: :cascade, on_delete: :nullify
|
9
|
+
t.inet :ip
|
10
|
+
t.string :slug, null: false
|
11
|
+
t.string :email
|
12
|
+
t.string :name
|
13
|
+
t.text :data
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def down
|
19
|
+
if ForeignUser.table_exists?
|
20
|
+
drop_table :foreign_users
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Biovision
|
2
|
+
module Base
|
3
|
+
module BaseMethods
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :current_page, :param_from_request
|
8
|
+
helper_method :current_user
|
9
|
+
end
|
10
|
+
|
11
|
+
# Get current page number from request
|
12
|
+
#
|
13
|
+
# @return [Integer]
|
14
|
+
def current_page
|
15
|
+
@current_page ||= (params[:page] || 1).to_s.to_i.abs
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get parameter from request and normalize it
|
19
|
+
#
|
20
|
+
# Casts request parameter to UTF-8 string end removes invalid characters
|
21
|
+
#
|
22
|
+
# @param [Symbol] param
|
23
|
+
# @return [String]
|
24
|
+
def param_from_request(param)
|
25
|
+
params[param].to_s.encode('UTF-8', 'UTF-8', invalid: :replace, replace: '')
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get current user from token cookie
|
29
|
+
#
|
30
|
+
# @return [User|nil]
|
31
|
+
def current_user
|
32
|
+
@current_user ||= Token.user_by_token cookies['token'], true
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
# Handle HTTP error with status 404 without raising exception
|
38
|
+
#
|
39
|
+
# @param [String] message
|
40
|
+
# @param [String] metric
|
41
|
+
# @param [Symbol|String] view
|
42
|
+
def handle_http_404(message, metric = nil, view = :not_found)
|
43
|
+
status = :not_found
|
44
|
+
default_metric = Metric::METRIC_HTTP_404
|
45
|
+
handle_http_error(message, metric || default_metric, view, status)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Handle HTTP error with status 401 without raising exception
|
49
|
+
#
|
50
|
+
# @param [String] message
|
51
|
+
# @param [String] metric
|
52
|
+
# @param [Symbol|String] view
|
53
|
+
def handle_http_401(message, metric = nil, view = :unauthorized)
|
54
|
+
status = :unauthorized
|
55
|
+
default_metric = Metric::METRIC_HTTP_401
|
56
|
+
handle_http_error(message, metric || default_metric, view, status)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Handle generic HTTP error without raising exception
|
60
|
+
#
|
61
|
+
# @param [String] message
|
62
|
+
# @param [String] metric
|
63
|
+
# @param [String|Symbol] view
|
64
|
+
# @param [Symbol] status
|
65
|
+
def handle_http_error(message, metric, view, status)
|
66
|
+
logger.warn "#{message}\n\t#{request.method} #{request.original_url}"
|
67
|
+
Metric.register(metric)
|
68
|
+
render view, status: status
|
69
|
+
end
|
70
|
+
|
71
|
+
# Restrict access for anonymous users
|
72
|
+
def restrict_anonymous_access
|
73
|
+
handle_http_401('Restricted anonymous access') if current_user.nil?
|
74
|
+
end
|
75
|
+
|
76
|
+
# Owner information for entity
|
77
|
+
#
|
78
|
+
# @param [Boolean] track
|
79
|
+
def owner_for_entity(track = false)
|
80
|
+
result = { user: current_user }
|
81
|
+
result.merge!(tracking_for_entity) if track
|
82
|
+
result
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [Agent]
|
86
|
+
def agent
|
87
|
+
@agent ||= Agent.named(request.user_agent || 'n/a')
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [Hash]
|
91
|
+
def tracking_for_entity
|
92
|
+
{ agent: agent, ip: request.env['HTTP_X_REAL_IP'] || request.remote_ip }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Biovision
|
2
|
+
module Base
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
initializer "biovision_base.load_base_methods" do
|
5
|
+
ActiveSupport.on_load(:action_controller) do
|
6
|
+
include Biovision::Base::BaseMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'kaminari'
|
12
|
+
require 'rails_i18n'
|
13
|
+
require 'carrierwave'
|
14
|
+
require 'mini_magick'
|
15
|
+
require 'carrierwave-bombshelter'
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Biovision
|
2
|
+
module Base
|
3
|
+
module PrivilegeMethods
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :current_user_has_privilege?, :current_user_in_group?
|
8
|
+
end
|
9
|
+
|
10
|
+
# @param [Symbol] privilege_name
|
11
|
+
def current_user_has_privilege?(privilege_name)
|
12
|
+
::UserPrivilege.user_has_privilege?(current_user, privilege_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param [Symbol] group_name
|
16
|
+
def current_user_in_group?(group_name)
|
17
|
+
::UserPrivilege.user_in_group?(current_user, group_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# @param [Symbol] privilege_name
|
23
|
+
def require_privilege(privilege_name)
|
24
|
+
return if current_user_has_privilege?(privilege_name)
|
25
|
+
handle_http_401("Current user has no privilege #{privilege_name}")
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param [Symbol] group_name
|
29
|
+
def require_privilege_group(group_name)
|
30
|
+
return if current_user_in_group?(group_name)
|
31
|
+
handle_http_401("Current user is not in group #{group_name}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
namespace :agents do
|
2
|
+
desc 'Load agents from YAML with deleting old data'
|
3
|
+
task load: :environment do
|
4
|
+
file_path = "#{Rails.root}/tmp/import/agents.yml"
|
5
|
+
if File.exist?(file_path)
|
6
|
+
puts 'Deleting old agents...'
|
7
|
+
Agent.destroy_all
|
8
|
+
puts 'Done. Importing...'
|
9
|
+
File.open(file_path, 'r') do |file|
|
10
|
+
YAML.load(file).each do |id, data|
|
11
|
+
entity = Agent.new id: id
|
12
|
+
entity.assign_attributes data
|
13
|
+
entity.save!
|
14
|
+
print "\r#{id} "
|
15
|
+
end
|
16
|
+
puts
|
17
|
+
end
|
18
|
+
Agent.connection.execute "select setval('agents_id_seq', (select max(id) from agents));"
|
19
|
+
puts "Done. We have #{Agent.count} agents now"
|
20
|
+
else
|
21
|
+
puts "Cannot find file #{file_path}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
desc 'Dump agents to YAML'
|
26
|
+
task dump: :environment do
|
27
|
+
file_path = "#{Rails.root}/tmp/export/agents.yml"
|
28
|
+
ignored = %w(id)
|
29
|
+
File.open file_path, 'w' do |file|
|
30
|
+
Agent.order('id asc').each do |entity|
|
31
|
+
file.puts "#{entity.id}:"
|
32
|
+
entity.attributes.reject { |a, v| ignored.include?(a) || v.nil? }.each do |attribute, value|
|
33
|
+
file.puts " #{attribute}: #{value.inspect}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
namespace :browsers do
|
2
|
+
desc 'Load browsers from YAML with deleting old data'
|
3
|
+
task load: :environment do
|
4
|
+
file_path = "#{Rails.root}/tmp/import/browsers.yml"
|
5
|
+
if File.exist?(file_path)
|
6
|
+
puts 'Deleting old browsers...'
|
7
|
+
Browser.destroy_all
|
8
|
+
puts 'Done. Importing...'
|
9
|
+
File.open(file_path, 'r') do |file|
|
10
|
+
YAML.load(file).each do |id, data|
|
11
|
+
entity = Browser.new id: id
|
12
|
+
entity.assign_attributes data
|
13
|
+
entity.save!
|
14
|
+
print "\r#{id} "
|
15
|
+
end
|
16
|
+
puts
|
17
|
+
end
|
18
|
+
Browser.connection.execute "select setval('browsers_id_seq', (select max(id) from browsers));"
|
19
|
+
puts "Done. We have #{Browser.count} browsers now"
|
20
|
+
else
|
21
|
+
puts "Cannot find file #{file_path}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
desc 'Dump browsers to YAML'
|
26
|
+
task dump: :environment do
|
27
|
+
file_path = "#{Rails.root}/tmp/export/browsers.yml"
|
28
|
+
ignored = %w(id agents_count)
|
29
|
+
File.open file_path, 'w' do |file|
|
30
|
+
Browser.order('id asc').each do |entity|
|
31
|
+
file.puts "#{entity.id}:"
|
32
|
+
entity.attributes.reject { |a, v| ignored.include?(a) || v.nil? }.each do |attribute, value|
|
33
|
+
file.puts " #{attribute}: #{value.inspect}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
namespace :codes do
|
2
|
+
desc 'Load codes from YAML with deleting old data'
|
3
|
+
task load: :environment do
|
4
|
+
file_path = "#{Rails.root}/tmp/import/codes.yml"
|
5
|
+
ignored = %w(agent)
|
6
|
+
if File.exist?(file_path)
|
7
|
+
puts 'Deleting old codes...'
|
8
|
+
Code.destroy_all
|
9
|
+
puts 'Done. Importing...'
|
10
|
+
File.open(file_path, 'r') do |file|
|
11
|
+
YAML.load(file).each do |id, data|
|
12
|
+
attributes = data.reject { |key| ignored.include? key }
|
13
|
+
entity = Code.new(id: id)
|
14
|
+
entity.assign_attributes(attributes)
|
15
|
+
|
16
|
+
if data.key?('agent')
|
17
|
+
entity.agent = Agent.named(data['agent'])
|
18
|
+
end
|
19
|
+
entity.save!
|
20
|
+
|
21
|
+
print "\r#{id} "
|
22
|
+
end
|
23
|
+
puts
|
24
|
+
end
|
25
|
+
Code.connection.execute "select setval('codes_id_seq', (select max(id) from codes));"
|
26
|
+
puts "Done. We have #{Code.count} codes now"
|
27
|
+
else
|
28
|
+
puts "Cannot find file #{file_path}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Dump codes to YAML'
|
33
|
+
task dump: :environment do
|
34
|
+
file_path = "#{Rails.root}/tmp/export/codes.yml"
|
35
|
+
ignored = %w(id ip agent_id)
|
36
|
+
File.open file_path, 'w' do |file|
|
37
|
+
Code.order('id asc').each do |entity|
|
38
|
+
print "\r#{entity.id} "
|
39
|
+
file.puts "#{entity.id}:"
|
40
|
+
entity.attributes.reject { |a, v| ignored.include?(a) || v.nil? }.each do |attribute, value|
|
41
|
+
file.puts " #{attribute}: #{value.inspect}"
|
42
|
+
end
|
43
|
+
file.puts " agent: #{entity.agent.name.inspect}" unless entity.agent.nil?
|
44
|
+
file.puts " ip: #{entity.ip}" unless entity.ip.blank?
|
45
|
+
end
|
46
|
+
puts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
namespace :tokens do
|
2
|
+
desc 'Load tokens from YAML with deleting old data'
|
3
|
+
task load: :environment do
|
4
|
+
file_path = "#{Rails.root}/tmp/import/tokens.yml"
|
5
|
+
ignored = %w(agent)
|
6
|
+
if File.exist?(file_path)
|
7
|
+
puts 'Deleting old tokens...'
|
8
|
+
Token.destroy_all
|
9
|
+
puts 'Done. Importing...'
|
10
|
+
File.open(file_path, 'r') do |file|
|
11
|
+
YAML.load(file).each do |id, data|
|
12
|
+
attributes = data.reject { |key| ignored.include? key }
|
13
|
+
entity = Token.new(id: id)
|
14
|
+
entity.assign_attributes(attributes)
|
15
|
+
|
16
|
+
if data.key?('agent')
|
17
|
+
entity.agent = Agent.named(data['agent'])
|
18
|
+
end
|
19
|
+
entity.save!
|
20
|
+
|
21
|
+
print "\r#{id} "
|
22
|
+
end
|
23
|
+
puts
|
24
|
+
end
|
25
|
+
Token.connection.execute "select setval('tokens_id_seq', (select max(id) from tokens));"
|
26
|
+
puts "Done. We have #{Token.count} tokens now"
|
27
|
+
else
|
28
|
+
puts "Cannot find file #{file_path}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Dump tokens to YAML'
|
33
|
+
task dump: :environment do
|
34
|
+
file_path = "#{Rails.root}/tmp/export/tokens.yml"
|
35
|
+
ignored = %w(id ip agent_id)
|
36
|
+
File.open file_path, 'w' do |file|
|
37
|
+
Token.order('id asc').each do |entity|
|
38
|
+
print "\r#{entity.id} "
|
39
|
+
file.puts "#{entity.id}:"
|
40
|
+
entity.attributes.reject { |a, v| ignored.include?(a) || v.nil? }.each do |attribute, value|
|
41
|
+
file.puts " #{attribute}: #{value.inspect}"
|
42
|
+
end
|
43
|
+
file.puts " agent: #{entity.agent.name.inspect}" unless entity.agent.nil?
|
44
|
+
file.puts " ip: #{entity.ip}" unless entity.ip.blank?
|
45
|
+
end
|
46
|
+
puts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Clean obsolete tokens'
|
51
|
+
task clean: :environment do
|
52
|
+
CleanTokensJob.perform_now
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
namespace :users do
|
4
|
+
desc 'Load users from YAML with deleting old data'
|
5
|
+
task load: :environment do
|
6
|
+
file_path = "#{Rails.root}/tmp/import/users.yml"
|
7
|
+
media_dir = "#{Rails.root}/tmp/import/users"
|
8
|
+
ignored = %w(image agent)
|
9
|
+
if File.exist?(file_path)
|
10
|
+
puts 'Deleting old users...'
|
11
|
+
User.destroy_all
|
12
|
+
puts 'Done. Importing...'
|
13
|
+
File.open(file_path, 'r') do |file|
|
14
|
+
YAML.load(file).each do |id, data|
|
15
|
+
attributes = data.reject { |key| ignored.include? key }
|
16
|
+
entity = User.new(id: id)
|
17
|
+
entity.assign_attributes(attributes)
|
18
|
+
if data.key?('image')
|
19
|
+
image_file = "#{media_dir}/#{id}/#{data['image']}"
|
20
|
+
if File.exist?(image_file)
|
21
|
+
entity.image = Pathname.new(image_file).open
|
22
|
+
end
|
23
|
+
end
|
24
|
+
if data.key?('agent')
|
25
|
+
entity.agent = Agent.named(data['agent'])
|
26
|
+
end
|
27
|
+
entity.save!
|
28
|
+
|
29
|
+
print "\r#{id} "
|
30
|
+
end
|
31
|
+
puts
|
32
|
+
end
|
33
|
+
User.connection.execute "select setval('users_id_seq', (select max(id) from users));"
|
34
|
+
puts "Done. We have #{User.count} users now"
|
35
|
+
else
|
36
|
+
puts "Cannot find file #{file_path}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Dump users to YAML'
|
41
|
+
task dump: :environment do
|
42
|
+
file_path = "#{Rails.root}/tmp/export/users.yml"
|
43
|
+
media_dir = "#{Rails.root}/tmp/export/users"
|
44
|
+
ignored = %w(id image ip agent_id follower_count followee_count comments_count)
|
45
|
+
Dir.mkdir(media_dir) unless Dir.exist?(media_dir)
|
46
|
+
File.open(file_path, 'w') do |file|
|
47
|
+
User.order('id asc').each do |entity|
|
48
|
+
print "\r#{entity.id} "
|
49
|
+
file.puts "#{entity.id}:"
|
50
|
+
entity.attributes.reject { |a, v| ignored.include?(a) || v.nil? }.each do |attribute, value|
|
51
|
+
file.puts " #{attribute}: #{value.inspect}"
|
52
|
+
end
|
53
|
+
|
54
|
+
unless entity.image.blank?
|
55
|
+
image_name = File.basename(entity.image.path)
|
56
|
+
Dir.mkdir("#{media_dir}/#{entity.id}") unless Dir.exist?("#{media_dir}/#{entity.id}")
|
57
|
+
FileUtils.copy(entity.image.path, "#{media_dir}/#{entity.id}/#{image_name}")
|
58
|
+
file.puts " image: #{image_name.inspect}"
|
59
|
+
end
|
60
|
+
|
61
|
+
file.puts " agent: #{entity.agent.name.inspect}" unless entity.agent.nil?
|
62
|
+
file.puts " ip: #{entity.ip}" unless entity.ip.blank?
|
63
|
+
end
|
64
|
+
puts
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|