Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-1.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacug committed Aug 19, 2022
2 parents 104ab0e + 131718b commit bbf2102
Show file tree
Hide file tree
Showing 25 changed files with 641 additions and 42 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
### 1.8.0

**Intersessional decisions**

* Make direct association between Eu Decisions and Documents
* Add validation on Eu Opinions (Event or Intersessional doc, not both at least one of the two)
* Update Eu Decisions SQL views
* Update FE to display link to Intersessional doc or static text based on user session

### 1.7.1

**CITES Trade DB**

* Update to version 2021.1(replace zip file on the server, update ENV var, text updates)
* Update to version 2022.1(replace zip file on the server, update ENV var, text updates)

### 1.7.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,30 @@
</td>
<td class="last">
{{#if isSignedIn }}
{{#if decision.intersessional_decision_id}}
<a class="legal-links" href="https://speciesplus.net/api/v1/documents/{{unbound decision.intersessional_decision_id}}">
Intersessional decision
</a>
{{/if}}
{{#if decision.private_url}}
<a class="legal-links" href="{{unbound decision.private_url}}">
{{decision.start_event.name}}
</a>
{{else}}
{{#if decision.start_event.url}}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
</a>
{{/if}}
{{/if}}
{{else}}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
{{#if decision.start_event.name }}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
{{else}}
Intersessional decision
{{/if}}
{{/if}}
</td>
</tr>
Expand Down Expand Up @@ -151,21 +160,30 @@
</td>
<td class="last">
{{#if isSignedIn }}
{{#if decision.intersessional_decision_id}}
<a class="legal-links" href="https://speciesplus.net/api/v1/documents/{{unbound decision.intersessional_decision_id}}">
Intersessional decision
</a>
{{/if}}
{{#if decision.private_url}}
<a class="legal-links" href="{{unbound decision.private_url}}">
{{decision.start_event.name}}
</a>
{{else}}
{{#if decision.start_event.url}}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
</a>
{{/if}}
{{/if}}
{{else}}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
{{#if decision.start_event.name }}
<a class="legal-links" href="{{unbound decision.start_event.url}}">
{{decision.start_event.name}}
</a>
{{else}}
Intersessional decision
{{/if}}
{{/if}}
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/api_usage_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Admin::ApiUsageController < Admin::AdminController
def index
@recent_requests = ApiRequest.recent_requests
@users_by_activity = ApiRequest.top_5_most_active_users
@users_by_activity = ApiRequest.top_50_most_active_users
end

def show
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/admin/eu_opinions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def load_lib_objects
@ec_srgs = Event.where("type = 'EcSrg' OR
type = 'EuRegulation' AND name IN ('No 338/97', 'No 938/97', 'No 750/2013')"
).order("effective_at DESC")
# this will only return intersessional docs
@documents = Document.where("event_id IS NULL AND type = 'Document::CommissionNotes'")
.order('date DESC, title')
end

def collection
Expand Down
69 changes: 69 additions & 0 deletions app/controllers/api/v1/eu_decisions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Api::V1::EuDecisionsController < ApplicationController

# makes params available to the ActiveModel::Serializers
serialization_scope :view_context

def index
@eu_decisions = eu_decision_search(sanitized_params)
render :json => @eu_decisions,
:each_serializer => CaptiveBreeding::EuDecisionSerializer
end

private

def permitted_params
params.permit(taxon_concept_ids: [], geo_entity_ids: [])
end

def eu_decision_search(params)
list = EuDecision.from('api_eu_decisions_view eu_decisions').
select(eu_decision_select_attrs).
joins('LEFT JOIN eu_suspensions_applicability_view v ON eu_decisions.id = v.id').
order(<<-SQL
geo_entity_en->>'name' ASC,
start_date DESC
SQL
)

list = list.where("eu_decisions.taxon_concept_id IN (?)", params['taxon_concept_ids']) if params['taxon_concept_ids'].present?
list = list.where("eu_decisions.geo_entity_id IN (?)", params['geo_entity_ids']) if params['geo_entity_ids'].present?

list.all
end

def eu_decision_select_attrs
string = %{
eu_decisions.notes,
eu_decisions.start_date,
v.original_start_date_formatted,
eu_decisions.is_current,
eu_decisions.geo_entity_id,
eu_decisions.start_event_id,
eu_decisions.term_id,
eu_decisions.source_id,
eu_decisions.eu_decision_type_id,
eu_decisions.term_id,
eu_decisions.source_id,
eu_decisions.nomenclature_note_en,
eu_decisions.nomenclature_note_fr,
eu_decisions.nomenclature_note_es,
eu_decision_type,
srg_history,
start_event,
end_event,
geo_entity_en,
taxon_concept,
term_en,
source_en
}
current_user ? "#{string},\n private_url" : string
end

def sanitized_params
filters = permitted_params.inject({}) do |h, (k,v)|
h[k] = v.reject(&:empty?).map!(&:to_i)
h
end
filters
end
end
11 changes: 11 additions & 0 deletions app/controllers/api/v1/shipments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def over_time_query
render json: @over_time_data
end

# TODO refactor to merge this method and the over_time one above together
def aggregated_over_time_query
# TODO Remember to implement permitted parameters here
query = @grouping_class.new(sanitized_attributes, params)
@aggregated_over_time_data = Rails.cache.fetch(['aggregated_over_time_data', params], expires_in: 1.week) do
query.aggregated_over_time_data
end

render json: @aggregated_over_time_data
end

def download_data
@download_data = Rails.cache.fetch(['download_data', params], expires_in: 1.week) do
Trade::DownloadDataRetriever.dashboard_download(download_params).to_a
Expand Down
2 changes: 1 addition & 1 deletion app/models/api_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ApiRequest < ActiveRecord::Base
RESPONSE_STATUSES = [200, 400, 401, 404, 422, 500]
CONTROLLERS = ['taxon_concepts', 'distributions', 'cites_legislation', 'eu_legislation', 'references']

def self.top_5_most_active_users
def self.top_50_most_active_users
subquery = self.recent.select(
[
:user_id,
Expand Down
1 change: 1 addition & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Document < ActiveRecord::Base
foreign_key: 'primary_language_document_id',
dependent: :nullify
has_many :citations, class_name: 'DocumentCitation', dependent: :destroy
has_many :eu_opinions
has_and_belongs_to_many :tags, class_name: 'DocumentTag', join_table: 'document_tags_documents'
validates :title, presence: true
validates :date, presence: true
Expand Down
10 changes: 10 additions & 0 deletions app/models/eu_opinion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@
#

class EuOpinion < EuDecision
attr_accessible :document_id

belongs_to :document

validates :start_date, presence: true
validate :event_or_document_presence

def event_or_document_presence
return if start_event_id.nil? ^ document_id.nil?
errors.add(:base, "Select at least an Event or a Document, but not both")
end
end
45 changes: 45 additions & 0 deletions app/serializers/captive_breeding/eu_decision_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class CaptiveBreeding::EuDecisionSerializer < ActiveModel::Serializer
attributes :taxon_concept, :notes, { :start_date_formatted => :start_date },
:is_current, :nomenclature_note_en, :nomenclature_note_fr,
:nomenclature_note_es,
:eu_decision_type,
:srg_history,
:geo_entity,
:start_event,
:source,
:term,
{ :original_start_date_formatted => :original_start_date },
:private_url

def taxon_concept
object['taxon_concept']
end

def eu_decision_type
object['eu_decision_type']
end

def srg_history
object['srg_history']
end

def geo_entity
{ 'id'=> object['geo_entity_id'] }.merge(object['geo_entity_en'])
end

def start_event
object['start_event']
end

def source
object['source_en']
end

def term
object['term_en']
end

def private_url
scope.current_user ? object['private_url'] : nil
end
end
7 changes: 6 additions & 1 deletion app/serializers/species/eu_decision_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Species::EuDecisionSerializer < ActiveModel::Serializer
:source,
:term,
{ :original_start_date_formatted => :original_start_date },
:private_url
:private_url,
:intersessional_decision_id

def eu_decision_type
object['eu_decision_type']
Expand Down Expand Up @@ -38,4 +39,8 @@ def term
def private_url
scope.current_user ? object['private_url'] : nil
end

def intersessional_decision_id
scope.current_user ? object['intersessional_decision_id'] : nil
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def eu_decision_select_attrs
'[' || (taxon_concept->>'rank')::TEXT || ' decision <i>' || (taxon_concept->>'full_name')::TEXT || '</i>]'
END AS subspecies_info
}
scope.current_user ? "#{string},\n private_url" : string
scope.current_user ? "#{string},\n private_url, \n intersessional_decision_id" : string
end

def cites_listing_changes
Expand Down
12 changes: 11 additions & 1 deletion app/views/admin/eu_opinions/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
<div class="controls">
<%= f.select :start_event_id,
options_from_collection_for_select(@ec_srgs, :id, :name, @eu_opinion.start_event_id),
{ }, :class => "select2"
{ :include_blank => true }, :class => "select2"
%>
</div>
</div>

<div class="control-group">
<%= f.label :document_id, "Intersessional decision", :class => 'control-label' %>
<div class="controls">
<%= f.select :document_id,
options_from_collection_for_select(@documents, :id, :title, @eu_opinion.document_id),
{ :include_blank => true }, { :class => "select2", :style => 'width: 440px' }
%>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
resources :sources, :only => [:index]
resources :purposes, :only => [:index]
resources :trade_plus_filters, only: :index
resources :eu_decisions, only: :index
resources :documents do
collection do
get 'download_zip'
Expand All @@ -39,6 +40,7 @@
get '/shipments/chart' => 'shipments#chart_query'
get '/shipments/grouped' => 'shipments#grouped_query'
get '/shipments/over_time' => 'shipments#over_time_query'
get '/shipments/aggregated_over_time' => 'shipments#aggregated_over_time_query'
get '/shipments/country' => 'shipments#country_query'
get '/shipments/search' => 'shipments#search_query'
get '/shipments/download' => 'shipments#download_data'
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20220803152826_add_document_ref_to_eu_decisions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDocumentRefToEuDecisions < ActiveRecord::Migration
def change
add_reference :eu_decisions, :document, index: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddIntersessionalDecisionsToEuDecisionsView < ActiveRecord::Migration
def up
execute "DROP VIEW IF EXISTS api_eu_decisions_view"
execute "CREATE VIEW api_eu_decisions_view AS #{view_sql('20220808165846', 'api_eu_decisions_view')}"

execute "DROP VIEW IF EXISTS eu_decisions_view"
execute "CREATE VIEW eu_decisions_view AS #{view_sql('20220819120523', 'eu_decisions_view')}"
end

def down
execute "DROP VIEW IF EXISTS api_eu_decisions_view"
execute "CREATE VIEW api_eu_decisions_view AS #{view_sql('20200807121747', 'api_eu_decisions_view')}"

execute "DROP VIEW IF EXISTS eu_decisions_view"
execute "CREATE VIEW eu_decisions_view AS #{view_sql('20200514150717', 'eu_decisions_view')}"
end
end
Loading

0 comments on commit bbf2102

Please sign in to comment.