Skip to content

Commit

Permalink
Merge branch 'export-custom-categories' of https://github.com/unepwcm…
Browse files Browse the repository at this point in the history
…c/ProtectedPlanet into export-custom-categories
  • Loading branch information
stanleypliu committed Aug 18, 2020
2 parents f7571ab + 32d1e1f commit ca61e85
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 37 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ group :development do
gem 'capistrano-git-with-submodules', '2.0.3'
gem 'capistrano-service'
gem 'awesome_print'
gem 'net-sftp'
# gem 'listen', '~> 3.1.5'
# gem 'spring-watcher-listen', '~> 2.0.0'
#
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,8 @@ GEM
thor (~> 0.19)
net-scp (2.0.0)
net-ssh (>= 2.6.5, < 6.0.0)
net-sftp (3.0.0)
net-ssh (>= 5.0.0, < 7.0.0)
net-ssh (5.2.0)
nio4r (2.5.2)
nokogiri (1.10.7)
Expand Down Expand Up @@ -1341,6 +1343,7 @@ DEPENDENCIES
minitest (~> 5.10, != 5.10.2)
mocha (~> 1.0.0)
neat
net-sftp
nokogiri (~> 1.10.4)
pg (~> 0.21)
phantompdf (~> 1.2.2)
Expand Down
81 changes: 52 additions & 29 deletions app/assets/stylesheets/components/_sticky.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,63 @@
$sticky-bar-background-color: $white;
$sticky-bar-padding: rem-calc(10);

@mixin sticky-bar-stuck {
@include box-shadow-grey-light;
width: 100%;

display: block;
position: fixed;
top: 0;
right: 0;
left: 0;
}

@mixin sticky-bar-wrapper {
background-color: $sticky-bar-background-color;
z-index: 1;
}

