apidae 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73aee2961a8445a742b2d0d44df6a19a4e8cb3c1
4
- data.tar.gz: 449b1eae7f0c4695b819d80994fcbed367142952
3
+ metadata.gz: 44825ff63506e2a48c0e3c3a9a283c804daae923
4
+ data.tar.gz: 2cd5dc90fe25340322d26717f75c2ca5757da753
5
5
  SHA512:
6
- metadata.gz: aa1900606d48edd2850598554f08ffd64976a93ffa82de433de0a093af8ebdf5b369c84d4b12e37c5d1ef165cd07bd832365cbdf686ab08b104e8a627ecae389
7
- data.tar.gz: b8948a4bc6c72ae3a561b8f6abcd0a025dd3d22274f64267cf7e729a1aecd90bc76bae441d82b9259e4c34d1f793a68fe395e33863849a50dd397dd9d72d7c8f
6
+ metadata.gz: ff6e9558ab78808534e1030184b40e1b3f4ca0cae89bc5692178726af3787592526b8991328e11c611b70d4818722fcaf57db5c6f410f9ce472496fbbf2c80aa
7
+ data.tar.gz: ff6c592151798073270a602ac0a2b3907bb51dafac6f6c3b9d4452f70e505280729af898f58f43f8de0312d2d49918f4490320f07c67fb510b305d789dd00436
@@ -5,6 +5,7 @@ module Apidae
5
5
  def index
6
6
  @objects = Obj.count
7
7
  @selections = Selection.count
8
+ @projects = Project.count
8
9
  @references = Reference.count
9
10
  @last_imports = FileImport.order(id: :desc).take(5)
10
11
  end
@@ -16,25 +16,20 @@ module Apidae
16
16
  # urlRecuperation : une chaine de caractères. L’URL de récupération du fichier d’export.
17
17
  # urlConfirmation : une chaine de caractères. L’URL de confirmation.
18
18
  def callback
19
- project_id = params[:projetId]
20
- if project_id == Rails.application.config.apidae_project_id
21
- export = Export.new(project_id: project_id, remote_status: params[:statut], oneshot: params[:ponctuel] == 'true',
22
- reset: params[:reinitialisation] == 'true', file_url: params[:urlRecuperation],
23
- confirm_url: params[:urlConfirmation], status: Export::PENDING)
24
- if export.save
25
- if Rails.application.config.respond_to?(:apidae_propagate_callback)
26
- uri = URI(Rails.application.config.apidae_propagate_callback)
27
- req = Net::HTTP::Post.new(uri)
28
- Net::HTTP.start(uri.hostname, uri.port) do |http|
29
- http.request(req, params.to_unsafe_h.to_query)
30
- end
19
+ export = Export.new(project_id: params[:projetId], remote_status: params[:statut], oneshot: params[:ponctuel] == 'true',
20
+ reset: params[:reinitialisation] == 'true', file_url: params[:urlRecuperation],
21
+ confirm_url: params[:urlConfirmation], status: Export::PENDING)
22
+ if export.save
23
+ if Rails.application.config.respond_to?(:apidae_propagate_callback)
24
+ uri = URI(Rails.application.config.apidae_propagate_callback)
25
+ req = Net::HTTP::Post.new(uri)
26
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: (uri.scheme == "https")) do |http|
27
+ http.request(req, params.to_unsafe_h.to_query)
31
28
  end
32
- head :ok
33
- else
34
- head :internal_server_error
35
29
  end
30
+ head :ok
36
31
  else
37
- head :not_found
32
+ head :internal_server_error
38
33
  end
39
34
  end
40
35
 
@@ -44,7 +39,7 @@ module Apidae
44
39
  begin
45
40
  open(e.file_url) do |f|
46
41
  begin
47
- FileImport.import(f)
42
+ FileImport.import(f, e.project_id)
48
43
  uri = URI(e.confirm_url)
49
44
  req = Net::HTTP::Post.new(uri)
