Пример #1
0
def edit_friends(request, friend=None, redirect_to='edit_friends', form_class=FriendshipForm, template_name="friends/edit_friends.html"):
    redirect_to=request.REQUEST.get(REDIRECT_FIELD_NAME, redirect_to)
    if redirect_to and '/' not in redirect_to:
        redirect_to=reverse(redirect_to)
    if friend:
        friend, friend_profile = get_user_profile(friend)
        if Friendship.objects.are_friends(friend, request.user):
            friendship, _ = Friendship.objects.get_or_create(from_user=request.user, to_user=friend)
        else:
            messages.add_message(request, messages.ERROR,"%s is not one of your contacts." % (friend.get_full_name() or friend.username))
            return {}, {'url':redirect_to }
        if request.method == 'POST':
            friend_form=form_class(request.POST, user=request.user, friend=friend, prefix=request.POST.get('prefix'))
            if friend_form.is_valid():
                friend_form.save()
                messages.add_message(request, messages.SUCCESS,"Contact information for %s saved." % (friend.first_name or friend.username))
                return {}, {'url':redirect_to }
        else:
            friend_form=form_class(instance=friendship, user=request.user, friend=friend, prefix='friend')
    else:
        friend_forms = []
        friendship_list = get_friends(request.user)
        counter = 0
        for f in friendship_list:
            counter += 1
            friendship, _ = Friendship.objects.get_or_create(from_user=request.user, to_user=f['friend'])
            friend_forms.append(form_class(instance=friendship, user=request.user, friend=f['friend'], prefix='friend_%s' % counter))
    return locals(), template_name
Пример #2
0
def export_friends(request):
    vcard = export_vcards([ec.friend.user for ec in get_friends(request.user)])
    response = HttpResponse(vcard, mimetype='text/x-vcard')
    response['Content-Disposition'] = 'attachment; filename=friends.vcf'
    return response