Commit 2d71d99f88a5282cb6813521570907dd7148ba77
1 parent
6c6548c5
Exists in
master
and in
3 other branches
updating migrations of pdf_file and create view of pdf file
Showing
10 changed files
with
681 additions
and
2 deletions
Show diff stats
amadeus/static/js/resources.js
| @@ -106,7 +106,7 @@ if (window.File && window.FileList && window.FileReader) { | @@ -106,7 +106,7 @@ if (window.File && window.FileList && window.FileReader) { | ||
| 106 | 106 | ||
| 107 | // initialize | 107 | // initialize |
| 108 | function Init() { | 108 | function Init() { |
| 109 | - var small = $("#id_file_content"), | 109 | + var small = $(".file-selector"), |
| 110 | filedrag = $(".filedrag"), | 110 | filedrag = $(".filedrag"), |
| 111 | common = $(".common-file-input"); | 111 | common = $(".common-file-input"); |
| 112 | 112 |
amadeus/urls.py
| @@ -38,6 +38,7 @@ urlpatterns = [ | @@ -38,6 +38,7 @@ urlpatterns = [ | ||
| 38 | url(r'^themes/', include('themes.urls', namespace = 'themes')), | 38 | url(r'^themes/', include('themes.urls', namespace = 'themes')), |
| 39 | url(r'^pendencies/', include('notifications.urls', namespace = 'notifications')), | 39 | url(r'^pendencies/', include('notifications.urls', namespace = 'notifications')), |
| 40 | url(r'^links/', include('links.urls', namespace='links')), | 40 | url(r'^links/', include('links.urls', namespace='links')), |
| 41 | + url(r'^pdf_files/', include('pdf_file.urls', namespace='pdf_files')), | ||
| 41 | #API | 42 | #API |
| 42 | url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')), | 43 | url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')), |
| 43 | #S3Direct | 44 | #S3Direct |
| @@ -0,0 +1,99 @@ | @@ -0,0 +1,99 @@ | ||
| 1 | +from django import forms | ||
| 2 | +from django.utils.translation import ugettext_lazy as _ | ||
| 3 | +from django.utils.html import strip_tags | ||
| 4 | + | ||
| 5 | +from subjects.models import Tag | ||
| 6 | + | ||
| 7 | +from .models import PDFFile | ||
| 8 | + | ||
| 9 | +class PDFFileForm(forms.ModelForm): | ||
| 10 | + subject = None | ||
| 11 | + MAX_UPLOAD_SIZE = 10*1024*1024 | ||
| 12 | + | ||
| 13 | + def __init__(self, *args, **kwargs): | ||
| 14 | + super(PDFFileForm, self).__init__(*args, **kwargs) | ||
| 15 | + self.subject = kwargs.get('initial').get('subject', None) | ||
| 16 | + | ||
| 17 | + if self.instance.id: | ||
| 18 | + self.subject = self.instance.topic.subject | ||
| 19 | + self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) | ||
| 20 | + | ||
| 21 | + self.fields['students'].queryset = self.subject.students.all() | ||
| 22 | + self.fields['groups'].queryset = self.subject.group_subject.all() | ||
| 23 | + | ||
| 24 | + tags = forms.CharField(label = _('Tags'), required = False) | ||
| 25 | + class Meta: | ||
| 26 | + model = PDFFile | ||
| 27 | + fields = ['name', 'file', 'brief_description', 'all_students', 'students', 'groups', 'visible'] | ||
| 28 | + labels = { | ||
| 29 | + 'name': _('File name'), | ||
| 30 | + } | ||
| 31 | + widgets = { | ||
| 32 | + 'brief_description': forms.Textarea, | ||
| 33 | + 'students': forms.SelectMultiple, | ||
| 34 | + 'groups': forms.SelectMultiple, | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + def clean_name(self): | ||
| 38 | + name = self.cleaned_data.get('name', '') | ||
| 39 | + | ||
| 40 | + topics = self.subject.topic_subject.all() | ||
| 41 | + | ||
| 42 | + for topic in topics: | ||
| 43 | + if self.instance.id: | ||
| 44 | + same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() | ||
| 45 | + else: | ||
| 46 | + same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() | ||
| 47 | + | ||
| 48 | + if same_name > 0: | ||
| 49 | + self._errors['name'] = [_('This subject already has a file link with this name')] | ||
| 50 | + | ||
| 51 | + return ValueError | ||
| 52 | + | ||
| 53 | + return name | ||
| 54 | + | ||
| 55 | + def clean_file(self): | ||
| 56 | + file = self.cleaned_data.get('file', False) | ||
| 57 | + | ||
| 58 | + if file: | ||
| 59 | + if hasattr(file, '_size'): | ||
| 60 | + if file._size > self.MAX_UPLOAD_SIZE: | ||
| 61 | + self._errors['file'] = [_("The file is too large. It should have less than 10MB.")] | ||
| 62 | + | ||
| 63 | + return ValueError | ||
| 64 | + | ||
| 65 | + elif not self.instance.pk: | ||
| 66 | + self._errors['file'] = [_('This field is required.')] | ||
| 67 | + | ||
| 68 | + return ValueError | ||
| 69 | + | ||
| 70 | + return file | ||
| 71 | + | ||
| 72 | + def save(self, commit = True): | ||
| 73 | + super(PDFFileForm, self).save(commit = True) | ||
| 74 | + | ||
| 75 | + self.instance.save() | ||
| 76 | + | ||
| 77 | + previous_tags = self.instance.tags.all() | ||
| 78 | + | ||
| 79 | + tags = self.cleaned_data['tags'].split(",") | ||
| 80 | + | ||
| 81 | + #Excluding unwanted tags | ||
| 82 | + for prev in previous_tags: | ||
| 83 | + if not prev.name in tags: | ||
| 84 | + self.instance.tags.remove(prev) | ||
| 85 | + | ||
| 86 | + for tag in tags: | ||
| 87 | + tag = tag.strip() | ||
| 88 | + | ||
| 89 | + exist = Tag.objects.filter(name = tag).exists() | ||
| 90 | + | ||
| 91 | + if exist: | ||
| 92 | + new_tag = Tag.objects.get(name = tag) | ||
| 93 | + else: | ||
| 94 | + new_tag = Tag.objects.create(name = tag) | ||
| 95 | + | ||
| 96 | + if not new_tag in self.instance.tags.all(): | ||
| 97 | + self.instance.tags.add(new_tag) | ||
| 98 | + | ||
| 99 | + return self.instance | ||
| 0 | \ No newline at end of file | 100 | \ No newline at end of file |
| @@ -0,0 +1,31 @@ | @@ -0,0 +1,31 @@ | ||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +# Generated by Django 1.10.4 on 2017-02-07 14:48 | ||
| 3 | +from __future__ import unicode_literals | ||
| 4 | + | ||
| 5 | +from django.db import migrations, models | ||
| 6 | +import django.db.models.deletion | ||
| 7 | +import pdf_file.models | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +class Migration(migrations.Migration): | ||
| 11 | + | ||
| 12 | + initial = True | ||
| 13 | + | ||
| 14 | + dependencies = [ | ||
| 15 | + ('topics', '0007_auto_20170123_1911'), | ||
| 16 | + ] | ||
| 17 | + | ||
| 18 | + operations = [ | ||
| 19 | + migrations.CreateModel( | ||
| 20 | + name='PDFFile', | ||
| 21 | + fields=[ | ||
| 22 | + ('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')), | ||
| 23 | + ('file', models.FileField(upload_to='/files', validators=[pdf_file.models.validate_file_extension], verbose_name='File')), | ||
| 24 | + ], | ||
| 25 | + options={ | ||
| 26 | + 'verbose_name': 'PDFFile', | ||
| 27 | + 'verbose_name_plural': 'PDFFiles', | ||
| 28 | + }, | ||
| 29 | + bases=('topics.resource',), | ||
| 30 | + ), | ||
| 31 | + ] |
pdf_file/models.py
| 1 | from django.db import models | 1 | from django.db import models |
| 2 | +from django.utils.translation import ugettext_lazy as _ | ||
| 2 | 3 | ||
| 4 | +from topics.models import Resource | ||
| 3 | # Create your models here. | 5 | # Create your models here. |
| 6 | +def validate_file_extension(value): | ||
| 7 | + valid_formats = [ | ||
| 8 | + 'application/pdf' | ||
| 9 | + ] | ||
| 10 | + | ||
| 11 | + if hasattr(value.file, 'content_type'): | ||
| 12 | + if not value.file.content_type in valid_formats: | ||
| 13 | + raise ValidationError(_('File not supported, use PDF format instead.')) | ||
| 14 | + | ||
| 15 | +class PDFFile(Resource): | ||
| 16 | + file = models.FileField(_('File'), upload_to='/files', validators = [validate_file_extension]) | ||
| 17 | + class Meta: | ||
| 18 | + verbose_name = "PDFFile" | ||
| 19 | + verbose_name_plural = "PDFFiles" | ||
| 20 | + | ||
| 21 | + @property | ||
| 22 | + def filename(self): | ||
| 23 | + return os.path.basename(self.file.name) | ||
| 24 | + | ||
| 25 | + def __str__(self): | ||
| 26 | + return self.name | ||
| 27 | + | ||
| 28 | + def access_link(self): | ||
| 29 | + return 'pdf_files:download' | ||
| 30 | + | ||
| 31 | + def update_link(self): | ||
| 32 | + return 'pdf_files:update' | ||
| 33 | + | ||
| 34 | + def delete_link(self): | ||
| 35 | + return 'pdf_files:delete' | ||
| 36 | + | ||
| 37 | + def delete_message(self): | ||
| 38 | + return _('Are you sure you want delete the PDF File') |
| @@ -0,0 +1,343 @@ | @@ -0,0 +1,343 @@ | ||
| 1 | +{% load static i18n %} | ||
| 2 | +{% load widget_tweaks %} | ||
| 3 | + | ||
| 4 | +<form method="post" action="" enctype="multipart/form-data"> | ||
| 5 | + {% csrf_token %} | ||
| 6 | + | ||
| 7 | + {% render_field form.control_subject %} | ||
| 8 | + | ||
| 9 | + <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> | ||
| 10 | + <label for="{{ form.name.auto_id }}">{{ form.name.label }} <span>*</span></label> | ||
| 11 | + {% render_field form.name class='form-control' %} | ||
| 12 | + | ||
| 13 | + <span id="helpBlock" class="help-block">{{ form.name.help_text }}</span> | ||
| 14 | + | ||
| 15 | + {% if form.name.errors %} | ||
| 16 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 17 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 18 | + <span aria-hidden="true">×</span> | ||
| 19 | + </button> | ||
| 20 | + <ul> | ||
| 21 | + {% for error in form.name.errors %} | ||
| 22 | + <li>{{ error }}</li> | ||
| 23 | + {% endfor %} | ||
| 24 | + </ul> | ||
| 25 | + </div> | ||
| 26 | + {% endif %} | ||
| 27 | + </div> | ||
| 28 | +<div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> | ||
| 29 | + {% render_field form.file class='file-selector' %} | ||
| 30 | + | ||
| 31 | + <div class="input-group common-file-input"> | ||
| 32 | + <input type="text" readonly="" class="form-control" placeholder="{% trans 'Choose your file...' %}"> | ||
| 33 | + <span class="input-group-btn input-group-sm"> | ||
| 34 | + <button type="button" class="btn btn-fab btn-fab-mini"> | ||
| 35 | + <i class="material-icons">attach_file</i> | ||
| 36 | + </button> | ||
| 37 | + </span> | ||
| 38 | + </div> | ||
| 39 | + | ||
| 40 | + <div class="filedrag"> | ||
| 41 | + {% if pdf_file %} | ||
| 42 | + <i class="fa fa-file-archive-o"></i> <br /> | ||
| 43 | + | ||
| 44 | + <small>{{ pdf_file.filename }}</small> | ||
| 45 | + {% else %} | ||
| 46 | + {% trans 'Click or drop the file here' %}<br /> | ||
| 47 | + | ||
| 48 | + <small>{% trans 'The file could not exceed 10MB.' %}</small> | ||
| 49 | + {% endif %} | ||
| 50 | + </div> | ||
| 51 | + | ||
| 52 | + <span id="helpBlock" class="help-block">{{ form.file.help_text }}</span> | ||
| 53 | + | ||
| 54 | + {% if form.file.errors %} | ||
| 55 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 56 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 57 | + <span aria-hidden="true">×</span> | ||
| 58 | + </button> | ||
| 59 | + <ul> | ||
| 60 | + {% for error in form.file.errors %} | ||
| 61 | + <li>{{ error }}</li> | ||
| 62 | + {% endfor %} | ||
| 63 | + </ul> | ||
| 64 | + </div> | ||
| 65 | + {% endif %} | ||
| 66 | + </div> | ||
| 67 | + | ||
| 68 | + <legend>{% trans 'Common resources settings' %}</legend> | ||
| 69 | + | ||
| 70 | + <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> | ||
| 71 | + <label for="{{ form.brief_description.auto_id }}">{{ form.brief_description.label }}</label> | ||
| 72 | + {% render_field form.brief_description class='form-control text_wysiwyg' %} | ||
| 73 | + | ||
| 74 | + <span id="helpBlock" class="help-block">{{ form.brief_description.help_text }}</span> | ||
| 75 | + | ||
| 76 | + {% if form.brief_description.errors %} | ||
| 77 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 78 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 79 | + <span aria-hidden="true">×</span> | ||
| 80 | + </button> | ||
| 81 | + <ul> | ||
| 82 | + {% for error in form.brief_description.errors %} | ||
| 83 | + <li>{{ error }}</li> | ||
| 84 | + {% endfor %} | ||
| 85 | + </ul> | ||
| 86 | + </div> | ||
| 87 | + {% endif %} | ||
| 88 | + </div> | ||
| 89 | + | ||
| 90 | + <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> | ||
| 91 | + <label for="{{ form.tags.auto_id }}">{{ form.tags.label }}</label> | ||
| 92 | + {% render_field form.tags class='form-control' data-role="tagsinput" %} | ||
| 93 | + | ||
| 94 | + <span id="helpBlock" class="help-block">{{ form.tags.help_text }}</span> | ||
| 95 | + | ||
| 96 | + {% if form.tags.errors %} | ||
| 97 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 98 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 99 | + <span aria-hidden="true">×</span> | ||
| 100 | + </button> | ||
| 101 | + <ul> | ||
| 102 | + {% for error in form.tags.errors %} | ||
| 103 | + <li>{{ error }}</li> | ||
| 104 | + {% endfor %} | ||
| 105 | + </ul> | ||
| 106 | + </div> | ||
| 107 | + {% endif %} | ||
| 108 | + </div> | ||
| 109 | + | ||
| 110 | + <div class="panel-group" id="professors_accordion" role="tablist" aria-multiselectable="true"> | ||
| 111 | + <div class="panel panel-info"> | ||
| 112 | + <div class="panel-heading"> | ||
| 113 | + <div class="row"> | ||
| 114 | + <div class="col-md-12"> | ||
| 115 | + <a data-parent="#professors_accordion" data-toggle="collapse" href="#notifications"> | ||
| 116 | + <h4 class="panel-title"> | ||
| 117 | + <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button><label>{% trans 'Pendencies Notifications' %}</label> | ||
| 118 | + </h4> | ||
| 119 | + </a> | ||
| 120 | + </div> | ||
| 121 | + </div> | ||
| 122 | + </div> | ||
| 123 | + <div id="notifications" class="panel-collapse collapse"> | ||
| 124 | + | ||
| 125 | + <div class="notifies"> | ||
| 126 | + <div style="text-align:left"> | ||
| 127 | + {% render_field pendencies_form.id %} | ||
| 128 | + {% render_field pendencies_form.resource %} | ||
| 129 | + {% render_field pendencies_form.subject class='pend_subj' %} | ||
| 130 | + | ||
| 131 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | ||
| 132 | + <label for="{{ pendencies_form.action.auto_id }}" class="pull-left action_label contol-label"> | ||
| 133 | + {% trans 'Action not performed by the user' %}: | ||
| 134 | + </label> | ||
| 135 | + <div class="col-md-3"> | ||
| 136 | + {% render_field pendencies_form.action class='form-control' %} | ||
| 137 | + </div> | ||
| 138 | + | ||
| 139 | + <br clear="all" /> | ||
| 140 | + | ||
| 141 | + <span id="helpBlock" class="help-block">{{ pendencies_form.action.help_text }}</span> | ||
| 142 | + | ||
| 143 | + {% if pendencies_form.action.errors %} | ||
| 144 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 145 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 146 | + <span aria-hidden="true">×</span> | ||
| 147 | + </button> | ||
| 148 | + <ul> | ||
| 149 | + {% for error in pendencies_form.action.errors %} | ||
| 150 | + <li>{{ error }}</li> | ||
| 151 | + {% endfor %} | ||
| 152 | + </ul> | ||
| 153 | + </div> | ||
| 154 | + {% endif %} | ||
| 155 | + </div> | ||
| 156 | + <br clear="all" /> | ||
| 157 | + <div class="row"> | ||
| 158 | + <div class="col-md-12"> | ||
| 159 | + <p>{% trans 'Wished period' %}: </p> | ||
| 160 | + </div> | ||
| 161 | + </div> | ||
| 162 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | ||
| 163 | + <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | ||
| 164 | + <label> | ||
| 165 | + {% render_field pendencies_form.begin_date_check class="begin_date" %} {{ pendencies_form.begin_date.label }} | ||
| 166 | + </label> | ||
| 167 | + </div> | ||
| 168 | + <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | ||
| 169 | + {% render_field pendencies_form.begin_date class='form-control datetime-picker begin_date_input' %} | ||
| 170 | + </div> | ||
| 171 | + </div> | ||
| 172 | + <div class="row"> | ||
| 173 | + <span id="helpBlock" class="help-block">{{ pendencies_form.begin_date.help_text }}</span> | ||
| 174 | + | ||
| 175 | + {% if pendencies_form.begin_date.errors %} | ||
| 176 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 177 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 178 | + <span aria-hidden="true">×</span> | ||
| 179 | + </button> | ||
| 180 | + <ul> | ||
| 181 | + {% for error in pendencies_form.begin_date.errors %} | ||
| 182 | + <li>{{ error }}</li> | ||
| 183 | + {% endfor %} | ||
| 184 | + </ul> | ||
| 185 | + </div> | ||
| 186 | + {% endif %} | ||
| 187 | + </div> | ||
| 188 | + <div class="form-group{% if pendencies_form.has_error %} has-error {% endif %} row"> | ||
| 189 | + <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 checkbox"> | ||
| 190 | + <label> | ||
| 191 | + {% render_field pendencies_form.end_date_check class="end_date" %} {{ pendencies_form.end_date.label }} | ||
| 192 | + </label> | ||
| 193 | + </div> | ||
| 194 | + <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4"> | ||
| 195 | + {% render_field pendencies_form.end_date class='form-control datetime-picker end_date_input' %} | ||
| 196 | + </div> | ||
| 197 | + </div> | ||
| 198 | + <div class="row"> | ||
| 199 | + <span id="helpBlock" class="help-block">{{ pendencies_form.end_date.help_text }}</span> | ||
| 200 | + | ||
| 201 | + {% if pendencies_form.end_date.errors %} | ||
| 202 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 203 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 204 | + <span aria-hidden="true">×</span> | ||
| 205 | + </button> | ||
| 206 | + <ul> | ||
| 207 | + {% for error in pendencies_form.end_date.errors %} | ||
| 208 | + <li>{{ error }}</li> | ||
| 209 | + {% endfor %} | ||
| 210 | + </ul> | ||
| 211 | + </div> | ||
| 212 | + {% endif %} | ||
| 213 | + </div> | ||
| 214 | + </div> | ||
| 215 | + </div> | ||
| 216 | + </div> | ||
| 217 | + </div> | ||
| 218 | + | ||
| 219 | + <div class="panel panel-info"> | ||
| 220 | + <div class="panel-heading"> | ||
| 221 | + <div class="row"> | ||
| 222 | + <div class="col-md-12"> | ||
| 223 | + <a data-parent="#professors_accordion" data-toggle="collapse" href="#students"> | ||
| 224 | + <h4 class="panel-title"> | ||
| 225 | + <button class="btn btn-default btn-xs text-center cat-selector"><i class="fa fa-angle-right fa-2x" aria-hidden="true"></i></button><label for="{{ form.students.auto_id }}">{{ form.students.label }}</label> | ||
| 226 | + </h4> | ||
| 227 | + </a> | ||
| 228 | + </div> | ||
| 229 | + </div> | ||
| 230 | + </div> | ||
| 231 | + <div id="students" class="panel-collapse collapse"> | ||
| 232 | + <div class="form-group{% if form.has_error %} has-error {% endif %}"> | ||
| 233 | + <div class=" checkbox"> | ||
| 234 | + <label for="{{ form.all_students.auto_id }}"> | ||
| 235 | + {% render_field form.all_students %} {{ form.all_students.label }} | ||
| 236 | + </label> | ||
| 237 | + </div> | ||
| 238 | + | ||
| 239 | + <span id="helpBlock" class="help-block">{{ form.all_students.help_text }}</span> | ||
| 240 | + | ||
| 241 | + {% if form.all_students.errors %} | ||
| 242 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 243 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 244 | + <span aria-hidden="true">×</span> | ||
| 245 | + </button> | ||
| 246 | + <ul> | ||
| 247 | + {% for error in form.all_students.errors %} | ||
| 248 | + <li>{{ error }}</li> | ||
| 249 | + {% endfor %} | ||
| 250 | + </ul> | ||
| 251 | + </div> | ||
| 252 | + {% endif %} | ||
| 253 | + </div> | ||
| 254 | + | ||
| 255 | + <p><em>{% trans 'Attribute students to file link' %}:</em></p> | ||
| 256 | + {% render_field form.students class='form-control' %} | ||
| 257 | + | ||
| 258 | + <span id="helpBlock" class="help-block">{{ form.students.help_text }}</span> | ||
| 259 | + | ||
| 260 | + {% if form.students.errors %} | ||
| 261 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 262 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 263 | + <span aria-hidden="true">×</span> | ||
| 264 | + </button> | ||
| 265 | + <ul> | ||
| 266 | + {% for error in form.students.errors %} | ||
| 267 | + <li>{{ error }}</li> | ||
| 268 | + {% endfor %} | ||
| 269 | + </ul> | ||
| 270 | + </div> | ||
| 271 | + {% endif %} | ||
| 272 | + | ||
| 273 | + <br clear="all" /> | ||
| 274 | + | ||
| 275 | + <p><em>{% trans 'Attribute groups to file link' %}:</em></p> | ||
| 276 | + {% render_field form.groups class='form-control' %} | ||
| 277 | + | ||
| 278 | + <span id="helpBlock" class="help-block">{{ form.groups.help_text }}</span> | ||
| 279 | + | ||
| 280 | + {% if form.groups.errors %} | ||
| 281 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 282 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 283 | + <span aria-hidden="true">×</span> | ||
| 284 | + </button> | ||
| 285 | + <ul> | ||
| 286 | + {% for error in form.groups.errors %} | ||
| 287 | + <li>{{ error }}</li> | ||
| 288 | + {% endfor %} | ||
| 289 | + </ul> | ||
| 290 | + </div> | ||
| 291 | + {% endif %} | ||
| 292 | + </div> | ||
| 293 | + </div> | ||
| 294 | + </div> | ||
| 295 | + | ||
| 296 | + <div class="form-group{% if form.has_error %} has-error {% endif %}"> | ||
| 297 | + <div class=" checkbox"> | ||
| 298 | + <label for="{{ form.visible.auto_id }}"> | ||
| 299 | + {% render_field form.visible %} {{ form.visible.label }} | ||
| 300 | + </label> | ||
| 301 | + </div> | ||
| 302 | + | ||
| 303 | + <span id="helpBlock" class="help-block">{{ form.visible.help_text }}</span> | ||
| 304 | + | ||
| 305 | + {% if form.visible.errors %} | ||
| 306 | + <div class="alert alert-danger alert-dismissible" role="alert"> | ||
| 307 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
| 308 | + <span aria-hidden="true">×</span> | ||
| 309 | + </button> | ||
| 310 | + <ul> | ||
| 311 | + {% for error in form.visible.errors %} | ||
| 312 | + <li>{{ error }}</li> | ||
| 313 | + {% endfor %} | ||
| 314 | + </ul> | ||
| 315 | + </div> | ||
| 316 | + {% endif %} | ||
| 317 | + </div> | ||
| 318 | + | ||
| 319 | + <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12"> | ||
| 320 | + <div class="text-center"> | ||
| 321 | + <input type="submit" value="{% trans 'Save' %}" class="btn btn-raised btn-success" /> | ||
| 322 | + </div> | ||
| 323 | + </div> | ||
| 324 | +</form> | ||
| 325 | +<script type="text/javascript"> | ||
| 326 | + $(function() { | ||
| 327 | + var begin_val = $('.begin_date_input').val(), | ||
| 328 | + end_val = $('.end_date_input').val(); | ||
| 329 | + | ||
| 330 | + if (begin_val != '') { | ||
| 331 | + $(".begin_date").prop('checked', true); | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + if (end_val != '') { | ||
| 335 | + $(".end_date").prop('checked', true); | ||
| 336 | + } | ||
| 337 | + | ||
| 338 | + {% if not pendencies_form.is_valid and pendencies_form.is_bound %} | ||
| 339 | + $("#notifications").collapse('toggle'); | ||
| 340 | + {% endif %} | ||
| 341 | + }); | ||
| 342 | +</script> | ||
| 343 | +<script type="text/javascript" src="{% static 'js/resources.js' %}"></script> | ||
| 0 | \ No newline at end of file | 344 | \ No newline at end of file |
| @@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
| 1 | +{% extends 'subjects/view.html' %} | ||
| 2 | + | ||
| 3 | +{% load static i18n django_bootstrap_breadcrumbs %} | ||
| 4 | + | ||
| 5 | +{% block style %} | ||
| 6 | + {{block.super}} | ||
| 7 | + <link rel="stylesheet" type="text/css" href="{% static "css/bootstrap-tagsinput.css" %}"> | ||
| 8 | +{% endblock %} | ||
| 9 | + | ||
| 10 | +{% block javascript %} | ||
| 11 | + {{block.super}} | ||
| 12 | + <script type="text/javascript" src="{% static "js/bootstrap-tagsinput.js" %} "></script> | ||
| 13 | +{% endblock %} | ||
| 14 | + | ||
| 15 | +{% block breadcrumbs %} | ||
| 16 | + {{ block.super }} | ||
| 17 | + | ||
| 18 | + {% breadcrumb topic 'subjects:topic_view' topic.subject.slug topic.slug %} | ||
| 19 | + | ||
| 20 | + {% trans 'Create File Link' as bread %} | ||
| 21 | + {% breadcrumb bread 'file_links:create' topic.slug %} | ||
| 22 | +{% endblock %} | ||
| 23 | + | ||
| 24 | +{% block content %} | ||
| 25 | + <div class="card"> | ||
| 26 | + <div class="card-content"> | ||
| 27 | + <div class="card-body"> | ||
| 28 | + {% include 'file_links/_form.html' %} | ||
| 29 | + </div> | ||
| 30 | + </div> | ||
| 31 | + </div> | ||
| 32 | + <br clear="all" /> | ||
| 33 | + <br clear="all" /> | ||
| 34 | +{% endblock %} |
| @@ -0,0 +1,8 @@ | @@ -0,0 +1,8 @@ | ||
| 1 | +from django.conf.urls import url | ||
| 2 | +from django.contrib.auth import views as auth_views | ||
| 3 | + | ||
| 4 | +from . import views | ||
| 5 | + | ||
| 6 | +urlpatterns = [ | ||
| 7 | + url(r'^create/(?P<slug>[\w_-]+)/$', views.PDFFileCreateView.as_view(), name='create'), | ||
| 8 | +] | ||
| 0 | \ No newline at end of file | 9 | \ No newline at end of file |
pdf_file/views.py
| 1 | -from django.shortcuts import render | ||
| 2 | 1 | ||
| 3 | # Create your views here. | 2 | # Create your views here. |
| 3 | +from django.views import generic | ||
| 4 | +from django.contrib.auth.mixins import LoginRequiredMixin | ||
| 5 | +from django.utils.translation import ugettext_lazy as _ | ||
| 6 | +from django.core.urlresolvers import reverse, reverse_lazy | ||
| 7 | +from django.shortcuts import get_object_or_404, redirect, render | ||
| 8 | + | ||
| 9 | +from amadeus.permissions import has_subject_permissions, has_resource_permissions | ||
| 10 | +from .forms import PDFFileForm | ||
| 11 | + | ||
| 12 | +from log.mixins import LogMixin | ||
| 13 | +from topics.models import Topic | ||
| 14 | + | ||
| 15 | +from pendencies.forms import PendenciesForm | ||
| 16 | + | ||
| 17 | + | ||
| 18 | +class PDFFileCreateView(LoginRequiredMixin, LogMixin , generic.CreateView): | ||
| 19 | + form_class = PDFFileForm | ||
| 20 | + template_name = 'pdf_file/create.html' | ||
| 21 | + | ||
| 22 | + login_url = reverse_lazy("users:login") | ||
| 23 | + redirect_field_name = 'next' | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + def dispatch(self, request, *args, **kwargs): | ||
| 27 | + slug = self.kwargs.get('slug', '') | ||
| 28 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 29 | + | ||
| 30 | + if not has_subject_permissions(request.user, topic.subject): | ||
| 31 | + return redirect(reverse_lazy('subjects:home')) | ||
| 32 | + | ||
| 33 | + return super(PDFFileCreateView, self).dispatch(request, *args, **kwargs) | ||
| 34 | + | ||
| 35 | + def get(self, request, *args, **kwargs): | ||
| 36 | + self.object = None | ||
| 37 | + | ||
| 38 | + form_class = self.get_form_class() | ||
| 39 | + form = self.get_form(form_class) | ||
| 40 | + | ||
| 41 | + slug = self.kwargs.get('slug', '') | ||
| 42 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 43 | + | ||
| 44 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 45 | + | ||
| 46 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | ||
| 47 | + | ||
| 48 | + def post(self, request, *args, **kwargs): | ||
| 49 | + self.object = None | ||
| 50 | + | ||
| 51 | + form_class = self.get_form_class() | ||
| 52 | + form = self.get_form(form_class) | ||
| 53 | + | ||
| 54 | + slug = self.kwargs.get('slug', '') | ||
| 55 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 56 | + | ||
| 57 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 58 | + | ||
| 59 | + if (form.is_valid() and pendencies_form.is_valid()): | ||
| 60 | + return self.form_valid(form, pendencies_form) | ||
| 61 | + else: | ||
| 62 | + return self.form_invalid(form, pendencies_form) | ||
| 63 | + | ||
| 64 | + def get_initial(self): | ||
| 65 | + initial = super(PDFFileCreateView, self).get_initial() | ||
| 66 | + | ||
| 67 | + slug = self.kwargs.get('slug', '') | ||
| 68 | + | ||
| 69 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 70 | + initial['subject'] = topic.subject | ||
| 71 | + | ||
| 72 | + return initial | ||
| 73 | + | ||
| 74 | + def form_invalid(self, form, pendencies_form): | ||
| 75 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | ||
| 76 | + | ||
| 77 | + def form_valid(self, form, pendencies_form): | ||
| 78 | + self.object = form.save(commit = False) | ||
| 79 | + | ||
| 80 | + slug = self.kwargs.get('slug', '') | ||
| 81 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 82 | + | ||
| 83 | + self.object.topic = topic | ||
| 84 | + self.object.order = topic.resource_topic.count() + 1 | ||
| 85 | + | ||
| 86 | + if not self.object.topic.visible and not self.object.topic.repository: | ||
| 87 | + self.object.visible = False | ||
| 88 | + | ||
| 89 | + self.object.save() | ||
| 90 | + | ||
| 91 | + pend_form = pendencies_form.save(commit = False) | ||
| 92 | + pend_form.resource = self.object | ||
| 93 | + | ||
| 94 | + if not pend_form.action == "": | ||
| 95 | + pend_form.save() | ||
| 96 | + | ||
| 97 | + self.log_context['category_id'] = self.object.topic.subject.category.id | ||
| 98 | + self.log_context['category_name'] = self.object.topic.subject.category.name | ||
| 99 | + self.log_context['category_slug'] = self.object.topic.subject.category.slug | ||
| 100 | + self.log_context['subject_id'] = self.object.topic.subject.id | ||
| 101 | + self.log_context['subject_name'] = self.object.topic.subject.name | ||
| 102 | + self.log_context['subject_slug'] = self.object.topic.subject.slug | ||
| 103 | + self.log_context['topic_id'] = self.object.topic.id | ||
| 104 | + self.log_context['topic_name'] = self.object.topic.name | ||
| 105 | + self.log_context['topic_slug'] = self.object.topic.slug | ||
| 106 | + self.log_context['pdf_file_id'] = self.object.id | ||
| 107 | + self.log_context['pdf_file_name'] = self.object.name | ||
| 108 | + self.log_context['pdf_file_slug'] = self.object.slug | ||
| 109 | + | ||
| 110 | + super(PDFFileCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | ||
| 111 | + | ||
| 112 | + return redirect(self.get_success_url()) | ||
| 113 | + | ||
| 114 | + def get_context_data(self, **kwargs): | ||
| 115 | + context = super(PDFFileCreateView, self).get_context_data(**kwargs) | ||
| 116 | + | ||
| 117 | + context['title'] = _('Create PDF File') | ||
| 118 | + | ||
| 119 | + slug = self.kwargs.get('slug', '') | ||
| 120 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 121 | + | ||
| 122 | + context['topic'] = topic | ||
| 123 | + context['subject'] = topic.subject | ||
| 124 | + | ||
| 125 | + return context | ||
| 126 | + | ||
| 127 | + def get_success_url(self): | ||
| 128 | + messages.success(self.request, _('The PDF File "%s" was added to the Topic "%s" of the virtual environment "%s" successfully!')%(self.object.name, self.object.topic.name, self.object.topic.subject.name)) | ||
| 129 | + | ||
| 130 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | ||
| 4 | \ No newline at end of file | 131 | \ No newline at end of file |
topics/templates/topics/list.html
| @@ -58,6 +58,7 @@ | @@ -58,6 +58,7 @@ | ||
| 58 | <li><a href="{% url 'links:create' topic.slug %}" > <i class="fa fa-globe"></i> {% trans "Link to Website" %}</a> | 58 | <li><a href="{% url 'links:create' topic.slug %}" > <i class="fa fa-globe"></i> {% trans "Link to Website" %}</a> |
| 59 | <li><a href="{% url 'webpages:create' topic.slug %}"><i class="fa fa-file-code-o"></i> {% trans 'Webpage' %}</a></li> | 59 | <li><a href="{% url 'webpages:create' topic.slug %}"><i class="fa fa-file-code-o"></i> {% trans 'Webpage' %}</a></li> |
| 60 | <li><a href="#"><i class="fa fa-question-circle-o"></i> {% trans 'Questionary' %}</a></li> | 60 | <li><a href="#"><i class="fa fa-question-circle-o"></i> {% trans 'Questionary' %}</a></li> |
| 61 | + <li><a href="{% url 'pdf_files:create' topic.slug %}"><i class="fa fa-file-pdf-o"></i> {% trans "PDF File" %}</a></li> | ||
| 61 | </ul> | 62 | </ul> |
| 62 | </div> | 63 | </div> |
| 63 | </div> | 64 | </div> |