Fix order bug and add index.

This commit is contained in:
BoHung Chiu 2022-12-04 00:26:47 +08:00
parent 980053c717
commit 82809c20cf
9 changed files with 142 additions and 96 deletions

View File

@ -73,7 +73,7 @@
color: #F2F2F2; color: #F2F2F2;
text-shadow: 0px -1px 0 rgba(0,0,0,0.4); text-shadow: 0px -1px 0 rgba(0,0,0,0.4);
letter-spacing: -0.5px; letter-spacing: -0.5px;
font-size: 30px; font-size: 2.5em;
font-family: 'Playfair Display SC', sans-serif; font-family: 'Playfair Display SC', sans-serif;
line-height: 28px; line-height: 28px;
z-index: 1; z-index: 1;
@ -220,7 +220,6 @@ div.rgbody{
display: inline-block; display: inline-block;
float: left; float: left;
color: #777; color: #777;
font-size: 11px;
line-height: 30px; line-height: 30px;
cursor: pointer; cursor: pointer;
width: 33%; width: 33%;
@ -382,7 +381,7 @@ div.rgbody{
top: 0; top: 0;
right: 0; right: 0;
margin: 0; margin: 0;
font-size: 23px; font-size: 1.92em;
opacity: 0; opacity: 0;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0); filter: alpha(opacity=0);
@ -422,7 +421,7 @@ div.rgbody{
text-align: right; text-align: right;
text-shadow: 0 1px 0 #ffffff; text-shadow: 0 1px 0 #ffffff;
letter-spacing: -0.1em; letter-spacing: -0.1em;
font-size: 12px; font-size: 1em;
font-family: 'Varela Round', sans-serif; font-family: 'Varela Round', sans-serif;
line-height: 40px; line-height: 40px;
} }
@ -477,7 +476,7 @@ div.rgbody{
} }
#fileupload #file-list { #fileupload #file-list {
position: relative; position: relative;
z-index: 1; z-index: 4;
height: 209px; height: 209px;
margin: 2px 0; margin: 2px 0;
} }
@ -542,13 +541,16 @@ div.rgbody{
.order-edit-notification{ .order-edit-notification{
background-color: #ffffd5; background-color: #ffffd5;
z-index: 10; z-index: 10;
display: none; display: none;
height: 25px; height: 25px;
margin-left: 40%; margin-left: 40%;
position: fixed; position: fixed;
text-align: center; text-align: center;
margin-top: 5px; margin-top: 5px;
top: 85px; top: 85px;
width: 250px; width: 250px;
font-size: 13px; font-size: 1.08em;
}
.bottomnav{
z-index: 4;
} }

View File

@ -275,11 +275,8 @@ class Admin::GalleriesController < OrbitAdminController
categories = @module_app.categories.enabled categories = @module_app.categories.enabled
@filter_fields = filter_fields(categories, @tags) @filter_fields = filter_fields(categories, @tags)
@filter_fields.delete(:status) @filter_fields.delete(:status)
albums1 = Album.where(:order.in => [nil,-1]).desc(:created_at).with_categories(filters("category")).with_tags(filters("tag")) albums = Album.all.asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
albums1 = search_data(albums1,[:name]) @fiter_albums = search_data(albums,[:name])
albums2 = Album.all.where(:order.ne=>-1).and(:order.ne=>nil).asc(:order).with_categories(filters("category")).with_tags(filters("tag"))
albums2 = search_data(albums2,[:name])
@fiter_albums = albums1.concat(albums2)
end end
def update_album_setting def update_album_setting
if params[:album_setting_limit] if params[:album_setting_limit]
@ -331,12 +328,10 @@ class Admin::GalleriesController < OrbitAdminController
def show def show
@album = Album.find(params[:id]) @album = Album.find(params[:id])
@images = @album.album_images.where(:order.in => [nil,-1]).desc(:created_at) @images = @album.album_images.asc(:order)
images = @album.album_images.where(:order.gt => -1).asc(:order)
@images = @images.concat(images)
image_content = [] image_content = []
@images.each do |image| @images.each do |image|
image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}} image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&:to_s)}
end end
@tags = @module_app.tags @tags = @module_app.tags
@photos_data = {"galleries" => image_content}.to_json @photos_data = {"galleries" => image_content}.to_json
@ -414,12 +409,10 @@ class Admin::GalleriesController < OrbitAdminController
def get_photoData_json def get_photoData_json
@album = Album.find(params[:id]) @album = Album.find(params[:id])
@images = @album.album_images.where(:order => [nil,-1]).desc(:created_at) @images = @album.album_images.asc(:order)
images = @album.album_images.where(:order.gt => -1).asc(:order)
@images = @images.concat(images)
image_content = [] image_content = []
@images.each do |image| @images.each do |image|
image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}} image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&:to_s)}
end end
render :json=>{"galleries" => image_content}.to_json render :json=>{"galleries" => image_content}.to_json
end end
@ -580,10 +573,14 @@ class Admin::GalleriesController < OrbitAdminController
def new_images def new_images
@album = Album.find(params[:album_id]) @album = Album.find(params[:album_id])
if params[:last_image_id].present? if params[:last_image_id].present?
lastimage = AlbumImage.find(params[:last_image_id]) lastimage = AlbumImage.find(params[:last_image_id]) rescue nil
@newimages = @album.album_images.where(:created_at.gt => lastimage.created_at,:order => -1).desc(:created_at) if lastimage
@newimages = @album.album_images.where(:order.lt=>lastimage.order).desc(:created_at)
else
@newimages = @album.album_images.where(:id.gt=>params[:last_image_id]).desc(:created_at)
end
else else
@newimages = @album.album_images.where(:order => [nil,-1]).desc(:created_at) @newimages = @album.album_images.desc(:created_at)
end end
render :layout=>false render :layout=>false
end end
@ -604,7 +601,7 @@ class Admin::GalleriesController < OrbitAdminController
tagsToDestroy = [] tagsToDestroy = []
tagsToAdd = [] tagsToAdd = []
new_tags = params[:album][:tags] || [] new_tags = params[:album][:tags] || []
old_tags = album.tags.collect{|t| t.id.to_s} old_tags = album.tag_ids.map(&:to_s)
old_tags.each do |tag| old_tags.each do |tag|
if !new_tags.include?(tag) if !new_tags.include?(tag)
tagsToDestroy << tag tagsToDestroy << tag
@ -627,7 +624,7 @@ class Admin::GalleriesController < OrbitAdminController
# tagsToAdd will contain all tag ids which ve to be added in tall album_images # tagsToAdd will contain all tag ids which ve to be added in tall album_images
images = album.album_images images = album.album_images
images.each do |image| images.each do |image|
image_tags = image.tags.collect{|t| t.id.to_s} image_tags = image.tag_ids.map(&:to_s)
tagsToAdd.each do |tag| tagsToAdd.each do |tag|
image_tags << tag image_tags << tag
end end

