diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb index 6688c7eec03b7a4a40feae9c6c85cfddf5cb7762..81ada3583a6a17a1815561e4720f1db53ebf6fc9 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 430a7ca6312e781d6a12179fdfe048acd71d27b9..77a6fc3b39f549115d6868b42cd1588889c94cb0 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 9a9c3f2bc993160eb2b1deabcc3f5dc79cc52f36..dce8b9c4207bc28b9dc43b021a902d05aa108b31 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 8b5a8b941dc615e1f5566fde8e1ad80940b085fb..572819e46e1851f21066002c8c5a68e0284b70f2 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 fe2dbca5b7431bf89f5f571e9fca5743479fa4dc..b636daf75acab65b93a8a2112e5d162677a8bdfa 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 0000000000000000000000000000000000000000..06e88a6fbf46d6568b153afa44b067afae4cde51 --- /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 098fff18349df342ae21221d75df270865a61400..7388fad413eab0eae30685a2cd5a910ddef0b115 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 1952f0ef84a3bce22cf88a4ca3e5fd62366cf313..91b44b6c05c0e127bc1053e6eeb199b5a129903a 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 1f549e2d5122e4290f1486da26be15fc5cb61415..2c2bd16424a7e3f65d966f36d39d432cc25c70fa 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 2568a32658cec2be3037135d97d46121decde9b4..038649e631b86d0420ae8fd777c6489ecf423a60 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 0000000000000000000000000000000000000000..092273e6de3e273f5904c3b6c6eaf730eed1a4db --- /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 285d783a704bf1ea6b263f1fbffdc33566bdc1cc..0ba9f4714819726d39776f8fcf390742d09ea702 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 2a4031ac1ecadf74cdfe87aa419972832dc88d7c..12d755878815ac0595d425f283722fe93278d70c 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 2d60279b34f9e325e495cee9232a85b7ca48d695..947c19ae200c0a97c4b54c67a74cf5cd73d82c1a 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 ee56cdab46a9825a18bc6659a4c442838a43f253..57ed3104c0ebf3605fe69cc9727ee367836a4548 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