def insert(self, index, track):
        """
        Inserts a track in the playlist. The playlist tracks are structured in
        a linked list, to insert an item in the list this method find the item
        in the right position and updates the links in both.

        If the value None is given as index, the track will be appended at the
        end of the list.
        """

        if index is not None:
            try:
                index = int(index)
            except:
                raise ValueError

            if index < 0:
                raise ValueError

        if track is None:
            raise ValueError

        rel = TrackPlaylistRelationship(playlist=self, track=track)

        query = TrackPlaylistRelationship.query.filter_by(playlist=self)
        count = query.count()

        if index is None:
            index = count

        if count == 0 and index > 0:
            raise ValueError

        if count > 0:
            if count > index:
                raise ValueError

            # r_track is not an actual track, but a relationship between the
            # playlist and a track.
            if index == count:  # Append at the end
                r_track = self.get_track_at(index - 1)
                rel.previous_track = r_track
            else:
                r_track = self.get_track_at(index)
                if not r_track:
                    raise ValueError
                rel.previous_track = r_track.previous_track
                r_track.previous_track = rel

            db.session.add(r_track)

        db.session.add(rel)
        db.session.commit()
dataset/ETHPy150Open tooxie/shiva-server/shiva/models.py/Playlist.insert/swapped operands of `>`