View File

@ -24,23 +24,28 @@ class Admin::ImagesController < OrbitAdminController
def changeorder def changeorder
type = params[:type] type = params[:type]
if type == "imgholder" if type == "imgholder"
album = Album.find(params[:album_id]) rescue nil
images = params[:imageids] images = params[:imageids]
images.each_with_index do |image, i| images.each_with_index.reverse_each do |image, i|
img = AlbumImage.find(image) rescue nil img = AlbumImage.find(image) rescue nil
if !img.nil? if !img.nil?
img.order = i img.order = i
img.save img.save
end end
end end
if album
album.update(:min_order=>-1)
end
elsif type == "orbit_gallery" elsif type == "orbit_gallery"
albums = params[:imageids] albums = params[:imageids]
albums.each_with_index do |albumid, i| albums.each_with_index.reverse_each do |albumid, i|
album = Album.find(albumid) rescue nil album = Album.find(albumid) rescue nil
if !album.nil? if !album.nil?
album.order = i album.order = i
album.save album.save
end end
end end
AlbumSetting.update_all(:min_order=>-1)
end end
render :json => {"success" => true}.to_json render :json => {"success" => true}.to_json
end end
@ -49,12 +54,12 @@ class Admin::ImagesController < OrbitAdminController
images = params['images'] images = params['images']
images.each do |image| images.each do |image|
img = AlbumImage.find(image) img = AlbumImage.find(image)
img.destroy
begin begin
FileUtils.rm_rf(File.dirname(img.file.path)) FileUtils.rm_rf(File.dirname(img.file.path))
rescue => e rescue => e
puts ["can't delete",e] puts ["can't delete",e]
end end
img.delete
end end
if params['delete_cover'] == "true" if params['delete_cover'] == "true"
album = Album.find(params['album_id']) album = Album.find(params['album_id'])

