- Changed HTML to support the new fancy ban system
- Added "banned" and "active_ban" bits in models.py
- IP bans are supported now.
This commit is contained in:
some weird guy
2023-08-24 12:20:32 -07:00
parent ca09f3295b
commit e49d856f71
10 changed files with 60 additions and 194 deletions
+5 -1
View File
@@ -142,6 +142,10 @@ class HistoryAdmin(admin.ModelAdmin):
class RoleAdmin(admin.ModelAdmin):
exclude = ('is_static', )
class BanAdmin(admin.ModelAdmin):
# Hide that shit for now, Eventually I plan to get rid of the user_tools_meta thing completely and just show IP addresses for staff like any normal site.
exclude = ('ip_address', )
#class BlockAdmin(admin.ModelAdmin)
admin.site.unregister(Group)
@@ -164,7 +168,7 @@ admin.site.register(models.ProfileHistory, HistoryAdmin)
admin.site.register(models.Post, PostAdmin)
admin.site.register(models.Comment, CommentAdmin)
admin.site.register(models.Ban)
admin.site.register(models.Ban, BanAdmin)
admin.site.register(models.Warning)
if settings.DEBUG:
+13 -8
View File
@@ -21,20 +21,25 @@ class CheckForBanMiddleware:
return response
def process_view(self, request, view_func, view_args, view_kwargs):
# If the user is not authenticated, there's no need to check for bans
if not request.user.is_authenticated:
return None
# Get one active ban that is not expired
active_ban = Ban.objects.filter(
# Get one active ban that is not expired for the user
active_user_ban = Ban.objects.filter(
to=request.user,
active=True,
expiry_date__gte=timezone.now()
).first()
if active_ban:
return HttpResponseForbidden('You are banned from this site. Reason: ' + active_ban.reason)
if active_user_ban:
return HttpResponseForbidden('You are banned from this site. Reason: ' + active_user_ban.reason)
# Get one active ban that is not expired for the IP address
ip_address = request.META.get('REMOTE_ADDR')
active_ip_ban = Ban.objects.filter(
ip_address=ip_address,
active=True,
expiry_date__gte=timezone.now(),
).first()
if active_ip_ban:
return HttpResponseForbidden('Your IP address is banned from this site. Reason: ' + active_ip_ban.reason)
return None
class ClosedMiddleware(object):
+7 -1
View File
@@ -207,6 +207,12 @@ class User(AbstractBaseUser):
if not g:
return settings.STATIC_URL + 'img/anonymous-mii.png'
return g
def banned(self):
banned = self.banned_user.filter(active=True, expiry_date__gt=timezone.now())
return banned.exists()
def active_ban(self):
ban = self.banned_user.filter(active=True, expiry_date__gt=timezone.now()).order_by('-created').first()
return ban
def mh(self):
origin_info = self.profile('origin_info')
if not origin_info:
@@ -582,7 +588,6 @@ class User(AbstractBaseUser):
return False
return user
# This will be a fucking pain in the ass!
class Ban(models.Model):
id = models.AutoField(primary_key=True)
created = models.DateTimeField(auto_now_add=True)
@@ -591,6 +596,7 @@ class Ban(models.Model):
reason = models.TextField(null=True, blank=True)
expiry_date = models.DateTimeField(help_text='The date and time on which this ban will expire, Set this way off into the future if this ban should be permanent')
active = models.BooleanField(default=True, help_text='Untick this to disable this ban')
ip_address = models.CharField(null=True, blank=True, max_length=256, help_text='Put an IP address in here to make this an IP ban. When using the ban function outside of the Django admin panel, this is filled in automatically.')
def is_expired(self):
if timezone.now() > self.expiry_date:
@@ -1,5 +1,5 @@
{% if not post.is_rm %}
{% load closedverse_tags %}<div id="post-{{ post.id }}" {% if post.spoils and not post.is_mine or not post.creator.is_active %}data-href-hidden{% else %}data-href{% endif %}="{% if post.is_reply %}{% url "main:comment-view" post.id %}{% else %}{% url "main:post-view" post.id %}{% endif %}" class="post post-subtype-default trigger{% if post.spoils and not post.is_mine or not post.creator.is_active or post.user_is_blocked %} hidden test-hidden{% endif %}{% if type == 2 %} post-list-outline{% endif %}" tabindex="0">
{% load closedverse_tags %}<div id="post-{{ post.id }}" {% if post.spoils and not post.is_mine or post.creator.banned %}data-href-hidden{% else %}data-href{% endif %}="{% if post.is_reply %}{% url "main:comment-view" post.id %}{% else %}{% url "main:post-view" post.id %}{% endif %}" class="post post-subtype-default trigger{% if post.spoils and not post.is_mine or post.creator.banned or post.user_is_blocked %} hidden test-hidden{% endif %}{% if type == 2 %} post-list-outline{% endif %}" tabindex="0">
{% if with_community_container %}
<p class="community-container">
{% if post.is_reply %}
@@ -15,9 +15,6 @@
<span class="owner-label">Community Owner</span>
{% endif %}
<p class="user-name"><a href="{% url "main:user-view" post.creator.username %}"{% if post.creator.color %}style=color:{{ post.creator.color }}{% endif %}>{{ post.creator.nickname }}</a></p>
{% if not post.creator.is_active %}
<p style="color: #f00;">Banned</p>
{% endif %}
<p class="timestamp-container">
<span class="spoiler-status{% if post.spoils %} spoiler{% endif %}">Spoilers·</span>
{% if post.has_edit %}
@@ -45,15 +42,15 @@
{% endif %}
{% if post.screenshot %}<a class="screenshot-container still-image" href="{% if post.is_reply %}{% url "main:comment-view" post.id %}{% else %}{% url "main:post-view" post.id %}{% endif %}"><img src="{{ post.screenshot }}"></a>{% endif %}
{% if post.video %}<div class="screenshot-container still-image"><video class="vidya" controls src="{{ post.video }}" style="max-width:100%;max-height: 450px;"></video></div>{% endif %}
{% if post.spoils and not post.is_mine and post.creator.is_active %}
{% if post.spoils and not post.is_mine and not post.creator.banned %}
<div class="hidden-content"><p>This post contains spoilers.</p>
<button type="button" class="hidden-content-button">View Post</button>
</div>
{% endif %}
{% if not post.creator.is_active %}
<div class="hidden-content"><p>Content hidden because its creator was banned. </p>
{% if post.creator.get_warned_reason %}
<p>Reason: {{post.creator.get_warned_reason}}
{% if post.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ post.creator.username }} was banned. </p>
{% if post.creator.active_ban %}
<p>Reason: {{post.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div>
@@ -74,7 +71,7 @@
<div class="reply symbol"><span class="symbol-label">Comments</span><span class="reply-count">{{ post.number_comments }}</span></div>
{% endif %}
</div>
{% if post.recent_comment and post.recent_comment.creator.is_active %}
{% if post.recent_comment and not post.recent_comment.creator.banned %}
<div class="recent-reply-content">
{% if post.number_comments > 1 %}
<div class="recent-reply-read-more-container" tabindex="0">
@@ -1,11 +1,8 @@
{% if not comment.is_rm %}{% load closedverse_tags %}<li id="comment-{{ comment.id }}" {% if comment.spoils and not comment.is_mine or not comment.creator.is_active %}data-href-hidden{% else %}data-href{% endif %}="{% url "main:comment-view" comment.id %}" class="post {% if comment.owner_post %}my{% else %}other{% endif %}{% if comment.spoils and not comment.is_mine or not comment.creator.is_active %} hidden{% endif %} trigger">
{% if not comment.is_rm %}{% load closedverse_tags %}<li id="comment-{{ comment.id }}" {% if comment.spoils and not comment.is_mine or comment.creator.banned %}data-href-hidden{% else %}data-href{% endif %}="{% url "main:comment-view" comment.id %}" class="post {% if comment.owner_post %}my{% else %}other{% endif %}{% if comment.spoils and not comment.is_mine or comment.creator.banned %} hidden{% endif %} trigger">
{% user_icon_container comment.creator comment.feeling %}
<div class="body">
<div class="header">
<p class="user-name"><a {% if comment.creator.color %}style=color:{{ comment.creator.color }}{% endif %} href="{% url "main:user-view" comment.creator.username %}">{{ comment.creator.nickname }}</a></p>
{% if not comment.creator.is_active %}
<p style="color: #f00;">Banned</p>
{% endif %}
<p class="timestamp-container">
<a class="timestamp" {% if comment.spoils and not comment.is_mine %}data-href-hidden{% else %}href{% endif %}="{% url "main:comment-view" comment.id %}">{% time comment.created %}</a>
{% if comment.drawing %}
@@ -26,15 +23,15 @@
{% if comment.screenshot %}
<div class="screenshot-container still-image"><img src="{{ comment.screenshot }}"></div>
{% endif %}
{% if comment.spoils and not comment.is_mine and comment.creator.is_active %}
{% if comment.spoils and not comment.is_mine and not comment.creator.banned %}
<div class="hidden-content"><p>This comment contains spoilers.</p>
<button type="button" class="hidden-content-button">View Comment</button>
</div>
{% endif %}
{% if not comment.creator.is_active %}
<div class="hidden-content"><p>Content hidden because its creator was banned. </p>
{% if comment.creator.get_warned_reason %}
<p>Reason: {{comment.creator.get_warned_reason}}
{% if comment.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ comment.creator.username }} was banned. </p>
{% if comment.creator.active_ban %}
<p>Reason: {{comment.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div>
@@ -1,4 +1,4 @@
{% load closedverse_tags %}<div {% if post.spoils and not post.is_mine or not post.creator.is_active %}data-href-hidden{% else %}data-href{% endif %}="{% url "main:post-view" post.id %}" class="post trigger{% if post.screenshot and not for_announcements %} with-image{% endif %}{% if post.spoils and not post.is_mine or not post.creator.is_active %} hidden test-hidden{% endif %}" tabindex="0">
{% load closedverse_tags %}<div {% if post.spoils and not post.is_mine or post.creator.banned %}data-href-hidden{% else %}data-href{% endif %}="{% url "main:post-view" post.id %}" class="post trigger{% if post.screenshot and not for_announcements %} with-image{% endif %}{% if post.spoils and not post.is_mine or post.creator.banned %} hidden test-hidden{% endif %}" tabindex="0">
<p class="community-container"><a {% if post.community.clickable %}href="{% url "main:community-view" post.community_id %}"{% endif %}><img src="{{ post.community.icon }}" class="community-icon">{{ post.community.name }}</a></p>
@@ -40,15 +40,15 @@
<p class="post-content-text">{{ post.body|truncatechars:100|linebreaksbr }}</p>
{% endif %}
{% endif %}
{% if post.spoils and not post.is_mine and post.creator.is_active %}
{% if post.spoils and not post.is_mine and not post.creator.banned %}
<div class="hidden-content"><p>This post contains spoilers.
<button type="button" class="hidden-content-button">View Post</button>
</p></div>
{% endif %}
{% if not post.creator.is_active %}
<div class="hidden-content"><p>Content hidden because its creator was banned. </p>
{% if post.creator.get_warned_reason %}
<p>Reason: {{post.creator.get_warned_reason}}
{% if post.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ post.creator.username }} was banned. </p>
{% if post.creator.active_ban %}
<p>Reason: {{post.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div>
@@ -1,12 +1,12 @@
{% load closedverse_tags %}{% load closedverse_user %}
{% if user.is_authenticated %}<div id="sidebar" class="{% if general %}general{% else %}user{% endif %}-sidebar">
{% if not user.is_active %}
{% if user.banned %}
<div class="notice" style="background-color: #ff9797;border: 1px solid #ff5252;">
<b>Notice</b>: This user account is disabled.
<b>Notice</b>: This user is banned.
<div>
{% if user.get_warned_reason %}
<p>Reason: "{{ user.get_warned_reason }}"</p>
{% if user.active_ban.reason %}
<p>Reason: "{{ user.active_ban.reason }}"</p>
{% endif %}
</div>
</div>
+4 -10
View File
@@ -1743,13 +1743,6 @@ def user_tools_bams(request, username):
user = get_object_or_404(User, username__iexact=username)
profile = user.profile()
profile.setup(request)
active_bans = user.banned_user.filter(active=True).exclude(expiry_date__lte=timezone.now())
if active_bans:
banned = True
active_ban = active_bans.first()
else:
banned = False
active_ban = None
if not request.user.can_manage():
return HttpResponseForbidden()
if user.has_authority(request.user):
@@ -1760,15 +1753,16 @@ def user_tools_bams(request, username):
ban = form.save(commit=False)
ban.to = user
ban.by = request.user
ban.ip_address = user.addr
ban.save()
return redirect('main:user-view', user)
if not banned:
if not user.banned():
form = Give_Ban_Form()
else: form = None
return render(request, 'closedverse_main/man/manage_bans.html', {
'user': user,
'banned': banned,
'active_ban': active_ban,
'banned': user.banned(),
'active_ban': user.active_ban(),
'profile': profile,
'form': form,
})