<commit_before>import os.path
import re

from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render, redirect
from django.utils.decorators import method_decorator
from django.views.generic import View

from music_player.models import Song, Album, Artist, json_list

# Left in scope here so that it is compiled at module load time.
byte_range_re = re.compile(r'bytes=(\d+)-(\d+)?')
# Used by multiple functions/methods below.
song_order = ('album__artist__artist', 'album__album', 'track_number')

def file_range_generator(field_file, block_size, start, stop):
  '''
  A generator capable of returning a portion of a fieldfile.
  Args:
   field_file - fieldfile, the file to read data from
   block_size - int, the size of the buffer to serve
   start - int, the first byte served
   stop - int, the last byte served
  Yield: The current chunk of bytes to serve
  '''
  field_file.seek(start)
  remaining = stop - start + 1
  while remaining > 0:
    if remaining < block_size:
      block_size = remaining
    yield field_file.read(block_size)
    remaining -= block_size

# Misc Views
@login_required
def player(request):
  '''Displays a basic music player view with controls.'''
  songs = Song.objects.select_related().all().order_by(*song_order)
  return render(request, 'soniferous/player.html', {'songs':songs})

# Songs 
class SongView(View):
  '''
  Provides a REST interface for accessing songs.
  '''
  @method_decorator(login_required)
  def get(self, request, pk=None):
    '''
    Displays a single song in json format if pk is provided.
    Otherwise, displays a json listing of all songs.
    '''
    if pk:
      song = get_object_or_404(Song.objects.select_related(), pk=pk)
      return JsonResponse(song.json_format())
    else:
      songs = Song.objects.select_related().all().order_by(*song_order)
      return JsonResponse(json_list('songs', songs))

  @method_decorator(staff_member_required)
  def delete(self, request, pk):
    '''
    Deletes the given song. Any associated artists or albums that are no<commit_msg>Forced song deletes to be atomic.<commit_after>import os.path
import re

from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.db import transaction
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render, redirect
from django.utils.decorators import method_decorator
from django.views.generic import View

from music_player.models import Song, Album, Artist, json_list

# Left in scope here so that it is compiled at module load time.
byte_range_re = re.compile(r'bytes=(\d+)-(\d+)?')
# Used by multiple functions/methods below.
song_order = ('album__artist__artist', 'album__album', 'track_number')

def file_range_generator(field_file, block_size, start, stop):
  '''
  A generator capable of returning a portion of a fieldfile.
  Args:
   field_file - fieldfile, the file to read data from
   block_size - int, the size of the buffer to serve
   start - int, the first byte served
   stop - int, the last byte served
  Yield: The current chunk of bytes to serve
  '''
  field_file.seek(start)
  remaining = stop - start + 1
  while remaining > 0:
    if remaining < block_size:
      block_size = remaining
    yield field_file.read(block_size)
    remaining -= block_size

# Misc Views
@login_required
def player(request):
  '''Displays a basic music player view with controls.'''
  songs = Song.objects.select_related().all().order_by(*song_order)
  return render(request, 'soniferous/player.html', {'songs':songs})

# Songs 
class SongView(View):
  '''
  Provides a REST interface for accessing songs.
  '''
  @method_decorator(login_required)
  def get(self, request, pk=None):
    '''
    Displays a single song in json format if pk is provided.
    Otherwise, displays a json listing of all songs.
    '''
    if pk:
      song = get_object_or_404(Song.objects.select_related(), pk=pk)
      return JsonResponse(song.json_format())
    else:
      songs = Song.objects.select_related().all().order_by(*song_order)
      return JsonResponse(json_list('songs', songs))

  @method_decorator(staff_member_required)
  @method_decorator(transaction.atomic)
  def delete(self, request, pk):
    '''
    Deletes the given song. Any associated artists or albums that are no
{'fraction_non_alphanumeric': 0.09359823399558499, 'fraction_numerical': 0.003532008830022075, 'mean_word_length': 4.293224299065421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4764365', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1342, 'n_words': 439}