2017-11-06 18:45:14 +00:00
import os
2021-05-26 20:01:52 +00:00
import sys
2017-11-06 18:45:14 +00:00
2018-02-16 20:07:53 +00:00
import kombu
2016-10-25 20:04:24 +00:00
from django . contrib . auth . models import Permission
from django . contrib . auth . models import User , Group
2018-03-19 18:21:01 +00:00
from django . core . exceptions import ObjectDoesNotExist , MultipleObjectsReturned
2017-11-06 18:45:14 +00:00
from django . core . files import File
2016-10-25 20:04:24 +00:00
from django . db . utils import ProgrammingError
2017-02-08 19:53:55 +00:00
from guardian . shortcuts import assign_perm
2018-02-15 21:23:29 +00:00
from worker import tasks as worker_tasks
2017-07-25 17:54:41 +00:00
from app . models import Preset
2017-11-06 18:45:14 +00:00
from app . models import Theme
2019-03-19 19:19:23 +00:00
from app . plugins import init_plugins
2017-02-08 19:53:55 +00:00
from nodeodm . models import ProcessingNode
2017-05-21 18:18:56 +00:00
# noinspection PyUnresolvedReferences
2017-11-06 18:45:14 +00:00
from webodm . settings import MEDIA_ROOT
2018-02-15 21:23:29 +00:00
from . import signals
2017-05-19 20:15:26 +00:00
import logging
2017-11-06 18:45:14 +00:00
from . models import Task , Setting
2017-02-08 20:23:21 +00:00
from webodm import settings
2017-05-19 20:15:26 +00:00
from webodm . wsgi import booted
2017-02-08 19:53:55 +00:00
2016-10-07 23:07:47 +00:00
2016-10-25 20:04:24 +00:00
def boot ( ) :
2017-05-19 20:15:26 +00:00
# booted is a shared memory variable to keep track of boot status
2018-02-15 21:23:29 +00:00
# as multiple gunicorn workers could trigger the boot sequence twice
2018-09-12 14:42:09 +00:00
if ( not settings . DEBUG and booted . value ) or settings . MIGRATING : return
2017-05-19 20:15:26 +00:00
booted . value = True
2016-10-07 23:07:47 +00:00
logger = logging . getLogger ( ' app.logger ' )
2017-05-19 20:15:26 +00:00
2018-02-09 18:46:45 +00:00
logger . info ( " Booting WebODM {} " . format ( settings . VERSION ) )
2017-05-19 20:15:26 +00:00
if settings . DEBUG :
2017-05-21 18:18:56 +00:00
logger . warning ( " Debug mode is ON (for development this is OK) " )
2016-10-07 23:07:47 +00:00
2018-03-29 21:28:15 +00:00
# Make sure our app/media/tmp folder exists
if not os . path . exists ( settings . MEDIA_TMP ) :
2018-05-13 17:30:44 +00:00
os . makedirs ( settings . MEDIA_TMP )
2018-03-29 21:28:15 +00:00
2016-10-07 23:07:47 +00:00
# Check default group
2016-10-08 01:00:34 +00:00
try :
default_group , created = Group . objects . get_or_create ( name = ' Default ' )
if created :
logger . info ( " Created default group " )
2016-10-07 23:07:47 +00:00
2017-02-08 19:53:55 +00:00
# Assign viewprocessing node object permission to default processing node (if present)
# Otherwise non-root users will not be able to process
try :
pnode = ProcessingNode . objects . get ( hostname = " node-odm-1 " )
assign_perm ( ' view_processingnode ' , default_group , pnode )
logger . info ( " Added view_processingnode permissions to default group " )
except ObjectDoesNotExist :
pass
2017-02-08 20:23:21 +00:00
2016-10-12 22:18:37 +00:00
# Add default permissions (view_project, change_project, delete_project, etc.)
2017-07-21 20:48:01 +00:00
for permission in ( ' _project ' , ' _task ' , ' _preset ' ) :
2016-10-08 01:00:34 +00:00
default_group . permissions . add (
2017-02-08 20:23:21 +00:00
* list ( Permission . objects . filter ( codename__endswith = permission ) )
)
2016-11-17 23:51:07 +00:00
# Add permission to view processing nodes
default_group . permissions . add ( Permission . objects . get ( codename = " view_processingnode " ) )
2017-02-08 20:23:21 +00:00
2018-03-19 18:21:01 +00:00
add_default_presets ( )
2017-07-25 17:54:41 +00:00
2017-11-06 18:45:14 +00:00
# Add settings
default_theme , created = Theme . objects . get_or_create ( name = ' Default ' )
if created :
logger . info ( " Created default theme " )
2020-03-20 17:00:09 +00:00
if settings . DEFAULT_THEME_CSS :
default_theme . css = settings . DEFAULT_THEME_CSS
default_theme . save ( )
2017-11-06 18:45:14 +00:00
if Setting . objects . all ( ) . count ( ) == 0 :
s = Setting . objects . create (
2020-03-20 15:32:12 +00:00
app_name = settings . APP_NAME ,
2017-11-06 18:45:14 +00:00
theme = default_theme )
2020-03-20 15:32:12 +00:00
s . app_logo . save ( os . path . basename ( settings . APP_DEFAULT_LOGO ) , File ( open ( settings . APP_DEFAULT_LOGO , ' rb ' ) ) )
2017-11-06 18:45:14 +00:00
logger . info ( " Created settings " )
2021-05-26 20:01:52 +00:00
2021-05-27 13:55:27 +00:00
init_plugins ( )
2018-01-30 16:48:16 +00:00
2016-10-25 22:08:15 +00:00
if not settings . TESTING :
2018-02-16 20:07:53 +00:00
try :
worker_tasks . update_nodes_info . delay ( )
except kombu . exceptions . OperationalError as e :
logger . error ( " Cannot connect to celery broker at {} . Make sure that your redis-server is running at that address: {} " . format ( settings . CELERY_BROKER_URL , str ( e ) ) )
2016-10-25 20:04:24 +00:00
2016-10-25 22:08:15 +00:00
except ProgrammingError :
2018-03-19 18:21:01 +00:00
logger . warning ( " Could not touch the database. If running a migration, this is expected. " )
def add_default_presets ( ) :
try :
2020-01-21 22:41:17 +00:00
Preset . objects . update_or_create ( name = ' Multispectral ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' texturing-skip-global-seam-leveling ' , ' value ' : True } ,
2021-02-05 19:49:13 +00:00
{ ' name ' : ' radiometric-calibration ' , ' value ' : ' camera ' } ,
2020-01-21 22:41:17 +00:00
] } )
2019-03-19 19:19:23 +00:00
Preset . objects . update_or_create ( name = ' Volume Analysis ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' dsm ' , ' value ' : True } ,
2019-03-19 19:19:23 +00:00
{ ' name ' : ' dem-resolution ' , ' value ' : ' 2 ' } ,
2021-09-27 14:56:24 +00:00
{ ' name ' : ' pc-geometric ' , ' value ' : True } ,
{ ' name ' : ' pc-quality ' , ' value ' : ' high ' } ,
{ ' name ' : ' use-3dmesh ' , ' value ' : True } , ] } )
2019-01-15 19:07:35 +00:00
Preset . objects . update_or_create ( name = ' 3D Model ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' mesh-octree-depth ' , ' value ' : " 12 " } ,
2019-01-15 19:07:35 +00:00
{ ' name ' : ' use-3dmesh ' , ' value ' : True } ,
2021-09-27 14:56:24 +00:00
{ ' name ' : ' pc-geometric ' , ' value ' : True } ,
2021-02-05 19:49:13 +00:00
{ ' name ' : ' pc-quality ' , ' value ' : ' high ' } ,
2019-12-02 17:57:16 +00:00
{ ' name ' : ' mesh-size ' , ' value ' : ' 300000 ' } ] } )
2019-01-15 19:07:35 +00:00
Preset . objects . update_or_create ( name = ' Buildings ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' mesh-size ' , ' value ' : ' 300000 ' } ,
2021-10-01 00:19:03 +00:00
{ ' name ' : ' pc-geometric ' , ' value ' : True } ,
2021-02-05 19:49:13 +00:00
{ ' name ' : ' pc-quality ' , ' value ' : ' high ' } ] } )
2019-01-15 19:07:35 +00:00
Preset . objects . update_or_create ( name = ' Point of Interest ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' mesh-size ' , ' value ' : ' 300000 ' } ,
2021-09-27 14:56:24 +00:00
{ ' name ' : ' pc-geometric ' , ' value ' : True } ,
2019-01-15 19:07:35 +00:00
{ ' name ' : ' use-3dmesh ' , ' value ' : True } ] } )
Preset . objects . update_or_create ( name = ' Forest ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' min-num-features ' , ' value ' : ' 18000 ' } ,
2021-09-27 14:56:24 +00:00
{ ' name ' : ' use-3dmesh ' , ' value ' : True } ,
{ ' name ' : ' pc-geometric ' , ' value ' : True } ,
2021-02-05 19:49:13 +00:00
{ ' name ' : ' feature-quality ' , ' value ' : ' ultra ' } ] } )
2018-03-19 18:21:01 +00:00
Preset . objects . update_or_create ( name = ' DSM + DTM ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' dsm ' , ' value ' : True } ,
{ ' name ' : ' dtm ' , ' value ' : True } ] } )
2018-03-19 18:21:01 +00:00
Preset . objects . update_or_create ( name = ' Fast Orthophoto ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' fast-orthophoto ' , ' value ' : True } ] } )
2018-06-01 02:13:26 +00:00
Preset . objects . update_or_create ( name = ' High Resolution ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
2019-05-20 14:05:56 +00:00
{ ' name ' : ' dsm ' , ' value ' : True } ,
2021-02-05 19:49:13 +00:00
{ ' name ' : ' pc-quality ' , ' value ' : ' high ' } ,
2019-01-15 19:07:35 +00:00
{ ' name ' : ' dem-resolution ' , ' value ' : " 2.0 " } ,
2021-10-01 01:11:35 +00:00
{ ' name ' : ' orthophoto-resolution ' , ' value ' : " 2.0 " } ,
2018-03-19 18:21:01 +00:00
] } )
Preset . objects . update_or_create ( name = ' Default ' , system = True ,
2021-12-08 18:11:54 +00:00
defaults = { ' options ' : [ { ' name ' : ' auto-boundary ' , ' value ' : True } ,
{ ' name ' : ' dsm ' , ' value ' : True } ] } )
2019-01-15 19:07:35 +00:00
2018-03-19 18:21:01 +00:00
except MultipleObjectsReturned :
# Mostly to handle a legacy code problem where
# multiple system presets with the same name were
# created if we changed the options
Preset . objects . filter ( system = True ) . delete ( )
add_default_presets ( )