Pretty nice commit overall, made the upload mechanic much more realistic and usable. Only thing left for the upload is the user id assignment. Check new ideas in discord.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
website/upload/__pycache__/validators.cpython-36.pyc
Normal file
BIN
website/upload/__pycache__/validators.cpython-36.pyc
Normal file
Binary file not shown.
Binary file not shown.
@ -1,9 +1,15 @@
|
||||
from django import forms
|
||||
from .models import Image
|
||||
from .models import Image, Videos
|
||||
|
||||
|
||||
class ImageForm(forms.ModelForm):
|
||||
"""Form for the image model"""
|
||||
class Meta:
|
||||
model = Image
|
||||
fields = ('image',)
|
||||
fields = ('title', 'image')
|
||||
|
||||
class VideoForm(forms.ModelForm):
|
||||
"""Form for the image model"""
|
||||
class Meta:
|
||||
model = Videos
|
||||
fields = ('title', 'video')
|
||||
|
25
website/upload/migrations/0004_auto_20210217_0041.py
Normal file
25
website/upload/migrations/0004_auto_20210217_0041.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Generated by Django 3.1.6 on 2021-02-16 22:41
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('upload', '0003_auto_20210209_1806'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='image',
|
||||
name='title',
|
||||
field=models.CharField(default=django.utils.timezone.now, max_length=200),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='image',
|
||||
name='image',
|
||||
field=models.ImageField(upload_to='images'),
|
||||
),
|
||||
]
|
25
website/upload/migrations/0005_videos.py
Normal file
25
website/upload/migrations/0005_videos.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Generated by Django 3.1.6 on 2021-02-16 22:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('upload', '0004_auto_20210217_0041'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Videos',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=100)),
|
||||
('video', models.FileField(upload_to='videos/')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'video',
|
||||
'verbose_name_plural': 'videos',
|
||||
},
|
||||
),
|
||||
]
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
website/upload/migrations/__pycache__/0005_videos.cpython-36.pyc
Normal file
BIN
website/upload/migrations/__pycache__/0005_videos.cpython-36.pyc
Normal file
Binary file not shown.
Binary file not shown.
@ -1,4 +1,27 @@
|
||||
from django.db import models
|
||||
from .validators import videoValidate, imageValidate
|
||||
|
||||
# def user_directory._path(instance, filename):
|
||||
# print(request.user.id)
|
||||
# return 'user_{0}/{1}'.format(instance.user.id, filename)
|
||||
# Ok i got hella angry, so pleaSE fix this bullshit
|
||||
# I want to be able to store each and every user's files in a separate folder, based on their id
|
||||
# I CANT GET A FUCKING LOGGED IN USER ID FROM THE MODELS THINGY IM FUCKING PISSED
|
||||
|
||||
class Image(models.Model):
|
||||
image = models.FileField(upload_to='static/')
|
||||
title = models.CharField(max_length=200)
|
||||
image = models.ImageField(upload_to=f"images/{upload_to}",validators=[imageValidate])
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
||||
class Videos(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
video = models.FileField(upload_to=f'videos/', validators=[videoValidate])
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'video'
|
||||
verbose_name_plural = 'videos'
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
15
website/upload/validators.py
Normal file
15
website/upload/validators.py
Normal file
@ -0,0 +1,15 @@
|
||||
def imageValidate(value):
|
||||
import os
|
||||
from django.core.exceptions import ValidationError
|
||||
ext = os.path.splitext(value.name)[1]
|
||||
valid_extensions = ['.jpg', '.png', '.gif']
|
||||
if not ext.lower() in valid_extensions:
|
||||
raise ValidationError('Unsupported file extension.')
|
||||
|
||||
def videoValidate(value):
|
||||
import os
|
||||
from django.core.exceptions import ValidationError
|
||||
ext = os.path.splitext(value.name)[1]
|
||||
valid_extensions = ['.mp4', '.mkv', '.avi']
|
||||
if not ext.lower() in valid_extensions:
|
||||
raise ValidationError('Unsupported file extension.')
|
@ -1,8 +1,9 @@
|
||||
from django.shortcuts import render, redirect
|
||||
import os.path
|
||||
from .forms import ImageForm
|
||||
from .forms import ImageForm, VideoForm
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
|
||||
|
||||
def uploadContent(request):
|
||||
if request.method == "GET":
|
||||
return render(request, "uploadChoice.html")
|
||||
@ -24,15 +25,25 @@ def imageUpload(request):
|
||||
if request.method == 'POST':
|
||||
form = ImageForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
myfile = request.FILES['image']
|
||||
fs = FileSystemStorage(location="./static/") #defaults to MEDIA_ROOT
|
||||
filename = fs.save(myfile.name, myfile)
|
||||
file_url = fs.url(filename)
|
||||
|
||||
return render(request, 'uploadImage.html', {
|
||||
'file_url': file_url,
|
||||
'file_name': myfile.name,
|
||||
})
|
||||
form.save()
|
||||
# Get the current instance object to display in the template
|
||||
img_obj = form.instance
|
||||
return render(request, 'uploadImage.html', {'form': form, 'img_obj': img_obj})
|
||||
|
||||
else:
|
||||
form = ImageForm()
|
||||
return render(request, 'uploadImage.html', {'form': form})
|
||||
|
||||
def videoUpload(request):
|
||||
"""Process videos uploaded by users"""
|
||||
if request.method == 'POST':
|
||||
form = VideoForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
img_obj = form.instance
|
||||
return render(request, 'uploadVideo.html', {'form': form, 'img_obj': img_obj})
|
||||
|
||||
else:
|
||||
form = VideoForm()
|
||||
return render(request, 'uploadVideo.html', {'form': form})
|
||||
|
||||
|
Reference in New Issue
Block a user