diff --git a/amadeus/static/js/resources.js b/amadeus/static/js/resources.js index 575d58b..37e7508 100644 --- a/amadeus/static/js/resources.js +++ b/amadeus/static/js/resources.js @@ -106,7 +106,7 @@ if (window.File && window.FileList && window.FileReader) { // initialize function Init() { - var small = $("#id_file_content"), + var small = $(".file-selector"), filedrag = $(".filedrag"), common = $(".common-file-input"); diff --git a/amadeus/urls.py b/amadeus/urls.py index 2b52fd2..81c1c7a 100644 --- a/amadeus/urls.py +++ b/amadeus/urls.py @@ -38,6 +38,7 @@ urlpatterns = [ url(r'^themes/', include('themes.urls', namespace = 'themes')), url(r'^pendencies/', include('notifications.urls', namespace = 'notifications')), url(r'^links/', include('links.urls', namespace='links')), + url(r'^pdf_files/', include('pdf_file.urls', namespace='pdf_files')), #API url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')), #S3Direct diff --git a/pdf_file/forms.py b/pdf_file/forms.py new file mode 100644 index 0000000..5c5f071 --- /dev/null +++ b/pdf_file/forms.py @@ -0,0 +1,99 @@ +from django import forms +from django.utils.translation import ugettext_lazy as _ +from django.utils.html import strip_tags + +from subjects.models import Tag + +from .models import PDFFile + +class PDFFileForm(forms.ModelForm): + subject = None + MAX_UPLOAD_SIZE = 10*1024*1024 + + def __init__(self, *args, **kwargs): + super(PDFFileForm, self).__init__(*args, **kwargs) + self.subject = kwargs.get('initial').get('subject', None) + + if self.instance.id: + self.subject = self.instance.topic.subject + self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) + + self.fields['students'].queryset = self.subject.students.all() + self.fields['groups'].queryset = self.subject.group_subject.all() + + tags = forms.CharField(label = _('Tags'), required = False) + class Meta: + model = PDFFile + fields = ['name', 'file', 'brief_description', 'all_students', 'students', 'groups', 'visible'] + labels = { + 'name': _('File name'), + } + widgets = { + 'brief_description': forms.Textarea, + 'students': forms.SelectMultiple, + 'groups': forms.SelectMultiple, + } + + def clean_name(self): + name = self.cleaned_data.get('name', '') + + topics = self.subject.topic_subject.all() + + for topic in topics: + if self.instance.id: + same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() + else: + same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() + + if same_name > 0: + self._errors['name'] = [_('This subject already has a file link with this name')] + + return ValueError + + return name + + def clean_file(self): + file = self.cleaned_data.get('file', False) + + if file: + if hasattr(file, '_size'): + if file._size > self.MAX_UPLOAD_SIZE: + self._errors['file'] = [_("The file is too large. It should have less than 10MB.")] + + return ValueError + + elif not self.instance.pk: + self._errors['file'] = [_('This field is required.')] + + return ValueError + + return file + + def save(self, commit = True): + super(PDFFileForm, self).save(commit = True) + + self.instance.save() + + previous_tags = self.instance.tags.all() + + tags = self.cleaned_data['tags'].split(",") + + #Excluding unwanted tags + for prev in previous_tags: + if not prev.name in tags: + self.instance.tags.remove(prev) + + for tag in tags: + tag = tag.strip() + + exist = Tag.objects.filter(name = tag).exists() + + if exist: + new_tag = Tag.objects.get(name = tag) + else: + new_tag = Tag.objects.create(name = tag) + + if not new_tag in self.instance.tags.all(): + self.instance.tags.add(new_tag) + + return self.instance \ No newline at end of file diff --git a/pdf_file/migrations/0001_initial.py b/pdf_file/migrations/0001_initial.py new file mode 100644 index 0000000..5c4f14c --- /dev/null +++ b/pdf_file/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.4 on 2017-02-07 14:48 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import pdf_file.models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('topics', '0007_auto_20170123_1911'), + ] + + operations = [ + migrations.CreateModel( + name='PDFFile', + fields=[ + ('resource_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='topics.Resource')), + ('file', models.FileField(upload_to='/files', validators=[pdf_file.models.validate_file_extension], verbose_name='File')), + ], + options={ + 'verbose_name': 'PDFFile', + 'verbose_name_plural': 'PDFFiles', + }, + bases=('topics.resource',), + ), + ] diff --git a/pdf_file/models.py b/pdf_file/models.py index 71a8362..67ada69 100644 --- a/pdf_file/models.py +++ b/pdf_file/models.py @@ -1,3 +1,38 @@ from django.db import models +from django.utils.translation import ugettext_lazy as _ +from topics.models import Resource # Create your models here. +def validate_file_extension(value): + valid_formats = [ + 'application/pdf' + ] + + if hasattr(value.file, 'content_type'): + if not value.file.content_type in valid_formats: + raise ValidationError(_('File not supported, use PDF format instead.')) + +class PDFFile(Resource): + file = models.FileField(_('File'), upload_to='/files', validators = [validate_file_extension]) + class Meta: + verbose_name = "PDFFile" + verbose_name_plural = "PDFFiles" + + @property + def filename(self): + return os.path.basename(self.file.name) + + def __str__(self): + return self.name + + def access_link(self): + return 'pdf_files:download' + + def update_link(self): + return 'pdf_files:update' + + def delete_link(self): + return 'pdf_files:delete' + + def delete_message(self): + return _('Are you sure you want delete the PDF File') diff --git a/pdf_file/templates/pdf_file/_form.html b/pdf_file/templates/pdf_file/_form.html new file mode 100644 index 0000000..8e0b58c --- /dev/null +++ b/pdf_file/templates/pdf_file/_form.html @@ -0,0 +1,343 @@ +{% load static i18n %} +{% load widget_tweaks %} + +
+ + \ No newline at end of file diff --git a/pdf_file/templates/pdf_file/create.html b/pdf_file/templates/pdf_file/create.html new file mode 100644 index 0000000..03fd602 --- /dev/null +++ b/pdf_file/templates/pdf_file/create.html @@ -0,0 +1,34 @@ +{% extends 'subjects/view.html' %} + +{% load static i18n django_bootstrap_breadcrumbs %} + +{% block style %} + {{block.super}} + +{% endblock %} + +{% block javascript %} + {{block.super}} + +{% endblock %} + +{% block breadcrumbs %} + {{ block.super }} + + {% breadcrumb topic 'subjects:topic_view' topic.subject.slug topic.slug %} + + {% trans 'Create File Link' as bread %} + {% breadcrumb bread 'file_links:create' topic.slug %} +{% endblock %} + +{% block content %} +