def get_story_file(request, file_id): try: file = ArticleAttachments.objects.get(pk=file_id) story = file.article allowed_objects = get_allowed_objects( request.user, ArticleCategory, 'reader') al = story.categories.filter(pk__in=allowed_objects) if len(al) == 0: return HttpResponseForbidden() except: return HttpResponseForbidden() path = "%s/%s" % (get_upload_articles_files(), file.attached_file.name) fp = open(path, 'rb') content_type = file.mimetype response = HttpResponse(fp.read(), content_type=content_type) response['Content-Length'] = os.path.getsize(path) response['Content-Disposition'] = "attachment; filename=%s" % os.path.basename( file.attached_file.name) return response
def edit_story(request, story): allowed_objects = get_allowed_objects( request.user, ArticleCategory, 'manager') try: story = ArticleStory.objects.select_related().get(pk=story) categories = story.categories.all() intersect = set(allowed_objects).intersection( set([category.pk for category in categories])) if story.owner != request.user and not intersect: return HttpResponseRedirect('/') except Exception as e: print e return HttpResponseRedirect('/') attachments = story.articleattachments_set.all() if request.method == 'POST': if 'bdelstory' in request.POST: story.articleattachments_set.all().delete() pk = story.pk story.delete() try: shutil.rmtree(u"%s/%s" % (get_upload_articles_images(), pk)) except: pass try: shutil.rmtree(u"%s/%s" % (get_upload_articles_files(), pk)) except: pass return HttpResponseRedirect(reverse('writer_stories_list', args=[])) form_story = ArticleStoryForm( request.POST, request.FILES, instance=story, request=request) if form_story.is_valid(): if 'selected_image' in request.POST: story.image.delete() try: shutil.rmtree(u"%s/%s" % (get_upload_articles_images(), story.pk)) except: pass if 'image' in request.FILES: story.mimetype = request.FILES['image'].content_type else: image_file = None story.modifier = request.user story.save() story.categories.clear() selected_categories = request.POST.getlist('_selected_action') story.categories = selected_categories form_story = ArticleStoryForm(instance=story, request=request) if len(attachments) < 2: fa_set = formset_factory( ArticleAttachmentsForm, extra=2 - len(attachments)) form_attachments_set = fa_set(request.POST, request.FILES) if form_attachments_set.is_valid(): for i in range(0, form_attachments_set.total_form_count()): form = form_attachments_set.forms[i] try: file = request.FILES['form-' + str( i) + '-attached_file'] attachment = ArticleAttachments(description=form.cleaned_data[ 'description'], mimetype=file.content_type) attachment.article = story attachment.save() attachment.attached_file.save(file.name, file) except: pass if 'selected_attachments' in request.POST: att = request.POST.getlist('selected_attachments') ArticleAttachments.objects.filter(pk__in=att).delete() attachments = story.articleattachments_set.all() extra = len(attachments) if extra < 2: fa_set = formset_factory(ArticleAttachmentsForm, extra=2 - extra) form_attachments_set = fa_set() else: form_attachments_set = None tags = map(int, request.POST.getlist('article_tags[]')) story.tags.clear() tags = ArticleTags.objects.filter(pk__in=tags) story.tags.add(*tags) if story.categories: notify_users(request, story, story.categories.all(), False) else: form_story = ArticleStoryForm(instance=story, request=request) extra = len(attachments) if extra < 2: fa_set = formset_factory(ArticleAttachmentsForm, extra=2 - extra) form_attachments_set = fa_set() else: form_attachments_set = None tags = ArticleTags.objects.all() story_tags = story.tags.all() for tag in tags: if tag in story_tags: tag.checked = True context = RequestContext(request) extra_context = {'form_story': form_story, 'story': story, 'tags': tags, 'form_attachments': form_attachments_set, 'attachments': attachments, 'categories': [c.pk for c in categories], } return render_to_response('articles/edit_article.html', extra_context, context_instance=context)
def get_edit_url(self, user): if self.owner == user: return reverse('edit_story', args=[self.pk]) allowed_objects = get_allowed_objects( user, ArticleCategory, 'manager') intersect = set(allowed_objects).intersection( set([category.pk for category in self.categories.all()])) if intersect: return reverse('edit_story', args=[self.pk]) return None def __unicode__(self): return self.title article_file_storage = FileSystemStorage( location=get_upload_articles_files(), base_url='/articles/file') def get_file_filepath_storage(instance, filename): s = os.path.splitext(filename) filename = u"%s%s" % (slugify(s[0]), s[1]) return os.path.join(u"%s/%s" % (str(instance.article_id), filename)) class ArticleAttachments(models.Model): attached_file = models.FileField( upload_to=get_file_filepath_storage, storage=article_file_storage) description = models.CharField(_('Description'), max_length=255) mimetype = models.CharField(max_length=255, editable=False) article = models.ForeignKey(ArticleStory, verbose_name=_( 'article'), blank=True, editable=False)