def blog_edit(request, blog_id): blog = get_object_or_404(Blog, id=blog_id) (root, name ,ext) = split_filepath(blog.image.path) image_file_name = '%s.%s' % (name, ext) if not request.user.is_staff and not request.user.id == blog.user.id: return render(request, '403.html', status=403) elif blog.trash: raise Http404 if request.method == 'POST': form = ModifyBlogForm(blog, request.POST) if form.is_valid(): action = request.POST.get('action', '') location, created = Location.objects.get_or_create( city__iexact = form.cleaned_data['city'], country__iexact = form.cleaned_data['country'], defaults = { 'city' : form.cleaned_data['city'], 'country': form.cleaned_data['country'] } ) blog.title = form.cleaned_data['title'] blog.description = form.cleaned_data['description'] blog.photographer_name = form.cleaned_data['photographer_name'] blog.related_url = form.cleaned_data['related_url'] blog.download_url = form.cleaned_data['download_url'] blog.location = location blog.allow_download = form.cleaned_data['allow_download'] blog.category = form.cleaned_data['category'] blog.mood = form.cleaned_data['mood'] blog.draft = 1 if action == 'draft' else 0 blog.trash = 1 if action == 'trash' else 0 if action == 'publish': blog.published = datetime.datetime.now() new_image_file = form.cleaned_data['image_file_name'] if new_image_file and (new_image_file != image_file_name): remove_blog_image(blog) blog.image = File(open('%s%s' % (settings.TEMP_BLOG_IMAGE_ROOT, new_image_file))) blog.image_captured_date = get_image_captured_date(new_image_file) blog.image_captured_device = get_image_captured_device(new_image_file) blog.save() blog.save_tags(form.cleaned_data['tags']) messages.success(request, 'Blog post updated. \ <a class="btn btn-success" href="%s">View post</a>' % reverse('blog_view', args=[blog.id])) if action == 'trash': return redirect('blog_view', blog_id=blog.id) else: return redirect(reverse('blog_edit', args=[blog.id])) else: form = ModifyBlogForm( blog, initial = { 'title' : blog.title, 'description' : blog.description, 'photographer_name' : blog.photographer_name, 'related_url' : blog.related_url, 'download_url' : blog.download_url, 'country' : blog.location.country, 'city' : blog.location.city, 'mood' : blog.mood, 'image_file_name':image_file_name, 'category' : blog.category, 'private' : str(int(blog.private)), 'allow_download' : blog.allow_download, 'tags' : blog.get_tags() }) context = { 'page_title' : 'Edit Blog', 'blog' : blog, 'form' : form, 'is_draft' : blog.draft, 'moods' : MOOD_CHOICES, 'visibilities' : PRIVATE_CHOICES, 'blog_image_file': blog.image.path, } return render(request, 'blog/blog_form.html', context)
def blog_create(request): blog_image_file = None if request.method == 'POST': form = ModifyBlogForm(None, request.POST) if form.is_valid(): action = request.POST.get('action', '') location, created = Location.objects.get_or_create( city__iexact = form.cleaned_data['city'], country__iexact = form.cleaned_data['country'], defaults = { 'city' : form.cleaned_data['city'], 'country' : form.cleaned_data['country'], } ) image_name = form.cleaned_data['image_file_name'] blog = Blog( title = form.cleaned_data['title'], description = form.cleaned_data['description'], photographer_name = form.cleaned_data['photographer_name'], related_url = form.cleaned_data['related_url'], download_url = form.cleaned_data['download_url'], user = request.user, location = location, allow_download = form.cleaned_data['allow_download'], image = File(open('%s%s' % (settings.TEMP_BLOG_IMAGE_ROOT, image_name))), image_captured_date = get_image_captured_date(image_name), image_captured_device = get_image_captured_device(image_name), category = form.cleaned_data['category'], mood = form.cleaned_data['mood'], draft = 1 if action == 'draft' else 0, trash = 1 if action == 'trash' else 0 ) if action == 'publish': blog.published = datetime.datetime.now() blog.save() blog.save_tags(form.cleaned_data['tags']) remove_temporary_blog_image(form.cleaned_data['image_file_name']) messages.success(request, 'Blog post created. \ <a class="btn btn-success" href="%s">View post</a>' % reverse('blog_view', args=[blog.id])) return redirect(reverse('blog_edit', args=[blog.id])) else: file_name = request.POST.get('image_file_name') if file_name: blog_image_file = '%s%s' % (settings.TEMP_BLOG_IMAGE_ROOT, file_name) else: form = ModifyBlogForm(None, initial={'allow_download': True}) context = { 'page_title' : 'Add New Story', 'form' : form, 'moods' : MOOD_CHOICES, 'visibilities' : PRIVATE_CHOICES, 'is_draft' : True, 'blog_image_file' : blog_image_file, } return render(request, 'blog/blog_form.html', context)