mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-22 09:31:08 +00:00
Check last user export too in post handler
This commit is contained in:
parent
145c67dd21
commit
ef57c0bc8b
1 changed files with 26 additions and 19 deletions
|
@ -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()
|
||||||
|
|
Loading…
Reference in a new issue