2017-11-06 18:45:14 +00:00
import logging
2017-11-07 21:48:27 +00:00
import os
2017-11-10 16:48:17 +00:00
from shutil import rmtree
2017-11-06 18:45:14 +00:00
2017-11-09 20:45:29 +00:00
from django . core . exceptions import ValidationError
2017-11-06 18:45:14 +00:00
from django . db import models
2017-11-10 16:48:17 +00:00
from django . db . models import signals
2017-11-07 21:48:27 +00:00
from django . dispatch import receiver
2017-11-06 18:45:14 +00:00
from imagekit . models import ImageSpecField
from imagekit . processors import ResizeToFit
2020-12-18 17:32:44 +00:00
from django . utils . translation import gettext_lazy as _
2017-11-09 20:45:29 +00:00
from webodm import settings
2017-11-10 16:48:17 +00:00
from . theme import Theme , update_theme_css
2017-11-06 18:45:14 +00:00
logger = logging . getLogger ( ' app.logger ' )
class Setting ( models . Model ) :
2020-12-18 17:32:44 +00:00
app_name = models . CharField ( max_length = 255 , blank = False , null = False , help_text = _ ( " The name of your application " ) , verbose_name = _ ( " App name " ) )
app_logo = models . ImageField ( upload_to = " settings/ " , blank = False , null = False , help_text = _ ( " A 512x512 logo of your application (.png or .jpeg) " ) , verbose_name = _ ( " App logo " ) )
2017-11-06 18:45:14 +00:00
app_logo_36 = ImageSpecField ( source = ' app_logo ' ,
processors = [ ResizeToFit ( 36 , 36 ) ] ,
format = ' PNG ' ,
options = { ' quality ' : 90 } )
app_logo_favicon = ImageSpecField ( source = ' app_logo ' ,
processors = [ ResizeToFit ( 48 , 48 ) ] ,
format = ' PNG ' ,
options = { ' quality ' : 90 } )
2020-12-18 17:32:44 +00:00
organization_name = models . CharField ( default = ' WebODM ' , max_length = 255 , blank = True , null = True , help_text = _ ( " The name of your organization " ) , verbose_name = _ ( " Organization name " ) )
organization_website = models . URLField ( default = ' https://github.com/OpenDroneMap/WebODM/ ' , max_length = 255 , blank = True , null = True , help_text = _ ( " The website URL of your organization " ) , verbose_name = _ ( " Organization website " ) )
theme = models . ForeignKey ( Theme , blank = False , null = False , on_delete = models . DO_NOTHING , verbose_name = _ ( " Theme " ) ,
help_text = _ ( " Active theme " ) )
2017-11-06 18:45:14 +00:00
2017-11-09 20:45:29 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( Setting , self ) . __init__ ( * args , * * kwargs )
# To help keep track of changes to the app_logo
self . __original_app_logo_name = self . app_logo . name
def save ( self , * args , * * kwargs ) :
# Cleanup old logo files if needed
if self . __original_app_logo_name != " " and \
2017-11-10 18:05:52 +00:00
self . app_logo . name != self . __original_app_logo_name and \
2017-11-10 19:29:29 +00:00
os . path . basename ( self . app_logo . name ) != os . path . basename ( self . __original_app_logo_name ) : # This last line will leave an old copy in the cache if the filename is the same name as the previous, but we don't care
2017-11-09 20:45:29 +00:00
old_logo_path = os . path . join ( settings . MEDIA_ROOT , self . __original_app_logo_name )
old_logo_path_caches = os . path . join ( settings . MEDIA_ROOT ,
" CACHE " ,
" images " ,
os . path . splitext ( self . __original_app_logo_name ) [ 0 ] )
if os . path . exists ( old_logo_path ) :
try :
os . unlink ( old_logo_path )
logger . info ( " Removed {} " . format ( old_logo_path ) )
except :
logger . warning ( " Cannot cleanup {} " . format ( old_logo_path ) )
if os . path . exists ( old_logo_path_caches ) :
try :
rmtree ( old_logo_path_caches )
logger . info ( " Removed {} " . format ( old_logo_path_caches ) )
except :
logger . warning ( " Cannot cleanup {} " . format ( old_logo_path_caches ) )
self . __original_app_logo_name = self . app_logo . name
super ( Setting , self ) . save ( * args , * * kwargs )
2017-11-06 18:45:14 +00:00
def __str__ ( self ) :
2020-12-18 21:54:00 +00:00
return str ( _ ( " Application " ) )
2020-12-18 17:32:44 +00:00
class Meta :
verbose_name = _ ( " Settings " )
verbose_name_plural = _ ( " Settings " )
2017-11-07 21:48:27 +00:00
2017-11-09 20:45:29 +00:00
@receiver ( signals . pre_save , sender = Setting , dispatch_uid = " setting_pre_save " )
def setting_pre_save ( sender , instance , * * kwargs ) :
if Setting . objects . count ( ) > 0 and instance . id != Setting . objects . get ( ) . id :
raise ValidationError ( " Can only create 1 %s instance " % Setting . __name__ )
2017-11-07 21:48:27 +00:00
@receiver ( signals . post_save , sender = Setting , dispatch_uid = " setting_post_save " )
def setting_post_save ( sender , instance , created , * * kwargs ) :
2017-11-10 16:48:17 +00:00
update_theme_css ( )