示例#1
0
def edit_friend(request, id):
    """
    Use this function if you want to edit a friend list.
    It takes an ``id`` argument, which is the **friend id**
    
    **Template:**
    ``friendycontrol/friend_form.html``
    
    **Context:**
        ``form``: The form object to render
        
        ``add``: ``True`` if you want to re-mark that is an *add* operation
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
    """
    try:
        friend = get_object_or_404(FriendPerson, id=id,
                                                 friend_of=request.user)
    except Http404:
        raise Http404("This friend does not exist.")
    if request.method == 'POST':
        form = FriendPersonForm(request.user, instance=friend, data = request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('friendy_show_main_friends'))
    else:
        form = FriendPersonForm(request.user, instance=friend)
    return render_to_response('friendycontrol/friend_form.html',
                              {'form': form,
                               'add': False},
                              context_instance = RequestContext(request))
示例#2
0
def add_friend(request):
    """
    This function creates a new :class:`friendycontrol.FriendPerson` object. 
    If everything was right,
    after submiting the content you must be redirected to a page where the 
    friendlist is shown.
    
    **Template:**
    ``friendycontrol/friend_form.html``
    
    **Context:**
        ``form``: The form object to render.
        
        ``add``: ``True`` if you want to re-mark that is an *add* operation.
    
    **Decorators:**
        :func:`django.contrib.auth.decorators.login_required`
    """
    if request.method == 'POST':
        form = FriendPersonForm(request.user, data=request.POST)
        try:
            if form.is_valid():
                friendperson = form.save(commit=False)
                friendperson.friend_of = request.user
                try:
                    get_object_or_404(FriendPerson, friend=friendperson.friend,
                                                    friend_of = request.user)                
                except Http404:
                    friendperson.save()
                    form.save_m2m()
                    return HttpResponseRedirect(reverse('friendy_show_main_friends'))
                else:
                    raise DuplicateValuesAreNotUnique
        except DuplicateValuesAreNotUnique:
            form.non_field_errors = 'Already exists this friend.'
    else:
        form = FriendPersonForm(request.user)
    return render_to_response('friendycontrol/friend_form.html',
                              {'form': form,
                               'add': True},
                              context_instance = RequestContext(request))