def post_moderate(request, uid): """Used to make display post moderate form given a post request.""" user = request.user post = Post.objects.filter(uid=uid).first() if request.method == "POST": form = forms.PostModForm(post=post, data=request.POST, user=user, request=request) if form.is_valid(): action = form.cleaned_data.get('action') comment = form.cleaned_data.get('comment') mod = auth.Moderate(user=user, post=post, action=action, comment=comment) messages.success(request=request, message=mod.msg) auth.log_action(user=user, log_text=f"{mod.msg} ; post.uid={post.uid}.") return redirect(mod.url) else: errors = ','.join([err for err in form.non_field_errors()]) messages.error(request, errors) return redirect(reverse("post_view", kwargs=dict(uid=post.root.uid))) else: form = forms.PostModForm(post=post, user=user, request=request) context = dict(form=form, post=post) return render(request, "forms/form_moderate.html", context)
def score(post, threshold=None): """ """ if not settings.CLASSIFY_SPAM: return if threshold is None: threshold = settings.SPAM_THRESHOLD # User's with high enough score automatically given green light. # if not post.author.profile.low_rep: # return # Search for spam similar to this post. post_score = compute_score(post=post) # Update the spam score. Post.objects.filter(id=post.id).update(spam_score=post_score) # If the score exceeds threshold it gets quarantined. if post_score >= threshold: Post.objects.filter(id=post.id).update(spam=Post.SUSPECT) auth.log_action(log_text=f"Quarantined post={post.uid}; spam score={post_score}")