Commit 89ceed5d35c43a974c5b685d96bcb989122d73ce
1 parent
3acf26d5
Exists in
master
and in
5 other branches
Finished create link, solved minor issues, finished function to set the image li…
…nk and show it [Issues: #197,#198,#120]
Showing
1 changed file
with
28 additions
and
7 deletions
Show diff stats
links/views.py
... | ... | @@ -6,12 +6,16 @@ from django.utils.translation import ugettext_lazy as _ |
6 | 6 | from django.shortcuts import get_object_or_404,redirect |
7 | 7 | from django.contrib.auth.mixins import LoginRequiredMixin |
8 | 8 | from rolepermissions.mixins import HasRoleMixin |
9 | +from core.mixins import NotificationMixin | |
10 | +from django.urls import reverse | |
11 | +from django.core.files.base import ContentFile | |
12 | + | |
9 | 13 | |
14 | +from .image_crawler import * | |
10 | 15 | from courses.models import Topic |
11 | 16 | from .models import Link |
12 | 17 | from .forms import * |
13 | -from core.mixins import NotificationMixin | |
14 | -from django.urls import reverse | |
18 | + | |
15 | 19 | |
16 | 20 | # Create your views here. |
17 | 21 | class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.CreateView): |
... | ... | @@ -20,19 +24,36 @@ class CreateLink(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.Cr |
20 | 24 | form_class = CreateLinkForm |
21 | 25 | success_url = reverse_lazy('course:manage') |
22 | 26 | context_object_name = 'form' |
27 | + def form_invalid(self,form): | |
28 | + context = super(CreateLink, self).form_invalid(form) | |
29 | + context.status_code = 400 | |
30 | + | |
31 | + return context | |
23 | 32 | |
24 | 33 | def form_valid(self, form): |
25 | 34 | self.object = form.save(commit = False) |
26 | 35 | topic = get_object_or_404(Topic, slug = self.kwargs.get('slug')) |
27 | 36 | self.object.topic = topic |
28 | - messages.success(self.request, _('Link created successfully!')) | |
29 | - | |
30 | 37 | self.object.save() |
38 | + self.link = Link.objects.get(slug = self.object.slug) | |
39 | + self.formato,self.baixado = get_images(self.link.link_url,self.link.slug) | |
40 | + self.caminho = 'links/static/images/%s'%(self.link.slug)+'%s'%(self.formato) | |
41 | + | |
31 | 42 | super(CreateLink, self).createNotification(message="created a Link at "+ self.object.topic.name, actor=self.request.user, |
32 | - resource_name=self.object.name, resource_link= reverse('course:view_topic', args=[self.object.topic.slug]), | |
43 | + resource_name=self.object.name, resource_link= reverse('course:view_topic', args=[self.object.topic.slug]), | |
33 | 44 | users=self.object.topic.subject.students.all()) |
34 | - | |
45 | + self.setImage() | |
35 | 46 | return self.get_success_url() |
47 | + def setImage(self): | |
48 | + if self.baixado: | |
49 | + with open(self.caminho,'rb') as f: | |
50 | + data = f.read() | |
51 | + nome = '%s'%(self.link.slug)+"%s"%(self.formato) | |
52 | + self.link.image.save(nome,ContentFile(data)) | |
53 | + else: | |
54 | + with open('links/static/images/default.jpg','rb') as f: | |
55 | + data = f.read() | |
56 | + self.link.image.save('default.jpg',ContentFile(data)) | |
36 | 57 | def get_context_data(self,**kwargs): |
37 | 58 | context = {} |
38 | 59 | context['links'] = Link.objects.all() |
... | ... | @@ -48,7 +69,7 @@ def deleteLink(request,linkname): |
48 | 69 | link.delete() |
49 | 70 | template_name = 'links/delete_link.html' |
50 | 71 | messages.success(request,_("Link deleted Successfully!")) |
51 | - | |
72 | + | |
52 | 73 | return redirect('course:manage') |
53 | 74 | |
54 | 75 | def render_link(request, id): | ... | ... |