cleanup and linting

This commit is contained in:
Hugh Rundle 2024-01-18 18:43:45 +11:00
parent 833f26fd0e
commit 469172947b
No known key found for this signature in database
GPG key ID: A7E35779918253F9
4 changed files with 26 additions and 18 deletions

View file

@ -2,14 +2,14 @@
import dataclasses import dataclasses
import logging import logging
import boto3
from s3_tar import S3Tar
from uuid import uuid4 from uuid import uuid4
from s3_tar import S3Tar
from django.db.models import CASCADE, BooleanField, FileField, ForeignKey, JSONField from django.db.models import CASCADE, BooleanField, FileField, ForeignKey, JSONField
from django.db.models import Q from django.db.models import Q
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.core.files.base import ContentFile, File from django.core.files.base import ContentFile
from django.utils import timezone from django.utils import timezone
from bookwyrm import settings, storage_backends from bookwyrm import settings, storage_backends
@ -18,7 +18,7 @@ from bookwyrm.models import AnnualGoal, ReadThrough, ShelfBook, List, ListItem
from bookwyrm.models import Review, Comment, Quotation from bookwyrm.models import Review, Comment, Quotation
from bookwyrm.models import Edition from bookwyrm.models import Edition
from bookwyrm.models import UserFollows, User, UserBlocks from bookwyrm.models import UserFollows, User, UserBlocks
from bookwyrm.models.job import ParentJob, ChildJob, ParentTask, SubTask from bookwyrm.models.job import ParentJob, ChildJob, ParentTask
from bookwyrm.tasks import app, IMPORTS from bookwyrm.tasks import app, IMPORTS
from bookwyrm.utils.tar import BookwyrmTarFile from bookwyrm.utils.tar import BookwyrmTarFile
@ -82,6 +82,7 @@ class AddBookToUserExportJob(ChildJob):
edition = ForeignKey(Edition, on_delete=CASCADE) edition = ForeignKey(Edition, on_delete=CASCADE)
# pylint: disable=too-many-locals
def start_job(self): def start_job(self):
"""Start the job""" """Start the job"""
try: try:
@ -185,7 +186,7 @@ class AddFileToTar(ChildJob):
parent_export_job = ForeignKey( parent_export_job = ForeignKey(
BookwyrmExportJob, on_delete=CASCADE, related_name="child_edition_export_jobs" BookwyrmExportJob, on_delete=CASCADE, related_name="child_edition_export_jobs"
) # TODO: do we actually need this? Does self.parent_job.export_data work? )
def start_job(self): def start_job(self):
"""Start the job""" """Start the job"""
@ -196,7 +197,6 @@ class AddFileToTar(ChildJob):
# but Hugh couldn't make that work # but Hugh couldn't make that work
try: try:
task_id = self.parent_export_job.task_id
export_data = self.parent_export_job.export_data export_data = self.parent_export_job.export_data
export_json = self.parent_export_job.export_json export_json = self.parent_export_job.export_json
json_data = DjangoJSONEncoder().encode(export_json) json_data = DjangoJSONEncoder().encode(export_json)
@ -209,9 +209,12 @@ class AddFileToTar(ChildJob):
f"exports/{str(self.parent_export_job.task_id)}.tar.gz", f"exports/{str(self.parent_export_job.task_id)}.tar.gz",
) )
# TODO: either encrypt the file or we will need to get it to the user # TODO: will need to get it to the user
# from this secure part of the bucket # from this secure part of the bucket
export_data.save("archive.json", ContentFile(json_data.encode("utf-8"))) export_data.save(
"archive.json",
ContentFile(json_data.encode("utf-8"))
)
s3_job.add_file(f"exports/{export_data.name}") s3_job.add_file(f"exports/{export_data.name}")
s3_job.add_file(f"images/{user.avatar.name}", folder="avatar") s3_job.add_file(f"images/{user.avatar.name}", folder="avatar")
@ -222,14 +225,12 @@ class AddFileToTar(ChildJob):
s3_job.tar() s3_job.tar()
# delete export json as soon as it's tarred # delete export json as soon as it's tarred
# there is probably a better way to do this # TODO: there is probably a better way to do this
# Currently this merely makes the file empty # Currently this merely makes the file empty even though
# we're using save=False
export_data.delete(save=False) export_data.delete(save=False)
else: else:
# TODO: is the export_data file open to the world?
logger.info("export file URL: %s", export_data.url)
export_data.open("wb") export_data.open("wb")
with BookwyrmTarFile.open(mode="w:gz", fileobj=export_data) as tar: with BookwyrmTarFile.open(mode="w:gz", fileobj=export_data) as tar:
@ -238,8 +239,8 @@ class AddFileToTar(ChildJob):
# Add avatar image if present # Add avatar image if present
if getattr(user, "avatar", False): if getattr(user, "avatar", False):
tar.add_image( tar.add_image(
user.avatar, filename="avatar", directory=f"avatar/" user.avatar, filename="avatar", directory="avatar/"
) # TODO: does this work? )
for book in editions: for book in editions:
if getattr(book, "cover", False): if getattr(book, "cover", False):
@ -319,7 +320,7 @@ def export_reading_goals_task(**kwargs):
reading_goals = AnnualGoal.objects.filter(user=job.user).distinct() reading_goals = AnnualGoal.objects.filter(user=job.user).distinct()
job.export_json["goals"] = [] job.export_json["goals"] = []
for goal in reading_goals: for goal in reading_goals:
exported_user["goals"].append( job.export_json["goals"].append(
{"goal": goal.goal, "year": goal.year, "privacy": goal.privacy} {"goal": goal.goal, "year": goal.year, "privacy": goal.privacy}
) )
job.save(update_fields=["export_json"]) job.save(update_fields=["export_json"])

View file

@ -148,8 +148,8 @@ def get_file_size(file):
return "" return ""
except Exception as e: # pylint: disable=broad-except except Exception as error: # pylint: disable=broad-except
print(e) print(error)
return "" return ""

View file

@ -141,6 +141,8 @@ class BookwyrmExport(TestCase):
book=self.edition, book=self.edition,
) )
# pylint: disable=E1121
def test_json_export_user_settings(self): def test_json_export_user_settings(self):
"""Test the json export function for basic user info""" """Test the json export function for basic user info"""
data = export_job.json_export(self.local_user) data = export_job.json_export(self.local_user)
@ -158,6 +160,8 @@ class BookwyrmExport(TestCase):
) )
self.assertEqual(user_data["settings"]["default_post_privacy"], "followers") self.assertEqual(user_data["settings"]["default_post_privacy"], "followers")
# pylint: disable=E1121
def test_json_export_extended_user_data(self): def test_json_export_extended_user_data(self):
"""Test the json export function for other non-book user info""" """Test the json export function for other non-book user info"""
data = export_job.json_export(self.local_user) data = export_job.json_export(self.local_user)
@ -180,6 +184,8 @@ class BookwyrmExport(TestCase):
self.assertEqual(len(json_data["blocks"]), 1) self.assertEqual(len(json_data["blocks"]), 1)
self.assertEqual(json_data["blocks"][0], "https://your.domain.here/user/badger") self.assertEqual(json_data["blocks"][0], "https://your.domain.here/user/badger")
# pylint: disable=E1121
def test_json_export_books(self): def test_json_export_books(self):
"""Test the json export function for extended user info""" """Test the json export function for extended user info"""

View file

@ -187,6 +187,7 @@ class ExportUser(View):
class ExportArchive(View): class ExportArchive(View):
"""Serve the archive file""" """Serve the archive file"""
# TODO: how do we serve s3 files?
def get(self, request, archive_id): def get(self, request, archive_id):
"""download user export file""" """download user export file"""
export = BookwyrmExportJob.objects.get(task_id=archive_id, user=request.user) export = BookwyrmExportJob.objects.get(task_id=archive_id, user=request.user)