Skip to content

Commit

Permalink
Streaming (#65)
Browse files Browse the repository at this point in the history
* Streaming backups

* Bugfix - not importing recording title, version title, etc.

* Bugfix - not importing recording title, version title, etc. Blacked.

* Bugfix - not importing recording title, version title, etc. Blacked.

* Removed work in recordings in work dict...

* Finished with imports for now.

* Cleanup

* login screen css

* login screen css
missing migration

* Removing opposite migrations
  • Loading branch information
matijakolaric authored Dec 10, 2024
1 parent 6660e23 commit b79078f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
31 changes: 26 additions & 5 deletions music_publisher/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rest_framework.renderers import JSONRenderer

from .models import (
Writer,
Work,
Expand All @@ -13,7 +15,7 @@
from rest_framework import viewsets, serializers, permissions, renderers
from rest_framework.response import Response
from django.utils.timezone import now
from django.http import Http404
from django.http import Http404, StreamingHttpResponse


class DjangoModelPermissionsIncludingView(permissions.DjangoModelPermissions):
Expand Down Expand Up @@ -305,8 +307,27 @@ class BackupViewSet(viewsets.ViewSet):
permission_classes = [IsSuperuser]
renderer_classes = [renderers.JSONRenderer]

def json(self, request, *args, **kwargs):
yield """{"works":["""
renderer = JSONRenderer()
for i, work_item in enumerate(
Work.objects.get_dict_items(Work.objects.all())
):
if i > 0:
yield ","
yield renderer.render(work_item)
yield """],"releases":["""
for i, release in enumerate(Release.objects.all()):
if i > 0:
yield ","
yield renderer.render(release.get_dict(with_tracks=True))
yield """]}"""

def list(self, request, *args, **kwargs):
d = {}
d.update(Work.objects.get_dict(Work.objects.all()))
d.update(CommercialRelease.objects.get_dict(Release.objects.all()))
return Response(d)
response = StreamingHttpResponse(
self.json(request, *args, **kwargs),
status=200,
content_type="application/json",
)
response["Cache-Control"] = ("no-cache",)
return response
8 changes: 5 additions & 3 deletions music_publisher/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class DataImporter(object):
"release_date",
"duration",
"isrc",
"artist_last",
"artist_first",
"artist_isni",
# "artist_last",
# "artist_first",
# "artist_isni",
"record_label",
]
REFERENCE_FIELDS = ["id", "cmo"]
Expand Down Expand Up @@ -492,6 +492,8 @@ def process_row(self, row):
isrc=recording.get("isrc"),
duration=recording.get("duration"),
release_date=recording.get("release_date"),
recording_title=recording.get("recording_title", ""),
version_title=recording.get("version_title", ""),
)
recording.clean_fields()
recording.clean()
Expand Down
24 changes: 18 additions & 6 deletions music_publisher/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,9 @@ def get_queryset(self):
"""
return super().get_queryset().prefetch_related("writers")

def get_dict(self, qs):
def get_dict_items(self, qs):
"""
Return a dictionary with works from the queryset
Yield dictionary items for works from the queryset
Args:
qs(django.db.models.query import QuerySet)
Expand All @@ -551,11 +551,23 @@ def get_dict(self, qs):
qs = qs.prefetch_related("recordings__tracks__release__release_label")
qs = qs.prefetch_related("workacknowledgement_set")

works = []

for work in qs:
j = work.get_dict()
works.append(j)
yield j

def get_dict(self, qs):
"""
Return a dictionary with works from the queryset
Args:
qs(django.db.models.query import QuerySet)
Returns:
dict: dictionary with works
"""

works = list(self.get_dict_items(qs))

return {
"works": works,
Expand Down Expand Up @@ -817,7 +829,7 @@ def get_dict(self, with_recordings=True):

if with_recordings:
j["recordings"] = [
recording.get_dict(with_releases=True)
recording.get_dict(with_releases=True, with_work=False)
for recording in self.recordings.all()
]

Expand Down
6 changes: 5 additions & 1 deletion music_publisher/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@
height: 1rem;
box-sizing: content-box;
}


.login input[type=text], .login input[type=password] {
height: 1.8rem;
}

#content h1 {color: var(--body-fg); padding: 8px}

h2 {padding: 8px}
Expand Down
6 changes: 5 additions & 1 deletion music_publisher/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datetime import datetime
from decimal import Decimal
from io import StringIO
import json

from django.contrib.admin.models import LogEntry
from django.contrib.admin.options import IS_POPUP_VAR
Expand Down Expand Up @@ -1761,7 +1762,10 @@ def test_endpoints(self):
request = factory.get(url)
force_authenticate(request, self.superuser)
response = BackupViewSet.as_view({"get": "list"})(request)
response.render()
d = json.loads(response.getvalue())
self.assertIn("works", d)
self.assertIn("releases", d)
self.assertEqual(len(d["releases"]), 4)
self.assertEqual(response.status_code, 200)

response = ArtistViewSet.as_view({"get": "list"})(request)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ Pillow==11.0.0
psycopg2-binary==2.9.10
waitress==3.0.2
whitenoise==6.8.2

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="django-music-publisher",
version="24.12",
version="24.12.1a1",
author="Matija Kolarić",
author_email="matijakolaric@users.noreply.github.com",
license="MIT License",
Expand Down

0 comments on commit b79078f

Please sign in to comment.