Esempio n. 1
0
def submit(request, task_id=None, solution_id=None):
    if solution_id:
        solution = get_object_or_404(Solution, pk=solution_id)
        if not solution.can_edit(request.user):
            return (403, u'Not allowed to edit the solution!')
        task = solution.task
        edit = True
    elif task_id:
        task = get_object_or_404(Task, pk=task_id)
        edit = False
    else:
        return 404

    if not task.solvable:
        return (403, u'This task is not solvable!')

    if not task.is_allowed_to_solve(request.user):
        return (403, u'You are not allowed to send solutions for this task.')

    if not edit:
        solution, dummy = Solution.objects.get_or_create(
                task=task, author=request.user)

    math_content = solution.content

    if request.method == 'POST':
        math_content_form = MathContentForm(request.POST, instance=math_content)
        if math_content_form.is_valid():
            math_content = math_content_form.save()

            was_solved = solution.is_solved()

            solution.content = math_content
            solution.status = SolutionStatus.SUBMITTED
            solution.date_created = datetime.now()
            solution.save()
            if not edit:
                _action.replace_or_add(request.user, _action.SOLUTION_SUBMIT,
                    action_object=solution, target=task)

            # update solved count if necessary
            delta = solution.is_solved() - was_solved
            if delta:
                _update_solved_count(delta, task, request.user.get_profile())

            return ("/solution/%d/" % (solution.id,),)
    else:
        math_content_form = MathContentForm(instance=math_content)

    math_content_form.fields['text'].widget.attrs['placeholder'] = 'Rješenje'
    return {
        'action_url': request.path,
        'form': math_content_form,
        'task': task,
    }
Esempio n. 2
0
def add_post(request):
    reply_to_id = request.POST['post_reply_id']
    reply_to = None if not reply_to_id else get_object_or_404(
        Post.objects.select_related('author'), pk=reply_to_id)

    math_content_form = MathContentForm(request.POST)
    if math_content_form.is_valid():
        content_type = get_object_or_404(ContentType,
                pk=request.POST['content_type_id'])
        try:
            object = content_type.get_object_for_this_type(
                    pk=request.POST['object_id'])
        except:
            raise Http404('Object does not exist.')

        if hasattr(object, 'can_send_post'):
            if not object.can_send_post(request.user):
                return HttpResponseForbidden(
                        "You are not allowed to send messages to this object!")

        object_author = getattr(object, 'author', None)
        object_author_id = None
        object_author_group = None
        if object_author:
            object_author_id = object_author.id
            try:
                object_author_group = Group.objects.get(
                        name=object_author.username)
            except Group.DoesNotExist:
                pass
                # TODO: report error

        content = math_content_form.save()
        post = Post.objects.create(content_object=object, author=request.user,
                last_edit_by=request.user, content=content)
        post.save()

        if reply_to:
            try:
                reply_to_author_group = Group.objects.get(
                    name=reply_to.author.username)
            except Group.DoesNotExist:
                pass
                # TODO: report error
        else:
            reply_to_author_group = None

        _action.add(request.user, _action.POST_SEND, action_object=post,
            target=object, group=reply_to_author_group)

    return HttpResponseRedirect(request.POST['post_redirect'])
Esempio n. 3
0
def edit_post(request, post_id=None):
    post = get_object_or_404(Post, pk=post_id)
    if not post.can_edit(request.user):
        return HttpResponseForbidden('Not allowed to edit this post.')

    if request.method == 'POST':
        math_content_form = MathContentForm(request.POST, instance=post.content)
        if math_content_form.is_valid():
            content = math_content_form.save()
            post.last_edit_by = request.user
            post.save()
            return HttpResponseRedirect(request.POST.get('next', '/'))

    return render_to_response('post_edit.html', {
                'next': request.GET.get('next', '/'),
                'math_content_form': MathContentForm(instance=post.content),
                'post': post,
            }, context_instance=RequestContext(request),
        )
Esempio n. 4
0
def new(request, group_id=None):
    if group_id:
        group = get_object_or_404(
                Group.objects.select_related('data', 'data__description'),
                id=group_id)
        if not group.data:
            return (400, 'You can\'t edit your own private user-group (or there is some data error).')

        usergroup = group.data

        # https://code.djangoproject.com/ticket/7190
        # (fixed in later versions of Django...)
        usergroup.hidden = bool(usergroup.hidden)

        perm = group.get_user_permissions(request.user)
        is_member = is_group_member(group.id, request.user.id)
        if EDIT not in perm:
            return (403, 'You do not have permission to edit this group\'s details.')

        description = usergroup.description
        edit = True
    else:
        group = usergroup = description = None
        is_member = False
        edit = False

    POST = request.POST if request.method == 'POST' else None

    group_form = GroupForm(POST, instance=group, prefix='x')
    usergroup_form = UserGroupForm(POST, instance=usergroup, prefix='y')
    description_form = MathContentForm(POST, instance=description, prefix='z')

    if request.method == 'POST':
        if group_form.is_valid() and usergroup_form.is_valid() \
                and description_form.is_valid():
            group = group_form.save()
            description = description_form.save();

            usergroup = usergroup_form.save(commit=False)
            usergroup.description = description

            if not edit:
                usergroup.group = group
                usergroup.author = request.user

                # Permissions assigned to the whole group (each member).
                # Every group member has perm to view the group itself.
                ObjectPermission.objects.create(content_object=group,
                    group=group, permission_type=VIEW)

            usergroup.save()

            return ('/usergroup/%d/' % group.id, )
        else: # reset necessary instances...
            group = get_object_or_404(Group.objects.select_related(
                'data', 'data__description'), id=group_id)

    return ('usergroup_new.html', {
        'can_edit': True,
        'group': group,
        'edit': edit,
        'is_member': is_member,
        'new_group': not edit,
        'forms': [group_form, usergroup_form, description_form],
    })
Esempio n. 5
0
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