Fakultas Ilmu Komputer UI

Commit c4793fd3 authored by igor lestin sianipar's avatar igor lestin sianipar
Browse files

Merge branch 'Coldfix-PBI-3-Unggah_Materi-Restriksi' into 'staging'

Coldfix pbi 3 unggah materi restriksi

See merge request ppl-fasilkom-ui/2020/ppl-c/diskominfo-depok-digipus/marjinal-digipus!62
parents 0c606bd1 a61d7887
......@@ -5,7 +5,7 @@ from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.contrib import messages
from django.core import serializers
from django.core.exceptions import PermissionDenied
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
......@@ -199,6 +199,12 @@ class UploadMateriView(TemplateView):
if form.is_valid():
materi = form.save(commit=False)
materi.uploader = request.user
konten = form.cleaned_data['content']
try:
self.validate_file_extension(konten)
except ValidationError:
messages.error(request, "Materi gagal diunggah, format file tidak sesuai")
return HttpResponseRedirect("/unggah/")
materi.save()
kateg = form.cleaned_data['categories']
for i in kateg:
......@@ -217,6 +223,12 @@ class UploadMateriView(TemplateView):
context = self.get_context_data(**kwargs)
context["form"] = UploadMateriForm
return self.render_to_response(context)
def validate_file_extension(self, value):
ext = os.path.splitext(value.name)[1] # [0] returns path+filename
valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls', '.mp4', '.mp3']
if not ext.lower() in valid_extensions:
raise ValidationError('Unsupported file extension.')
class UploadMateriHTML(TemplateView):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment