Пример #1
0
def user_moderate(user, target, status):
    """
    Performs a moderator action on a user. 
    """
    if not user.can_moderate:
        msg = "User %s not a moderator" % user.id
        return False, msg

    if not auth.authorize_user_edit(user=user, target=target, strict=False):
        msg = "User %s not authorized to moderate %s" % (user.id, target.id)
        return False, msg

    # banning can only be applied by admins
    ban = status == USER_BANNED

    if ban and user.profile.is_admin:
        Post.objects.filter(author=target).update(status=POST_DELETED)

    target.profile.status = status
    target.profile.save()
    text = notegen.user_moderator_action(user=user, target=target)
    send_note(target=target, content=text, sender=user, both=True, type=NOTE_MODERATOR, url=user.get_absolute_url())

    msg = "User status set to %s" % target.profile.get_status_display()

    return True, msg
Пример #2
0
def user_moderate(user, target, status): 
    """
    Performs a moderator action on a user. 
    """
    if not user.can_moderate:
        msg = 'User %s not a moderator' %user.id
        return False, msg
    
    if not auth.authorize_user_edit(user=user, target=target, strict=False):
        msg = 'User %s not authorized to moderate %s' % (user.id, target.id)
        return False, msg



    # banning can only be applied by admins
    ban = (status == USER_BANNED)

    if ban and user.profile.is_admin:
            Post.objects.filter(author=target).update(status=POST_DELETED)

    target.profile.status = status
    target.profile.save()
    text = notegen.user_moderator_action(user=user, target=target)
    send_note(target=target, content=text, sender=user, both=True, type=NOTE_MODERATOR, url=user.get_absolute_url() )

    msg = 'User status set to %s' % target.profile.get_status_display()

    return True, msg
Пример #3
0
def user_profile(request, uid, tab='activity'):
    "User's profile page"

    if not models.User.objects.filter(id=uid):
        messages.error(request, "This user does not exist. It has perhaps been deleted.")
        return html.redirect("/")
        
    user = request.user
    target = models.User.objects.get(id=uid)
    awards = []
    page   = None
    
    # some information is only visible to the user
    target.writeable = auth.authorize_user_edit(target=target, user=user, strict=False)
    target.showall = (target == user)

    params = html.Params(tab=tab, sort='', title="User %s" % target.profile.display_name)

    # these do not actually get executed unless explicitly rendered in the page
    bookmarks = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).select_related('post', 'post__author__profile').order_by('id')
    awards = models.Award.objects.filter(user=target).select_related('badge').order_by('-date')

    # we need to collate and count the awards
    answer_count = models.Post.objects.filter(author=target, type=POST_ANSWER).count()
    question_count = models.Post.objects.filter(author=target, type=POST_QUESTION).count()
    comment_count = models.Post.objects.filter(author=target, type=POST_COMMENT).count()
    post_count = models.Post.objects.filter(author=target).count()
    vote_count = models.Vote.objects.filter(author=target).count()
    award_count = models.Award.objects.filter(user=target).count()
    note_count  = models.Note.objects.filter(target=target, unread=True).count()
    bookmarks_count  = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).count()
    
    if tab in [ 'activity', 'created' ]:
        if tab == 'created':
            notes = models.Note.objects.filter(sender=target, target=target, type=NOTE_USER).select_related('author', 'author__profile', 'root').order_by('-date')
        else:
            notes = models.Note.objects.filter(target=target, type=NOTE_USER).exclude(sender=target).select_related('author', 'author__profile', 'root').order_by('-date')
            
        page  = get_page(request, notes, per_page=15)
        # we evalute it here so that subsequent status updates won't interfere
        page.object_list = list(page.object_list)
        if user == target:
            models.Note.objects.filter(target=target, unread=True).update(unread=False)
            models.UserProfile.objects.filter(user=target).update(new_messages=0)
            note_count = 0
            
    elif tab == 'bookmarks':
        bookmarks = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).select_related('post', 'post__author__profile').order_by('-date')
        page  = get_page(request, bookmarks, per_page=15)
    
    elif tab =="moderator":
        notes = models.Note.objects.filter(target=target, type=NOTE_MODERATOR).select_related('author', 'author__profile').order_by('-date')
        page  = get_page(request, notes, per_page=15)

    params.update(dict(question_count=question_count, answer_count=answer_count, note_count=note_count, bookmarks_count=bookmarks_count,
            comment_count=comment_count, post_count=post_count, vote_count=vote_count, award_count=award_count))
    
    return html.template(request, name='user.profile.html', awards=awards,
        user=request.user,target=target, params=params, page=page)
