(
        self, new_prev_id: Optional[int], element_id: int, new_next_id: Optional[int]
    )
Moves the song specified by :param element_id:
        between the two songs :param new_prev_id: and :param new_next_id:.
Moves the song specified by :param element_id:
        between the two songs :param new_prev_id: and :param new_next_id:.
Moves the song specified by : param element_id : between the two songs : param new_prev_id : and : param new_next_id : .
def reorder(
        self, new_prev_id: Optional[int], element_id: int, new_next_id: Optional[int]
    ) -> None:
        """Moves the song specified by :param element_id:
        between the two songs :param new_prev_id: and :param new_next_id:."""

        try:
            new_prev = self.get(id=new_prev_id)
        except core.models.QueuedSong.DoesNotExist:
            new_prev = None
        try:
            to_reorder = self.get(id=element_id)
        except core.models.QueuedSong.DoesNotExist:
            raise ValueError("reordered song does not exist")
        try:
            new_next = self.get(id=new_next_id)
        except core.models.QueuedSong.DoesNotExist:
            new_next = None

        first = self.first()
        last = self.last()
        # check validity of request
        if new_prev is None and new_next is None:
            # to_reorder has to be the only element in the queue
            if first != to_reorder or last != to_reorder:
                raise ValueError("reordered song is not the only one")
        if new_prev is None and new_next is not None:
            # new_next has to be the first element
            if new_next != first:
                raise ValueError("given first is not head of the queue")
        if new_prev is not None and new_next is None:
            # new_prev has to be the last element
            if new_prev != last:
                raise ValueError("given last is not tail of the queue")
        if new_prev is not None and new_next is not None:
            # new_prev and new_next have to be adjacent
            if new_next.index != new_prev.index + 1:
                raise ValueError("given pair of songs is not adjacent")

        try:
            old_prev = self.get(index=to_reorder.index - 1)
        except core.models.QueuedSong.DoesNotExist:
            old_prev = None
        try:
            old_next = self.get(index=to_reorder.index + 1)
        except core.models.QueuedSong.DoesNotExist:
            old_next = None

        # update indices
        moving_up = True
        if new_prev is not None:
            if new_prev.index > to_reorder.index:
                moving_up = False
        elif new_next is not None:
            if new_next.index > to_reorder.index:
                moving_up = False

        to_update = self.all()
        if moving_up:
            if old_prev is not None:
                to_update = to_update.filter(index__lte=old_prev.index)
            new_index = 1
            if new_next is not None:
                new_index = new_next.index
                to_update = to_update.filter(index__gte=new_next.index)
            to_update.update(index=F("index") + 1)
        else:
            new_index = 1
            if new_prev is not None:
                new_index = new_prev.index
                to_update = to_update.filter(index__lte=new_prev.index)
            if old_next is not None:
                to_update = to_update.filter(index__gte=old_next.index)
            to_update.update(index=F("index") - 1)

        to_reorder.index = new_index
        to_reorder.save()
def reorder ( self , new_prev_id : Optional [ int ] , element_id : int , new_next_id : Optional [ int ] ) -> None : try : new_prev = self . get ( id = new_prev_id ) except core . models . QueuedSong . DoesNotExist : new_prev = None try : to_reorder = self . get ( id = element_id ) except core . models . QueuedSong . DoesNotExist : raise ValueError ( "reordered song does not exist" ) try : new_next = self . get ( id = new_next_id ) except core . models . QueuedSong . DoesNotExist : new_next = None first = self . first ( ) last = self . last ( ) # check validity of request if new_prev is None and new_next is None : # to_reorder has to be the only element in the queue if first != to_reorder or last != to_reorder : raise ValueError ( "reordered song is not the only one" ) if new_prev is None and new_next is not None : # new_next has to be the first element if new_next != first : raise ValueError ( "given first is not head of the queue" ) if new_prev is not None and new_next is None : # new_prev has to be the last element if new_prev != last : raise ValueError ( "given last is not tail of the queue" ) if new_prev is not None and new_next is not None : # new_prev and new_next have to be adjacent if new_next . index != new_prev . index + 1 : raise ValueError ( "given pair of songs is not adjacent" ) try : old_prev = self . get ( index = to_reorder . index - 1 ) except core . models . QueuedSong . DoesNotExist : old_prev = None try : old_next = self . get ( index = to_reorder . index + 1 ) except core . models . QueuedSong . DoesNotExist : old_next = None # update indices moving_up = True if new_prev is not None : if new_prev . index > to_reorder . index : moving_up = False elif new_next is not None : if new_next . index > to_reorder . index : moving_up = False to_update = self . all ( ) if moving_up : if old_prev is not None : to_update = to_update . filter ( index__lte = old_prev . index ) new_index = 1 if new_next is not None : new_index = new_next . index to_update = to_update . filter ( index__gte = new_next . index ) to_update . update ( index = F ( "index" ) + 1 ) else : new_index = 1 if new_prev is not None : new_index = new_prev . index to_update = to_update . filter ( index__lte = new_prev . index ) if old_next is not None : to_update = to_update . filter ( index__gte = old_next . index ) to_update . update ( index = F ( "index" ) - 1 ) to_reorder . index = new_index to_reorder . save ( )