Add thread locking and permissions

Mods can lock threads from replying now. Yay!

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-08 21:55:25 -07:00
parent e866cbae0f
commit a3ebf75e83
4 changed files with 36 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ from django import forms
from django.forms import ModelForm, ModelChoiceField from django.forms import ModelForm, ModelChoiceField
from django.forms.models import fields_for_model from django.forms.models import fields_for_model
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext as _
from guardian.shortcuts import get_objects_for_user from guardian.shortcuts import get_objects_for_user
from board.models import Ban, Post, Report, ReportReason, ReportRecord from board.models import Ban, Post, Report, ReportReason, ReportRecord
from hcaptcha.fields import hCaptchaField from hcaptcha.fields import hCaptchaField
@@ -38,7 +39,7 @@ class PostForm(ModelForm):
capcode = self.cleaned_data["capcode"] capcode = self.cleaned_data["capcode"]
if capcode: if capcode:
if not self.user or not self.user.has_perm("board.use_capcode", capcode): if not self.user or not self.user.has_perm("board.use_capcode", capcode):
raise ValidationError("Could not create post") raise ValidationError(_("Could not create post"))
class ReplyForm(PostForm): class ReplyForm(PostForm):
@@ -67,10 +68,12 @@ class ReplyForm(PostForm):
def clean(self): def clean(self):
super().clean() super().clean()
if self.instance.op.lock:
raise ValidationError(_("This thread is locked, you cannot reply to it"))
capcode = self.cleaned_data["capcode"] capcode = self.cleaned_data["capcode"]
if capcode: if capcode:
if not self.user or not self.user.has_perm("board.use_capcode", capcode): if not self.user or not self.user.has_perm("board.use_capcode", capcode):
raise ValidationError("Could not create post") raise ValidationError(_("Could not create post"))
class ReportForm(ModelForm): class ReportForm(ModelForm):
@@ -142,7 +145,7 @@ class PostModifyForm(ModelForm):
model = Post model = Post
# we specify fields up here too because otherwise they won't be # we specify fields up here too because otherwise they won't be
# recognized by the form to update values # recognized by the form to update values
fields = ["sticky", "bump"] fields = ["sticky", "bump", "lock"]
def __init__(self, *args, user, **kwargs): def __init__(self, *args, user, **kwargs):
super(PostModifyForm, self).__init__(*args, **kwargs) super(PostModifyForm, self).__init__(*args, **kwargs)
@@ -152,6 +155,8 @@ class PostModifyForm(ModelForm):
fields += ["sticky"] fields += ["sticky"]
if self.user.has_perm("board.set_bump"): if self.user.has_perm("board.set_bump"):
fields += ["bump"] fields += ["bump"]
if self.user.has_perm("board.set_lock") and not self.instance.op:
fields += ["lock"]
# NOTE: # NOTE:
# We do *not* need to check permissions against these fields we're # We do *not* need to check permissions against these fields we're
# setting down here in the self.clean() function in the case that a # setting down here in the self.clean() function in the case that a

View File

@@ -93,8 +93,10 @@ class Post(models.Model):
ip = models.GenericIPAddressField() ip = models.GenericIPAddressField()
# Capcode # Capcode
capcode = models.ForeignKey("Capcode", null=True, blank=True, on_delete=SET_NULL) capcode = models.ForeignKey("Capcode", null=True, blank=True, on_delete=SET_NULL)
# Post is stickied # Post is stickied - it remains at the top of the bump list.
sticky = models.BooleanField(default=False, blank=True) sticky = models.BooleanField(default=False, blank=True)
# Post is locked - nobody can post in it.
lock = models.BooleanField(default=False, blank=True)
# Creation time # Creation time
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
# Last bump time # Last bump time
@@ -122,6 +124,7 @@ class Post(models.Model):
permissions = [ permissions = [
("set_sticky", "Can sticky post"), ("set_sticky", "Can sticky post"),
("set_bump", "Can bumplock post"), ("set_bump", "Can bumplock post"),
("set_lock", "Can lock post"),
] ]
def save(self, *args, **kwargs): def save(self, *args, **kwargs):

View File

@@ -27,9 +27,12 @@
</div> </div>
{% endif %} {% endif %}
{# Post ID, sticky, username, time #} {# Post ID, status icons, username, time #}
{% if post.sticky and not post.op %} {% if post.sticky and not post.op %}
<span title="Stickied" class="fa fa-sticky-note"></span> <span title="Stickied" class="fa fa-thumb-tack"></span>
{% endif %}
{% if post.lock and not post.op %}
<span title="Locked" class="fa fa-lock"></span>
{% endif %} {% endif %}
<a href="#p{{post.id}}">#.</a> <a href="#p{{post.id}}">#.</a>
<span class="post_id">{{post.id}}</span> <span class="post_id">{{post.id}}</span>

View File

@@ -28,17 +28,32 @@ input[type=text] {
{% block content %} {% block content %}
<form method="post" action="{% url 'board:reply_create' url=board.url id=post.id %}" enctype="multipart/form-data"> <form method="post" action="{% url 'board:reply_create' url=board.url id=post.id %}" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{% if form.errors %}
{{form.non_field_errors}}
{% endif %}
<table> <table>
{% for field in form %} {% for field in form %}
{% if field.name != "capcode" %} {% if field.name != "capcode" %}
<tr> {% if field.errors %}
<th>{{field.label_tag}}</th> <tr>
<td>{{field}}</td> <th></th>
</tr> <td>{{field.errors}}</td>
</tr>
{% endif %}
<tr>
<th>{{field.label_tag}}</th>
<td>{{field}}</td>
</tr>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if capcodes %} {% if capcodes %}
<tr> <tr>
{% if form.capcode.errors %}
<tr>
<th></th>
<td>{{form.capcode.errors}}</td>
</tr>
{% endif %}
<th>{{form.capcode.label_tag}}</th> <th>{{form.capcode.label_tag}}</th>
<td> <td>
{{form.capcode}} {{form.capcode}}