Пример #1
0
def friends(request, template_name="friends_app/invitations.html"):
    """
    Show lists of friend requests sent/received and invites sent/status.
    'send invite' link
    """
    friend_form=InviteFriendForm()
    if request.method == "POST":
        invitation_id = request.POST.get("invitation", None)
        if request.POST["action"] == "accept":
            try:
                invitation = FriendshipInvitation.objects.get(id=invitation_id)
                if invitation.to_user == request.user:
                    invitation.accept()
                    request.user.message_set.create(message=_("Accepted friendship request from %(from_user)s") % {'from_user': invitation.from_user})
            except FriendshipInvitation.DoesNotExist:
                pass
        elif request.POST["action"] == "decline":
            try:
                invitation = FriendshipInvitation.objects.get(id=invitation_id)
                if invitation.to_user == request.user:
                    invitation.decline()
                    request.user.message_set.create(message=_("Declined friendship request from %(from_user)s") % {'from_user': invitation.from_user})
            except FriendshipInvitation.DoesNotExist:
                pass
        elif request.POST["action"] == "friend":
            friend_form=InviteFriendForm(request.user,request.POST)
            if friend_form.is_valid():
                friend_form.save(request.user)
            else:
                pass
                # print friend_form.errors

    invites_received = request.user.invitations_to.invitations().order_by("-sent")
    invites_sent = request.user.invitations_from.invitations().order_by("-sent")
    joins_sent = request.user.join_from.all().order_by("-sent")
    
    contacts=Contact.objects.filter(user=request.user)

    friends = Friendship.objects.friends_for_user(request.user)

    return render_to_response(template_name, {
        "invites_received": invites_received,
        'friend_form':friend_form,
        "invites_sent": invites_sent,
        "joins_sent": joins_sent,
        "contacts": contacts,
        "friends": friends,
    }, context_instance=RequestContext(request))
Пример #2
0
def profile(request, username, template_name="profiles/profile.html", extra_context=None):
    other_user = get_object_or_404(User, username=username)

    if not other_user.is_active:
        return render_to_response('profiles/deleted.html', {}, context_instance=RequestContext(request)) 

    """
    This is really really neat code, but dunno where to put it since 
    address is now handled via ajax widget...!!
    
    # if changed city, prompt to update networks
    if profile_form.cleaned_data['city'] != other_user.get_profile().city:
        # join new network
        # TODO: use geocoding to find closest network(s)
        try:
            network = Network.objects.get(name=profile_form.cleaned_data['city'], network_type='R')

            if not network.user_is_member(other_user):
                message = loader.get_template("profiles/suggest_network.html")
                c = Context({'network': network, 'action': 'join'})
                request.user.message_set.create(message=message.render(c))
        except Network.DoesNotExist:
            pass
            
        # leave old network
        try:
            network = Network.objects.get(name=other_user.get_profile().city, network_type='R')
            if network.user_is_member(other_user):
                message = loader.get_template("profiles/suggest_network.html")
                c = Context({'network': network, 'action': 'leave', 'user': other_user})
                request.user.message_set.create(message=message.render(c))
        except Network.DoesNotExist:
            pass
    """
    
    if extra_context == None:
        extra_context = {}
        
    profile = other_user.get_profile()
    if profile.membership_expiry != None and profile.membership_expiry > date.today():
        extra_context['regular'] = True
    if profile.membership_expiry == None or \
        profile.membership_expiry < date.today() + timedelta(30):
        extra_context['renew'] = True  

