Commit 781cb416ae8849eff166b124bc134c18d4a378b7
1 parent
ad562111
Exists in
master
and in
3 other branches
Adding file link update
Showing
8 changed files
with
157 additions
and
6 deletions
Show diff stats
amadeus/static/css/base/amadeus.css
file_link/forms.py
| @@ -65,6 +65,12 @@ class FileLinkForm(forms.ModelForm): | @@ -65,6 +65,12 @@ class FileLinkForm(forms.ModelForm): | ||
| 65 | 65 | ||
| 66 | return ValueError | 66 | return ValueError |
| 67 | 67 | ||
| 68 | + | ||
| 69 | + if not self.instance.pk: | ||
| 70 | + self._errors['file_content'] = [_('This field is required.')] | ||
| 71 | + | ||
| 72 | + return ValueError | ||
| 73 | + | ||
| 68 | return file_content | 74 | return file_content |
| 69 | 75 | ||
| 70 | def save(self, commit = True): | 76 | def save(self, commit = True): |
file_link/models.py
| 1 | +import os | ||
| 1 | from django.db import models | 2 | from django.db import models |
| 2 | from django.core.exceptions import ValidationError | 3 | from django.core.exceptions import ValidationError |
| 3 | from django.utils.translation import ugettext_lazy as _ | 4 | from django.utils.translation import ugettext_lazy as _ |
| @@ -21,12 +22,16 @@ def validate_file_extension(value): | @@ -21,12 +22,16 @@ def validate_file_extension(value): | ||
| 21 | raise ValidationError(_('File not supported.')) | 22 | raise ValidationError(_('File not supported.')) |
| 22 | 23 | ||
| 23 | class FileLink(Resource): | 24 | class FileLink(Resource): |
| 24 | - file_content = models.FileField(_('File'), upload_to = 'files/', validators = [validate_file_extension]) | 25 | + file_content = models.FileField(_('File'), blank = True, upload_to = 'files/', validators = [validate_file_extension]) |
| 25 | 26 | ||
| 26 | class Meta: | 27 | class Meta: |
| 27 | verbose_name = _('File Link') | 28 | verbose_name = _('File Link') |
| 28 | verbose_name_plural = _('File Links') | 29 | verbose_name_plural = _('File Links') |
| 29 | 30 | ||
| 31 | + @property | ||
| 32 | + def filename(self): | ||
| 33 | + return os.path.basename(self.file_content.name) | ||
| 34 | + | ||
| 30 | def __str__(self): | 35 | def __str__(self): |
| 31 | return self.name | 36 | return self.name |
| 32 | 37 | ||
| @@ -34,7 +39,7 @@ class FileLink(Resource): | @@ -34,7 +39,7 @@ class FileLink(Resource): | ||
| 34 | return 'file_links:download' | 39 | return 'file_links:download' |
| 35 | 40 | ||
| 36 | def update_link(self): | 41 | def update_link(self): |
| 37 | - return 'webpages:update' | 42 | + return 'file_links:update' |
| 38 | 43 | ||
| 39 | def delete_link(self): | 44 | def delete_link(self): |
| 40 | return 'webpages:delete' | 45 | return 'webpages:delete' |
file_link/templates/file_links/_form.html
| @@ -39,9 +39,15 @@ | @@ -39,9 +39,15 @@ | ||
| 39 | </div> | 39 | </div> |
| 40 | 40 | ||
| 41 | <div class="filedrag"> | 41 | <div class="filedrag"> |
| 42 | - {% trans 'Click or drop the file here' %}<br /> | ||
| 43 | - | ||
| 44 | - <small>{% trans 'The file could not exceed 10MB.' %}</small> | 42 | + {% if file_link %} |
| 43 | + <i class="fa fa-file-archive-o"></i> <br /> | ||
| 44 | + | ||
| 45 | + <small>{{ file_link.filename }}</small> | ||
| 46 | + {% else %} | ||
| 47 | + {% trans 'Click or drop the file here' %}<br /> | ||
| 48 | + | ||
| 49 | + <small>{% trans 'The file could not exceed 10MB.' %}</small> | ||
| 50 | + {% endif %} | ||
| 45 | </div> | 51 | </div> |
| 46 | 52 | ||
| 47 | <span id="helpBlock" class="help-block">{{ form.file_content.help_text }}</span> | 53 | <span id="helpBlock" class="help-block">{{ form.file_content.help_text }}</span> |
| @@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
| 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 'Edit: ' as bread %} | ||
| 21 | + {% with bread|add:file_link.name as bread_slug %} | ||
| 22 | + {% breadcrumb bread_slug 'file_links:update' topic.slug file_link.slug %} | ||
| 23 | + {% endwith %} | ||
| 24 | +{% endblock %} | ||
| 25 | + | ||
| 26 | +{% block content %} | ||
| 27 | + <div class="card"> | ||
| 28 | + <div class="card-content"> | ||
| 29 | + <div class="card-body"> | ||
| 30 | + {% include 'file_links/_form.html' %} | ||
| 31 | + </div> | ||
| 32 | + </div> | ||
| 33 | + </div> | ||
| 34 | + <br clear="all" /> | ||
| 35 | + <br clear="all" /> | ||
| 36 | +{% endblock %} |
file_link/urls.py
| @@ -5,5 +5,6 @@ from . import views | @@ -5,5 +5,6 @@ from . import views | ||
| 5 | 5 | ||
| 6 | urlpatterns = [ | 6 | urlpatterns = [ |
| 7 | url(r'^create/(?P<slug>[\w_-]+)/$', views.CreateView.as_view(), name = 'create'), | 7 | url(r'^create/(?P<slug>[\w_-]+)/$', views.CreateView.as_view(), name = 'create'), |
| 8 | + url(r'^update/(?P<topic_slug>[\w_-]+)/(?P<slug>[\w_-]+)/$', views.UpdateView.as_view(), name = 'update'), | ||
| 8 | url(r'^download/(?P<slug>[\w_-]+)/$', views.DownloadFile.as_view(), name = 'download'), | 9 | url(r'^download/(?P<slug>[\w_-]+)/$', views.DownloadFile.as_view(), name = 'download'), |
| 9 | ] | 10 | ] |
file_link/views.py
| @@ -145,4 +145,98 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView): | @@ -145,4 +145,98 @@ class CreateView(LoginRequiredMixin, generic.edit.CreateView): | ||
| 145 | def get_success_url(self): | 145 | def get_success_url(self): |
| 146 | messages.success(self.request, _('The File Link "%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)) | 146 | messages.success(self.request, _('The File Link "%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)) |
| 147 | 147 | ||
| 148 | + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | ||
| 149 | + | ||
| 150 | +class UpdateView(LoginRequiredMixin, generic.UpdateView): | ||
| 151 | + login_url = reverse_lazy("users:login") | ||
| 152 | + redirect_field_name = 'next' | ||
| 153 | + | ||
| 154 | + template_name = 'file_links/update.html' | ||
| 155 | + model = FileLink | ||
| 156 | + form_class = FileLinkForm | ||
| 157 | + context_object_name = 'file_link' | ||
| 158 | + | ||
| 159 | + def dispatch(self, request, *args, **kwargs): | ||
| 160 | + slug = self.kwargs.get('topic_slug', '') | ||
| 161 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 162 | + | ||
| 163 | + if not has_subject_permissions(request.user, topic.subject): | ||
| 164 | + return redirect(reverse_lazy('subjects:home')) | ||
| 165 | + | ||
| 166 | + return super(UpdateView, self).dispatch(request, *args, **kwargs) | ||
| 167 | + | ||
| 168 | + def get(self, request, *args, **kwargs): | ||
| 169 | + self.object = self.get_object() | ||
| 170 | + | ||
| 171 | + form_class = self.get_form_class() | ||
| 172 | + form = self.get_form(form_class) | ||
| 173 | + | ||
| 174 | + slug = self.kwargs.get('topic_slug', '') | ||
| 175 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 176 | + | ||
| 177 | + pend_form = self.object.pendencies_resource.all() | ||
| 178 | + | ||
| 179 | + if len(pend_form) > 0: | ||
| 180 | + pendencies_form = PendenciesForm(instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 181 | + else: | ||
| 182 | + pendencies_form = PendenciesForm(initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 183 | + | ||
| 184 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | ||
| 185 | + | ||
| 186 | + def post(self, request, *args, **kwargs): | ||
| 187 | + self.object = self.get_object() | ||
| 188 | + | ||
| 189 | + form_class = self.get_form_class() | ||
| 190 | + form = self.get_form(form_class) | ||
| 191 | + | ||
| 192 | + slug = self.kwargs.get('topic_slug', '') | ||
| 193 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 194 | + | ||
| 195 | + pend_form = self.object.pendencies_resource.all() | ||
| 196 | + | ||
| 197 | + if len(pend_form) > 0: | ||
| 198 | + pendencies_form = PendenciesForm(self.request.POST, instance = pend_form[0], initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 199 | + else: | ||
| 200 | + pendencies_form = PendenciesForm(self.request.POST, initial = {'subject': topic.subject.id, 'actions': [("", "-------"),("view", _("Visualize"))]}) | ||
| 201 | + | ||
| 202 | + if (form.is_valid() and pendencies_form.is_valid()): | ||
| 203 | + return self.form_valid(form, pendencies_form) | ||
| 204 | + else: | ||
| 205 | + return self.form_invalid(form, pendencies_form) | ||
| 206 | + | ||
| 207 | + def form_invalid(self, form, pendencies_form): | ||
| 208 | + return self.render_to_response(self.get_context_data(form = form, pendencies_form = pendencies_form)) | ||
| 209 | + | ||
| 210 | + def form_valid(self, form, pendencies_form): | ||
| 211 | + self.object = form.save(commit = False) | ||
| 212 | + | ||
| 213 | + if not self.object.topic.visible and not self.object.topic.repository: | ||
| 214 | + self.object.visible = False | ||
| 215 | + | ||
| 216 | + self.object.save() | ||
| 217 | + | ||
| 218 | + pend_form = pendencies_form.save(commit = False) | ||
| 219 | + pend_form.resource = self.object | ||
| 220 | + | ||
| 221 | + if not pend_form.action == "": | ||
| 222 | + pend_form.save() | ||
| 223 | + | ||
| 224 | + return redirect(self.get_success_url()) | ||
| 225 | + | ||
| 226 | + def get_context_data(self, **kwargs): | ||
| 227 | + context = super(UpdateView, self).get_context_data(**kwargs) | ||
| 228 | + | ||
| 229 | + context['title'] = _('Update File Link') | ||
| 230 | + | ||
| 231 | + slug = self.kwargs.get('topic_slug', '') | ||
| 232 | + topic = get_object_or_404(Topic, slug = slug) | ||
| 233 | + | ||
| 234 | + context['topic'] = topic | ||
| 235 | + context['subject'] = topic.subject | ||
| 236 | + | ||
| 237 | + return context | ||
| 238 | + | ||
| 239 | + def get_success_url(self): | ||
| 240 | + messages.success(self.request, _('The File Link "%s" was updated successfully!')%(self.object.name)) | ||
| 241 | + | ||
| 148 | return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) | 242 | return reverse_lazy('subjects:view', kwargs = {'slug': self.object.topic.subject.slug}) |
| 149 | \ No newline at end of file | 243 | \ No newline at end of file |
webpage/templates/webpages/update.html
| @@ -10,7 +10,6 @@ | @@ -10,7 +10,6 @@ | ||
| 10 | {% block javascript %} | 10 | {% block javascript %} |
| 11 | {{block.super}} | 11 | {{block.super}} |
| 12 | <script type="text/javascript" src="{% static "js/bootstrap-tagsinput.js" %} "></script> | 12 | <script type="text/javascript" src="{% static "js/bootstrap-tagsinput.js" %} "></script> |
| 13 | - <script type="text/javascript" src="{% static "js/jquery.formset.js" %} "></script> | ||
| 14 | {% endblock %} | 13 | {% endblock %} |
| 15 | 14 | ||
| 16 | {% block breadcrumbs %} | 15 | {% block breadcrumbs %} |