Пример #4
0
def user_profile(request, uid, tab='activity'):
    "User's profile page"

    if not models.User.objects.filter(id=uid):
        messages.error(request, "This user does not exist. It has perhaps been deleted.")
        return html.redirect("/")
        
    user = request.user
    target = models.User.objects.get(id=uid)
    awards = []
    page   = None
    
    # some information is only visible to the user
    target.writeable = auth.authorize_user_edit(target=target, user=user, strict=False)
    target.showall = (target == user)

    params = html.Params(tab=tab, sort='')

    # these do not actually get executed unless explicitly rendered in the page
    bookmarks = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).select_related('post', 'post__author__profile').order_by('id')
    awards = models.Award.objects.filter(user=target).select_related('badge').order_by('-date')

    # we need to collate and count the awards
    answer_count = models.Post.objects.filter(author=target, type=POST_ANSWER).count()
    question_count = models.Post.objects.filter(author=target, type=POST_QUESTION).count()
    comment_count = models.Post.objects.filter(author=target, type=POST_COMMENT).count()
    post_count = models.Post.objects.filter(author=target).count()
    vote_count = models.Vote.objects.filter(author=target).count()
    award_count = models.Award.objects.filter(user=target).count()
    note_count  = models.Note.objects.filter(target=target, unread=True).count()
    bookmarks_count  = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).count()
    
    if tab == 'activity':
        notes = models.Note.objects.filter(target=target, type=NOTE_USER).select_related('author', 'author__profile', 'root').order_by('-date')
        page  = get_page(request, notes, per_page=15)
        # we evalute it here so that subsequent status updates won't interfere
        page.object_list = list(page.object_list)
        if user==target:
            models.Note.objects.filter(target=target).update(unread=False)
            models.UserProfile.objects.filter(user=target).update(new_messages=0)
            note_count = 0
        
    elif tab == 'bookmarks':
        bookmarks = models.Vote.objects.filter(author=target, type=VOTE_BOOKMARK).select_related('post', 'post__author__profile').order_by('-date')
        page  = get_page(request, bookmarks, per_page=15)
    
    elif tab =="moderator":
        notes = models.Note.objects.filter(target=target, type=NOTE_MODERATOR).select_related('author', 'author__profile').order_by('-date')
        page  = get_page(request, notes, per_page=15)

    params.update(dict(question_count=question_count, answer_count=answer_count, note_count=note_count, bookmarks_count=bookmarks_count,
            comment_count=comment_count, post_count=post_count, vote_count=vote_count, award_count=award_count))
    
    return html.template(request, name='user.profile.html', awards=awards,
        user=request.user,target=target, params=params, page=page)
Пример #5
0
def user_edit(request, uid):
    "User's profile page"
    
    target = models.User.objects.select_related('profile').get(id=uid)
    
    allow = auth.authorize_user_edit(target=target, user=request.user, strict=False)
    if not allow:
        messages.error(request, "unable to edit this user")
        return html.redirect(target.profile.get_absolute_url() )
    
    # valid incoming fields
    fields = "display_name about_me website location my_tags scholar hide_ads".split()
        
    if request.method == 'GET':
        initial = dict(email=target.email)
        for field in fields:
            initial[field] = getattr(target.profile, field) or ''                
        form = UserForm(initial)
        return html.template(request, name='user.edit.html', target=target, form=form)
    elif request.method == 'POST':
        
        form = UserForm(request.POST)
        if not form.is_valid():
            return html.template(request, name='user.edit.html', target=target, form=form)
        else:
            for field in fields:
                setattr(target.profile, field, form.cleaned_data[field])

            # hiding ads requires a minimum reputation
            if target.profile.hide_ads and target.profile.score < settings.AD_MIN_REP:
                target.profile.hide_ads = False
                messages.warning(request, "The reputation needed to hide ads is %s" % (settings.AD_MIN_REP * 10))

            # check the new email
            new_email = form.cleaned_data['email'].strip()
            if new_email != target.email and models.User.objects.filter(email=new_email):
                # cannot set your email to an existing other user 'semail
                messages.error(request, "This email is aready taken - please merge the accounts!")
            else:
                target.email = new_email
            
            target.profile.save()
            target.save()
            
            url = reverse('main.server.views.user_profile', kwargs=dict(uid=target.id))
            return html.redirect(url)
Пример #6
0
def user_moderate(user, target, status): 
    """
    Performs a moderator action on a user. 
    """
    if not user.can_moderate:
        msg = 'User %s not a moderator' %user.id
        return False, msg
    
    if not auth.authorize_user_edit(user=user, target=target, strict=False):
        msg = 'User %s not authorized to moderate %s' % (user.id, target.id)
        return False, msg

    target.profile.status = status
    target.profile.save()
    text = notegen.user_moderator_action(user=user, target=target)
    send_note(target=target, content=text, sender=user, both=True, type=NOTE_MODERATOR, url=user.get_absolute_url() )

    msg = 'User status set to %s' % target.profile.get_status_display()
    return True, msg