#    if template_name == None:
#        return pinaxprofile(request, username, extra_context=extra_context)
#    else:
#        return pinaxprofile(request, username, template_name, extra_context)
    if extra_context is None:
        extra_context = {}
    
    other_user = get_object_or_404(User, username=username)
    
    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)
        
        if is_friend:
            invite_form = None
            previous_invitations_to = None
            previous_invitations_from = None
            if request.method == "POST":
                if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here
                    Friendship.objects.remove(request.user, other_user)
                    request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'from_user': other_user.visible_name()})
                    is_friend = False
                    invite_form = InviteFriendForm(request.user, {
                        'to_user': username,
                        'message': ugettext("Let's be friends!"),
                    })
        
        else:
            if request.user.is_authenticated() and request.method == "POST":
                if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here
                    invite_form = InviteFriendForm(request.user, request.POST)
                    if invite_form.is_valid():
                        invite_form.save()
                else:
                    invite_form = InviteFriendForm(request.user, {
                        'to_user': username,
                        'message': ugettext("Let's be friends!"),
                    })
                    invitation_id = request.POST.get("invitation", None)
                    if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here
                        try:
                            invitation = FriendshipInvitation.objects.get(id=invitation_id)
                            if invitation.to_user == request.user:
                                try:                                # have gotten IntegrityError from this... #573
                                    invitation.accept()             # (accepting an already-accepted invite, maybe?)
                                except:
                                    pass
                                request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user.visible_name()})
                        except FriendshipInvitation.DoesNotExist:
                            pass
                    elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here
                        try:
                            invitation = FriendshipInvitation.objects.get(id=invitation_id)
                            if invitation.to_user == request.user:
                                invitation.decline()
                                request.user.message_set.create(message=_("You have declined the friendship request from %(from_user)s") % {'from_user': invitation.from_user.visible_name()})
                        except FriendshipInvitation.DoesNotExist:
                            pass
            else:
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be friends!"),
                })
        
            is_friend = Friendship.objects.are_friends(request.user, other_user)

        #is_following = Following.objects.is_following(request.user, other_user)
        #other_friends = Friendship.objects.friends_for_user(other_user)
        
        friends_qry = Friendship.objects.filter(Q(from_user=other_user) | Q(to_user=other_user))\
                                        .select_related(depth=1).order_by('?')[:10]
        other_friends = []
        for f in friends_qry:
            if f.to_user == other_user:
                other_friends.append(f.from_user)
            else:
                other_friends.append(f.to_user)
        
        if request.user == other_user:
            is_me = True
        else:
            is_me = False
            
        pending_requests = FriendshipInvitation.objects.filter(to_user=other_user, status=2).count()
        previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user)
        previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user)
        
        # friends & admins have visibility.
        has_visibility = is_friend or request.user.has_module_perms("profiles")
        if not has_visibility:      #but so does your chapter's exec
            mygrps = Network.objects.filter(members__user=request.user, members__is_admin=True, is_active=True).order_by('name')
            if len(list(set(mygrps) & set(other_user.get_networks()))) > 0:
                has_visibility = True
        
    else:
        other_friends = []
        is_friend = False
        is_me = False
        is_following = False
        pending_requests = 0
        invite_form = None
        previous_invitations_to = None
        previous_invitations_from = None
        has_visibility = False
    
    return render_to_response(template_name, dict({
        "is_me": is_me,
        "is_friend": is_friend,
        #"is_following": is_following,
        "is_exec_over": is_exec_over(other_user, request.user), 
        "other_user": other_user,
        "other_friends": other_friends,
        "invite_form": invite_form,
        "previous_invitations_to": previous_invitations_to,
        "previous_invitations_from": previous_invitations_from,
        "pending_requests": pending_requests,
        "has_visibility": has_visibility,
    }, **extra_context), context_instance=RequestContext(request))
Пример #3
0
def profile(request, username, template_name="profiles/profile.html", extra_context=None):
    other_user = User.objects.get(username=username)
    if request.user == other_user:
        if request.method == "POST":
            if request.POST["action"] == "update":
                profile_form = ProfileForm(request.POST, instance=other_user.get_profile())
                if profile_form.is_valid():
                    
                    # if changed city, prompt to update networks
                    if profile_form.cleaned_data['city'] != other_user.get_profile().city:
                        # join new network
                        # TODO: use geocoding to find closest network(s)
                        try:
                            network = Network.objects.get(name=profile_form.cleaned_data['city'], network_type='R')

                            if not network.user_is_member(other_user):
                                message = loader.get_template("profiles/suggest_network.html")
                                c = Context({'network': network, 'action': 'join'})
                                request.user.message_set.create(message=message.render(c))
                        except Network.DoesNotExist:
                            pass
                            
                        # leave old network
                        try:
                            network = Network.objects.get(name=other_user.get_profile().city, network_type='R')
                            if network.user_is_member(other_user):
                                message = loader.get_template("profiles/suggest_network.html")
                                c = Context({'network': network, 'action': 'leave', 'user': other_user})
                                request.user.message_set.create(message=message.render(c))
                        except Network.DoesNotExist:
                            pass
    
    if extra_context == None:
        extra_context = {}
        
    profile = other_user.get_profile()
    if profile.membership_expiry != None and profile.membership_expiry > date.today():
        extra_context['regular'] = True
    if profile.membership_expiry == None or \
        profile.membership_expiry < date.today() + timedelta(30):
        extra_context['renew'] = True  

