diff options
| author | Luc Donnet | 2017-12-05 17:28:25 +0100 | 
|---|---|---|
| committer | GitHub | 2017-12-05 17:28:25 +0100 | 
| commit | 9f1aa4d40d7d968963dae5799e15ad5da4cbcbc5 (patch) | |
| tree | ca3ab17437207e04b7aabaca313078b0f4327163 | |
| parent | e66e96516094151f352b14b0dca6e6994e7eb4d0 (diff) | |
| parent | b0bf768dfc1b977bcc758ba974cdd1494d7bab56 (diff) | |
| download | chouette-core-9f1aa4d40d7d968963dae5799e15ad5da4cbcbc5.tar.bz2 | |
Merge pull request #132 from af83/5127_new_api_endpoints
Refs #5127 Add two new endpoints to the API
| -rw-r--r-- | app/controllers/api/v1/internals/application_controller.rb | 16 | ||||
| -rw-r--r-- | app/controllers/api/v1/internals/compliance_check_sets_controller.rb | 46 | ||||
| -rw-r--r-- | app/controllers/api/v1/internals/netex_imports_controller.rb | 104 | ||||
| -rw-r--r-- | app/controllers/compliance_check_sets_controller.rb | 2 | ||||
| -rw-r--r-- | app/controllers/imports_controller.rb | 4 | ||||
| -rw-r--r-- | app/models/compliance_check_set.rb | 9 | ||||
| -rw-r--r-- | config/breadcrumbs.rb | 5 | ||||
| -rw-r--r-- | config/locales/compliance_check_sets.en.yml | 2 | ||||
| -rw-r--r-- | config/locales/compliance_check_sets.fr.yml | 2 | ||||
| -rw-r--r-- | config/routes.rb | 7 | ||||
| -rw-r--r-- | config/secrets.yml | 3 | ||||
| -rw-r--r-- | db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 1 | 
13 files changed, 200 insertions, 6 deletions
| diff --git a/app/controllers/api/v1/internals/application_controller.rb b/app/controllers/api/v1/internals/application_controller.rb new file mode 100644 index 000000000..77b74f5f6 --- /dev/null +++ b/app/controllers/api/v1/internals/application_controller.rb @@ -0,0 +1,16 @@ +module Api +  module V1 +    module Internals +      class ApplicationController < ActionController::Base +        respond_to :json +        layout false +        before_action :authenticate + +        private +        def authenticate +          authenticate_with_http_token { |token| Rails.application.secrets.api_token == token } +        end +      end +    end +  end +end diff --git a/app/controllers/api/v1/internals/compliance_check_sets_controller.rb b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb new file mode 100644 index 000000000..db92c3fad --- /dev/null +++ b/app/controllers/api/v1/internals/compliance_check_sets_controller.rb @@ -0,0 +1,46 @@ +module Api +  module V1 +    module Internals +      class ComplianceCheckSetsController < ApplicationController +        include ControlFlow + +        def validated +          find_compliance_check_set + +          if @compliance_check_set.update_status +            render :validated +          else +            render json: { +              status: "error", +              messages: @compliance_check_set.errors.full_messages +            } +          end +        end + +        def notify_parent +          find_compliance_check_set +          if  @compliance_check_set.notify_parent && @compliance_check_set.parent +            render json: { +              status: "ok", +              message:"#{@compliance_check_set.parent_type} (id: #{@compliance_check_set.parent_id}) successfully notified at #{l(@compliance_check_set.notified_parent_at)}" +            } +          else +            render json: {status: "error", message: @compliance_check_set.errors.full_messages } +          end          +        end + +        private + +        def find_compliance_check_set +          @compliance_check_set = ComplianceCheckSet.find(params[:id]) +        rescue ActiveRecord::RecordNotFound +          render json: { +            status: "error",  +            message: "Record not found" +          } +          finish_action!    +        end +      end +    end +  end +end   diff --git a/app/controllers/api/v1/internals/netex_imports_controller.rb b/app/controllers/api/v1/internals/netex_imports_controller.rb new file mode 100644 index 000000000..89bc1b81d --- /dev/null +++ b/app/controllers/api/v1/internals/netex_imports_controller.rb @@ -0,0 +1,104 @@ +module Api +  module V1 +    module Internals +      class NetexImportsController < ApplicationController +        include ControlFlow + +        def create +          respond_to do | format | +            format.json(&method(:create_models)) +          end +        end + +        def notify_parent +          find_netex_import +          if @netex_import.notify_parent +            render json: { +              status: "ok", +              message:"#{@netex_import.parent_type} (id: #{@netex_import.parent_id}) successfully notified at #{l(@netex_import.notified_parent_at)}" +            } +          else +            render json: {status: "error", message: @netex_import.errors.full_messages } +          end +        end + +        private + +        def find_netex_import +          @netex_import = NetexImport.find(params[:id]) +        rescue ActiveRecord::RecordNotFound +          render json: { +            status: "error",  +            message: "Record not found" +          } +          finish_action!    +        end + +        def find_workbench +          @workbench = Workbench.find(netex_import_params['workbench_id']) +        rescue ActiveRecord::RecordNotFound +          render json: {errors: {'workbench_id' => 'missing'}}, status: 406 +          finish_action! +        end + +        def create_models +          find_workbench +          create_referential +          create_netex_import +        end + +        def create_netex_import +          attributes = netex_import_params.merge creator: "Webservice" + +          attributes = attributes.merge referential_id: @new_referential.id + +          @netex_import = NetexImport.new attributes +          @netex_import.save! + +          unless @netex_import.referential +            Rails.logger.info "Can't create referential for import #{@netex_import.id}: #{@new_referential.inspect} #{@new_referential.metadatas.inspect} #{@new_referential.errors.full_messages}" +            @netex_import.messages.create criticity: :error, message_key: "referential_creation" +          end +        rescue ActiveRecord::RecordInvalid +          render json: {errors: @netex_import.errors}, status: 406 +          finish_action! +        end + +        def create_referential +          @new_referential = +            Referential.new( +              name: netex_import_params['name'], +              organisation_id: @workbench.organisation_id, +              workbench_id: @workbench.id, +              metadatas: [metadata] +            ) +          @new_referential.save +        end + +        def metadata +          metadata = ReferentialMetadata.new + +          if netex_import_params['file'] +            netex_file = STIF::NetexFile.new(netex_import_params['file'].to_io) +            frame = netex_file.frames.first + +            if frame +              metadata.periodes = frame.periods + +              line_objectids = frame.line_refs.map { |ref| "STIF:CODIFLIGNE:Line:#{ref}" } +              metadata.line_ids = @workbench.lines.where(objectid: line_objectids).pluck(:id) +            end +          end + +          metadata +        end + +        def netex_import_params +          params +            .require('netex_import') +            .permit(:file, :name, :workbench_id, :parent_id, :parent_type) +        end +      end +    end +  end +end diff --git a/app/controllers/compliance_check_sets_controller.rb b/app/controllers/compliance_check_sets_controller.rb index 03b920030..600c69126 100644 --- a/app/controllers/compliance_check_sets_controller.rb +++ b/app/controllers/compliance_check_sets_controller.rb @@ -12,7 +12,7 @@ class ComplianceCheckSetsController < ChouetteController        @q_for_form = scope.ransack(params[:q])        format.html {          @compliance_check_sets = ModelDecorator.decorate( -          @q_for_form.result, +          @q_for_form.result.order(created_at: :desc),            with: ComplianceCheckSetDecorator          )        } diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 5e23a1795..46d34efda 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -77,10 +77,10 @@ class ImportsController < ChouetteController    end    def sort_column -    parent.imports.column_names.include?(params[:sort]) ? params[:sort] : 'name' +    parent.imports.column_names.include?(params[:sort]) ? params[:sort] : 'created_at'    end    def sort_direction -    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc' +    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'desc'    end    def decorate_imports(imports) diff --git a/app/models/compliance_check_set.rb b/app/models/compliance_check_set.rb index d734e5f5a..7c55561af 100644 --- a/app/models/compliance_check_set.rb +++ b/app/models/compliance_check_set.rb @@ -19,6 +19,15 @@ class ComplianceCheckSet < ActiveRecord::Base      where('created_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end)    end +  def notify_parent +    if parent +      # parent.child_change +      update(notified_parent_at: DateTime.now) +    else +      errors.add(:base, I18n.t('compliance_check_sets.errors.no_parent')) +    end +  end +    def update_status      statuses = compliance_check_resources.map do |resource|        case resource.status diff --git a/config/breadcrumbs.rb b/config/breadcrumbs.rb index 14c3d5470..970f933f0 100644 --- a/config/breadcrumbs.rb +++ b/config/breadcrumbs.rb @@ -81,6 +81,11 @@ crumb :import do |workbench, import|    parent :imports, workbench  end +crumb :import_resources do |import, import_resources| +  link I18n.t('import_resources.index.title'), workbench_import_import_resources_path(import.workbench, import) +  parent :import, import.workbench, import +end +  crumb :organisation do |organisation|    link breadcrumb_name(organisation), organisation_path(organisation)  end diff --git a/config/locales/compliance_check_sets.en.yml b/config/locales/compliance_check_sets.en.yml index 87d9abef0..8023da0f7 100644 --- a/config/locales/compliance_check_sets.en.yml +++ b/config/locales/compliance_check_sets.en.yml @@ -22,6 +22,8 @@ en:        table_state: "%{lines_status} lines imported on %{lines_in_compliance_check_set} in the archive"        table_explanation: "These controls apply to all imported data and condition the construction of your organization's offer."        metrics: "%{ok_count} ok, %{error_count} errors, %{warning_count} warnings, %{uncheck_count} n/a" +    errors: +      no_parent: "The compliance check set doesn't any parent"    activerecord:      attributes:        compliance_check_set: diff --git a/config/locales/compliance_check_sets.fr.yml b/config/locales/compliance_check_sets.fr.yml index 84db8b934..0382f2937 100644 --- a/config/locales/compliance_check_sets.fr.yml +++ b/config/locales/compliance_check_sets.fr.yml @@ -18,6 +18,8 @@ fr:        table_state: "%{lines_status} lignes importées sur %{lines_in_compliance_check_set} présentes dans l'archive"        table_explanation: Ces contrôles s’appliquent pour toutes les données importées et conditionnent la construction de l’offre de votre organisation        metrics: "%{ok_count} ok, %{error_count} errors, %{warning_count} warnings, %{uncheck_count} n/a" +    errors: +      no_parent: "Le jeux de contrôle n'a pas de parent"    activerecord:      attributes:        compliance_check_set: diff --git a/config/routes.rb b/config/routes.rb index 8ea8168bf..65fa62557 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,9 +64,10 @@ ChouetteIhm::Application.routes.draw do        resources :stop_areas, only: [:index, :show]        resources :time_tables, only: [:index, :show]        resources :vehicle_journeys, only: :show -      patch 'compliance_check_sets/:id', -        to: 'compliance_check_sets#validated', -        as: 'compliance_check_set' +      namespace :internals do +        get 'compliance_check_sets/:id/notify_parent', to: 'compliance_check_sets#notify_parent' +        get 'netex_imports/:id/notify_parent', to: 'netex_imports#notify_parent' +      end      end    end diff --git a/config/secrets.yml b/config/secrets.yml index 2f9d5abe6..0de3fd6ef 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -13,6 +13,7 @@  development:    secret_key_base: e901adb90fc1fff22f7237d138ef232a5425b67e81ccb3ce7003aab2c002d35a7c3d593c0fca3dcb21e8c911a8260c984a830d1d61776ad3005eb373abebc695    api_endpoint: "http://localhost:8080/chouette_iev/" +  api_token: VFHir2GWWjuRNZnHHnQD5Hn+ubRMQ1kNLnu7oCLf+4KR8+PmYqb1EzKZmmuRfVP/yxS0aQ3NklfNbbgUatTtly5540oo4L6ePdbYkwDzrBXF9xgYekOlTCwIGSl430mluv3wcXNEbrRLu2CevIBULtiRZriAEYVOpp9G+lQI+t8=    google_analytic_tracker: "UA-AAAAAAAA"    # geoportail_api_key: "aaaaaaaaaaaaaaaaaaaaaa"    newrelic_licence_key: "" @@ -21,6 +22,7 @@ development:  test:    secret_key_base: 54f61aab23322611dd0bbf73b7f034db34281f7f4b3c4992eaaff20ecc9673bbd467beaa6fcb48379ca69b80bc5662deac4e33ca144f2482146123d3e966016a    api_endpoint: "http://localhost:8080/chouette_iev/" +  api_token: VFHir2GWWjuRNZnHHnQD5Hn+ubRMQ1kNLnu7oCLf+4KR8+PmYqb1EzKZmmuRfVP/yxS0aQ3NklfNbbgUatTtly5540oo4L6ePdbYkwDzrBXF9xgYekOlTCwIGSl430mluv3wcXNEbrRLu2CevIBULtiRZriAEYVOpp9G+lQI+t8=    google_analytic_tracker: "UA-AAAAAAAA"    # geoportail_api_key: "aaaaaaaaaaaaaaaaaaaaaa"    osrm_endpoint: "http://router.project-osrm.org" @@ -30,6 +32,7 @@ test:  production:    secret_key_base: 54f61aab23322611dd0bbf73b7f034db34281f7f4b3c4992eaaff20ecc9673bbd467beaa6fcb48379ca69b80bc5662deac4e33ca144f2482146123d3e966016a    api_endpoint: "http://localhost:8080/chouette_iev/" +  api_token: VFHir2GWWjuRNZnHHnQD5Hn+ubRMQ1kNLnu7oCLf+4KR8+PmYqb1EzKZmmuRfVP/yxS0aQ3NklfNbbgUatTtly5540oo4L6ePdbYkwDzrBXF9xgYekOlTCwIGSl430mluv3wcXNEbrRLu2CevIBULtiRZriAEYVOpp9G+lQI+t8=    google_analytic_tracker: "UA-AAAAAAAA"    # geoportail_api_key: "aaaaaaaaaaaaaaaaaaaaaa"    newrelic_licence_key: "" diff --git a/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb b/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb new file mode 100644 index 000000000..3bf6b8aa1 --- /dev/null +++ b/db/migrate/20171130131328_add_notify_parent_at_to_compliance_check_sets.rb @@ -0,0 +1,5 @@ +class AddNotifyParentAtToComplianceCheckSets < ActiveRecord::Migration +  def change +    add_column :compliance_check_sets, :notified_parent_at, :datetime +  end +end diff --git a/db/schema.rb b/db/schema.rb index 4650d8812..c94b43da1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -195,6 +195,7 @@ ActiveRecord::Schema.define(version: 20171128112629) do      t.string   "name"      t.datetime "started_at"      t.datetime "ended_at" +    t.datetime "notified_parent_at"    end    add_index "compliance_check_sets", ["compliance_control_set_id"], name: "index_compliance_check_sets_on_compliance_control_set_id", using: :btree | 
