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 add_new_story(request): allowed_objects = get_allowed_objects( request.user, ArticleCategory, 'writer') if len(allowed_objects) == 0: return HttpResponseRedirect('/') if request.method == 'POST': form_story = ArticleStoryForm(request.POST, request.FILES, request=request) fa_set = formset_factory(ArticleAttachmentsForm, extra=2) form_attachments_set = fa_set(request.POST, request.FILES) if form_story.is_valid(): if 'image' in request.FILES: image_file = request.FILES['image'] mimetype = image_file.content_type else: image_file = None mimetype = '' if image_file: story = form_story.save() upload_path = get_upload_articles_images() path_src = u"%s/%s" % (upload_path, story.image) path_dest = u"%s/%s" % (upload_path, story.pk) os.makedirs(path_dest) shutil.move(path_src, path_dest) s = os.path.splitext(image_file.name) filename = u"%s%s" % (slugify(s[0]), s[1]) story.image = "%s/%s" % (story.pk, filename) else: story = form_story.save(commit=False) story.mimetype = mimetype story.owner = request.user story.save() story.categories.clear() selected_categories = request.POST.getlist('_selected_action') story.categories = selected_categories 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 tags = map(int, request.POST.getlist('article_tags[]')) tags = ArticleTags.objects.filter(pk__in=tags) story.tags.add(*tags) if story.categories: notify_users(request, story, story.categories.all(), True) return HttpResponseRedirect(reverse('writer_stories_list', args=[])) else: form_story = ArticleStoryForm(request=request) fa_set = formset_factory(ArticleAttachmentsForm, extra=2) form_attachments_set = fa_set() tags = ArticleTags.objects.all() context = RequestContext(request) extra_context = {'form_story': form_story, 'form_attachments': form_attachments_set, 'tags': tags, } return render_to_response('articles/new_article.html', extra_context, context_instance=context)