0002-views-block-upload-when-MAX_DOCUMENTS_PER_USER-limit.patch
fargo/fargo/views.py | ||
---|---|---|
34 | 34 |
from django.utils.http import quote |
35 | 35 |
from django.utils.translation import ugettext as _ |
36 | 36 |
from django.utils.decorators import method_decorator |
37 |
from django.utils.functional import cached_property |
|
37 | 38 |
from django.conf import settings |
38 | 39 | |
39 | 40 |
from django_tables2 import SingleTableMixin |
... | ... | |
53 | 54 |
self.logger = logging.getLogger(__name__) |
54 | 55 | |
55 | 56 | |
56 |
class CommonUpload(Logger, CreateView): |
|
57 |
class Documents(object): |
|
58 |
def get_queryset(self): |
|
59 |
return models.UserDocument.objects \ |
|
60 |
.filter(user=self.request.user) \ |
|
61 |
.select_related('document', 'user') |
|
62 | ||
63 |
@cached_property |
|
64 |
def count(self): |
|
65 |
return self.get_queryset().filter(origin__isnull=True).count() |
|
66 | ||
67 |
@cached_property |
|
68 |
def full(self): |
|
69 |
return self.count >= settings.FARGO_MAX_DOCUMENTS_PER_USER |
|
70 | ||
71 | ||
72 |
class CommonUpload(Logger, Documents, CreateView): |
|
57 | 73 |
form_class = forms.UploadForm |
58 | 74 |
model = models.UserDocument |
59 | 75 |
template_name = 'fargo/upload.html' |
... | ... | |
77 | 93 |
homepage = reverse('home') |
78 | 94 |
return self.request.GET.get(REDIRECT_FIELD_NAME, homepage) |
79 | 95 | |
96 |
def dispatch(self, request, *args, **kwargs): |
|
97 |
if self.full: |
|
98 |
return HttpResponseRedirect(self.get_success_url()) |
|
99 |
return super(Upload, self).dispatch(request, *args, **kwargs) |
|
100 | ||
80 | 101 |
def post(self, request, *args, **kwargs): |
81 | 102 |
if 'cancel' in request.POST: |
82 | 103 |
return HttpResponseRedirect(self.get_success_url()) |
83 | 104 |
return super(Upload, self).post(request, *args, **kwargs) |
84 | 105 | |
85 | 106 | |
86 |
class Documents(object): |
|
87 |
def get_queryset(self): |
|
88 |
return models.UserDocument.objects \ |
|
89 |
.filter(user=self.request.user) \ |
|
90 |
.select_related('document', 'user') |
|
91 | ||
92 | ||
93 |
class Homepage(Documents, SingleTableMixin, CommonUpload): |
|
107 |
class Homepage(SingleTableMixin, CommonUpload): |
|
94 | 108 |
'''Show documents of users, eventually paginate and sort them.''' |
95 | 109 |
template_name = 'fargo/home.html' |
96 | 110 |
form_class = forms.UploadForm |
... | ... | |
108 | 122 |
max_size = ctx['max_portfolio_size'] = settings.FARGO_MAX_DOCUMENT_BOX_SIZE |
109 | 123 |
ctx['occupancy_ratio'] = float(occupancy) / max_size |
110 | 124 |
ctx['occupancy_ratio_percent'] = float(occupancy) * 100.0 / max_size |
125 |
ctx['max_documents_per_user'] = settings.FARGO_MAX_DOCUMENTS_PER_USER |
|
126 |
ctx['full'] = self.full |
|
127 |
ctx['count'] = self.count |
|
111 | 128 |
return ctx |
112 | 129 | |
130 |
def post(self, request, *args, **kwargs): |
|
131 |
if self.full: |
|
132 |
return HttpResponseRedirect('') |
|
133 |
return super(CommonUpload, self).post(request, *args, **kwargs) |
|
134 | ||
135 | ||
113 | 136 | |
114 | 137 |
class PickView(object): |
115 | 138 |
def dispatch(self, request, *args, **kwargs): |
fargo/templates/fargo/home.html | ||
---|---|---|
44 | 44 |
<div class="cell"> |
45 | 45 |
<h2>{% if site_title %}{{ site_title }}{% else %}{% trans "Portfolio" %}{% endif %}</h2> |
46 | 46 |
<div id="user-files"> |
47 |
{% if count %} |
|
48 |
<div id="user-files-count"> |
|
49 |
{% blocktrans count documents_count=count trimmed %} |
|
50 |
You have {{ documents_count }} document stored. |
|
51 |
{% plural %} |
|
52 |
You have {{ documents_count }} documents stored. |
|
53 |
{% endblocktrans %} |
|
54 |
</div> |
|
55 |
{% endif %} |
|
56 | ||
47 | 57 |
{% if not table.page %} |
48 | 58 |
<div class="table-container"> |
49 | 59 |
{% endif %} |
... | ... | |
51 | 61 |
{% if not table.page %} |
52 | 62 |
</div> |
53 | 63 |
{% endif %} |
54 |
|
|
64 | ||
65 |
{% if full %} |
|
66 |
<div class="infonotice"> |
|
67 |
{% blocktrans count max_documents_per_user=max_documents_per_user trimmed %} |
|
68 |
The limit of {{ max_documents_per_user }} document per user is reached. |
|
69 |
You cannot add any more documents. |
|
70 |
{% plural %} |
|
71 |
The limit of {{ max_documents_per_user }} documents per user is reached. |
|
72 |
You cannot add any more documents. |
|
73 |
{% endblocktrans %} |
|
74 |
</div> |
|
75 |
{% else %} |
|
55 | 76 |
<form id="send-file" method="post" enctype="multipart/form-data"> |
56 | 77 |
{% csrf_token %} |
57 | 78 |
{{ form.non_field_errors }} |
... | ... | |
70 | 91 |
</div> |
71 | 92 |
<button>{% trans "Upload a new document" %}</button> |
72 | 93 |
</form> |
94 |
{% endif %} |
|
73 | 95 |
</div> |
74 | 96 |
</div> |
75 | 97 |
{% endblock %} |
tests/test_public.py | ||
---|---|---|
129 | 129 |
resp = app.get('/') |
130 | 130 |
assert 'monfichier.txt' not in resp.text |
131 | 131 |
resp = app.get(delete_url, status=404) |
132 | ||
133 | ||
134 |
def test_max_documents_per_user(app, private_settings, john_doe): |
|
135 |
private_settings.FARGO_MAX_DOCUMENTS_PER_USER = 1 |
|
136 |
login(app, user=john_doe) |
|
137 |
response1 = app.get('/') |
|
138 |
assert len(response1.forms) == 2 |
|
139 |
form = response1.form |
|
140 |
form['content'] = Upload('monfichier.pdf', b'coin', 'application/pdf') |
|
141 |
response2 = form.submit().follow() |
|
142 |
assert 'monfichier.pdf' in response2.text |
|
143 |
assert len(response2.forms) == 0 |
|
144 | ||
145 |
response2 = form.submit().follow() |
|
146 |
assert 'monfichier.pdf' in response2.text |
|
147 |
assert len(response2.forms) == 0 |
|
148 | ||
149 |
assert UserDocument.objects.count() == 1 |
|
150 | ||
151 |
response = app.get('/upload/') |
|
152 |
assert response.location == '/' |
|
132 |
- |