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):
|