50
45
  Net::HTTP.start(uri.hostname, uri.port) do |http|
@@ -0,0 +1,35 @@
1
+ require_dependency "apidae/application_controller"
2
+
3
+ module Apidae
4
+ class ProjectsController < ApplicationController
5
+ before_action :set_project, only: [:edit, :update]
6
+
7
+ def index
8
+ @projects = Project.all
9
+ end
10
+
11
+ def edit
12
+ session[:referrer] = request.referrer
13
+ end
14
+
15
+ def update
16
+ if @project.update(project_params)
17
+ referrer = session.delete(:referrer)
18
+ redirect_to referrer, notice: 'Le projet a bien été mis à jour'
19
+ else
20
+ flash.now[:alert] = "Une erreur s'est produite lors la mise à jour du projet"
21
+ render :edit
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def set_project
28
+ @project = Project.find(params[:id])
29
+ end
30
+
31
+ def project_params
32
+ params.require(:project).permit(:name, :api_key)
33
+ end
34
+ end
35
+ end
@@ -14,7 +14,7 @@ module Apidae
14
14
  DELETED_FILE = 'objets_supprimes.json'
15
15
  SELECTIONS_FILE = 'selections.json'
16
16
 
17
- def self.import(zip_file)
17
+ def self.import(zip_file, project_id)
18
18
  Zip::File.open(zip_file) do |zfile|
19
19
  result = {created: 0, updated: 0, deleted: 0, selections: []}
20
20
  Reference.import(zfile.read(REFERENCES_FILE))
@@ -30,21 +30,21 @@ module Apidae
30
30
  elsif file.name.include?(DELETED_FILE)
31
31
  delete_objects(zfile.read(file.name), result)
32
32
  elsif file.name.include?(SELECTIONS_FILE)
33
- add_or_update_selections(zfile.read(file.name), result)
33
+ add_or_update_selections(project_id, zfile.read(file.name), result)
34
34
  end
35
35
  end
36
36
  end
37
- create(result.except(:selections).merge({remote_file: zip_file, status: STATUS_COMPLETE}))
37
+ create(result.except(:selections).merge({remote_file: (zip_file.is_a?(File) ? zip_file.path : zip_file), status: STATUS_COMPLETE}))
38
38
  logger.info "Import results : #{result}"
39
39
  result
40
40
  end
41
41
  end
42
42
 
43
- def self.import_dir(dir)
43
+ def self.import_dir(project_id, dir)
44
44
  result = {created: 0, updated: 0, deleted: 0, selections: []}
45
45
  import_updates(File.join(dir, MODIFIED_DIR), result)
46
46
  import_deletions(File.join(dir, DELETED_FILE), result)
47
- import_selections(File.join(dir,SELECTIONS_FILE), result)
47
+ import_selections(project_id, File.join(dir, SELECTIONS_FILE), result)
48
48
  logger.info "Import results : #{result}"
49
49
  result
50
50
  end
@@ -140,20 +140,21 @@ module Apidae
140
140
  # end
141
141
  # end
142
142
 
143
- def self.import_selections(json_file, result)
143
+ def self.import_selections(project_id, json_file, result)
144
144
  selections_json = File.read(json_file)
145
- add_or_update_selections(selections_json, result)
145
+ add_or_update_selections(project_id, selections_json, result)
146
146
  end
147
147
 
148
- def self.add_or_update_selections(selections_json, result)
148
+ def self.add_or_update_selections(project_id, selections_json, result)
149
149
  selections_hashes = JSON.parse(selections_json, symbolize_names: true)
150
- deleted_ids = Selection.all.collect {|sel| sel.apidae_id}.uniq - selections_hashes.collect {|sel| sel[:id]}
150
+ project = Project.find_or_create_by(apidae_id: project_id)
151
+ deleted_ids = Selection.where(apidae_project_id: project.id).collect {|sel| sel.apidae_id}.uniq - selections_hashes.collect {|sel| sel[:id]}
151
152
  Selection.where(apidae_id: deleted_ids).delete_all
