diff --git a/WTV2D/settings.py b/WTV2D/settings.py index 55da38a..ccfb92d 100755 --- a/WTV2D/settings.py +++ b/WTV2D/settings.py @@ -117,5 +117,8 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.0/howto/static-files/ - +STATIC_ROOT = os.path.join(BASE_DIR, 'static/') STATIC_URL = '/static/' + +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #folder przechowywanych zdjec +MEDIA_URL = '/media/' diff --git a/WTV2D/urls.py b/WTV2D/urls.py index b9aae7e..46f299a 100755 --- a/WTV2D/urls.py +++ b/WTV2D/urls.py @@ -15,8 +15,17 @@ Including another URLconf """ from django.contrib import admin from django.urls import path, include +from django.conf import settings +from django.conf.urls.static import static +from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns = [ path('admin/', admin.site.urls), path('', include('core.urls')), ] + +if settings.DEBUG: + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +else: + urlpatterns += staticfiles_urlpatterns() + diff --git a/core/forms.py b/core/forms.py new file mode 100644 index 0000000..9474d70 --- /dev/null +++ b/core/forms.py @@ -0,0 +1,13 @@ +from django import forms +from .models import VideoFile + +# Form for upload video to the server +class VideoUploadForm(forms.ModelForm): + class Meta: + model = VideoFile + fields = ['file'] + + def __init__(self, *args, **kwargs): + super(VideoUploadForm, self).__init__(*args, **kwargs) + # In html input only accept video uploads + self.fields['file'].widget.attrs.update({'class': 'form-control border-0', 'accept': 'video/*'}) diff --git a/core/models.py b/core/models.py index 71a8362..41ccc28 100755 --- a/core/models.py +++ b/core/models.py @@ -1,3 +1,31 @@ from django.db import models +from enum import Enum # Create your models here. +class VideoFile(models.Model): + """ + A class for uploading video to the server. + """ + + def forDjango(cls): + cls.do_not_call_in_templates = True + return cls + + @forDjango + class VideoFileStatus(Enum): + NEW = 1 + PROCESSING = 4 + READY = 7 + + file = models.FileField(upload_to="", max_length=1024) # If upload_to=="" then file will be uploaded to the MEDIA_ROOT path set in settings. + uploaded_at = models.DateTimeField(auto_now_add=True) + fps = models.IntegerField(default=25) + video_width = models.IntegerField(default=0, blank=True) + video_height = models.IntegerField(default=0, blank=True) + status = models.IntegerField(default=VideoFileStatus.NEW.value) + + + @property + def url(self): + return self.file.url + diff --git a/core/migrations/templates/core/home.html b/core/static/css/main.css similarity index 100% rename from core/migrations/templates/core/home.html rename to core/static/css/main.css diff --git a/core/templates/core/home.html b/core/templates/core/home.html index e69de29..ff7693d 100644 --- a/core/templates/core/home.html +++ b/core/templates/core/home.html @@ -0,0 +1,31 @@ +{% load static %} + + + + + + + + Web-TV2D + + +
+
+

Submit your video

+
+ {% csrf_token %} + {% for message in messages %} + {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %} + {{message}} + {% elif message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %} + {{message}} + {% endif %} + {% endfor %} + {{ form.file }} + +
+
+
+ + diff --git a/core/views.py b/core/views.py index 7648767..82f2ada 100755 --- a/core/views.py +++ b/core/views.py @@ -1,7 +1,29 @@ +import cv2 +from django.http import HttpResponseRedirect from django.shortcuts import render +from django.contrib import messages +from core.forms import VideoUploadForm +from core.models import VideoFile # Create your views here. def home(request): - return render(request, 'core/home.html') + form = VideoUploadForm() + if request.method == "POST": + form = VideoUploadForm(request.POST, request.FILES) + if form.is_valid(): + video_file = VideoFile(file=request.FILES['file']) + video_file.save() + + video = cv2.VideoCapture(video_file.file.path) + video_file.fps = video.get(cv2.CAP_PROP_FPS) + video_file.video_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) + video_file.video_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) + video_file.save() + + else: + messages.error(request, "Something went wrong") + + context = {'form': form} + return render(request, 'core/home.html', context)