View File

@ -37,33 +37,10 @@ class GalleriesController < ApplicationController
def index def index
@album_setting = AlbumSetting.first @album_setting = AlbumSetting.first
params = OrbitHelper.params page_no = OrbitHelper.page_number
album_tp = Album.filter_by_categories.filter_by_tags data_count = OrbitHelper.page_data_count
all_count = album_tp.count albums = Album.filter_by_categories.filter_by_tags.asc(:order).page(page_no).per(data_count)
page_data_count = OrbitHelper.page_data_count album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
no_order_count = album_tp.where(:order.in=>[-1,nil]).count
with_order_count = all_count - no_order_count
with_order_total_pages = (with_order_count.to_f / page_data_count).ceil
albums_with_order = album_tp.asc(:order).where(:order.ne=>-1).and(:order.ne=>nil).to_a rescue []
page_no = (params[:page_no] || 1).to_i
if page_no < with_order_total_pages
albums_no_order = []
elsif page_no == with_order_total_pages
if albums_with_order.count == page_data_count
albums_no_order = []
else
albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[0...(page_data_count - albums_with_order.count)]
end
else
albums_with_order = []
start_index = page_data_count*(page_no - 1 - with_order_total_pages)
if with_order_count != 0
start_index += (page_data_count - (with_order_count % page_data_count))
end
albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[start_index...(start_index+page_data_count)].to_a
end
albums = albums_with_order.concat(albums_no_order)
album_color_map = AlbumColor.where(:album_id.in=> albums.map{|v| v.id}).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
galleries = albums.collect do |a| galleries = albums.collect do |a|
doc = Nokogiri::HTML(a.description.to_s) doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text alt_text = doc.text.empty? ? 'gallery image' : doc.text
@ -86,7 +63,7 @@ class GalleriesController < ApplicationController
{ {
"albums" => galleries, "albums" => galleries,
"extras" => {"widget-title"=>"Gallery"}, "extras" => {"widget-title"=>"Gallery"},
"total_pages" => album_tp.total_pages "total_pages" => albums.total_pages
} }
end end
def show def show
@ -132,8 +109,8 @@ class GalleriesController < ApplicationController
@album_setting = AlbumSetting.first @album_setting = AlbumSetting.first
params = OrbitHelper.params params = OrbitHelper.params
tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags
albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags).to_a albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags)
album_color_map = AlbumColor.where(:album_id.in=> albums.map{|v| v.id}).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
galleries = albums.collect.with_index do |a,i| galleries = albums.collect.with_index do |a,i|
doc = Nokogiri::HTML(a.description.to_s) doc = Nokogiri::HTML(a.description.to_s)
alt_text = doc.text.empty? ? 'gallery image' : doc.text.strip alt_text = doc.text.empty? ? 'gallery image' : doc.text.strip
@ -178,7 +155,7 @@ class GalleriesController < ApplicationController
album_color_map = AlbumColor.where(:album_id.in=> album_ids).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h album_color_map = AlbumColor.where(:album_id.in=> album_ids).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h
params = OrbitHelper.params params = OrbitHelper.params
counts = OrbitHelper.widget_data_count counts = OrbitHelper.widget_data_count
images = AlbumImage.where({album_id:{"$in"=>album_ids}}).desc(:id).limit(counts *5).sample(counts) images = AlbumImage.where(:album_id.in=>album_ids).desc(:id).limit(counts *5).sample(counts)
images = images.each_with_index.collect do |a,i| images = images.each_with_index.collect do |a,i|
colors = album_color_map[a.album_id] || [] colors = album_color_map[a.album_id] || []
alt_text = (a.description.blank? ? "gallery image" : Nokogiri::HTML(a.description).text().strip) alt_text = (a.description.blank? ? "gallery image" : Nokogiri::HTML(a.description).text().strip)
@ -208,7 +185,6 @@ class GalleriesController < ApplicationController
end end
def imgs(album_id) def imgs(album_id)
album = Album.find(album_id) album = Album.find(album_id)
tag_names = Array.new
images = album.album_images.asc(:order) images = album.album_images.asc(:order)
output = Array.new output = Array.new
images.each do |values| images.each do |values|

View File

@ -13,20 +13,29 @@ class Album
field :uid, type: String field :uid, type: String
field :rss2_id, type: String field :rss2_id, type: String
field :order, type: Integer, default: -1 field :order, type: Integer, default: -1
field :min_order, type: Integer, default: -1
field :resize_gravity field :resize_gravity
# has_and_belongs_to_many :tags, :class_name => "GalleryTag" # has_and_belongs_to_many :tags, :class_name => "GalleryTag"
has_many :album_images, :autosave => true, :dependent => :destroy has_many :album_images, :autosave => true, :dependent => :destroy
has_many :album_colors, :autosave => true, :dependent => :destroy has_many :album_colors, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :album_images, :allow_destroy => true accepts_nested_attributes_for :album_images, :allow_destroy => true
accepts_nested_attributes_for :album_colors, :allow_destroy => true accepts_nested_attributes_for :album_colors, :allow_destroy => true
before_save do |record| index({"order" => 1}, { unique: false, background: false })
if record.order.nil? || record.order == -1 before_create do |record|
topest_order = Album.all.pluck(:order).sort{|a,b| a.to_i<=>b.to_i}.first topest_order = AlbumSetting.pluck(:min_order).first.to_i
if topest_order.nil? || topest_order == 0 if topest_order.nil? || topest_order == 0
record.order = -2 tmp_order = -2
else else
record.order = topest_order - 1 tmp_order = topest_order - 1
end end
record.order = tmp_order
AlbumSetting.update_all(:min_order=>tmp_order)
true
end
after_destroy do
min_order = AlbumSetting.pluck(:min_order).first
if self.order == min_order
AlbumSetting.update_all(:min_order=>min_order + 1)
end end
end end
def resize_gravity def resize_gravity

View File

