def answer_vote_buttons(post, user): from forum.models import CustomBadge context = dict(post=post, user_vote='none', can_comment=user.can_comment(post), is_voting_restricted=CustomBadge.is_voting_restricted(user, post)) if user.is_authenticated(): context['user_vote'] = {1: 'up', -1: 'down', None: 'none'}[VoteAction.get_for(user, post)] return context
def comment(request, id): post = get_object_or_404(Node, id=id) user = request.user if not request.method == 'POST': raise CommandException(_("Invalid request")) vote_type = request.POST.get('vote_type', 'comment') is_voting = (vote_type in ('up', 'down')) if not user.is_authenticated(): msg = is_voting and _('vote') or _('comment') raise AnonymousNotAllowedException(msg) from forum.models import CustomBadge if is_voting and CustomBadge.is_voting_restricted(user, post): msg = _( 'Only users with the badge (or the badge owners) are allowed to vote.' ) raise CommandException(msg) if is_voting and user == post.author: raise CannotDoOnOwnException(_('vote')) if vote_type == 'up' and not user.can_vote_up(): raise NotEnoughRepPointsException(_('upvote')) if vote_type == 'down' and not user.can_vote_down(): raise NotEnoughRepPointsException(_('downvote')) user_vote_count_today = user.get_vote_count_today() if is_voting and user_vote_count_today >= user.can_vote_count_today(): raise NotEnoughLeftException(_('votes'), str(settings.MAX_VOTES_PER_DAY)) comment_text = request.POST.get('comment', '').strip() if not len(comment_text): raise CommandException(_("Comment is empty")) if len(comment_text) < settings.FORM_MIN_COMMENT_BODY: raise CommandException( _("At least %d characters required on comment body.") % settings.FORM_MIN_COMMENT_BODY) if len(comment_text) > settings.FORM_MAX_COMMENT_BODY: raise CommandException( _("No more than %d characters on comment body.") % settings.FORM_MAX_COMMENT_BODY) old_vote = VoteAction.get_action_for(node=post, user=user) if is_voting and old_vote: is_too_old = (old_vote.action_date < datetime.datetime.now() - datetime.timedelta(days=int(settings.DENY_UNVOTE_DAYS))) if is_too_old: raise CommandException( _("Sorry but you cannot cancel a vote after %(ndays)d %(tdays)s from the original vote" ) % { 'ndays': int(settings.DENY_UNVOTE_DAYS), 'tdays': ungettext('day', 'days', int(settings.DENY_UNVOTE_DAYS)) }) if 'id' in request.POST: comment = get_object_or_404(Comment, id=request.POST['id']) if not user.can_edit_comment(comment): raise NotEnoughRepPointsException(_('edit comments')) else: if not user.can_comment(post): raise NotEnoughRepPointsException(_('comment')) if is_voting: new_vote_cls = (vote_type == 'up') and VoteUpAction or VoteDownAction score_inc = 0 if old_vote: old_vote.cancel(ip=request.META['REMOTE_ADDR']) score_inc += (old_vote.__class__ == VoteDownAction) and 1 or -1 if old_vote.__class__ != new_vote_cls: new_vote_cls(user=user, node=post, ip=request.META['REMOTE_ADDR']).save() score_inc += (new_vote_cls == VoteUpAction) and 1 or -1 else: vote_type = "none" if 'id' in request.POST: comment = ReviseAction(user=user, node=comment, ip=request.META['REMOTE_ADDR']).save(data=dict( text=comment_text)).node else: comment = CommentAction( user=user, ip=request.META['REMOTE_ADDR']).save( data=dict(text=comment_text, parent=post)).node if not is_voting: comment_type = VoteComment.COMMENT elif old_vote.__class__ != new_vote_cls and new_vote_cls == VoteUpAction: comment_type = VoteComment.VOTE_UP elif old_vote.__class__ != new_vote_cls and new_vote_cls != VoteUpAction: comment_type = VoteComment.VOTE_DOWN elif old_vote.__class__ == VoteUpAction: comment_type = VoteComment.CANCEL_VOTE_UP else: comment_type = VoteComment.CANCEL_VOTE_DOWN vote_comment = VoteComment(comment_type=comment_type, comment=comment) vote_comment.save() if comment.active_revision.revision == 1: response = { 'commands': { 'insert_comment': [ id, comment.id, comment.comment, user.decorated_name, user.get_profile_url(), reverse('delete_comment', kwargs={'id': comment.id}), reverse('node_markdown', kwargs={'id': comment.id}), reverse('convert_comment', kwargs={'id': comment.id}), user.can_delete_comment(comment) ] } } else: response = { 'commands': { 'update_comment': [comment.id, comment.comment] } } if is_voting: response['commands']['update_post_score'] = [id, score_inc] response['commands']['update_user_post_vote'] = [id, vote_type] votes_left = (int(settings.MAX_VOTES_PER_DAY) - user_vote_count_today) + (vote_type == 'none' and -1 or 1) if int(settings.START_WARN_VOTES_LEFT) >= votes_left: response['message'] = _("You have %(nvotes)s %(tvotes)s left today.") % \ {'nvotes': votes_left, 'tvotes': ungettext('vote', 'votes', votes_left)} return response
def comment(request, id): post = get_object_or_404(Node, id=id) user = request.user if not request.method == 'POST': raise CommandException(_("Invalid request")) vote_type = request.POST.get('vote_type', 'comment') is_voting = (vote_type in ('up', 'down')) if not user.is_authenticated(): msg = is_voting and _('vote') or _('comment') raise AnonymousNotAllowedException(msg) from forum.models import CustomBadge if is_voting and CustomBadge.is_voting_restricted(user, post): msg = _('Only users with the badge (or the badge owners) are allowed to vote.') raise CommandException(msg) if is_voting and user == post.author: raise CannotDoOnOwnException(_('vote')) if vote_type == 'up' and not user.can_vote_up(): raise NotEnoughRepPointsException(_('upvote')) if vote_type == 'down' and not user.can_vote_down(): raise NotEnoughRepPointsException(_('downvote')) user_vote_count_today = user.get_vote_count_today() if is_voting and user_vote_count_today >= user.can_vote_count_today(): raise NotEnoughLeftException(_('votes'), str(settings.MAX_VOTES_PER_DAY)) comment_text = request.POST.get('comment', '').strip() if not len(comment_text): raise CommandException(_("Comment is empty")) if len(comment_text) < settings.FORM_MIN_COMMENT_BODY: raise CommandException(_("At least %d characters required on comment body.") % settings.FORM_MIN_COMMENT_BODY) if len(comment_text) > settings.FORM_MAX_COMMENT_BODY: raise CommandException(_("No more than %d characters on comment body.") % settings.FORM_MAX_COMMENT_BODY) old_vote = VoteAction.get_action_for(node=post, user=user) if is_voting and old_vote: is_too_old = (old_vote.action_date < datetime.datetime.now() - datetime.timedelta(days=int(settings.DENY_UNVOTE_DAYS))) if is_too_old: raise CommandException( _("Sorry but you cannot cancel a vote after %(ndays)d %(tdays)s from the original vote") % {'ndays': int(settings.DENY_UNVOTE_DAYS), 'tdays': ungettext('day', 'days', int(settings.DENY_UNVOTE_DAYS))} ) if 'id' in request.POST: comment = get_object_or_404(Comment, id=request.POST['id']) if not user.can_edit_comment(comment): raise NotEnoughRepPointsException( _('edit comments')) else: if not user.can_comment(post): raise NotEnoughRepPointsException( _('comment')) if is_voting: new_vote_cls = (vote_type == 'up') and VoteUpAction or VoteDownAction score_inc = 0 if old_vote: old_vote.cancel(ip=request.META['REMOTE_ADDR']) score_inc += (old_vote.__class__ == VoteDownAction) and 1 or -1 if old_vote.__class__ != new_vote_cls: new_vote_cls(user=user, node=post, ip=request.META['REMOTE_ADDR']).save() score_inc += (new_vote_cls == VoteUpAction) and 1 or -1 else: vote_type = "none" if 'id' in request.POST: comment = ReviseAction(user=user, node=comment, ip=request.META['REMOTE_ADDR']).save( data=dict(text=comment_text)).node else: comment = CommentAction(user=user, ip=request.META['REMOTE_ADDR']).save( data=dict(text=comment_text, parent=post)).node if not is_voting: comment_type = VoteComment.COMMENT elif old_vote.__class__ != new_vote_cls and new_vote_cls == VoteUpAction: comment_type = VoteComment.VOTE_UP elif old_vote.__class__ != new_vote_cls and new_vote_cls != VoteUpAction: comment_type = VoteComment.VOTE_DOWN elif old_vote.__class__ == VoteUpAction: comment_type = VoteComment.CANCEL_VOTE_UP else: comment_type = VoteComment.CANCEL_VOTE_DOWN vote_comment = VoteComment(comment_type=comment_type, comment=comment) vote_comment.save() if comment.active_revision.revision == 1: response = { 'commands': { 'insert_comment': [id, comment.id, comment.comment, user.decorated_name, user.get_profile_url(), reverse('delete_comment', kwargs={'id': comment.id}), reverse('node_markdown', kwargs={'id': comment.id}), reverse('convert_comment', kwargs={'id': comment.id}), user.can_delete_comment(comment)] } } else: response = {'commands': {'update_comment': [comment.id, comment.comment]}} if is_voting: response['commands']['update_post_score'] = [id, score_inc] response['commands']['update_user_post_vote'] = [id, vote_type] votes_left = (int(settings.MAX_VOTES_PER_DAY) - user_vote_count_today) + (vote_type == 'none' and -1 or 1) if int(settings.START_WARN_VOTES_LEFT) >= votes_left: response['message'] = _("You have %(nvotes)s %(tvotes)s left today.") % \ {'nvotes': votes_left, 'tvotes': ungettext('vote', 'votes', votes_left)} return response