From 42fb3b654e246b873ab85dcb9ce5239ba43112d5 Mon Sep 17 00:00:00 2001 From: Jijeong Lee Date: Thu, 7 Dec 2023 22:20:48 -0800 Subject: [PATCH] update modes frequency Increase new mode frequency(+1) and decrease old frequency(-1) --- emission/core/wrapper/user.py | 26 +++++++++++++++++++------- emission/net/api/cfc_webapp.py | 10 +++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/emission/core/wrapper/user.py b/emission/core/wrapper/user.py index 947dbe9cd..45c491958 100644 --- a/emission/core/wrapper/user.py +++ b/emission/core/wrapper/user.py @@ -236,17 +236,29 @@ def getModes(self): sortedModes = dict(sorted(filteredModes.items(), key=lambda x: (x[1]["frequency"]), reverse=True)) return sortedModes - def insertMode(self, mode): + def updateModes(self, updated_mode): from datetime import datetime - modes = self.getModes() - if mode in modes: - modes[mode]['frequency'] = modes[mode]['frequency'] + 1 - else: - modes[mode] = { + user = get_profile_db().find_one({'user_id': self.uuid}) + modes = user['modes'] + old_mode = updated_mode['old_mode'] + new_mode = updated_mode['new_mode'] + is_new_mode_must_added = updated_mode['is_new_mode_must_added'] + + if new_mode in modes: + updated_frequency = modes[new_mode]['frequency'] + 1 + modes[new_mode]['frequency'] = updated_frequency + + if is_new_mode_must_added and not new_mode in modes: + modes[new_mode] = { 'createdAt': datetime.now(), 'frequency': 1, 'isActive': True, } + + if old_mode in modes: + updated_frequency = modes[old_mode]['frequency'] - 1 + modes[old_mode]['frequency'] = updated_frequency + get_profile_db().update_one({'user_id': self.uuid}, {'$set': {'modes': modes}}) - return modes + return self.getModes() \ No newline at end of file diff --git a/emission/net/api/cfc_webapp.py b/emission/net/api/cfc_webapp.py index b6eafdca4..944684736 100644 --- a/emission/net/api/cfc_webapp.py +++ b/emission/net/api/cfc_webapp.py @@ -313,14 +313,14 @@ def getUserModes(): user = User.fromUUID(user_uuid) return user.getModes() -@post('/mode/create') -def createUserMode(): +@post('/mode/update') +def updateUserMode(): logging.debug("Called createUserMode") - new_mode = request.json['mode'] + updated_mode = request.json['updated_mode'] user_uuid = getUUID(request) user = User.fromUUID(user_uuid) - to_return = user.insertMode(new_mode) - logging.debug("Successfully created mode for user %s" % user_uuid) + to_return = user.updateModes(updated_mode) + logging.debug("Successfully updated mode for user %s" % user_uuid) return {"modes": to_return} @post('/result/metrics/')