def save_to_mongo(self): print("save_to_mongo()") Database.update(collection='invaders', query={'mac': self.mac}, data={'$push': { 'commands': self.json() }})
def change_username(username, new_username): user_data = Database.find_one(collection, {'username': username}) if user_data is None: raise UserErrors.UserNotExistsError("Your User doesn't Exist") Database.update(collection=collection, query={'username': username}, data={"$set": { 'username': new_username }}) return True
def change_password(username, new_password): user_data = Database.find_one(collection, {'username': username}) if user_data is None: raise UserErrors.UserNotExistsError("Your User doesn't Exist") Database.update( collection=collection, query={'username': username}, data={"$set": { 'password': Utils.hash_password(new_password) }}) return True
def make_node_active(mac): node_data = Database.find_one(collection=collection, query={'mac': mac}) if node_data['active'] is True: raise NodeErrors.NodeAlreadyActiveError('Node is Already Active') Database.update(collection=collection, query={'mac': mac}, data={"$set": { 'active': True }}) return True
def make_group_inactive(gname): group_data = Database.find_one(collection=collection, query={'gname': gname}) if group_data['active'] is False: raise GroupErrors.GroupAlreadyInActiveError( 'Group is Already InActive') Database.update(collection=collection, query={'gname': gname}, data={"$set": { 'active': False }}) return True
def make_user_active(username): user_data = Database.find_one(collection, {'username': username}) if user_data is None: raise UserErrors.UserNotExistsError("Your User doesn't Exist") if user_data['active'] is True: raise UserErrors.UserisAlreadyActiveError("User is Already Active") Database.update(collection=collection, query={'username': username}, data={"$set": { 'active': True }}) return True
def make_group_active( gname ): # as there is node in only one group, making group inactive should make nodes under that group also inactive group_data = Database.find_one(collection=collection, query={'gname': gname}) if group_data['active'] is True: raise GroupErrors.GroupAlreadyActiveError( 'Group is Already Active') Database.update(collection=collection, query={'gname': gname}, data={"$set": { 'active': True }}) return True
def remove_node_from_group(gname, node): group_data = Database.find_one(collection=collection, query={'gname': gname}) if group_data is None: raise GroupErrors.GroupNotExistsError('Group Not Exists') node_data = Database.find_one(collection=node_table, query={'mac': node}) if node_data is None: raise NodeErrors.NodeNotExistsError('No Node with name') group_node_data = Database.find_one(collection=collection, query={'nodes': node}) if group_node_data is None: raise GroupErrors.NodeNotExistsInaGroupError( node + 'Not Exists in a group') Database.update(collection=collection, query={'gname': gname}, data={'$pull': { 'nodes': node }}) return True
def add_node_to_group(gname, node): group_data = Database.find_one(collection=collection, query={'gname': gname}) if group_data is None: raise GroupErrors.GroupNotExistsError('Group Not Exists') node_data = Database.find_one(collection=node_table, query={'mac': node}) if node_data is None: raise NodeErrors.NodeNotExistsError('No Node with that name') group_node_data = Database.find_one(collection=collection, query={'nodes': node}) if group_node_data is not None: raise GroupErrors.NodeExistsInaGroupError( node + 'Already Exists in a group') Database.update(collection=collection, query={'gname': gname}, data={'$push': { 'nodes': node }}) return True
def remove_user_from_group(gname, user): group_data = Database.find_one(collection=collection, query={'gname': gname}) if group_data is None: raise GroupErrors.GroupNotExistsError('Group Not Exists') user_data = Database.find_one(collection=user_table, query={'username': user}) if user_data is None: raise UserErrors.UserNotExistsError("No Users with that name") group_user_data = Database.find_one(collection=collection, query={ 'gname': gname, 'users': user }) if group_user_data is None: raise GroupErrors.UserNotExistsInGroupError( 'User Not Exists in this group') Database.update(collection=collection, query={'gname': gname}, data={'$pull': { 'users': user }}) return True
def hello(): request_data = request.get_json() print(request_data) data = Database.find_one(collection, {'mac': request_data['mac']}) if data is None: print(untruce) untruce.add(request_data['mac']) return {'message': 'Unknown'} if data is not None: Database.update(collection, query={'mac': request_data['mac']}, data={"$set": { 'reporte': datetime.now() }}) if 'token' in request_data: base = Database.find_one( collection, { 'mac': request_data['mac'], 'commands.token': request_data['token'] }) if base is not None: Database.update(collection, query={ 'mac': request_data['mac'], 'commands.token': request_data['token'] }, data={ "$set": { "commands.$.response": request_data['response'] } }) Database.update(collection, query={ 'mac': request_data['mac'], 'commands.token': request_data['token'] }, data={"$set": { "commands.$.tag": 'true' }}) requeste = {'mac': request_data['mac']} print(request_data['mac']) rev = Database.find_one(collection, { 'mac': request_data['mac'], 'commands.serve': 'true' }) if rev is not None: randie = rev['commands'] print(randie) for i in randie: if i['serve'] == 'true': rrr = i['token'] jjj = i['command'] print(rrr) print(jjj) requeste = { 'mac': rev['mac'], 'token': rrr, 'command': jjj } Database.update( collection, query={ 'mac': rev['mac'], 'commands.token': i['token'] }, data={"$set": { "commands.$.serve": 'mild' }}) break print(requeste) return jsonify(requeste)
def exec(mac): request_data = request.get_json() user = get_jwt_identity() claims = get_jwt_claims() if claims['is_admin'] is not True: try: if Pretty.is_mac_valid(mac): pass except NodeErrors.NodeNotExistsError as e: return jsonify(message=e.message), 400 except NodeErrors.NodeInActiveError as e: return jsonify(message=e.message), 400 auth = Group.find_user_and_node_in_same_group(mac, user) if auth is not True: return jsonify( message='You are not Authorised to access this system'), 401 data = Database.find_one(collection, {'mac': mac}) if data is not None: rand = random.randint(1000, 9999) print(rand) statuse = statuss(data) print(statuse) if statuse: josn = Commands(mac=mac, token=rand, serve='true', command=request_data['command'], tag='false', response='') josn.save_to_mongo() else: return jsonify({'output': 'router is offline'}) a = datetime.now() b = timedelta(seconds=timeout) c = a + b while datetime.now() < c: r = Database.find_one(collection, { 'mac': mac, 'commands.token': rand, 'commands.tag': 'true' }) randie = r['commands'] for i in randie: if i['token'] == rand and i['tag'] == 'true': response = i['response'] print('horrible') print(response) Database.update( collection, query={'mac': mac}, data={'$pull': { 'commands': { 'token': rand } }}) return jsonify(response) Database.update(collection, query={'mac': mac}, data={'$pull': { 'commands': { 'token': rand } }}) print('server timeout for' + str(rand)) return jsonify({'output': 'server time out or server busy'}) return jsonify({'output': 'unable to find with that mac'})