This commit is contained in:
some weird guy
2023-10-19 16:16:35 -07:00
parent bb7e453ed4
commit 3d9be1f95f
9 changed files with 116 additions and 62 deletions
+44 -25
View File
@@ -1,6 +1,6 @@
from django import forms from django import forms
import uuid import uuid
from PIL import Image from PIL import Image, UnidentifiedImageError
import io import io
from .models import * from .models import *
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
@@ -13,34 +13,52 @@ from django.core.validators import EmailValidator
def compress_and_resize_content(image, icon=False): def compress_and_resize_content(image, icon=False):
try: try:
im = Image.open(image) im = open_and_convert_image(image)
im = im.convert('RGB')
if icon == True: if icon:
width, height = im.size im = crop_to_square(im)
min_dimension = min(width, height) im = resize_image(im, (100, 100))
left = (width - min_dimension) / 2
top = (height - min_dimension) / 2
right = (width + min_dimension) / 2
bottom = (height + min_dimension) / 2
im = im.crop((left, top, right, bottom))
im.thumbnail((100, 100))
else: else:
im.thumbnail((1200, 1200)) im = resize_image(im, (1200, 1200))
return save_image_to_webp(im)
output = io.BytesIO() except UnidentifiedImageError:
# no more webp print("Error: Unable to identify the image file.")
im.save(output, format='JPEG', quality=85)
output.seek(0)
random_name = f"{uuid.uuid4()}.jpg"
return ContentFile(output.read(), name=random_name)
except:
return image return image
except Exception as e:
print(f"Error: {e}")
return image
def open_and_convert_image(image):
im = Image.open(image)
if im.mode in ('RGBA', 'LA') or (im.mode == 'P' and 'transparency' in im.info):
return im.convert('RGBA')
else:
return im.convert('RGB')
def crop_to_square(im):
width, height = im.size
min_dimension = min(width, height)
left = (width - min_dimension) / 2
top = (height - min_dimension) / 2
right = (width + min_dimension) / 2
bottom = (height + min_dimension) / 2
return im.crop((left, top, right, bottom))
def resize_image(im, size):
im.thumbnail(size)
return im
def save_image_to_webp(im):
output = io.BytesIO()
im.save(output, format='WEBP', quality=85, method=6, minimize_size=True)
output.seek(0)
random_name = f"{uuid.uuid4()}.webp"
return ContentFile(output.read(), name=random_name)
class message_form(forms.ModelForm): class message_form(forms.ModelForm):
body = forms.CharField(max_length=2200, required=True) body = forms.CharField(max_length=2200, required=True)
file = forms.FileField(required=False) file = forms.FileField(required=False)
feeling_id = forms.IntegerField(required=False) feeling_id = forms.IntegerField(required=False, min_value=0, max_value=5)
def clean_file(self): def clean_file(self):
file = self.cleaned_data.get('file') file = self.cleaned_data.get('file')
@@ -62,7 +80,7 @@ class message_form(forms.ModelForm):
class comment_form(forms.ModelForm): class comment_form(forms.ModelForm):
body = forms.CharField(max_length=2200, required=True) body = forms.CharField(max_length=2200, required=True)
file = forms.FileField(required=False) file = forms.FileField(required=False)
feeling_id = forms.IntegerField(required=False) feeling_id = forms.IntegerField(required=False, min_value=0, max_value=5)
is_spoiler = forms.BooleanField(required=False) is_spoiler = forms.BooleanField(required=False)
def clean_file(self): def clean_file(self):
@@ -86,7 +104,7 @@ class post_form(forms.ModelForm):
body = forms.CharField(max_length=2200, required=True) body = forms.CharField(max_length=2200, required=True)
url = forms.URLField(required=False) url = forms.URLField(required=False)
file = forms.FileField(required=False) file = forms.FileField(required=False)
feeling_id = forms.IntegerField(required=False) feeling_id = forms.IntegerField(required=False, min_value=0, max_value=5)
is_spoiler = forms.BooleanField(required=False) is_spoiler = forms.BooleanField(required=False)
def clean_file(self): def clean_file(self):
@@ -254,7 +272,8 @@ class give_ban(forms.ModelForm):
] ]
# In the future we can add options for IP bans and shit. # In the future we can add options for IP bans and shit.
reason = forms.CharField(required=True, widget=forms.Textarea(attrs={'class': 'textarea'})) reason = forms.CharField(required=True, widget=forms.Textarea(attrs={'class': 'textarea'}))
expiry_date = forms.ChoiceField(required=False, choices=BAN_OPTIONS, initial=1) expiry_date = forms.ChoiceField(required=True, choices=BAN_OPTIONS, initial=1)
purge = forms.ChoiceField(required=True, choices=((1, "Purge"), (2, "Don't purge")))
def clean_expiry_date(self): def clean_expiry_date(self):
expiry_choice = self.cleaned_data['expiry_date'] expiry_choice = self.cleaned_data['expiry_date']
+5 -1
View File
@@ -57,7 +57,7 @@ class UserManager(BaseUserManager):
profile.origin_info = json.dumps(nn) profile.origin_info = json.dumps(nn)
user.avatar_type = 2 user.avatar_type = 2
else: else:
user.avatar_input = util.get_gravatar(email) or ('s' if getrandbits(1) else '') user.avatar_input = util.get_gravatar(email) or None
user.avatar_type = 1 user.avatar_type = 1
user.set_password(password) user.set_password(password)
@@ -242,6 +242,10 @@ class User(AbstractBaseUser, PermissionsMixin):
return int(limit) - recent_posts return int(limit) - recent_posts
def get_class(self): def get_class(self):
if self.banned() or not self.is_active:
first = settings.STATIC_URL + "img/banned.svg"
second = "Banned"
return [first, second]
if not self.role: if not self.role:
return [None, None] return [None, None]
second = self.role.organization second = self.role.organization
@@ -1,5 +1,5 @@
{% if not post.is_rm %} {% if not post.is_rm %}
{% 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"> {% load closedverse_tags %}<div id="post-{{ post.id }}" {% if post.spoils and not post.is_mine %}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.user_is_blocked %} hidden test-hidden{% endif %}{% if type == 2 %} post-list-outline{% endif %}" tabindex="0">
{% if with_community_container %} {% if with_community_container %}
<p class="community-container"> <p class="community-container">
{% if post.is_reply %} {% if post.is_reply %}
@@ -38,23 +38,15 @@
{% else %}<a>File attached</a> {% else %}<a>File attached</a>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if post.spoils and not post.is_mine and not post.creator.banned %} {% if post.user_is_blocked %}
<div class="hidden-content"><p>Content hidden because you've blocked this user.{% if post.user_is_blocked %} Oh yeah, and spoilers too I guess.{% endif %}</p>
<button type="button" class="hidden-content-button">View Anyway</button>
</div>
{% elif post.spoils and not post.is_mine %}
<div class="hidden-content"><p>This post contains spoilers.</p> <div class="hidden-content"><p>This post contains spoilers.</p>
<button type="button" class="hidden-content-button">View Post</button> <button type="button" class="hidden-content-button">View Post</button>
</div> </div>
{% endif %} {% endif %}
{% if post.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ post.creator.username }} was banned. </p>
{% if post.creator.active_ban.reason %}
<p>Reason: {{post.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div>
{% elif post.user_is_blocked %}
<div class="hidden-content"><p>Content hidden because you've blocked this user.</p>
<button type="button" class="hidden-content-button">View Anyway</button>
</div>
{% endif %}
<div class="post-meta"> <div class="post-meta">
<button type="button" {% if not post.can_yeah %}disabled{% endif %} class="symbol submit yeah-button <button type="button" {% if not post.can_yeah %}disabled{% endif %} class="symbol submit yeah-button
{% if post.has_yeah %}empathy-added{% endif %} {% if post.has_yeah %}empathy-added{% endif %}
@@ -67,7 +59,7 @@
<div class="reply symbol"><span class="symbol-label">Comments</span><span class="reply-count">{{ post.number_comments }}</span></div> <div class="reply symbol"><span class="symbol-label">Comments</span><span class="reply-count">{{ post.number_comments }}</span></div>
{% endif %} {% endif %}
</div> </div>
{% if post.recent_comment and not post.recent_comment.creator.banned %} {% if post.recent_comment %}
<div class="recent-reply-content"> <div class="recent-reply-content">
{% if post.number_comments > 1 %} {% if post.number_comments > 1 %}
<div class="recent-reply-read-more-container" tabindex="0"> <div class="recent-reply-read-more-container" tabindex="0">
@@ -1,4 +1,4 @@
{% 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"> {% if not comment.is_rm %}{% load closedverse_tags %}<li id="comment-{{ comment.id }}" {% if comment.spoils and not comment.is_mine %}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 %} hidden{% endif %} trigger">
{% user_icon_container comment.creator comment.feeling %} {% user_icon_container comment.creator comment.feeling %}
<div class="body"> <div class="body">
<div class="header"> <div class="header">
@@ -20,19 +20,11 @@
{% else %}<a>File attached</a> {% else %}<a>File attached</a>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if comment.spoils and not comment.is_mine and not comment.creator.banned %} {% if comment.spoils and not comment.is_mine %}
<div class="hidden-content"><p>This comment contains spoilers.</p> <div class="hidden-content"><p>This comment contains spoilers.</p>
<button type="button" class="hidden-content-button">View Comment</button> <button type="button" class="hidden-content-button">View Comment</button>
</div> </div>
{% endif %} {% endif %}
{% if comment.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ comment.creator.username }} was banned. </p>
{% if comment.creator.active_ban.reason %}
<p>Reason: {{comment.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div>
{% endif %}
<div class="reply-meta"> <div class="reply-meta">
<button type="button" {% if not comment.can_yeah %}disabled{% endif %} class="symbol submit yeah-button <button type="button" {% if not comment.can_yeah %}disabled{% endif %} class="symbol submit yeah-button
@@ -1,4 +1,4 @@
{% 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.file_type == 1 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"> {% load closedverse_tags %}<div {% if post.spoils and not post.is_mine %}data-href-hidden{% else %}data-href{% endif %}="{% url "main:post-view" post.id %}" class="post trigger{% if post.file_type == 1 and not for_announcements %} with-image{% endif %}{% if post.spoils and not post.is_mine %} 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> <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>
@@ -33,17 +33,9 @@
{% else %} {% else %}
<p class="post-content-text">{{ post.body|truncatechars:100|linebreaksbr }}</p> <p class="post-content-text">{{ post.body|truncatechars:100|linebreaksbr }}</p>
{% endif %} {% endif %}
{% if post.spoils and not post.is_mine and not post.creator.banned %} {% if post.spoils and not post.is_mine %}
<div class="hidden-content"><p>This post contains spoilers. <div class="hidden-content"><p>This post contains spoilers.
<button type="button" class="hidden-content-button">View Post</button> <button type="button" class="hidden-content-button">View Post</button>
</p></div>
{% endif %}
{% if post.creator.banned %}
<div class="hidden-content"><p>Content hidden because {{ post.creator.username }} was banned. </p>
{% if post.creator.active_ban.reason %}
<p>Reason: {{post.creator.active_ban.reason }}
{% endif %}
<button type="button" class="hidden-content-button">View Anyway</button>
</p></div> </p></div>
{% endif %} {% endif %}
{% if not for_announcements %} {% if not for_announcements %}
@@ -11,6 +11,9 @@
<p class="settings-label">Ban this user for:</p> <p class="settings-label">Ban this user for:</p>
{{ form.expiry_date }} {{ form.expiry_date }}
<p class='note'>{{ user.username }} will be banned from {{ brand_name }} until the time runs out.</p> <p class='note'>{{ user.username }} will be banned from {{ brand_name }} until the time runs out.</p>
<p class="settings-label">Purge this user?</p>
{{ form.purge }}
<p class='note'>Should we purge {{ user.username }}? All comments and posts will be removed.</p>
</li> </li>
{% csrf_token %} {% csrf_token %}
<div class="form-buttons"> <div class="form-buttons">
+3
View File
@@ -1595,6 +1595,9 @@ def user_tools_bans(request, username):
ban.to = user ban.to = user
ban.by = request.user ban.by = request.user
ban.ip_address = user.addr ban.ip_address = user.addr
if form.cleaned_data.get('purge') == "1":
Post.real.filter(creator=user, is_rm=False).update(is_rm=True, status=5)
Comment.real.filter(creator=user, is_rm=False).update(is_rm=True, status=5)
ban.save() ban.save()
AuditLog.objects.create(type=5, user=user, by=request.user) AuditLog.objects.create(type=5, user=user, by=request.user)
return redirect('main:user-view', user) return redirect('main:user-view', user)
+49
View File
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="20"
height="20.000002"
viewBox="0 0 5.2916665 5.2916671"
version="1.1"
id="svg1"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview1"
pagecolor="#ffffff"
bordercolor="#000000"
borderopacity="0.25"
inkscape:showpageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm" />
<defs
id="defs1" />
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-0.26458321,-0.2645832)">
<circle
style="fill:none;fill-opacity:0;stroke:#ff0000;stroke-width:0.529167;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
id="path1"
cx="2.9104166"
cy="2.9104166"
r="2.3812499" />
<circle
id="path2"
style="fill:#000000;stroke:#ff0000;stroke-width:0.264583"
cx="1.3128459"
cy="1.0046126"
r="0.0030412138" />
<path
style="fill:none;fill-opacity:0;stroke:#ff0000;stroke-width:0.529167;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
d="M 1.4420967,1.4045288 4.6411153,4.5459695"
id="path3"
sodipodi:nodetypes="cc" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB