From 48f1ee840290adcf6b0cdd58ca4bf32d20516a37 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Fri, 8 Oct 2021 10:51:39 +0200 Subject: [PATCH] wordpress page import --- .../communication/websites_controller.rb | 11 +++++++ app/models/communication/website.rb | 14 ++++++++ .../communication/website/imported/page.rb | 32 +++++++++++++++++-- .../communication/website/imported/website.rb | 26 +++++++++++++++ app/models/communication/website/page.rb | 9 ++++-- .../communication/websites/import.html.erb | 11 +++++++ .../communication/websites/show.html.erb | 10 ++++++ config/locales/communication/en.yml | 10 ++++++ config/locales/communication/fr.yml | 10 ++++++ config/routes/communication.rb | 4 +++ ..._to_communication_website_imported_page.rb | 7 ++++ db/schema.rb | 5 ++- .../communication/website/imported/pages.yml | 4 ++- .../website/imported/websites.yml | 1 - .../website/imported/page_test.rb | 3 ++ 15 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 app/views/admin/communication/websites/import.html.erb create mode 100644 db/migrate/20211008082521_add_fields_to_communication_website_imported_page.rb diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index 6688c7eec..81ada3583 100644 --- a/app/controllers/admin/communication/websites_controller.rb +++ b/app/controllers/admin/communication/websites_controller.rb @@ -14,6 +14,17 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati breadcrumb end + def import + if request.post? + @website.import! + flash[:notice] = t('communication.website.imported.launched') + end + @imported_website = @website.imported_website + @imported_pages = @imported_website.pages + breadcrumb + add_breadcrumb Communication::Website::Imported::Website.model_name.human + end + def edit breadcrumb add_breadcrumb t('edit') diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index 430a7ca63..77a6fc3b3 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -38,6 +38,20 @@ class Communication::Website < ApplicationRecord "https://#{ domain }" end + def import! + unless imported? + self.imported_website = Communication::Website::Imported::Website.where(website: self, university: university) + .first_or_create + + end + imported_website.run! + imported_website + end + + def imported? + !imported_website.nil? + end + def to_s "#{name}" end diff --git a/app/models/communication/website/imported/page.rb b/app/models/communication/website/imported/page.rb index 9a9c3f2bc..dce8b9c42 100644 --- a/app/models/communication/website/imported/page.rb +++ b/app/models/communication/website/imported/page.rb @@ -3,7 +3,10 @@ # Table name: communication_website_imported_pages # # id :uuid not null, primary key +# content :text +# path :text # status :integer default(0) +# title :string # created_at :datetime not null # updated_at :datetime not null # page_id :uuid not null @@ -24,6 +27,31 @@ # class Communication::Website::Imported::Page < ApplicationRecord belongs_to :university - belongs_to :website, class_name: 'Communication::Website::Imported::Website' - belongs_to :page, class_name: 'Communication::Website::Page' + belongs_to :website, + class_name: 'Communication::Website::Imported::Website' + belongs_to :page, + class_name: 'Communication::Website::Page', + optional: true + + before_validation :sync_page + + def to_s + "#{title}" + end + + protected + + def sync_page + if page.nil? + self.page = Communication::Website::Page.new university: university, + website: website.website, # Real website, not imported website + slug: path + self.page.title = "TMP" + self.page.save + end + # TODO only if not modified + page.title = title.to_s + page.text = content.to_s + page.save + end end diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index 8b5a8b941..572819e46 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -23,4 +23,30 @@ class Communication::Website::Imported::Website < ApplicationRecord belongs_to :university belongs_to :website, class_name: 'Communication::Website' has_many :pages, class_name: 'Communication::Website::Imported::Page' + + def run! + load("#{website.domain_url}/wp-json/wp/v2/pages").each do |hash| + url = hash['link'] + path = URI(url).path + title = hash['title']['rendered'] + content = hash['content']['rendered'] + page = pages.where(university: university, path: path).first_or_create + page.title = title + page.content = content + page.save + end + end + + protected + + def load(url) + uri = URI(url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + # IUT Bordeaux Montaigne pb with certificate + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + JSON.parse(response.body) + end end diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index fe2dbca5b..b636daf75 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -34,8 +34,11 @@ class Communication::Website::Page < ApplicationRecord belongs_to :university - belongs_to :website, foreign_key: :communication_website_id - belongs_to :parent, class_name: 'Communication::Website::Page', optional: true + belongs_to :website, + foreign_key: :communication_website_id + belongs_to :parent, + class_name: 'Communication::Website::Page', + optional: true validates :title, presence: true @@ -68,7 +71,7 @@ class Communication::Website::Page < ApplicationRecord end def make_path - self.path = "#{parent&.path}/#{slug}" + self.path = "#{parent&.path}/#{slug}".gsub('//', '/') end def publish_to_github diff --git a/app/views/admin/communication/websites/import.html.erb b/app/views/admin/communication/websites/import.html.erb new file mode 100644 index 000000000..06e88a6fb --- /dev/null +++ b/app/views/admin/communication/websites/import.html.erb @@ -0,0 +1,11 @@ +<% content_for :title, @website %> +<% content_for :buttons do %> + <%= link_to t('communication.website.imported.refresh'), + import_admin_communication_website_path, + method: :post, + class: button_classes %> +<% end %> + +<% @imported_pages.each do |page| %> + <%= page %> +<% end %> diff --git a/app/views/admin/communication/websites/show.html.erb b/app/views/admin/communication/websites/show.html.erb index 098fff183..7388fad41 100644 --- a/app/views/admin/communication/websites/show.html.erb +++ b/app/views/admin/communication/websites/show.html.erb @@ -21,5 +21,15 @@ <%= render 'admin/communication/website/pages/list', pages: @website.pages.recent %> <% content_for :buttons do %> + <% if @website.imported? %> + <%= link_to t('communication.website.imported.show'), + import_admin_communication_website_path, + class: button_classes %> + <% else %> + <%= link_to t('communication.website.imported.launch'), + import_admin_communication_website_path, + method: :post, + class: button_classes %> + <% end %> <%= edit_link @website %> <% end %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index 1952f0ef8..91b44b6c0 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -1,4 +1,11 @@ en: + communication: + website: + imported: + launch: Launch import + refresh: Refresh import + show: Show import + pending: Import in progress activemodel: models: communication: Communication @@ -10,6 +17,9 @@ en: communication/website/page: one: Page other: Pages + communication/website/imported/website: + one: Imported website + other: Imported websites attributes: communication/website: name: Name diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index 1f549e2d5..2c2bd1642 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -1,4 +1,11 @@ fr: + communication: + website: + imported: + launch: Importer le site + refresh: Relancer l'import + show: Voir l'import + pending: Import en cours activemodel: models: communication: Communication @@ -10,6 +17,9 @@ fr: communication/website/page: one: Page other: Pages + communication/website/imported/website: + one: Site importé + other: Sites importés attributes: communication/website: name: Nom diff --git a/config/routes/communication.rb b/config/routes/communication.rb index 2568a3265..038649e63 100644 --- a/config/routes/communication.rb +++ b/config/routes/communication.rb @@ -1,5 +1,9 @@ namespace :communication do resources :websites do resources :pages, controller: 'website/pages' + member do + get :import + post :import + end end end diff --git a/db/migrate/20211008082521_add_fields_to_communication_website_imported_page.rb b/db/migrate/20211008082521_add_fields_to_communication_website_imported_page.rb new file mode 100644 index 000000000..092273e6d --- /dev/null +++ b/db/migrate/20211008082521_add_fields_to_communication_website_imported_page.rb @@ -0,0 +1,7 @@ +class AddFieldsToCommunicationWebsiteImportedPage < ActiveRecord::Migration[6.1] + def change + add_column :communication_website_imported_pages, :title, :string + add_column :communication_website_imported_pages, :content, :text + add_column :communication_website_imported_pages, :path, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 285d783a7..0ba9f4714 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_10_07_144729) do +ActiveRecord::Schema.define(version: 2021_10_08_082521) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -73,6 +73,9 @@ ActiveRecord::Schema.define(version: 2021_10_07_144729) do t.integer "status", default: 0 t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "title" + t.text "content" + t.text "path" t.index ["page_id"], name: "index_communication_website_imported_pages_on_page_id" t.index ["university_id"], name: "index_communication_website_imported_pages_on_university_id" t.index ["website_id"], name: "index_communication_website_imported_pages_on_website_id" diff --git a/test/fixtures/communication/website/imported/pages.yml b/test/fixtures/communication/website/imported/pages.yml index 2a4031ac1..12d755878 100644 --- a/test/fixtures/communication/website/imported/pages.yml +++ b/test/fixtures/communication/website/imported/pages.yml @@ -3,7 +3,10 @@ # Table name: communication_website_imported_pages # # id :uuid not null, primary key +# content :text +# path :text # status :integer default(0) +# title :string # created_at :datetime not null # updated_at :datetime not null # page_id :uuid not null @@ -22,7 +25,6 @@ # fk_rails_... (university_id => universities.id) # fk_rails_... (website_id => communication_website_imported_websites.id) # -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: university: one diff --git a/test/fixtures/communication/website/imported/websites.yml b/test/fixtures/communication/website/imported/websites.yml index 2d60279b3..947c19ae2 100644 --- a/test/fixtures/communication/website/imported/websites.yml +++ b/test/fixtures/communication/website/imported/websites.yml @@ -19,7 +19,6 @@ # fk_rails_... (university_id => universities.id) # fk_rails_... (website_id => communication_websites.id) # -# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: university: one diff --git a/test/models/communication/website/imported/page_test.rb b/test/models/communication/website/imported/page_test.rb index ee56cdab4..57ed3104c 100644 --- a/test/models/communication/website/imported/page_test.rb +++ b/test/models/communication/website/imported/page_test.rb @@ -3,7 +3,10 @@ # Table name: communication_website_imported_pages # # id :uuid not null, primary key +# content :text +# path :text # status :integer default(0) +# title :string # created_at :datetime not null # updated_at :datetime not null # page_id :uuid not null -- GitLab