Skip to content
Snippets Groups Projects
Commit bb966a9e authored by Arnaud Levy's avatar Arnaud Levy
Browse files

only if useful

parent 025e6b50
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,7 @@ class Communication::Website::Imported::Medium < ApplicationRecord
escaped_source_url = Addressable::URI.parse(value['source_url']).display_uri.to_s
self.file_url = escaped_source_url
self.filename = File.basename(URI(escaped_source_url).path)
# TODO unify with page and post?
self.remote_created_at = DateTime.parse(value['date_gmt'])
self.remote_updated_at = DateTime.parse(value['modified_gmt'])
end
......
......@@ -58,6 +58,8 @@ class Communication::Website::Imported::Page < ApplicationRecord
self.content = value['content']['rendered']
self.parent = value['parent']
self.featured_medium = value['featured_media'] == 0 ? nil : website.media.find_by(identifier: value['featured_media'])
self.created_at = value['date_gmt']
self.updated_at = value['modified_gmt']
end
def to_s
......@@ -74,10 +76,14 @@ class Communication::Website::Imported::Page < ApplicationRecord
self.page.title = "Untitled"
self.page.save
end
# TODO only if not modified since import
# Don't touch if there are local changes (this would destroy some nice work)
return if page.updated_at > updated_at
# Don't touch if there are no remote changes (this would do useless server workload)
return if page.updated_at == updated_at
puts "Update page #{page.id}"
page.slug = slug
page.title = Wordpress.clean title.to_s
page.description = Wordpress.clean excerpt.to_s
page.description = ActionView::Base.full_sanitizer.sanitize excerpt.to_s
page.text = Wordpress.clean content.to_s
page.save
end
......
......@@ -57,7 +57,9 @@ class Communication::Website::Imported::Post < ApplicationRecord
self.title = value['title']['rendered']
self.excerpt = value['excerpt']['rendered']
self.content = value['content']['rendered']
self.published_at = value['date']
self.created_at = value['date_gmt']
self.updated_at = value['modified_gmt']
self.published_at = value['date_gmt']
self.featured_medium = website.media.find_by(identifier: value['featured_medium'])
end
......@@ -74,12 +76,18 @@ class Communication::Website::Imported::Post < ApplicationRecord
self.post.title = "Untitled" # No title yet
self.post.save
end
# TODO only if not modified since import
# Don't touch if there are local changes (this would destroy some nice work)
return if post.updated_at > updated_at
# Don't touch if there are no remote changes (this would do useless server workload)
return if post.updated_at == updated_at
title = Wordpress.clean title.to_s
puts "Update post #{post.id}"
post.title = title unless title.blank? # If there is no title, leave it with "Untitled"
post.slug = slug
post.description = Wordpress.clean excerpt.to_s
post.description = ActionView::Base.full_sanitizer.sanitize excerpt.to_s
post.text = Wordpress.clean content.to_s
post.created_at = created_at
post.updated_at = updated_at
post.published_at = published_at if published_at
post.save
end
......
......@@ -57,14 +57,14 @@ class Communication::Website::Imported::Website < ApplicationRecord
page.data = data
page.save
end
pages.find_each do |page|
# The order will treat parents before children
pages.order(:url).find_each do |page|
next if page.parent.blank?
parent = pages.where(identifier: page.parent).first
next if parent.nil?
generated_page = page.page
generated_page.parent = parent.page
generated_page.save
# TODO save children
end
end
......
......@@ -45,7 +45,6 @@ class Wordpress
posts = []
loop do
batch = load_paged url, page
puts "Load page #{page}"
break if batch.is_a?(Hash) || batch.empty?
posts += batch
page += 1
......@@ -54,6 +53,7 @@ class Wordpress
end
def load_paged(url, page)
puts "Load #{url } on page #{page}"
load_url "#{url}?page=#{page}&per_page=100"
end
......
......@@ -5,6 +5,7 @@ en:
imported:
from: Imported from
launch: Launch import
launched: Import in progress
media:
file_size: File size
not_imported_yet: Not imported yet
......
......@@ -5,6 +5,7 @@ fr:
imported:
from: Importé depuis
launch: Importer le site
launched: Importation en cours
media:
file_size: Taille du fichier
not_imported_yet: Non importé pour le moment
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment