Exemplo n.º 1
0
def _process_special_fields(task, form, user_tags_dict, request, just_tags=False):
    """ This function handles view-level processing of some special
    fields on the task form. This function is used in both single_task
    and tasklist views.

      str_tags:
        This function processes the cleaned str_tag data, saving the
        data to the task instance and creating any new tags. The user_tags_dict
        is also updated with any created tags.

      added_time:
        This function processes the cleaned add_time data, now in integer
        format, adding it to the measure of time_spent on the task.

      completed and date_completed:
        If completed is set to True while date_completed is left blank,
        this function sets date_completed to the current date.

    """
    # tag_strings is a list of all unique, non-blank tags for the task.
    tag_strings = form.cleaned_data['str_tags']
    tag_list = []
    for tag_str in tag_strings:
        try:
            tag = user_tags_dict[tag_str]
        except KeyError:
            # Tag does not exist and should be created.
            tag = Tag(user=request.user, name=tag_str)
            tag.save()
            user_tags_dict[tag_str] = tag
            messages.info(request,
                          'New tag "' + tag.name + '" was created.')
        tag_list.append(tag)
    task.tags = tag_list

    # These additional fields do not exist on the minimal list of tasks.
    if not just_tags:
        
        added_time = form.cleaned_data['add_time']
        if added_time is not None:
            if task.time_spent is None:
                task.time_spent = added_time
            else:
                task.time_spent += added_time

        # If the user indicates the task as complete but does not
        # enter a date_completed, the current date is entered.
        if task.completed and "completed" in form.changed_data:
            # We know the task was just set as completed. 
            if task.date_completed is None:
                task.date_completed = datetime.date.today()
Exemplo n.º 2
0
def new_user(request):
    """ This view handles the creation of a new user account.

    """
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            # Note that form validation checks for existing users with the same
            # username.
            
            username_data = form.cleaned_data['username']
            password_data = form.cleaned_data['password1']

            u = User.objects.create_user(username_data, '', password=password_data)
            u.save()
            p = TLAUserProfile(user=u)
            p.save()

            # Create some examples for new users, to better illustrate the
            # tasklist operation. 
            example_tag = Tag(
                name="Chariot",
                user=u,
                description="Things to do in my awesome chariot.")
            example_tag.save()
            example_task = Task(
                user=u,
                name="Ride chariot across sky",
                description="Same old, same old",
                priority=6,
                est=840)
            example_task.save()
            example_task.tags.add(example_tag)

            user = authenticate(username=username_data, password=password_data)
            if user is not None:
                # At this point we know the password matches, which is what we
                # expect considering that the user was just created with this
                # same password.
                if user.is_active:
                    logout(request)  # Since we aren't sure a user wasn't logged in.
                    login(request, user)
                    messages.success(request, "User created!")
                    return HttpResponseRedirect(reverse('TLA_About'))
                else:
                    # This should never be reached, as the form will be considered
                    # invalid if a user with that name already exists.
                    messages.error(request, "User inactive!")

            else:
                # This should never be reached, as the user was just created with
                # the same password information which was just used in the
                # authentication attempt. 
                messages.error(request,"Somehow login failed after user creation!")

    else:
        form = UserCreationForm()

    return render_to_response(
        'TaskListApp/new_user.html',
        {'form':form},
        context_instance=RequestContext(request))