def refresh_thumbnails(self, request, queryset): generated = 0 errors = 0 for task in queryset: if task.is_file(): filename = task.file_attachment.get_full_path_and_filename() try: thumbnail_filename = create_file_thumbnail(filename) except ThumbnailRenderingException: errors += 1 continue generated += 1 task.cache_file_attachment_thumbnail_url = \ media_path_to_url(thumbnail_filename) task.save() self.message_user(request, "Generated {} thumbnail(s). {} error(s).".format( generated, errors))
def get_url(self): return media_path_to_url(self.file.name)
def new(request, task_id=None, is_file=None, is_lecture=None): """ New Task and Edit Task + New TaskFile and Edit TaskFile """ content_type = ContentType.objects.get_for_model(Task) if task_id: task = get_object_or_404(Task.objects.select_related('content'), pk=task_id) perm = task.get_user_permissions(request.user) if EDIT not in perm: return 403 math_content = task.content edit = True is_file = task.is_file() is_lecture = task.is_lecture else: perm = [] task = math_content = None edit = False # Make sure each lecture is a file. assert is_lecture and is_file or not is_lecture form_class = TaskLectureForm if is_lecture \ else (TaskFileForm if is_file else TaskForm) math_content_label = 'Opis' if is_file else None # else default if request.method == 'POST': old_hidden = getattr(task, 'hidden', -1) old_solvable = getattr(task, 'solvable', -1) # Files can have blank description (i.e. math content) task_form = form_class(request.POST, instance=task, user=request.user) math_content_form = MathContentForm(request.POST, instance=math_content, blank=is_file, label=math_content_label, auto_preview=False) attachment_form = is_file and not edit \ and AttachmentForm(request.POST, request.FILES) if task_form.is_valid() and math_content_form.is_valid() \ and (not attachment_form or attachment_form.is_valid()): task = task_form.save(commit=False) math_content = math_content_form.save() if not edit: if attachment_form: attachment, attachment_form = check_and_save_attachment( request, math_content) task.file_attachment = attachment # This is a file. path = attachment.get_full_path_and_filename() try: thumbnail_path = create_file_thumbnail(path) task.cache_file_attachment_thumbnail_url = \ media_path_to_url(thumbnail_path) except ThumbnailRenderingException: pass # Immediately remember file url, so that we don't have to # access Attachment table to show the link. task.cache_file_attachment_url = attachment.get_url() else: task.file_attachment = None # This is a task. if is_file: task.cache_file_attachment_url = task.file_attachment.get_url() if not edit: task.author = request.user task.content = math_content task.save() # Do not call task_form.save_m2m()! set_tags(task, task_form.cleaned_data['tags']) # TODO: signals! if not edit or old_hidden != task.hidden \ or old_solvable != task.solvable: \ invalidate_folder_cache_for_task(task) # send action if creating a new nonhidden task if not edit: # TODO: signals! type = _action.LECTURE_ADD if is_lecture \ else (_action.FILE_ADD if is_file else _action.TASK_ADD) _action.add(request.user, type, action_object=task, target=task) # TODO: izbrisati task_new_finish.html i url #return HttpResponseRedirect('/task/%d/' % task.id if edit else '/task/new/finish/') return HttpResponseRedirect(task.get_absolute_url()) else: task_form = form_class(instance=task) math_content_form = MathContentForm(instance=math_content, blank=is_file, label=math_content_label, auto_preview=False) attachment_form = is_file and not edit and AttachmentForm() forms = [task_form, math_content_form] if attachment_form: forms.append(attachment_form) data = get_task_folder_data(task, request.user) if task else {} data.update({ 'action_url': request.path, 'bulk_add_url': '/task/new/bulk/', 'can_edit_permissions': EDIT_PERMISSIONS in perm, 'content_type': content_type, 'edit': edit, 'forms': forms, 'is_file': is_file, 'is_lecture': is_lecture, 'lectures_folder_url': settings.LECTURES_FOLDER_URL, 'task_name': task.name if task else None, # Convenience. 'task': task, }) return data