@ -3,14 +3,40 @@ class AlbumImage
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include OrbitTag::Taggable include OrbitTag::Taggable
mount_uploader :file, GalleryUploader mount_uploader :file, GalleryUploader
field :title field :title
field :title_translations, type: Hash, default: {} field :title_translations, type: Hash, default: {}
field :description, type: String, localize: true, default: "" field :description, type: String, localize: true, default: ""
field :rss2_id, type: String field :rss2_id, type: String
field :order, type: Integer, default: -1 field :order, type: Integer, default: -1
belongs_to :album
# has_and_belongs_to_many :tags, :class_name => "GalleryTag" has_many :album_crops, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :album_crops, :allow_destroy => true
# has_and_belongs_to_many :tags, :class_name => "GalleryTag"
index({"order" => 1}, { unique: false, background: false })
before_create do
tmp_album = self.album
min_order = nil
if tmp_album
min_order = tmp_album.min_order
min_order = -1 if min_order == 0
end
if min_order.nil?
self.order = -Time.now.to_i
else
tmp_order = min_order - 1
self.order = tmp_order
Album.where(:id=>tmp_album.id).update_all(:min_order=>tmp_order) #present call save callback twice due to autosave
end
true
end
after_destroy do
min_order = Album.where(:id=>self.album_id).pluck(:min_order).first
puts "min_order: #{min_order} #{self.order}"
if self.order == min_order
Album.where(:id=>self.album_id).update_all(:min_order=>min_order + 1)
end
end
def title_translations def title_translations
tmp = super || {} tmp = super || {}
if tmp == {} if tmp == {}
@ -21,7 +47,4 @@ class AlbumImage
def title def title
self.title_translations[I18n.locale] self.title_translations[I18n.locale]
end end
belongs_to :album
has_many :album_crops, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :album_crops, :allow_destroy => true
end end

View File

@ -6,4 +6,5 @@ class AlbumSetting
ResizeGravities = %w[Center NorthWest North NorthEast West East SouthWest South SouthEast] ResizeGravities = %w[Center NorthWest North NorthEast West East SouthWest South SouthEast]
field :album_card_background_color, default: '' field :album_card_background_color, default: ''
field :album_card_text_color, default: '#000000' field :album_card_text_color, default: '#000000'
field :min_order, type: Integer, default: -1
end end

View File

@ -1,4 +1,48 @@
Rails.application.routes.draw do Rails.application.routes.draw do
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
if AlbumSetting.count == 0
album_setting = AlbumSetting.create
else
album_settings = AlbumSetting.all.to_a
album_setting = album_settings.first
if album_settings.count > 1
album_settings[1..-1].each do |album_setting|
album_setting.destroy
end
end
end
s = Site.first
update_flag = s.respond_to?(:tmp_flags)
need_update = !update_flag || !(s.tmp_flags.include?('ga1'))
if need_update
albums1 = Album.where(:order.in => [nil,-1]).desc(:created_at)
albums2 = Album.all.where(:order.ne=>-1).and(:order.ne=>nil).asc(:order)
fiter_albums = albums1.concat(albums2)
fiter_albums.each_with_index do |album, i|
album.order = i
album.min_order = -1
album.save
images1 = album.album_images.where(:order.in => [nil,-1]).desc(:created_at).to_a
images2 = album.album_images.where(:order.gt => -1).asc(:order).to_a
images = images1.concat(images2)
images.each_with_index do |image, j|
image.order = i
image.save
end
end
AlbumSetting.update_all(:min_order=>-1)
if update_flag
s = Site.first
s.tmp_flags << 'ga1'
s.save
end
puts "Gallery order fix!"
end
Album.create_indexes
AlbumImage.create_indexes
end
end
locales = Site.first.in_use_locales rescue I18n.available_locales locales = Site.first.in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
get "/xhr/galleries/theater/:id" => "galleries#theater" get "/xhr/galleries/theater/:id" => "galleries#theater"

View File

@ -21,17 +21,6 @@ module Gallery
puts ['error in gallery',e] puts ['error in gallery',e]
end end
require File.expand_path('../../../app/models/album_setting', __FILE__)
if defined?(AlbumSetting)
if AlbumSetting.first.nil?
AlbumSetting.create()
elsif AlbumSetting.count > 1
AlbumSetting.all.to_a[1..-1].each do |album_setting|
album_setting.destroy
end
end
end
OrbitApp.registration "Gallery", :type => "ModuleApp" do OrbitApp.registration "Gallery", :type => "ModuleApp" do
module_label "gallery.gallery" module_label "gallery.gallery"
base_url File.expand_path File.dirname(__FILE__) base_url File.expand_path File.dirname(__FILE__)