OSDN Git Service

merge
[pettanr/pettanr.git] / app / models / original_picture.rb
index 0332fbd..5380713 100644 (file)
@@ -1,8 +1,10 @@
-class OriginalPicture < ActiveRecord::Base
+class OriginalPicture < Peta::Content
+  load_manifest
   belongs_to :artist
   belongs_to :original_picture_license_group
   has_one :resource_picture
   has_many :pictures
+  has_many :resource_picture_pictures
   
   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
   validates :width, :presence => true, :numericality => true, :natural_number => true
@@ -11,29 +13,23 @@ class OriginalPicture < ActiveRecord::Base
   validates :artist_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :md5, :presence => true, :length => {:minimum => 32, :maximum => 32}
   
-  def supply_default
+  scope :find_index, -> (operators) do 
+    where(artist_id: operators.artist.id)
   end
   
-  def overwrite ar
-    self.uploaded_at = Time.now
-    self.artist_id = ar.id
+  def supply_default
+    self.artist_id = nil
   end
   
-  def own? ar
-    if ar.is_a?(Author)
-      self.artist_id == ar.artist.id
-    elsif ar.is_a?(Artist)
-      self.artist_id == ar.id
-    else
-      false
-    end
+  def overwrite operators
+    self.uploaded_at = Time.now
+    return unless operators.artist
+    self.artist_id = operators.artist.id
   end
   
-  def visible? ar
-    return true if ar.is_a?(Admin)
-    self.own?(ar)
+  def visible? operators
+    self.own? operators
   end
-  
   def filename
     "#{self.id}.#{self.ext}"
   end
@@ -55,6 +51,19 @@ class OriginalPicture < ActiveRecord::Base
     {:src => self.url, :width => tw, :height => th}
   end
   
+  def symbol_option
+    self.tmb_opt_img_tag
+  end
+  
+  def filer_caption
+    self.revision
+  end
+  
+  def revision
+    head = self.history.first
+    head ? head.revision : 'unpublished'
+  end
+  
   def unpublished?
     self.published_at == nil and self.stopped_at == nil
   end
@@ -73,71 +82,14 @@ class OriginalPicture < ActiveRecord::Base
     self.published_at != nil
   end
   
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
-  end
-  
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
-  end
-  
-  def self.page_size prm = self.default_page_size
-    page_size = prm.to_i
-    page_size = self.max_page_size if page_size > self.max_page_size
-    page_size = self.default_page_size if page_size < 1
-    page_size
-  end
-  
-  def self.mylist artist_id, page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['original_pictures.artist_id = ?', artist_id], :order => 'original_pictures.updated_at desc'})
-    OriginalPicture.find(:all, opt)
-  end
-  
   def history 
-    Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
-  end
-  
-  def self.list_opt
-    {:include => {:resource_picture => {}, :pictures => {}}}
-  end
-  
-  def self.list_json_opt
-    {:include => {:resource_picture => {}, :pictures => {}}}
-  end
-  
-  def self.show cid, ar
-    opt = {}
-    opt.merge!(self.show_opt)
-    res = OriginalPicture.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(ar)
-    res
-  end
-  
-  def self.edit cid, ar
-    opt = {}
-    opt.merge!(self.show_opt)
-    res = OriginalPicture.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(ar)
-    res
+    Picture.where(original_picture_id: self.id).order(revision: :desc)
   end
   
   def self.show_opt
     {:include => {:resource_picture => {}, :pictures => {}}}
   end
   
-  def self.show_json_opt
-    {:include => {:resource_picture => {}, :pictures => {}}}
-  end
-  
   def store(imager)
     unless imager
       self.errors.add :base, I18n.t('errors.invalid_image')
@@ -164,15 +116,9 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def self.export(dt = nil)
-    opt = {}
-    cond = if dt
-      ['artists.author_id is not null and original_pictures.updated_at >= ?', dt]
-    else
-      'artists.author_id is not null'
-    end
-    opt.merge!({:conditions => cond}) 
-    opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'})
-    OriginalPicture.find(:all, opt)
+    original_pictures = OriginalPicture.includes(:resource_picture, :artist).where('artists.author_id is not null')
+    original_pictures = original_pictures.where(['original_pictures.updated_at >= ?', dt]) if dt
+    original_pictures.order('original_pictures.id').references(:artist)
   end
   
   def list_as_json_with_resource_picture
@@ -199,6 +145,11 @@ class OriginalPicture < ActiveRecord::Base
       self.pictures.each do |picture|
         res = picture.unpublish
         raise ActiveRecord::Rollback unless res
+        if picture.resource_picture_picture
+          picture.resource_picture_picture.original_picture_id = nil
+          picture.resource_picture_picture.resource_picture_id = nil
+          raise ActiveRecord::Rollback unless picture.resource_picture_picture.save
+        end
       end
       res = self.destroy
       raise ActiveRecord::Rollback unless res
@@ -206,4 +157,56 @@ class OriginalPicture < ActiveRecord::Base
     res
   end
   
+  def self.publish oid, lsname, attr
+    l = License.find_by(name: lsname)
+    op = OriginalPicture.find oid
+    lg = l.license_group
+    attr[:license_id] = l.id
+    
+    ctl = lg.module_name.pluralize + '::Attribute'
+    le = ctl.constantize.new attr
+    
+    rp = ResourcePicture.new
+    rp.attributes = le.resource_picture_attributes op
+    rp.overwrite op
+    
+    imager = PettanImager.load op.restore
+    rp.store imager
+  end
+  
+  def self.upload fn, auth
+    b = Base64.encode64(File.open(fn, 'rb').read)
+    u = 'http://localhost:3000/original_pictures'
+    r = RestClient.post(u, 
+      {:original_picture => {:file  => b}, :auth_token => auth}.to_json, 
+      :content_type => :json, :accept => :json
+    )
+    o = JSON.parse r
+    oid = o['id']
+    oid
+  end
+  
+  def self.auto_publish dirname, auth
+    Dir.glob File.expand_path(dirname) + '/*' do |filename|
+      if File.directory?(filename)
+        img = nil
+        lsname = nil
+        attr  = nil
+        Dir.glob(filename + '/*') do |fn|
+          ext = File.extname(fn).downcase
+          case ext
+          when '.json'
+            json = JSON.parse(File.open(fn).read)
+            lsname = json["license_name"]
+            attr = json["attributes"]
+          when '.png', '.gif', '.jpeg'
+            img = fn
+          end
+        end
+        oid = OriginalPicture.upload img, auth
+        OriginalPicture.publish oid, lsname, attr
+      end
+    end
+  end
+  
 end