#NEWLINE# @ detail_route ( methods = [ 'put' ] ) #NEWLINE# def set_state( self , request , pk = None ) : #NEWLINE# #INDENT# "\n        Special view endpoint to set and reset a state of a motion.\n\n        Send PUT {'state': <state_id>} to set and just PUT {} to reset the\n        state. Only managers can use this view.\n        " #NEWLINE# motion = self . get_object ( ) #NEWLINE# state = request . data . get ( 'state' ) #NEWLINE# if ( state is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# state_id = int ( state ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# raise ValidationError ( { #NEWLINE# #INDENT# 'detail' : _ ( 'Invalid data. State must be an integer.' ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# if ( state_id not in [ item . id for item in motion . state . next_states . all ( ) ] ) : #NEWLINE# #INDENT# raise ValidationError ( { #NEWLINE# #INDENT# 'detail' : ( _ ( 'You can not set the state to %(state_id)d.' ) % { #NEWLINE# #INDENT# 'state_id' : state_id , #NEWLINE# #UNINDENT# } ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# motion . set_state ( state_id ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# motion . reset_state ( ) #NEWLINE# #UNINDENT# motion . save ( update_fields = [ 'state' , 'identifier' ] ) #NEWLINE# message = ( _ ( 'The state of the motion was set to %s.' ) % item . state . name ) #NEWLINE# motion . write_log ( message_list = [ ugettext_noop ( 'State set to' ) , ' ' , motion . state . name ] , person = request . user ) #NEWLINE# return Response ( { #NEWLINE# #INDENT# 'detail' : message , #NEWLINE# #UNINDENT# } )