importosfromdatetimeimportdatetimefromdjango.dbimportmodelsfromdjango.confimportsettingsfromdjango.utils.translationimportugettext_lazyas_fromimagekit.modelsimportImageSpecfromimagekit.processorsimportAnchor,ResizeToFill,ResizeToFit,Transposefrommptt.fieldsimportTreeForeignKeyfrommptt.modelsimportMPTTModelfromgaleriaimportEXIFDISPLAY_IMAGE_PROCESSORS=getattr(settings,'GALERIA_DISPLAY_IMAGE_PROCESSORS',[Transpose(Transpose.AUTO),ResizeToFit(width=600,height=600)])THUMBNAIL_IMAGE_PROCESSORS=getattr(settings,'GALERIA_THUMBNAIL_IMAGE_PROCESSORS',[Transpose(Transpose.AUTO),ResizeToFill(width=128,height=128,anchor=Anchor.CENTER)])COVER_IMAGE_PROCESSORS=getattr(settings,'GALERIA_COVER_IMAGE_PROCESSORS',[Transpose(Transpose.AUTO),ResizeToFill(width=128,height=128,anchor=Anchor.CENTER)])ORDER_CHOICES=(('-date_added',_('Descending by addition date')),('date_added',_('Ascending by addition date')),('-date_taken',_('Descending by date taken')),('date_daten',_('Ascending by date taken')),('-date_modified',_('Descending by modification date')),('date_modified',_('Ascending by modification date')),)classAlbumManager(models.Manager):use_for_related_fields=Truedefpublic(self):returnsuper(AlbumManager,self).filter(is_public=True)classAlbum(MPTTModel):title=models.CharField(_('title'),max_length=256)slug=models.SlugField(_('slug'),max_length=256,help_text=_('Automatically built from the title. A slug is a short ''label generally used in URLs.'),)description=models.TextField(_('description'),blank=True)is_public=models.BooleanField(_('is public'),default=True,help_text=_('Only public albums will be displayed in the default views.'))order=models.CharField(_('order'),max_length=16,choices=ORDER_CHOICES,default='-date_added',help_text=_('The default order of pictures for this album.'))parent=models.ForeignKey('self',null=True,blank=True,verbose_name=_('parent album'),related_name='children')cover=models.ForeignKey('galeria.Picture',verbose_name=_('cover'),related_name='cover',null=True,blank=True,on_delete=models.SET_NULL)date_added=models.DateTimeField(_('date added'),auto_now_add=True)objects=AlbumManager()classMeta:unique_together=(('slug','parent'),)verbose_name=_('album')verbose_name_plural=_('albums')def__unicode__(self):returnunicode(self.title)@models.permalinkdefget_absolute_url(self):return('galeria-album',None,{'slug':str(self.slug)})@propertydefavailable_cover(self):ifself.cover:returnself.coverelifself.parent:returnself.parent.available_coverelse:returnNone@propertydefordered_pictures(self):returnself.pictures.order_by(self.order)defget_album_tree(self):tree=[self.slug]ifself.parent:tree=self.parent.get_album_tree()+treereturntreedefpicture_imagefield_path(instance,filename):album_tree='/'.join(instance.album.get_album_tree())returnos.path.join('galeria',album_tree,filename)classPictureManager(models.Manager):use_for_related_fields=Truedefpublic(self):returnsuper(PictureManager,self).filter(is_public=True)classPicture(models.Model):title=models.CharField(_('title'),max_length=256)slug=models.SlugField(_('slug'),max_length=256,help_text=_('Automatically built from the title. A slug is a short ''label generally used in URLs.'),)date_added=models.DateTimeField(_('date added'),auto_now_add=True)date_modified=models.DateTimeField(_('date modified'),auto_now=True)date_taken=models.DateTimeField(_('date taken'),null=True,editable=False)original_image=models.ImageField(_('image'),upload_to=picture_imagefield_path)display_image=ImageSpec(DISPLAY_IMAGE_PROCESSORS,image_field='original_image',options={'quality':90},)thumbnail_image=ImageSpec(THUMBNAIL_IMAGE_PROCESSORS,image_field='original_image',format='JPEG',options={'quality':75},)cover_image=ImageSpec(COVER_IMAGE_PROCESSORS,image_field='original_image',format='JPEG',options={'quality':75},)description=models.TextField(_('description'),blank=True)is_public=models.BooleanField(_('is public'),default=True,help_text=_('Public images will be displayed in the default views.'))album=TreeForeignKey('galeria.Album',verbose_name=_('album'),related_name='pictures')objects=PictureManager()classMeta:ordering=('-date_added',)get_latest_by='date_added'verbose_name=_('picture')verbose_name_plural=_('pictures')unique_together=(('slug','album'),)@propertydefEXIF(self):image_file=open(self.original_image.path,'rb')try:data=EXIF.process_file(image_file)exceptException,e:# TODO: log exceptiontry:data=EXIF.process_file(image_file,details=False)exceptException,e:pass# TODO: log exceptionimage_file.close()returndatadefsave(self,*args,**kwargs):super(Picture,self).save(*args,**kwargs)ifnotself.date_taken:exif_date=self.EXIF.get('EXIF DateTimeOriginal',None)ifexif_date:self.date_taken=datetime.strptime(str(exif_date),'%Y:%m:%d %H:%M:%S')super(Picture,self).save(*args,**kwargs)def__unicode__(self):returnunicode(self.title)@models.permalinkdefget_absolute_url(self):return('galeria-picture',None,{'year':str(self.date_added.year),'month':str(self.date_added.month).zfill(2),'day':str(self.date_added.day).zfill(2),'slug':str(self.slug)})