diff --git a/closedverse_main/forms.py b/closedverse_main/forms.py index e4a5c6d..01d8613 100644 --- a/closedverse_main/forms.py +++ b/closedverse_main/forms.py @@ -1,27 +1,76 @@ from django import forms +import uuid +from PIL import Image +import io from .models import * +from django.core.files.base import ContentFile from django.contrib.auth.password_validation import validate_password from django.core.exceptions import ValidationError from django.utils.timezone import timedelta from closedverse import settings +# will be used for community icons and profile pictures +def compress_and_resize_icon(image): + im = Image.open(image) + im = im.convert('RGB') # Convert to RGB + + # Crop to 1:1 aspect ratio + 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 + im = im.crop((left, top, right, bottom)) + + # Resize to 100 by 100 or smaller + im.thumbnail((100, 100)) + + # Compress the image + output = io.BytesIO() + im.save(output, format='WEBP', quality=85) + output.seek(0) + random_name = f"{uuid.uuid4()}.webp" + return ContentFile(output.read(), name=random_name) + +def compress_and_resize_content(image): + im = Image.open(image) + im = im.convert('RGB') + + # Resize to 1,200 by 1,200 or smaller + im.thumbnail((1200, 1200)) + + # Compress the image + output = io.BytesIO() + im.save(output, format='WEBP', quality=85) + output.seek(0) + random_name = f"{uuid.uuid4()}.webp" + return ContentFile(output.read(), name=random_name) + # I do want to move each and every form over to here. Not only will this trivialize making new forms, this will also make it more secure or something. class CommunitySettingForm(forms.ModelForm): - community_name = forms.CharField(max_length=64,required=True) - community_description = forms.CharField(max_length = 2200,required=False) - community_platform = forms.IntegerField(max_value = 7, min_value = 0, required = True) - force_login = forms.BooleanField(required = False) - community_icon = forms.ImageField(required = False) - community_banner = forms.ImageField(required = False) + description = forms.CharField(max_length = 2200,required=False, widget=forms.Textarea(attrs={'class': 'textarea'})) + def clean_ico(self): + ico = self.cleaned_data.get('ico') + if ico: + return compress_and_resize_icon(image=ico) + return ico + + def clean_banner(self): + banner = self.cleaned_data.get('banner') + if banner: + return compress_and_resize_content(image=banner) + return banner + class Meta: model = Community fields = ( - 'community_name', - 'community_description', - 'community_platform', - 'force_login', - 'community_icon', - 'community_banner' + 'name', + 'description', + 'platform', + 'require_auth', + 'ico', + 'banner', ) class Settomgs_Change_Password(forms.Form): diff --git a/closedverse_main/models.py b/closedverse_main/models.py index 61b3e42..981885a 100644 --- a/closedverse_main/models.py +++ b/closedverse_main/models.py @@ -668,7 +668,7 @@ class Community(models.Model): banner = models.ImageField(null=True, blank=True) # Type: 0 - general, 1 - game, 2 - special type = models.SmallIntegerField(default=0, help_text='The category the community belongs in, setting this to None will remove the community.', choices=((0, 'General'), (1, 'Game'), (2, 'Special'), (3, 'User Community'), (4, 'Hide'))) - platform = models.SmallIntegerField(default=0, choices=((0, 'none'), (1, '3ds'), (2, 'wii u'), (3, 'switch'), (4, 'both'), (5, 'PC'), (6, 'Xbox'), (7, 'Playstation'))) + platform = models.SmallIntegerField(default=0, choices=((0, 'None'), (1, '3DS'), (2, 'Wii U'), (3, 'Switch'), (4, '3DS and Wii U'), (5, 'PC'), (6, 'Xbox'), (7, 'Playstation'))) tags = models.CharField(blank=True, help_text='Provides special functionality for specific communities.', null=True, max_length=255, choices=(('announcements', 'main announcement community'), ('changelog', 'main changelog'), ('activity', 'Activity Feed posting community'), ('general', 'General Discussion Community'))) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) diff --git a/closedverse_main/templates/closedverse_main/community_create.html b/closedverse_main/templates/closedverse_main/community_create.html index 44bf6c9..9163fa5 100644 --- a/closedverse_main/templates/closedverse_main/community_create.html +++ b/closedverse_main/templates/closedverse_main/community_create.html @@ -5,37 +5,49 @@
{% endblock %} \ No newline at end of file diff --git a/closedverse_main/templates/closedverse_main/community_tools.html b/closedverse_main/templates/closedverse_main/community_tools.html index 1dac816..7bb44d8 100644 --- a/closedverse_main/templates/closedverse_main/community_tools.html +++ b/closedverse_main/templates/closedverse_main/community_tools.html @@ -10,49 +10,39 @@Set name:
Community description:
- + {{ form.description }}- Require login: + {{ form.require_auth }}Require login:
If this is on, users will need to sign in to view your community.
-I plan on removing this feature as it's implemented in an extremely janky way. Or at least improving it, so you are no longer forced to sign in to view yeahs from anyone. The whole idea behind this feature is to prevent web crawling or unauthorized third parties from seeing content without an account.
Platform
If your community is for a video game, you can set the platform here. This will show an icon corresponding to what platform you pick.
diff --git a/closedverse_main/views.py b/closedverse_main/views.py index 0daa28b..d337cdf 100644 --- a/closedverse_main/views.py +++ b/closedverse_main/views.py @@ -310,7 +310,7 @@ def signup_page(request): return HttpResponseBadRequest("please do not use a vpn ok thanks") if request.POST.get('origin_id'): if not request.POST.get('mh'): - return HttpResponseBadRequest("sorry didn't get the mii image attribute. you might need to wait or just refresh, sorry") + return HttpResponseBadRequest("sorry didn't get the mii image attribute. you might need to wait or just refresh, sorry") if User.nnid_in_use(request.POST['origin_id']): return HttpResponseBadRequest("That Nintendo Network ID is already in use, that would cause confusion.") #mii = get_mii(request.POST['origin_id']) @@ -497,7 +497,7 @@ def user_view(request, username): user.avatar = ('s' if getrandbits(1) else '') else: if not request.POST.get('mh'): - return json_response('i think you gotta wait for the nnid to retrieve') + return json_response('i think you gotta wait for the nnid to retrieve') user.has_mh = True #getmii = get_mii(request.POST.get('origin_id')) #if not getmii: @@ -992,8 +992,10 @@ def community_tools(request, community): can_edit = the_community.can_edit_community(request) if not can_edit: raise Http404() + form = CommunitySettingForm(instance=the_community) return render(request, 'closedverse_main/community_tools.html', { 'title': 'Community tools', + 'form': form, 'community': the_community, 'activity_feed': activity_feed, }) @@ -1006,22 +1008,11 @@ def community_tools_set(request, community): can_edit = the_community.can_edit_community(request) if not can_edit: return HttpResponseForbidden() - form = CommunitySettingForm(request.POST, request.FILES, request , instance=the_community) + form = CommunitySettingForm(request.POST, request.FILES, instance=the_community) if not form.is_valid(): return json_response(form.errors.as_text()) - community = form.save(commit=False) - community.name = form.cleaned_data.get('community_name') - community.description = form.cleaned_data.get('community_description') - community.platform = form.cleaned_data.get('community_platform') - community.require_auth = True if form.cleaned_data.get('force_login') else False - if form.cleaned_data.get('community_icon'): - upload = util.image_upload(form.cleaned_data.get('community_icon'), True, icon=True) - community.ico = upload - if form.cleaned_data.get('community_banner'): - upload = util.image_upload(form.cleaned_data.get('community_banner'), True, banner=True) - community.banner = upload - community.save() - AuditLog.objects.create(type=4, community=community, user=community.creator, by=request.user) + form.save() + AuditLog.objects.create(type=4, community=the_community, user=the_community.creator, by=request.user) return HttpResponse() else: raise Http404() @@ -1032,28 +1023,28 @@ def community_create(request): raise Http404() if request.user.c_tokens < 1: raise Http404() + form = CommunitySettingForm() return render(request, 'closedverse_main/community_create.html', { 'title': 'Create a community', + 'form': form, 'tokens': request.user.c_tokens, }) def community_create_action(request): - user = request.user if request.method == 'POST': if not request.user.is_authenticated: - raise Http404() - if user.c_tokens < 1: - return json_response("You don't have any tokens left") - if len(request.POST.get('community_name')) == 0 or len(request.POST.get('community_name')) >= 100: - return json_response('Your community name is either too short or too long.') - if len(request.POST.get('community_description')) >= 1024: - return json_response('Your community description is too long.') - if int(request.POST.get('community_platform')) >= 8: - return json_response('Invalid Platform type.') - get = request.POST.get - user.c_tokens -= 1 - user.save() - community = Community.objects.create(name=get('community_name'), description=get('community_description'), type=3, platform=get('community_platform'), creator=user) + return HttpResponseForbidden() + if request.user.c_tokens < 1: + return HttpResponseForbidden() + form = CommunitySettingForm(request.POST, request.FILES) + if not form.is_valid(): + return json_response(form.errors.as_text()) + community = form.save() + community.type = 3 + community.creator = request.user + community.save() return json_response('Community has been created, check the front page!', 'Done') + else: + raise Http404() @login_required def post_create(request, community): if request.method == 'POST':