<filename>lib/User.py
import redis as rd
import json

class User:

    def __init__(self, id):
        self.id = id
        self.cache = rd.StrictRedis()

    def save_thumbs_change(self, track_id, change):
        
        try:
            data = self.get_data()
        except AttributeError:
            # must be no data yet
            data = {'thumbs_tracks': {}}
        
        tracks = data['thumbs_tracks']
        if track_id not in tracks:
            tracks[track_id] = {
                'was_upvoted': False,
                'was_downvoted': False
            }

        if change == 'up':
            tracks[track_id]['was_upvoted'] = not tracks[track_id]['was_upvoted']
        else: 
            tracks[track_id]['was_downvoted'] = not tracks[track_id]['was_downvoted']
        
        self.save_data(data)

    def get_data(self):
        data = self.cache.get(self.id)
        data = json.loads(data.decode('utf-8'))
        return data

    def save_data(self, data):
        data = json.dumps(data)
        self.cache.set(self.id, data)

        

<filename>lib/User.py
import redis as rd
import json

class User:

    def __init__(self, id):
        self.id = id
        self.cache = rd.StrictRedis()

    def save_thumbs_change(self, track_id, change):
        
        try:
            data = self.get_data()
        except AttributeError:
            # must be no data yet
            data = {'thumbs_tracks': {}}
        
        tracks = data['thumbs_tracks']
        if track_id not in tracks:
            tracks[track_id] = {
                'was_upvoted': False,
                'was_downvoted': False
            }

        if change == 'up':
            tracks[track_id]['was_upvoted'] = not tracks[track_id]['was_upvoted']
        else: 
            tracks[track_id]['was_downvoted'] = not tracks[track_id]['was_downvoted']
        
        self.save_data(data)

    def get_data(self):
        data = self.cache.get(self.id)
        data = json.loads(data.decode('utf-8'))
        return data

    def save_data(self, data):
        data = json.dumps(data)
        self.cache.set(self.id, data)

        

# must be no data yet