diff --git a/emission/core/wrapper/user.py b/emission/core/wrapper/user.py index 6e31cb3e1..947dbe9cd 100644 --- a/emission/core/wrapper/user.py +++ b/emission/core/wrapper/user.py @@ -228,3 +228,25 @@ def unregister(userEmail): get_uuid_db().delete_one({'user_email': userEmail}) get_profile_db().delete_one({'user_id': uuid}) return uuid + + def getModes(self): + user = get_profile_db().find_one({'user_id': self.uuid}) + modes = user['modes'] + filteredModes = {key: value for key, value in modes.items() if value.get('isActive', False)} + sortedModes = dict(sorted(filteredModes.items(), key=lambda x: (x[1]["frequency"]), reverse=True)) + return sortedModes + + def insertMode(self, mode): + from datetime import datetime + modes = self.getModes() + if mode in modes: + modes[mode]['frequency'] = modes[mode]['frequency'] + 1 + else: + modes[mode] = { + 'createdAt': datetime.now(), + 'frequency': 1, + 'isActive': True, + } + get_profile_db().update_one({'user_id': self.uuid}, {'$set': {'modes': modes}}) + return modes + \ No newline at end of file diff --git a/emission/net/api/cfc_webapp.py b/emission/net/api/cfc_webapp.py index e585d6a25..b6eafdca4 100644 --- a/emission/net/api/cfc_webapp.py +++ b/emission/net/api/cfc_webapp.py @@ -306,6 +306,23 @@ def getUserProfile(): user = User.fromUUID(user_uuid) return user.getProfile() +@post('/mode/get') +def getUserModes(): + logging.debug("Called getUserModes") + user_uuid = getUUID(request) + user = User.fromUUID(user_uuid) + return user.getModes() + +@post('/mode/create') +def createUserMode(): + logging.debug("Called createUserMode") + new_mode = request.json['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) + return {"modes": to_return} + @post('/result/metrics/') def summarize_metrics(time_type): _fill_aggregate_backward_compat(request)