def handle_noargs(self, **options):
     last_year = date.today() - timedelta(days=365)
     users = User.objects.filter(last_login__gt=last_year, is_active=True, is_bulk=False)
     
     print "Running", users.count(), "users"
     
     i = 0
     
     for u in users:
         profile, created = UsageProfile.objects.get_or_create(user=u)
         
         ctx = get_organization_role(u)
         
         profile.is_chapter_member = ctx['role_chapter_member']
         profile.is_exec = ctx['role_is_exec']
         profile.is_president = ctx['role_is_president']
         profile.is_jf = ctx['role_is_jf']
         profile.is_aps = ctx['role_is_aps']
         profile.is_office = ctx['role_is_office']
         profile.is_alumni = ctx['role_is_alumni']
         
         profile.usage_profile = usage_profile(u)
         
         profile.save()
         
         i = i + 1
         print "(", i, ")", u.visible_name()
     
     return 'Profiles run.'
    def handle_noargs(self, **options):
        last_year = date.today() - timedelta(days=365)
        users = User.objects.filter(last_login__gt=last_year,
                                    is_active=True,
                                    is_bulk=False)

        print "Running", users.count(), "users"

        i = 0

        for u in users:
            profile, created = UsageProfile.objects.get_or_create(user=u)

            ctx = get_organization_role(u)

            profile.is_chapter_member = ctx['role_chapter_member']
            profile.is_exec = ctx['role_is_exec']
            profile.is_president = ctx['role_is_president']
            profile.is_jf = ctx['role_is_jf']
            profile.is_aps = ctx['role_is_aps']
            profile.is_office = ctx['role_is_office']
            profile.is_alumni = ctx['role_is_alumni']

            profile.usage_profile = usage_profile(u)

            profile.save()

            i = i + 1
            print "(", i, ")", u.visible_name()

        return 'Profiles run.'
Beispiel #3
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))
Beispiel #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))