|
4
|
1 |
from django.conf import settings |
|
|
2 |
from django.contrib.auth.decorators import login_required |
|
|
3 |
from django.core.paginator import Paginator, InvalidPage, EmptyPage |
|
|
4 |
from django.shortcuts import redirect |
|
|
5 |
from django.utils.decorators import method_decorator |
|
27
|
6 |
from django.views.decorators.cache import never_cache |
|
4
|
7 |
from django.views.generic.base import View, TemplateResponseMixin |
|
|
8 |
from ldt.ldt_utils.models import Project, Content |
|
27
|
9 |
from ldt.ldt_utils.views.content import write_content_base |
|
|
10 |
from ldt.ldt_utils.utils import generate_uuid |
|
20
|
11 |
from metadatacomposer.forms import ImageUploadModelForm |
|
|
12 |
from metadatacomposer.models import Image |
|
15
|
13 |
|
|
|
14 |
|
|
|
15 |
import logging #@UnresolvedImport |
|
|
16 |
logger = logging.getLogger(__name__) |
|
|
17 |
|
|
4
|
18 |
|
|
5
|
19 |
class MetadataComposerContextView(View): |
|
4
|
20 |
|
|
|
21 |
branding = "iri" |
|
|
22 |
|
|
|
23 |
def get_context_dict(self, request): |
|
|
24 |
context = {} |
|
|
25 |
context["branding"] = self.branding |
|
30
|
26 |
context["top_header_css"] = "metadatacomposer/%s/composer_header_css.html" % self.branding |
|
|
27 |
context["top_header_partial"] = "metadatacomposer/%s/composer_header.html" % self.branding |
|
4
|
28 |
return context |
|
5
|
29 |
|
|
4
|
30 |
|
|
5
|
31 |
|
|
|
32 |
class MetadataComposerHome(TemplateResponseMixin, MetadataComposerContextView): |
|
4
|
33 |
|
|
|
34 |
def get_template_names(self): |
|
|
35 |
return "metadatacomposer_home.html" |
|
|
36 |
|
|
|
37 |
@method_decorator(login_required) |
|
27
|
38 |
@method_decorator(never_cache) |
|
4
|
39 |
def dispatch(self, *args, **kwargs): |
|
|
40 |
return super(MetadataComposerHome, self).dispatch(*args, **kwargs) |
|
|
41 |
|
|
|
42 |
def get(self, request, branding="iri", **kwargs): |
|
|
43 |
self.branding = branding |
|
|
44 |
|
|
|
45 |
projects = Project.safe_objects.filter(owner=request.user).order_by('-modification_date')[:6] |
|
20
|
46 |
images = Image.objects.order_by('-modification_date')[:6] |
|
4
|
47 |
contents = Content.safe_objects.order_by('-update_date')[:6] |
|
|
48 |
|
|
|
49 |
context = self.get_context_dict(request) |
|
20
|
50 |
context.update({"projects":projects, "images": images, "contents": contents}) |
|
4
|
51 |
return self.render_to_response(context) |
|
5
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
class MetadataComposerProjectList(TemplateResponseMixin, MetadataComposerContextView): |
|
4
|
56 |
|
|
5
|
57 |
def get_template_names(self): |
|
|
58 |
return "metadatacomposer_project_list.html" |
|
|
59 |
|
|
|
60 |
@method_decorator(login_required) |
|
27
|
61 |
@method_decorator(never_cache) |
|
5
|
62 |
def dispatch(self, *args, **kwargs): |
|
|
63 |
return super(MetadataComposerProjectList, self).dispatch(*args, **kwargs) |
|
|
64 |
|
|
|
65 |
def get(self, request, branding="iri", **kwargs): |
|
|
66 |
self.branding = branding |
|
|
67 |
page = request.GET.get("page") or 1 |
|
|
68 |
|
|
|
69 |
projects = Project.safe_objects.prefetch_related("contents").order_by('-modification_date').filter(owner=request.user) |
|
|
70 |
nb = getattr(settings, 'METADATACOMPOSER_ELEMENTS_PER_PAGE', 9) |
|
|
71 |
if page=="x": |
|
|
72 |
nb = projects.count() |
|
|
73 |
|
|
|
74 |
paginator = Paginator(projects, nb) |
|
|
75 |
try: |
|
|
76 |
results = paginator.page(page) |
|
|
77 |
except (EmptyPage, InvalidPage): |
|
|
78 |
results = paginator.page(paginator.num_pages) |
|
|
79 |
|
|
|
80 |
context = self.get_context_dict(request) |
|
|
81 |
context.update({"results":results}) |
|
|
82 |
return self.render_to_response(context) |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
class MetadataComposerResourceList(TemplateResponseMixin, MetadataComposerContextView): |
|
|
87 |
|
|
|
88 |
def get_template_names(self): |
|
|
89 |
return "metadatacomposer_resource_list.html" |
|
|
90 |
|
|
|
91 |
@method_decorator(login_required) |
|
27
|
92 |
@method_decorator(never_cache) |
|
5
|
93 |
def dispatch(self, *args, **kwargs): |
|
|
94 |
return super(MetadataComposerResourceList, self).dispatch(*args, **kwargs) |
|
|
95 |
|
|
|
96 |
def get(self, request, branding="iri", **kwargs): |
|
|
97 |
self.branding = branding |
|
|
98 |
|
|
26
|
99 |
# We get the first page of images |
|
|
100 |
image_results = get_images(1) |
|
15
|
101 |
# We get the first contents page and theirs projects |
|
5
|
102 |
content_results = get_contents_and_projects(1, request.user) |
|
|
103 |
|
|
|
104 |
context = self.get_context_dict(request) |
|
26
|
105 |
context.update({"image_results":image_results, "content_results":content_results}) |
|
|
106 |
return self.render_to_response(context) |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
class MetadataComposerImagePagination(TemplateResponseMixin, MetadataComposerContextView): |
|
|
111 |
|
|
|
112 |
def get_template_names(self): |
|
|
113 |
return "partial/resource_image_list.html" |
|
|
114 |
|
|
|
115 |
@method_decorator(login_required) |
|
27
|
116 |
@method_decorator(never_cache) |
|
26
|
117 |
def dispatch(self, *args, **kwargs): |
|
|
118 |
return super(MetadataComposerImagePagination, self).dispatch(*args, **kwargs) |
|
|
119 |
|
|
|
120 |
def get(self, request, branding="iri", **kwargs): |
|
|
121 |
self.branding = branding |
|
|
122 |
page = request.GET.get("page") or 1 |
|
|
123 |
# Get current contents page and theirs projects |
|
|
124 |
image_results = get_images(page) |
|
|
125 |
|
|
|
126 |
context = self.get_context_dict(request) |
|
|
127 |
context.update({"image_results":image_results}) |
|
5
|
128 |
return self.render_to_response(context) |
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
class MetadataComposerContentPagination(TemplateResponseMixin, MetadataComposerContextView): |
|
|
133 |
|
|
|
134 |
def get_template_names(self): |
|
|
135 |
return "partial/resource_content_list.html" |
|
|
136 |
|
|
|
137 |
@method_decorator(login_required) |
|
27
|
138 |
@method_decorator(never_cache) |
|
5
|
139 |
def dispatch(self, *args, **kwargs): |
|
|
140 |
return super(MetadataComposerContentPagination, self).dispatch(*args, **kwargs) |
|
|
141 |
|
|
|
142 |
def get(self, request, branding="iri", **kwargs): |
|
|
143 |
self.branding = branding |
|
|
144 |
page = request.GET.get("page") or 1 |
|
15
|
145 |
# Get current contents page and theirs projects |
|
5
|
146 |
content_results = get_contents_and_projects(page, request.user) |
|
|
147 |
|
|
|
148 |
context = self.get_context_dict(request) |
|
|
149 |
context.update({"content_results":content_results}) |
|
|
150 |
return self.render_to_response(context) |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
26
|
154 |
def get_images(page): |
|
|
155 |
|
|
|
156 |
# We get the current's page images |
|
|
157 |
images = Image.objects.order_by('-modification_date') |
|
|
158 |
#nb = getattr(settings, 'METADATACOMPOSER_ELEMENTS_PER_PAGE', 9) |
|
|
159 |
nb = 2 |
|
|
160 |
if page=="x": |
|
|
161 |
nb = images.count() |
|
|
162 |
paginator = Paginator(images, nb) |
|
|
163 |
try: |
|
|
164 |
results = paginator.page(page) |
|
|
165 |
except (EmptyPage, InvalidPage): |
|
|
166 |
results = paginator.page(paginator.num_pages) |
|
|
167 |
|
|
|
168 |
return results |
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
5
|
172 |
def get_contents_and_projects(page, user): |
|
|
173 |
|
|
|
174 |
# We get the current's page contents |
|
15
|
175 |
# prefetch_related("project_set") is unfortunately useless because we have to filter the project queryset later |
|
5
|
176 |
contents = Content.safe_objects.order_by('-update_date') |
|
|
177 |
nb = getattr(settings, 'METADATACOMPOSER_ELEMENTS_PER_PAGE', 9) |
|
|
178 |
if page=="x": |
|
|
179 |
nb = contents.count() |
|
|
180 |
paginator = Paginator(contents, nb) |
|
|
181 |
try: |
|
|
182 |
results = paginator.page(page) |
|
|
183 |
except (EmptyPage, InvalidPage): |
|
|
184 |
results = paginator.page(paginator.num_pages) |
|
|
185 |
|
|
|
186 |
# We add the user's projects for each content |
|
|
187 |
results_object_list = [] |
|
|
188 |
for content in results.object_list: |
|
15
|
189 |
# We filter the content's projects with the user's ones |
|
5
|
190 |
projects = content.project_set.all().filter(owner=user) |
|
|
191 |
results_object_list.append({"content":content, "projects":projects}) |
|
15
|
192 |
|
|
5
|
193 |
results.object_list = results_object_list |
|
|
194 |
return results |
|
19
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
class MetadataComposerModalVideo(TemplateResponseMixin, MetadataComposerContextView): |
|
|
199 |
|
|
|
200 |
def get_template_names(self): |
|
|
201 |
return "metadatacomposer_modal_video.html" |
|
|
202 |
|
|
|
203 |
@method_decorator(login_required) |
|
27
|
204 |
@method_decorator(never_cache) |
|
19
|
205 |
def dispatch(self, *args, **kwargs): |
|
|
206 |
return super(MetadataComposerModalVideo, self).dispatch(*args, **kwargs) |
|
|
207 |
|
|
27
|
208 |
def get(self, request, branding="iri", iri_id=None, **kwargs): |
|
19
|
209 |
self.branding = branding |
|
27
|
210 |
|
|
|
211 |
# Generate fake id to validate ContentForm |
|
|
212 |
if not iri_id: |
|
|
213 |
iri_id = generate_uuid() |
|
|
214 |
content = None |
|
|
215 |
else: |
|
|
216 |
content = Content.safe_objects.select_related("media_obj").get(iri_id=iri_id) |
|
|
217 |
|
|
19
|
218 |
context = self.get_context_dict(request) |
|
27
|
219 |
context.update({"iri_id":iri_id, "content":content}) |
|
19
|
220 |
return self.render_to_response(context) |
|
27
|
221 |
|
|
|
222 |
def post(self, request, branding="iri", iri_id=None, **kwargs): |
|
|
223 |
self.branding = branding |
|
|
224 |
|
|
|
225 |
# We create the media |
|
|
226 |
content_form, media_form, picture_form, form_status, _, current_front_project, e, traceback = write_content_base(request, iri_id) |
|
|
227 |
# And test creation |
|
|
228 |
if (content_form == False and media_form == False and picture_form == False and form_status == False and current_front_project == False): |
|
|
229 |
#message=_("An error occurred - Please try again or contact webmaster") |
|
|
230 |
#title = _("Error") |
|
|
231 |
raise e, None, traceback |
|
|
232 |
return redirect(request.META['HTTP_REFERER']) |
|
19
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
class MetadataComposerModalImage(TemplateResponseMixin, MetadataComposerContextView): |
|
|
237 |
|
|
|
238 |
def get_template_names(self): |
|
|
239 |
return "metadatacomposer_modal_image.html" |
|
|
240 |
|
|
|
241 |
@method_decorator(login_required) |
|
27
|
242 |
@method_decorator(never_cache) |
|
19
|
243 |
def dispatch(self, *args, **kwargs): |
|
|
244 |
return super(MetadataComposerModalImage, self).dispatch(*args, **kwargs) |
|
|
245 |
|
|
|
246 |
def get(self, request, branding="iri", **kwargs): |
|
|
247 |
self.branding = branding |
|
26
|
248 |
image_pk = request.GET.get("image") or None |
|
20
|
249 |
|
|
|
250 |
# Add form |
|
26
|
251 |
if image_pk: |
|
|
252 |
form = ImageUploadModelForm(instance=Image.objects.get(pk=image_pk)) |
|
|
253 |
else: |
|
|
254 |
form = ImageUploadModelForm() |
|
20
|
255 |
|
|
19
|
256 |
context = self.get_context_dict(request) |
|
26
|
257 |
context.update({"form":form, "image_pk":image_pk}) |
|
19
|
258 |
return self.render_to_response(context) |
|
20
|
259 |
|
|
|
260 |
def post(self, request, branding="iri", **kwargs): |
|
|
261 |
self.branding = branding |
|
|
262 |
|
|
|
263 |
# Check form |
|
26
|
264 |
if 'image_pk' in request.POST: |
|
|
265 |
form = ImageUploadModelForm(request.POST, request.FILES, instance=Image.objects.get(pk=request.POST['image_pk'])) |
|
|
266 |
else: |
|
|
267 |
form = ImageUploadModelForm(request.POST, request.FILES) |
|
20
|
268 |
if form.is_valid(): |
|
|
269 |
# If an image id was in the form, we update the existing image |
|
|
270 |
form.save() |
|
26
|
271 |
return redirect(request.META['HTTP_REFERER']) |
|
20
|
272 |
else: |
|
|
273 |
context = self.get_context_dict(request) |
|
|
274 |
context.update({"form":form}) |
|
26
|
275 |
return redirect("composer_modal_image", branding=branding) |
|
20
|
276 |
|
|
|
277 |
|
|
|
278 |
|