152
153
  selections_hashes.each do |selection_data|
153
154
  logger.info "Updating selection #{selection_data[:id]}"
154
- Selection.add_or_update(selection_data)
155
+ Selection.add_or_update(selection_data, project.id)
155
156
  end
156
- result[:selections] = Selection.all
157
+ result[:selections] = Selection.where(apidae_project_id: project_id)
157
158
  .collect {|sel| {apidae_id: sel.apidae_id, reference: sel.reference, objects: sel.objects.count}}
158
159
  end
159
160
  end
@@ -125,7 +125,7 @@ module Apidae
125
125
  {
126
126
  short_desc: node_value(data_hash, :descriptifCourt),
127
127
  long_desc: node_value(data_hash, :descriptifDetaille),
128
- theme_desc: data_hash[:descriptifsThematises].blank? ? [] : Hash[data_hash[:descriptifsThematises].map {|th| [node_id(th, :theme), node_value(th, :description)]}],
128
+ theme_desc: data_hash[:descriptifsThematises].blank? ? {} : Hash[data_hash[:descriptifsThematises].map {|th| [node_id(th, :theme), node_value(th, :description)]}],
129
129
  private_desc: private_data.blank? ? {} : Hash[private_data.map {|d| [d[:nomTechnique], node_value(d, :descriptif)]}]
130
130
  }
131
131
  end
@@ -0,0 +1,5 @@
1
+ module Apidae
2
+ class Project < ActiveRecord::Base
3
+ has_many :apidae_selections, class_name: 'Apidae::Selection', foreign_key: :apidae_project_id
4
+ end
5
+ end
@@ -4,6 +4,7 @@ module Apidae
4
4
  class Selection < ActiveRecord::Base
5
5
  has_many :apidae_selection_objects, class_name: 'Apidae::SelectionObject', foreign_key: :apidae_selection_id
6
6
  has_many :objects, class_name: 'Apidae::Obj', source: :apidae_object, through: :apidae_selection_objects
7
+ belongs_to :apidae_project, optional: true, class_name: 'Apidae::Project', foreign_key: :apidae_project_id
7
8
 
8
9
  AGENDA_ENDPOINT = 'agenda/detaille/list-identifiants'
9
10
  SELECTION_ENDPOINT = 'recherche/list-identifiants'
@@ -13,9 +14,10 @@ module Apidae
13
14
  validates_presence_of :apidae_id, :reference
14
15
  before_validation :generate_reference, on: :create
15
16
 
16
- def self.add_or_update(selection_data)
17
+ def self.add_or_update(selection_data, apidae_proj_id)
17
18
  apidae_sel = Selection.where(apidae_id: selection_data[:id]).first_or_initialize
18
19
  apidae_sel.label = selection_data[:nom]
20
+ apidae_sel.apidae_project_id = apidae_proj_id
19
21
  apidae_sel.save!
20
22
 
21
23
  # Note : should be done with basic collection assignment, but can't make it work...
@@ -134,8 +136,8 @@ module Apidae
134
136
  def build_args(endpoint, opts = {})
