Check last user export too in post handler

This commit is contained in:
Bart Schuurmans 2024-03-26 13:16:08 +01:00
parent 145c67dd21
commit ef57c0bc8b

View file

@ -148,21 +148,35 @@ class Export(View):
@method_decorator(login_required, name="dispatch") @method_decorator(login_required, name="dispatch")
class ExportUser(View): class ExportUser(View):
""" """
Let users export user data to import into another Bookwyrm instance Let users request and download an archive of user data to import into
This view creates signed URLs to pre-processed export files in another Bookwyrm instance.
s3 storage on load (if they exist) and allows the user to create
a new file.
""" """
user_jobs = None
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.user_jobs = BookwyrmExportJob.objects.filter(user=request.user).order_by(
"-created_date"
)
def new_export_blocked_until(self):
"""whether the user is allowed to request a new export"""
last_job = self.user_jobs.first()
if not last_job:
return None
site = models.SiteSettings.objects.get()
blocked_until = last_job.created_date + timedelta(
hours=site.user_import_time_limit
)
return blocked_until if blocked_until > timezone.now() else None
def get(self, request): def get(self, request):
"""Request tar file""" """Request tar file"""
jobs = BookwyrmExportJob.objects.filter(user=request.user).order_by(
"-created_date"
)
exports = [] exports = []
for job in jobs: for job in self.user_jobs:
export = {"job": job} export = {"job": job}
if job.export_data: if job.export_data:
@ -178,16 +192,7 @@ class ExportUser(View):
exports.append(export) exports.append(export)
site = models.SiteSettings.objects.get() next_available = self.new_export_blocked_until()
hours = site.user_import_time_limit
allowed = (
jobs.first().created_date < timezone.now() - timedelta(hours=hours)
if jobs.first()
else True
)
next_available = (
jobs.first().created_date + timedelta(hours=hours) if not allowed else False
)
paginated = Paginator(exports, settings.PAGE_LENGTH) paginated = Paginator(exports, settings.PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page")) page = paginated.get_page(request.GET.get("page"))
data = { data = {
@ -202,6 +207,8 @@ class ExportUser(View):
def post(self, request): def post(self, request):
"""Trigger processing of a new user export file""" """Trigger processing of a new user export file"""
if self.new_export_blocked_until() is not None:
return HttpResponse(status=429) # Too Many Requests
job = BookwyrmExportJob.objects.create(user=request.user) job = BookwyrmExportJob.objects.create(user=request.user)
job.start_job() job.start_job()