OSDN Git Service

t#31868:refact element form
[pettanr/pettanr.git] / app / models / original_picture.rb
index 7bdacca..28bf7b6 100644 (file)
@@ -15,22 +15,20 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def overwrite ar
+    self.uploaded_at = Time.now
     self.artist_id = ar.id
   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 own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    ar = OriginalPicture.get_artist_from_roles roles
+    return false unless ar
+    self.artist_id == ar.id
   end
   
-  def visible? ar
-    return true if ar.is_a?(Admin)
-    self.own?(ar)
+  def visible? roles
+    return true if self.admin_role_check roles
+    self.own?(roles)
   end
   
   def filename
@@ -55,19 +53,21 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def unpublished?
-    self.pictures.empty?
+    self.published_at == nil and self.stopped_at == nil
   end
   
   def stopped?
-    self.pictures.any? and self.resource_picture == nil
+    self.stopped_at != nil
   end
   
   def unlicensed?
-    self.pictures.any? and self.resource_picture and self.updated_at > self.pictures.first.head.updated_at
+    dt = self.published_at || self.stopped_at
+    return false unless dt
+    self.uploaded_at > dt
   end
   
   def published?
-    self.pictures.any? and self.resource_picture and self.updated_at < self.pictures.first.head.updated_at
+    self.published_at != nil
   end
   
   def self.default_page_size
@@ -91,31 +91,35 @@ class OriginalPicture < ActiveRecord::Base
     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)
+  def self.mylist_where ar
+    ['original_pictures.artist_id = ?', ar.id]
   end
   
-  def history 
-    Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
+  def self.mylist ar, page = 1, page_size = self.default_page_size
+    OriginalPicture.where(self.mylist_where(ar)).includes(OriginalPicture.list_opt).order('original_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist_paginate ar, page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(OriginalPicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:resource_picture => {}, :pictures => {}}}
+    {:resource_picture => {}, :pictures => {} }
   end
   
   def self.list_json_opt
     {:include => {:resource_picture => {}, :pictures => {}}}
   end
   
-  def self.show cid, ar
+  def history 
+    Picture.find(:all, {:conditions => ['pictures.original_picture_id = ?', self.id], :order => 'pictures.revision desc'} )
+  end
+  
+  def self.show cid, roles
     opt = {}
     opt.merge!(self.show_opt)
     res = OriginalPicture.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(ar)
+    raise ActiveRecord::Forbidden unless res.visible?(roles)
     res
   end
   
@@ -203,4 +207,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.classname.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