@ -10,6 +10,7 @@ from datetime import date
class nbiotDeviceForm ( FlaskForm ) : # Defines the self class to be used for the user update
name = StringField ( ' Device Name ' , validators = [ DataRequired ( ) , Length ( min = 3 , max = 50 ) ] )
model = StringField ( ' Device Model ' , validators = [ DataRequired ( ) , Length ( min = 3 , max = 50 ) ] )
serial_no = StringField ( ' Serial Number ' , validators = [ DataRequired ( ) , Length ( min = 3 , max = 50 ) ] )
device_id = StringField ( ' Device Id ' , validators = [ DataRequired ( ) , Length ( min = 3 , max = 50 ) ] )
imsi = StringField ( ' Imsi No ' , validators = [ DataRequired ( ) , Length ( min = 10 , max = 50 ) ] )
@ -19,71 +20,75 @@ class nbiotDeviceForm(FlaskForm): # Defines the self class to be used for the u
registration_date = DateField ( ' Registration Date ' , format = ' % Y- % m- %d ' , default = date . today , validators = [ DataRequired ( ) ] )
activation_date = DateField ( ' Activation Date ' , format = ' % Y- % m- %d ' , default = date . today , validators = [ DataRequired ( ) ] )
deactivation_date = DateField ( ' Deactivation Date ' , format = ' % Y- % m- %d ' , default = date . today , validators = [ DataRequired ( ) ] )
owner_id = SelectField ( ' Owner ' , validators = [ DataRequired ( ) ] )
user_id = SelectField ( ' User ' )
device_ owner_id = SelectField ( ' Owner ' , validators = [ DataRequired ( ) ] )
device_ user_id = SelectField ( ' User ' )
status_id = SelectField ( ' Status ' , validators = [ DataRequired ( ) ] )
type_id = SelectField ( ' Type ' , validators = [ DataRequired ( ) ] )
area_id = SelectField ( ' Area ' , validators = [ DataRequired ( ) ] )
manufacturer_id = SelectField ( ' Manufacturer ' , validators = [ DataRequired ( ) ] ,
render_kw = {
' hx-target ' : ' #manufacturer_id ' , # Target div or modal for the popup
' hx-swap ' : ' outerHTML ' # Swap the outer HTML of the target element
} )
company_id = SelectField ( ' Managed By ' , validators = [ DataRequired ( ) ] )
company_manufacturer_id = SelectField ( ' Manufacturer ' , validators = [ DataRequired ( ) ] ,
render_kw = { " custom_option " : " search_field " ,
" query_table " : " Companies " ,
" query_key " : " name " ,
" query_return " : " name "
} )
company_owner_id = SelectField ( ' Managed By ' , validators = [ DataRequired ( ) ] ,
render_kw = { " custom_option " : " search_field " ,
" query_table " : " Companies " ,
" query_key " : " name " ,
" query_return " : " name "
} )
image_file = FileField ( ' Update Sensor Picture ' , validators = [ FileAllowed ( [ ' jpg ' , ' png ' ] ) ] )
submit = SubmitField ( )
def populate_for_updating ( self , device ) :
self . originalModel = device
self . submit . label . text = " Update "
self . user_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_user_id ) ]
self . owner_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_owner_id ) ]
self . manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_manufacturer_id ) ]
self . company_ id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_owner_id ) ]
self . device_ user_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_user_id ) ]
self . device_ owner_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_owner_id ) ]
self . company_ manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_manufacturer_id ) ]
self . company_ owner_ id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_owner_id ) ]
self . status_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryStatusNamesWithDefault ( device . status_id ) ]
self . type_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryTypeNamesWithDefault ( device . type_id ) ]
self . area_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryAreaNamesWithDefault ( device . area_id ) ]
def populate_for_adding ( self , device ) :
self . originalModel = device
self . submit . label . text = " Add "
if self . manufacturer_id. data :
self . manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( self . manufacturer_id. data ) ]
if self . company_ manufacturer_id. data :
self . company_ manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( self . company_ manufacturer_id. data ) ]
self . user_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_user_id ) ]
self . owner_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_owner_id ) ]
self . manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_manufacturer_id ) ]
self . company_ id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_owner_id ) ]
self . device_ user_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_user_id ) ]
self . device_ owner_id. choices = [ ( row . id , row . username ) for row in userUtils . queryUserNamesWithDefault ( device . device_owner_id ) ]
self . company_ manufacturer_id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_manufacturer_id ) ]
self . company_ owner_ id. choices = [ ( row . id , row . name ) for row in companyUtils . queryNamesWithDefault ( device . company_owner_id ) ]
self . status_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryStatusNamesWithDefault ( device . status_id ) ]
self . type_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryTypeNamesWithDefault ( device . type_id ) ]
self . area_id . choices = [ ( row . id , row . name ) for row in sensorUtils . queryAreaNamesWithDefault ( device . area_id ) ]
#custom validators specific naming convention: validate_<fieldname>. This is how Flask-WTF knows which field the validator is associated with.
def validate_name ( self , input ) :
if input . data != self . name :
content = nbiotDevice . query . filter_by ( name = input . data ) . first ( )
if content :
raise ValidationError ( ' That device name is taken please choose another one ' )
if input . data != self . originalModel . name :
if ( nbiotDevice . query . filter ( nbiotDevice . name . ilike ( f ' % { input . data } % ' ) ) . first ( ) ) :
raise ValidationError ( ' This Name already exists. Please choose a different one. ' )
def validate_device_id ( self , input ) :
if input . data != self . device_id :
content = nbiotDevice . query . filter_by ( device_id = input . data ) . first ( )
if content :
raise ValidationError ( ' That device id is taken please choose another one ' )
def validate_serial_no ( self , input ) :
if input . data != self . originalModel . serial_no :
if ( nbiotDevice . query . filter ( nbiotDevice . serial_no . ilike ( f ' % { input . data } % ' ) ) . first ( ) ) :
raise ValidationError ( ' This Serial Number is already registered. ' )
def validate_imsi ( self , input ) :
if input . data != self . imsi :
content = nbiotDevice . query . filter_by ( imsi = input . data ) . first ( )
if content :
raise ValidationError ( ' That IMSI id is taken please choose another one ' )
if input . data != self . originalModel . imsi :
if ( nbiotDevice . query . filter ( nbiotDevice . imsi . ilike ( f ' % { input . data } % ' ) ) . first ( ) ) :
raise ValidationError ( ' This IMSI no is already registered. ' )
def validate_iccid ( self , input ) :
if input . data != self . iccid :
content = nbiotDevice . query . filter_by ( iccid = input . data ) . first ( )
if content :
raise ValidationError ( ' That ICCID id is taken please choose another one ' )
if input . data != self . originalModel . iccid :
if ( nbiotDevice . query . filter ( nbiotDevice . iccid . ilike ( f ' % { input . data } % ' ) ) . first ( ) ) :
raise ValidationError ( ' This ICCID no is already registered. ' )
def validate_ip ( self , input ) :
if input . data != self . ip :
content = nbiotDevice . query . filter_by ( ip = input . data ) . first ( )
if content :
raise ValidationError ( ' That IP id is taken please choose another one ' )
if input . data != self . originalModel . ip :
if ( nbiotDevice . query . filter ( nbiotDevice . ip . ilike ( f ' % { input . data } % ' ) ) . first ( ) ) :
raise ValidationError ( ' A devie already has this IP ' )