@ -28,7 +28,8 @@ class Image(models.Model):
owner_model = fields . Char (
owner_model = fields . Char (
required = True )
required = True )
storage = fields . Selection (
storage = fields . Selection (
[ ( ' url ' , ' URL ' ) , ( ' file ' , ' OS file ' ) , ( ' db ' , ' Database ' ) ] ,
[ ( ' url ' , ' URL ' ) , ( ' file ' , ' OS file ' ) , ( ' db ' , ' Database ' ) ,
( ' filestore ' , ' Filestore ' ) ] ,
required = True )
required = True )
name = fields . Char (
name = fields . Char (
' Image title ' ,
' Image title ' ,
@ -37,6 +38,9 @@ class Image(models.Model):
extension = fields . Char (
extension = fields . Char (
' File extension ' ,
' File extension ' ,
readonly = True )
readonly = True )
attachment_id = fields . Many2one (
' ir.attachment ' ,
string = ' Attachment ' )
file_db_store = fields . Binary (
file_db_store = fields . Binary (
' Image stored in database ' ,
' Image stored in database ' ,
filters = ' *.png,*.jpg,*.gif ' )
filters = ' *.png,*.jpg,*.gif ' )
@ -84,6 +88,10 @@ class Image(models.Model):
" default_owner_ %s " % f not in self . env . context
" default_owner_ %s " % f not in self . env . context
for f in ( " id " , " model " ) )
for f in ( " id " , " model " ) )
@api.multi
def _get_image_from_filestore ( self ) :
return self . attachment_id . datas
@api.multi
@api.multi
def _get_image_from_db ( self ) :
def _get_image_from_db ( self ) :
return self . file_db_store
return self . file_db_store
@ -157,6 +165,11 @@ class Image(models.Model):
self . name , self . extension = os . path . splitext ( self . filename )
self . name , self . extension = os . path . splitext ( self . filename )
self . name = self . _make_name_pretty ( self . name )
self . name = self . _make_name_pretty ( self . name )
@api.onchange ( ' attachment_id ' )
def _onchange_attachmend_id ( self ) :
if self . attachment_id :
self . name = self . attachment_id . res_name
@api.constrains ( ' storage ' , ' url ' )
@api.constrains ( ' storage ' , ' url ' )
def _check_url ( self ) :
def _check_url ( self ) :
if self . storage == ' url ' and not self . url :
if self . storage == ' url ' and not self . url :
@ -174,3 +187,9 @@ class Image(models.Model):
if self . storage == ' db ' and not self . file_db_store :
if self . storage == ' db ' and not self . file_db_store :
raise exceptions . ValidationError (
raise exceptions . ValidationError (
' You must provide an attached file for the image. ' )
' You must provide an attached file for the image. ' )
@api.constrains ( ' storage ' , ' attachment_id ' )
def _check_store ( self ) :
if self . storage == ' filestore ' and not self . attachment_id :
raise exceptions . ValidationError (
' You must provide an attachment for the image. ' )