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

wordpress page import

parent b24c18d7
No related branches found
No related tags found
No related merge requests found
Showing
with 149 additions and 8 deletions
......@@ -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')
......
......@@ -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
......
......@@ -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
......@@ -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
......@@ -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
......
<% 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 %>
......@@ -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 %>
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
......
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
......
namespace :communication do
resources :websites do
resources :pages, controller: 'website/pages'
member do
get :import
post :import
end
end
end
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
......@@ -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"
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
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