//----------------------------------------
// sticky bar
//----------------------------------------
.sticky-bar {
&--basic {
@include gutters;
width: 100%;

@include breakpoint($large) { padding: 0; }

.sticky-bar {
&__wrapper {
background-color: $sticky-bar-background-color;
padding-top: $sticky-bar-padding;
padding-bottom: $sticky-bar-padding;
z-index: 1;

&.sticky-bar--stuck {
@include box-shadow-grey-light;
width: 100%;

display: block;
position: fixed;
top: 0;
right: 0;
left: 0;

.sticky-bar__content { @include gutters; }
}
.sticky-bar--basic {
@include gutters;
width: 100%;

@include breakpoint($large) { padding: 0; }

.sticky-bar {
&__wrapper {
@include sticky-bar-wrapper;
padding-top: $sticky-bar-padding;
padding-bottom: $sticky-bar-padding;

&.sticky-bar--stuck {
@include sticky-bar-stuck;

.sticky-bar__content { @include gutters; }
}
}

&__content {
margin: 0 auto;
max-width: $site-width;
}
}
}

.sticky-bar--slide-in {
width: 100%;

.sticky-bar {
&__wrapper {
@include sticky-bar-wrapper;
top: rem-calc(-200);

&.sticky-bar--stuck {
@include sticky-bar-stuck;

&__content {
margin: 0 auto;
max-width: $site-width;
transition: top linear .25s;
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions app/assets/stylesheets/components/charts/_chart-legend.scss
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@
margin: rem-calc(14 0);

.chart__legend-li { margin-right: rem-calc(14); }
.chart__legend-item { @include chart-legend-item; }

.chart__legend-item {
@include chart-legend-item;
@include flex-v-center;
}

.chart__legend-index { @include chart-legend-index; }
.chart__legend-key { @include chart-legend-key; }

.chart__legend-key {
@include chart-legend-key;
@include breakpoint-down($small) {
width: rem-calc(16); height: rem-calc(16);
}
}
.chart__legend-title { line-height: 1; }
}

Expand Down
2 changes: 2 additions & 0 deletions app/views/green_list/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= render partial: "./partials/bars/topbar-secondary" %>

<%= render partial: "./layouts/partials/hero-thematic", locals: {
button_text: t('thematic_area.green_list.hero.button_text'),
button_url: search_areas_path(filters: @filters),
Expand Down
16 changes: 11 additions & 5 deletions app/views/partials/bars/_topbar-secondary.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div class="topbar--secondary">
<div class="topbar__container container">
<%= render "partials/social/sharing" %>
<sticky-bar
class="sticky-bar--slide-in"
trigger-element=".sm-trigger-sticky-bar"
>
<div class="topbar__container container">
<%= render "partials/social/sharing" %>

<%= render partial: "partials/download/download", locals: { classes: 'download--small' } %>
</div>
</div>
<%= render partial: "partials/download/download", locals: { classes: 'download--small' } %>
</div>
</sticky-bar>
</div>
<span class="sm-trigger-sticky-bar"></span>
2 changes: 1 addition & 1 deletion lib/modules/stats/country_statistics_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def import(iso3=nil)
jrc_terr_area: stat[TERR_AREA_ATTRIBUTE]
}
endpoints.each do |name|
attribute = STATISTICS_API[name][:attribute]
attribute = STATISTICS_API[name.to_sym][:attribute]
attr_name = "percentage_#{name}"

attrs[attr_name] = stat[attribute]
Expand Down
157 changes: 157 additions & 0 deletions lib/modules/sync_seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
class SyncSeeds
require 'net/ssh'
require 'net/scp'

def initialize(server, username)
@username = username
@server = server
end

def start_session
Net::SSH.start(@server, @username) do |session|
@session = session
yield
end
end

def list_local_files(location)
Dir.glob('*', base: location)
end

def list_remote_files(location)
@session.sftp.dir.glob(location, '*')
end

def delete_files(files, location)
files.each do |file|
puts "Removing #{file} as it no longer exists remotely"
FileUtils.rm_rf(File.join(location, file))
end
end

def files_for_deletion(local_list, remote_list, location)
files = local_list - remote_list

if files.empty?
puts "No files to delete"
else
delete_files(files, location)
end
end

def download_files(source, dest)
puts "Downloading #{source} as it's newer or not found locally"
return @session.scp.download!(source, dest) if File.file?(dest)
@session.scp.download!(source, dest, recursive: true)
end

def create_paths(relative_path)
{ local_path: File.join(@local_base, relative_path), remote_path: File.join(@remote_base, relative_path) }
end

def check_if_newer(parent_folder:, local_item:, remote_item:, remote_path:, local_path:, base: @local_base)
downloaded = false

if parent_folder.include?(local_item)
yield if block_given?

is_newer = Time.at(remote_item.attributes.mtime) >= File.stat(local_path).mtime
# If there are any outdated files, will trigger download
if is_newer
if Dir.glob('*', base: @local_base).include?(local_item)
download_files(remote_path, local_path)
# Will be hit if local_item is a file or folder inside a directory
elsif Dir.glob('**/*', base: base).include?(local_item)
download_files(remote_path, local_path)
downloaded = true
end
end
else
# Just download it if it doesn't exist at all
download_files(remote_path, @local_base)
downloaded = true
end

downloaded
end

def compare_folders(wildcard:, local:, remote:, base:)
puts "Checking to see what files need to be deleted from #{local}"

remote_list = @session.sftp.dir.glob(remote, wildcard).map do |f|
f.name.force_encoding('UTF-8')
end

local_list = Dir.glob(wildcard, base: local)

files_for_deletion(local_list, remote_list, base)
end

# When folders need to be checked recursively
def check_inside_folder(folder, local_list)
paths = create_paths(folder)

compare_folders(wildcard: '**/*', local: paths[:local_path], remote: paths[:remote_path], base: paths[:local_path])

local_folder_content = Dir.glob('**/*', base: paths[:local_path])
remote_folder_content = @session.sftp.dir.glob(paths[:remote_path], '**/*')

remote_folder_content.each do |file|
# Go through the various files and folders and check to see if they exist locally
local_file = file.name.force_encoding('UTF-8')

check = check_if_newer(
parent_folder: local_folder_content,
local_item: local_file,
remote_item: file,
remote_path: paths[:remote_path],
local_path: paths[:local_path],
base: paths[:local_path]
)

# We don't want to download the whole folder again if it's already been re-downloaded once
# Break out of loop if already downloaded
break if check == true
end
end

def main_task(local_list:, remote_list:, local_base:, remote_base:)
@local_base = local_base
@remote_base = remote_base

remote_list.each do |object|
# There are files with non-ASCII characters (i.e. accented) in the CMS files
name = object.name.force_encoding('UTF-8')
paths = create_paths(name)

check_if_newer(
parent_folder: local_list,
local_item: name,
remote_item: object,
remote_path: paths[:remote_path],
local_path: paths[:local_path]
) do
check_inside_folder(name, local_list) if object.attributes.directory?
end
end
end

# Piggybacks on existing Comfy modules
# TODO - the arguments used here for the site and folder
# (e.g. protectedplanet or protected-planet) can be different
# for different environments, need to change both this and the staging seeds rake task
def commence_comfy_import(answer)
logger = ComfortableMexicanSofa.logger
ComfortableMexicanSofa.logger = Logger.new(STDOUT)

if answer == 'all'
Rake::Task["comfy:cms_seeds:import"].invoke('protected-planet', 'protectedplanet')
else
module_name = "ComfortableMexicanSofa::Seeds::#{answer.singularize}::Importer".constantize
module_name.new('protected-planet', 'protectedplanet').import!
end

ComfortableMexicanSofa.logger = logger
end

end
Loading

0 comments on commit ca61e85

Please sign in to comment.