def edit_attachments(request, id): content = get_object_or_404(MathContent, id=id) if request.method == 'POST': attachment, form = check_and_save_attachment(request, content) else: form = AttachmentForm() data = { 'content': content, 'form': form, } # Type-specific tuning and permissions: try: task = Task.objects.get(content_id=id) if not task.is_allowed_to_edit(request.user): return 403 data['task'] = task data.update(get_task_folder_data(task, request.user)) except Task.DoesNotExist: # Only Task's MathContent can have attachments! return 404 return data
def detail(request, id): task = get_object_or_404(Task, id=id) perm = task.get_user_permissions(request.user) if VIEW not in perm: return (403, "Not allowed to view this task!") if not check_prerequisites_for_task(task, request.user, perm): return (403, "Prerequisites not met, not allowed to view the task!") # Remember my solution. try: solution = Solution.objects.get(author_id=request.user.id, task_id=task.id) except: solution = None task.cache_solution = solution data = {"task": task, "can_edit": EDIT in perm, "solution": solution} featured_folder_id = getattr(settings, "FEATURED_LECTURES_FOLDER_ID", None) if task.is_lecture and featured_folder_id and can_edit_featured_lectures(request.user): # The case where task is hidden is handled in the template. data["can_select_as_featured"] = True data["is_featured"] = FolderTask.objects.filter(folder_id=featured_folder_id, task_id=id).exists() folder_data = get_task_folder_data(task, request.user) if folder_data: data.update(folder_data) return data
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