#    if template_name == None:
#        return pinaxprofile(request, username, extra_context=extra_context)
#    else:
#        return pinaxprofile(request, username, template_name, extra_context)
    if extra_context is None:
        extra_context = {}
    
    other_user = get_object_or_404(User, username=username)
    
    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)
        is_following = Following.objects.is_following(request.user, other_user)
        other_friends = Friendship.objects.friends_for_user(other_user)
        if request.user == other_user:
            is_me = True
        else:
            is_me = False
    else:
        other_friends = []
        is_friend = False
        is_me = False
        is_following = False
    
    if is_friend:
        invite_form = None
        previous_invitations_to = None
        previous_invitations_from = None
        if request.method == "POST":
            if request.POST.get("action") == "remove": # @@@ perhaps the form should just post to friends and be redirected here
                Friendship.objects.remove(request.user, other_user)
                request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'from_user': other_user.visible_name()})
                is_friend = False
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be friends!"),
                })
    
    else:
        if request.user.is_authenticated() and request.method == "POST":
            if request.POST.get("action") == "invite": # @@@ perhaps the form should just post to friends and be redirected here
                invite_form = InviteFriendForm(request.user, request.POST)
                if invite_form.is_valid():
                    invite_form.save()
            else:
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be friends!"),
                })
                invitation_id = request.POST.get("invitation", None)
                if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here
                    try:
                        invitation = FriendshipInvitation.objects.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.accept()
                            request.user.message_set.create(message=_("You have accepted the friendship request from %(from_user)s") % {'from_user': invitation.from_user.visible_name()})
                            is_friend = True
                            other_friends = Friendship.objects.friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
                elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here
                    try:
                        invitation = FriendshipInvitation.objects.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.decline()
                            request.user.message_set.create(message=_("You have declined the friendship request from %(from_user)s") % {'from_user': invitation.from_user.visible_name()})
                            other_friends = Friendship.objects.friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
        else:
            invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's be friends!"),
            })
    
    previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user)
    previous_invitations_from = FriendshipInvitation.objects.invitations(to_user=request.user, from_user=other_user)
    
    return render_to_response(template_name, dict({
        "is_me": is_me,
        "is_friend": is_friend,
        "is_following": is_following,
        "other_user": other_user,
        "other_friends": other_friends,
        "invite_form": invite_form,
        "previous_invitations_to": previous_invitations_to,
        "previous_invitations_from": previous_invitations_from,
    }, **extra_context), context_instance=RequestContext(request))
Пример #4
0
def profile(request,
            username,
            template_name="profiles/profile.html",
            extra_context=None):
    other_user = get_object_or_404(User, username=username)

    if not other_user.is_active \
       and not other_user.emailaddress_set.count() \
       and request.user.has_module_perms("profiles"):
        return render_to_response('profiles/deleted.html', {},
                                  context_instance=RequestContext(request))
    """
    This is really really neat code, but dunno where to put it since 
    address is now handled via ajax widget...!!
    
    # if changed city, prompt to update networks
    if profile_form.cleaned_data['city'] != other_user.get_profile().city:
        # join new network
        # TODO: use geocoding to find closest network(s)
        try:
            network = Network.objects.get(name=profile_form.cleaned_data['city'], network_type='R')

            if not network.user_is_member(other_user):
                message = loader.get_template("profiles/suggest_network.html")
                c = Context({'network': network, 'action': 'join'})
                request.user.message_set.create(message=message.render(c))
        except Network.DoesNotExist:
            pass
            
        # leave old network
        try:
            network = Network.objects.get(name=other_user.get_profile().city, network_type='R')
            if network.user_is_member(other_user):
                message = loader.get_template("profiles/suggest_network.html")
                c = Context({'network': network, 'action': 'leave', 'user': other_user})
                request.user.message_set.create(message=message.render(c))
        except Network.DoesNotExist:
            pass
    """

    if extra_context == None:
        extra_context = {}

    profile = other_user.get_profile()
    if profile.membership_expiry != None and profile.membership_expiry > date.today(
    ):
        extra_context['regular'] = True
    if profile.membership_expiry == None or \
        profile.membership_expiry < date.today() + timedelta(30):
        extra_context['renew'] = True


