Esempio n. 1
0
    def update(self, user, value):
        value = int(value)
        score, created = Score.objects.get_or_create(object_id=self.instance.pk,
            content_type=self.get_content_type(), key=self.field.key)
        vote, created = Vote.objects.get_or_create(object_id=self.instance.pk,
            content_type=self.content_type, key=self.field.key, user=user,
            defaults={'value': value})

        if created:
            score.count += 1
        else:
            score.sum -= vote.value
            vote.value = value
            
        # TODO: pametnije ovo napraviti
        if value == 0:
            score.count -= 1
            # vote.delete() # don't delete yet, first remove the Activity
        else:
            score.sum += vote.value
            vote.save()

        score.save()

        if self.field.type == AVERAGE:
            field_value = 0 if score.count == 0 \
                            else float(score.sum) / score.count
        else: # SUM
            field_value = score.sum

        old_field_value = getattr(self.instance, self.field_name, 0)
        setattr(self.instance, self.field_name, field_value)
        self.instance.save()
        
        if self.field.on_update:
            # automatically cache the function pointer itself, don't search
            # for the function every time
            self.field.on_update = get_callable(self.field.on_update)
            self.field.on_update(self.instance, self.field_name,
                old_field_value, field_value)

        if self.field.action_type:
            authors_group_id = hasattr(self.instance, 'author_id') \
                and self.instance.author.get_profile().private_group_id
            if value == 0:
                remove(user, self.field.action_type[0], action_object=vote,
                    target=self.instance, group_id=authors_group_id)
            else:
                replace_or_add(user, self.field.action_type, action_object=vote,
                    target=self.instance, group_id=authors_group_id)

        if value == 0:
            vote.delete() # ok, now delete

        return field_value
Esempio n. 2
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. 3
0
def _do_mark(request, solution, task):
    """
        Update solution status:
            As Solved
            To Do
            Blank

        Or mark / unmark official flag

        Creates Solution if it doesn't exist (in that case Task is given).

        Returns None if no error.
    """

    action = request.POST['action']

    # check requset and privileges
    if action not in ['official0', 'official1', 'blank', 'as_solved', 'todo']:
        return (403, u'Action "%s" not valid.' % action)

    if action in ['official0', 'official1'] and \
            not can_mark_as_official(request.user, task):
        return (403, u'No permission to mark as official solution.')

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

    if not task.is_allowed_to_solve(request.user):
        return (403, u'Not allowed to view the task or send solutions.')

    # as_solved, todo, blank
    if solution is None:
        solution, dummy = Solution.objects.get_or_create(
                task=task, author=request.user)
    elif not solution.can_edit(request.user):
        return (403, 'Not allowed to modify this solution.')

    # keep track of the number of solutions for the task
    was_solved = solution.is_solved()

    # update
    if action in ['official0', 'official1']:
        solution.is_official = action == 'official1'
    elif action in ['blank', 'as_solved', 'todo']:
        if action != 'blank':
            solution.date_created = datetime.now()
        solution.status = SOLUTION_STATUS_BY_NAME[action]

    solution.save()


    # log the action
    # TODO: use signals!
    if action in ['official1', 'as_solved', 'todo']:
        type_desc = {'official1': _action.SOLUTION_AS_OFFICIAL,
                     'as_solved': _action.SOLUTION_AS_SOLVED,
                     'todo': _action.SOLUTION_TODO,
            }
        _action.replace_or_add(request.user, type_desc[action],
            action_object=solution, target=task)
    elif action == 'official0':
        # temporary solution...
        _action.remove(request.user, type=_action.SOLUTION_AS_OFFICIAL[0],
            action_object=solution, target=task)
    elif action == 'blank':
        _action.remove(request.user, type=_action.SOLUTION_SEND,
            action_object=solution, target=task)


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

    return None     # ok