def get_user_update_msg(user): user_update = Users_update() user_Gset = GSet() user_Gset.add(user.to_json()) user_update.users = json_util.dumps(user_Gset.toDict()) msg = {"type": 'Users_update', "data": {'users': user_update.users}} return json_util.dumps(msg)
def update_user(user, **kwargs): """ Function to add user update (i.e. addition of user to middleware database) """ check_users() # checks whether any user exists in that collection user_updates = Users_update.objects().first() user_updates_GSet_dict = json_util.loads(user_updates.users) user_updates_GSet = GSet() user_updates_GSet.__dict__ = user_updates_GSet_dict user_updates_GSet.add(user.to_json()) user_updates.users = json_util.dumps(user_updates_GSet.__dict__) user_updates.save()
def check_users(): user_updates = Users_update.objects().first() if user_updates is None: user_update = Users_update() user_updates_GSet = GSet() user_update.users = json_util.dumps(user_updates_GSet.__dict__) user_update.save()
def add_user_update_to_underlying(): middleware_users = middlewareDatabaseRead.get_user_updates() if middleware_users==None: return middleware_users_Gset = GSet().loadFromDict(json_util.loads(middleware_users.users)) users = [ User.from_json(element) for element in middleware_users_Gset.payload ] if len(users) > 0: for user in users: underlyingDatabaseWrite.add_user_by_object(user)
def merge_users(users): middleware_users = middlewareDatabaseRead.get_user_updates() if middleware_users==None: middleware_user_update = User_update() middleware_user_update.users = users middleware_user_update.save() return middleware_users_Gset = GSet().loadFromDict(json_util.loads(middleware_users.users)) users_Gset = GSet().loadFromDict(json_util.loads(users)) middleware_users_Gset.merge(users_Gset) middleware_users.users = json_util.dumps(middleware_users_Gset.toDict()) middleware_users.save()
def __init__(self): self.A = GSet() self.R = GSet()
class TwoPSet: def __init__(self): self.A = GSet() self.R = GSet() def add(self, elem): self.A.add(elem) def remove(self, elem): self.R.add(elem) def query(self, elem): return self.A.query(elem) and not self.R.query(elem) def compare(self, tps2): return self.A.compare(tps2.A) and self.R.compare(tps2.R) def merge(self, tps2): self.A.merge(tps2.A) self.R.merge(tps2.R) # self.display() def display(self): print("A: ", end="") self.A.display() print("R: ", end="") self.R.display() def addedValues(self): addedValues = [] for elem in self.A.payload: if self.A.payload.count(elem) > self.R.payload.count(elem): if elem not in addedValues: addedValues.append(elem) return addedValues def removedValues(self): removedValues = [] for elem in self.R.payload: if self.R.payload.count(elem) > self.A.payload.count(elem): if elem not in removedValues: removedValues.append(elem) return removedValues def toDict(self): self.A = self.A.toDict() self.R = self.R.toDict() # print(self.__dict__) return self.__dict__ def loadFromDict(self, dict_input): A = GSet() R = GSet() self.A = A.loadFromDict(dict_input['A']) self.R = R.loadFromDict(dict_input['R']) return self
def loadFromDict(self, dict_input): A = GSet() R = GSet() self.A = A.loadFromDict(dict_input['A']) self.R = R.loadFromDict(dict_input['R']) return self
dbName = None dbName_middleware = None for _ in range(NUM_FAKE_USERS): user = get_fake_user() add_user(name=user.name, username=user.username, nationality=user.nationality, age=user.age) users = readUsers(dbName=dbName) # print_users(users) users_update = middlewareDatabaseRead.get_user_updates( dbName=dbName_middleware) users_Gset = GSet() users_Gset.__dict__ = json_util.loads(users_update.users) users_from_Gset = json_util.loads(users_update.users)['payload'] users_loaded = [] last_username = "" for element in users_from_Gset: user = User.from_json(element, created=True) users_loaded.append(user) last_username = user.username update_user_name(last_username, "Rakesh") user_fullname_updates = middlewareDatabaseRead.get_fullname_updates_by_username( last_username) # print(last_username, user_fullname_updates.update_value)