135
137
  {
136
138
  url: "#{Rails.application.config.apidae_api_url}/#{endpoint}",
137
- apiKey: Rails.application.config.apidae_api_key,
138
- projetId: Rails.application.config.apidae_project_id,
139
+ apiKey: apidae_project ? apidae_project.api_key : '',
140
+ projetId: apidae_project.apidae_id,
139
141
  first: opts[:first] || 0,
140
142
  count: opts[:count] || MAX_COUNT,
141
143
  selectionIds: opts[:selection_ids],
@@ -1,5 +1,6 @@
1
1
  <%= render layout: "/layouts/#{Rails.application.config.apidae_layout}" do |styles| %>
2
2
  <div id="apidae_header" class="<%= styles[:header] %>">
3
+ <%= link_to pluralize(@projects, 'projet', 'projets'), apidae.projects_path, class: styles[:projects] %>
3
4
  <%= link_to pluralize(@selections, 'sélection', 'sélections'), apidae.selections_path, class: styles[:selections] %>
4
5
  <%= link_to pluralize(@objects, 'objet touristique', 'objets touristiques'), apidae.objects_path, class: styles[:objects] %>
5
6
  <%= link_to (pluralize(@references, 'élément', 'éléments') + ' de référence'), apidae.references_path, class: styles[:references] %>
@@ -0,0 +1,33 @@
1
+ <%= render layout: "/layouts/#{Rails.application.config.apidae_layout}" do |styles| %>
2
+ <div id="apidae_header" class="<%= styles[:header] %>">
3
+ <%= link_to 'Retour', :back, class: styles[:back] %>
4
+ <h1 class="<%= styles[:h1] %>">Apidae - Modifier le projet</h1>
5
+ </div>
6
+ <div id="apidae_edit_project" class="<%= styles[:wrapper] %>">
7
+ <div id="apidae_form" class="<%= styles[:body] %>">
8
+ <%= form_for(@project, class: styles[:form]) do |f| %>
9
+ <% if @project.errors.any? %>
10
+ <div id="apidae_form_errors">
11
+ <ul>
12
+ <% @project.errors.full_messages.each do |message| %>
13
+ <li><%= message %></li>
14
+ <% end %>
15
+ </ul>
16
+ </div>
17
+ <% end %>
18
+
19
+ <div class="<%= styles[:form_field] %>">
20
+ <div><%= f.label :name %></div>
21
+ <div><%= f.text_field :name %></div>
22
+ </div>
23
+ <div class="<%= styles[:form_field] %>">
24
+ <div><%= f.label :api_key %></div>
25
+ <div><%= f.text_field :api_key %></div>
26
+ </div>
27
+ <div class="<%= styles[:form_actions] %>">
28
+ <%= f.submit 'Valider' %>
29
+ </div>
30
+ <% end %>
31
+ </div>
32
+ </div>
33
+ <% end %>
@@ -0,0 +1,35 @@
1
+ <%= render layout: "/layouts/#{Rails.application.config.apidae_layout}" do |styles| %>
2
+ <div id="apidae_header" class="<%= styles[:header] %>">
3
+ <%= link_to 'Retour', root_path, class: styles[:back] %>
4
+ <h1 class="<%= styles[:h1] %>">Apidae - Projets</h1>
5
+ </div>
6
+ <div class="<%= styles[:wrapper] %>">
7
+ <div class="<%= styles[:body] %>">
8
+ <table id="apidae_projects" class="<%= styles[:table] %>">
9
+ <thead class="<%= styles[:table_head] %>">
10
+ <tr>
11
+ <th>Nom</th>
12
+ <th>Identifiant</th>
13
+ <th>Mise à jour</th>
14
+ <th></th>
15
+ </tr>
16
+ </thead>
17
+ <tbody class="<%= styles[:table_body] %>">
18
+ <% @projects.each do |proj| %>
19
+ <tr>
20
+ <td><%= proj.name %></td>
21
+ <td><%= proj.apidae_id %></td>
22
+ <td><%= proj.updated_at.strftime('Le %d/%m/%Y à %H:%M') if proj.updated_at %></td>
23
+ <td><%= link_to 'Modifier', edit_project_path(proj) %></td>
24
+ </tr>
25
+ <% end %>
26
+ <% if @projects.empty? %>
27
+ <tr>
28
+ <td colspan="4">Aucun projet pour le moment.</td>
29
+ </tr>
30
+ <% end %>
31
+ </tbody>
32
+ </table>
33
+ </div>
34
+ </div>
35
+ <% end %>
@@ -3,8 +3,8 @@
3
3
  <%= link_to 'Retour', root_path, class: styles[:back] %>
4
4
  <h1 class="<%= styles[:h1] %>">Apidae - Eléments de référence</h1>
5
5
  </div>
6
- <div id="apidae_dashboard" class="<%= styles[:wrapper] %>">
7
- <div id="apidae_imports_panel" class="<%= styles[:body] %>">
6
+ <div class="<%= styles[:wrapper] %>">
7
+ <div class="<%= styles[:body] %>">
8
8
  <table id="apidae_references" class="<%= styles[:table] %>">
9
9
  <thead class="<%= styles[:table_head] %>">
10
10
  <tr>
data/config/routes.rb CHANGED
@@ -5,6 +5,7 @@ Apidae::Engine.routes.draw do
5
5
  resources :objects, only: [:index], path: 'objets'
6
6
  end
7
7
  resources :references, only: [:index]
8
+ resources :projects, only: [:index, :edit, :update], path: 'projets'
8
9
 
9
10
  match 'import/callback', via: :post, to: 'import#callback'
10
11
  match 'import/run', via: :post, to: 'import#run'
@@ -0,0 +1,5 @@
1
+ class AddProjectIdToSelections < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :apidae_selections, :apidae_project_id, :integer
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class CreateApidaeProjects < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :apidae_projects do |t|
4
+ t.string :name
5
+ t.integer :apidae_id
6
+ t.string :api_key
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
data/lib/apidae/engine.rb CHANGED
@@ -4,6 +4,9 @@ module Apidae
4
4
  # Include all helpers from main app
5
5
  config.to_prepare do
6
6
  ApplicationController.helper(ActionView::Helpers::ApplicationHelper)
7
+ Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
8
+ require_dependency(c)
9
+ end
7
10
  end
8
11
  end
9
12
  end
@@ -1,3 +1,3 @@
1
1
  module Apidae
2
- VERSION = "0.6.3"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apidae
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Baptiste Vilain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-22 00:00:00.000000000 Z
11
+ date: 2018-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -84,6 +84,7 @@ files:
84
84
  - app/controllers/apidae/dashboard_controller.rb
85
85
  - app/controllers/apidae/import_controller.rb
86
86
  - app/controllers/apidae/objects_controller.rb
87
+ - app/controllers/apidae/projects_controller.rb
87
88
  - app/controllers/apidae/references_controller.rb
88
89
  - app/controllers/apidae/selections_controller.rb
89
90
  - app/helpers/apidae/api_helper.rb
@@ -97,6 +98,7 @@ files:
97
98
  - app/models/apidae/export.rb
98
99
  - app/models/apidae/file_import.rb
99
100
  - app/models/apidae/obj.rb
101
+ - app/models/apidae/project.rb
100
102
  - app/models/apidae/reference.rb
101
103
  - app/models/apidae/selection.rb
102
104
  - app/models/apidae/selection_object.rb
@@ -110,6 +112,8 @@ files:
110
112
  - app/views/apidae/objects/new.html.erb
111
113
  - app/views/apidae/objects/show.html.erb
112
114
  - app/views/apidae/objects/show.json.jbuilder
115
+ - app/views/apidae/projects/edit.html.erb
116
+ - app/views/apidae/projects/index.html.erb
113
117
  - app/views/apidae/references/index.html.erb
114
118
  - app/views/apidae/selections/_form.html.erb
115
119
  - app/views/apidae/selections/edit.html.erb
@@ -150,6 +154,8 @@ files:
150
154
  - db/migrate/20180519170210_remove_insee_code_unicity.rb
151
155
  - db/migrate/20180521211735_destroy_attached_files.rb
152
156
  - db/migrate/20180625050400_rename_objects_to_obj.rb
157
+ - db/migrate/20181024072424_add_project_id_to_selections.rb
158
+ - db/migrate/20181024072843_create_apidae_projects.rb
153
159
  - lib/apidae.rb
154
160
  - lib/apidae/engine.rb
155
161
  - lib/apidae/version.rb