From c4c011a1bb8de5e0c7baf52b44dc296d023d1513 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 3 Apr 2018 10:44:38 +0200 Subject: [PATCH] publish occupancy (#16828) As an absolute size, as a ratio of the max size and as a percentage of the max size. --- fargo/fargo/models.py | 4 ++++ fargo/fargo/views.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/fargo/fargo/models.py b/fargo/fargo/models.py index 4f0ceee..23a97b9 100644 --- a/fargo/fargo/models.py +++ b/fargo/fargo/models.py @@ -212,6 +212,10 @@ class Document(models.Model): thumbnail.height, data_url) + @classmethod + def occupancy_for_user(cls, user): + return float(sum(document.content.size for document in cls.objects.filter(user_documents__user=user).distinct())) + def __unicode__(self): return u'%s %s' % (os.path.basename(self.content.name), self.content_hash[:6]) diff --git a/fargo/fargo/views.py b/fargo/fargo/views.py index 904a3c5..54f4f0f 100644 --- a/fargo/fargo/views.py +++ b/fargo/fargo/views.py @@ -86,6 +86,10 @@ class Homepage(Documents, SingleTableMixin, CommonUpload): ctx = super(Homepage, self).get_context_data(**kwargs) ctx['include_edit_link'] = settings.INCLUDE_EDIT_LINK ctx['max_document_size'] = settings.FARGO_MAX_DOCUMENT_SIZE + occupancy = ctx['occupancy'] = models.Document.occupancy_for_user(self.request.user) + max_size = ctx['max_portfolio_size'] = settings.FARGO_MAX_DOCUMENT_BOX_SIZE + ctx['occupancy_ratio'] = float(occupancy) / max_size + ctx['occupancy_ratio_percent'] = float(occupancy) * 100.0 / max_size return ctx def get_success_url(self): @@ -182,7 +186,7 @@ class Thumbnail(Documents, View): thumbnail = user_document.document.thumbnail if not thumbnail: raise Http404 - return HttpResponse(thumbnail.chunks(), content_type='image/jpeg') + return HttpResponse(thumbnail.read(), content_type='image/jpeg') class RemoteDownload(Download): -- 2.16.3