#    if template_name == None:
#        return pinaxprofile(request, username, extra_context=extra_context)
#    else:
#        return pinaxprofile(request, username, template_name, extra_context)
    if extra_context is None:
        extra_context = {}

    other_user = get_object_or_404(User, username=username)

    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)

        if is_friend:
            invite_form = None
            previous_invitations_to = None
            previous_invitations_from = None
            if request.method == "POST":
                if request.POST.get(
                        "action"
                ) == "remove":  # @@@ perhaps the form should just post to friends and be redirected here
                    Friendship.objects.remove(request.user, other_user)
                    request.user.message_set.create(
                        message=_(
                            "You have removed %(from_user)s from friends") %
                        {'from_user': other_user.visible_name()})
                    is_friend = False
                    invite_form = InviteFriendForm(
                        request.user, {
                            'to_user': username,
                            'message': ugettext("Let's be friends!"),
                        })

        else:
            if request.user.is_authenticated() and request.method == "POST":
                if request.POST.get(
                        "action"
                ) == "invite":  # @@@ perhaps the form should just post to friends and be redirected here
                    invite_form = InviteFriendForm(request.user, request.POST)
                    if invite_form.is_valid():
                        invite_form.save()
                else:
                    invite_form = InviteFriendForm(
                        request.user, {
                            'to_user': username,
                            'message': ugettext("Let's be friends!"),
                        })
                    invitation_id = request.POST.get("invitation", None)
                    if request.POST.get(
                            "action"
                    ) == "accept":  # @@@ perhaps the form should just post to friends and be redirected here
                        try:
                            invitation = FriendshipInvitation.objects.get(
                                id=invitation_id)
                            if invitation.to_user == request.user:
                                try:  # have gotten IntegrityError from this... #573
                                    invitation.accept(
                                    )  # (accepting an already-accepted invite, maybe?)
                                except:
                                    pass
                                request.user.message_set.create(
                                    message=
                                    _("You have accepted the friendship request from %(from_user)s"
                                      ) % {
                                          'from_user':
                                          invitation.from_user.visible_name()
                                      })
                        except FriendshipInvitation.DoesNotExist:
                            pass
                    elif request.POST.get(
                            "action"
                    ) == "decline":  # @@@ perhaps the form should just post to friends and be redirected here
                        try:
                            invitation = FriendshipInvitation.objects.get(
                                id=invitation_id)
                            if invitation.to_user == request.user:
                                invitation.decline()
                                request.user.message_set.create(
                                    message=
                                    _("You have declined the friendship request from %(from_user)s"
                                      ) % {
                                          'from_user':
                                          invitation.from_user.visible_name()
                                      })
                        except FriendshipInvitation.DoesNotExist:
                            pass
            else:
                invite_form = InviteFriendForm(
                    request.user, {
                        'to_user': username,
                        'message': ugettext("Let's be friends!"),
                    })

            is_friend = Friendship.objects.are_friends(request.user,
                                                       other_user)

        #is_following = Following.objects.is_following(request.user, other_user)
        #other_friends = Friendship.objects.friends_for_user(other_user)

        friends_qry = Friendship.objects.filter(Q(from_user=other_user) | Q(to_user=other_user))\
                                        .select_related(depth=1).order_by('?')[:10]
        other_friends = []
        for f in friends_qry:
            if f.to_user == other_user:
                other_friends.append(f.from_user)
            else:
                other_friends.append(f.to_user)

        if request.user == other_user:
            is_me = True
        else:
            is_me = False

        pending_requests = FriendshipInvitation.objects.filter(
            to_user=other_user, status=2).count()
        previous_invitations_to = FriendshipInvitation.objects.invitations(
            to_user=other_user, from_user=request.user)
        previous_invitations_from = FriendshipInvitation.objects.invitations(
            to_user=request.user, from_user=other_user)

        # friends & admins have visibility.
        has_visibility = is_friend or request.user.has_module_perms("profiles")
        if not has_visibility:  #but so does your chapter's exec
            mygrps = Network.objects.filter(members__user=request.user,
                                            members__is_admin=True,
                                            is_active=True).order_by('name')
            if len(list(set(mygrps) & set(other_user.get_networks()))) > 0:
                has_visibility = True

    else:
        other_friends = []
        is_friend = False
        is_me = False
        is_following = False
        pending_requests = 0
        invite_form = None
        previous_invitations_to = None
        previous_invitations_from = None
        has_visibility = False

    uprofile = False
    if request.user.has_module_perms("profiles"):
        try:
            from stats.models import usage_profile
            uprofile = usage_profile(other_user)
        except:
            pass

    if request.user == other_user:
        # user can always see their own post, regardless of group visibility
        # (ie, if I write some posts to a private group then leave the group,
        #  those posts should still show in this listing)
        topics = GroupTopic.objects.get_for_user(other_user)

    else:
        # start with all visible topics
        topics = GroupTopic.objects.visible(request.user)

        # then restrict further to only ones by the given user
        topics = GroupTopic.objects.get_for_user(other_user, topics)

    return render_to_response(
        template_name,
        dict(
            {
                "is_me": is_me,
                "is_friend": is_friend,
                #"is_following": is_following,
                "is_exec_over": is_exec_over(other_user, request.user),
                "other_user": other_user,
                "other_friends": other_friends,
                "invite_form": invite_form,
                "previous_invitations_to": previous_invitations_to,
                "previous_invitations_from": previous_invitations_from,
                "pending_requests": pending_requests,
                "has_visibility": has_visibility,
                "other_usage_profile": uprofile,
                "topics": topics
            },
            **extra_context),
        context_instance=RequestContext(request))