diff --git a/generators/index.py b/generators/index.py index fae2b04..3b0814a 100644 --- a/generators/index.py +++ b/generators/index.py @@ -196,4 +196,3 @@ class Index(DynastieGenerator): self.addReport('Nothing changed') return self.report - diff --git a/generators/post.py b/generators/post.py index 189d6a6..c2ae0e3 100644 --- a/generators/post.py +++ b/generators/post.py @@ -1,6 +1,7 @@ +import datetime import os from xml.dom.minidom import parse, parseString -from dynastie.generators.generator import DynastieGenerator +from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer from dynastie.generators.index import Index from django.db import models @@ -57,3 +58,55 @@ class Post(Index): return self.report + def createPreview(self, values, dom, root, node): + now = datetime.datetime.now() + + v = {} + v['title'] = self.createLinkElem(dom, '/preview.html', values['title']) + v['author'] = values['author'] + v['date'] = now.strftime("%A, %d %B %Y %H:%m") + v['post_content'] = '' + + self.simpleTransform(v, dom, root, node) + + content_nodes = root.getElementsByTagName("div") + post_transform = ('post_content') + for content_node in content_nodes: + the_class = content_node.getAttribute('class') + if not the_class in post_transform: + continue + if the_class == 'post_content': + new_node = dom.createTextNode(values['content']) + content_node.appendChild(new_node) + + post_nodes = dom.getElementsByTagNameNS(self.URI, "post") + post_elem = post_nodes[0] + post_elem.parentNode.removeChild(post_elem) + + def preview(self, src, values): + from dynastie.models import Blog + + hooks = {'post' : self.createPreview} + + if not os.path.exists(src + '/_post.html'): + self.addError('No _post.html found, exiting') + return self.report + + try: + dom = parse(src + '/_post.html') + except xml.dom.DOMException as e: + self.addError('Error parsing _post.html : ' + e) + return self.report + + post_nodes = dom.getElementsByTagNameNS(self.URI, "posts") + + if post_nodes is None: + self.addError('No tag dyn:posts found') + return self.report + + nodes = dom.getElementsByTagName("*") + nodes[0] = self.parse(src, hooks, values, dom, nodes[0]) + writer = StrictUTF8Writer() + nodes[0].writexml(writer) + + return writer.getvalue().encode('utf-8') diff --git a/templates/add_post.html b/templates/add_post.html index 2e036fe..2209789 100644 --- a/templates/add_post.html +++ b/templates/add_post.html @@ -2,36 +2,13 @@ {% block head %} - + {% endblock %} {% block content %} -
{% endblock %} diff --git a/templates/edit_post.html b/templates/edit_post.html index f8775b1..67ebcf3 100644 --- a/templates/edit_post.html +++ b/templates/edit_post.html @@ -2,34 +2,13 @@ {% block head %} - + {% endblock %} {% block content %} - {% endblock %} diff --git a/urls.py b/urls.py index 6a988c3..a8ed039 100644 --- a/urls.py +++ b/urls.py @@ -25,6 +25,7 @@ urlpatterns = patterns('', url(r'^post/edit/(\d+)$', 'dynastie.views.edit_post', name='edit_post'), url(r'^post/delete/(\d+)$','dynastie.views.delete_post', name='delete_post'), url(r'^generate/(\d+)$', 'dynastie.views.generate', name='generate'), + url(r'^preview/(\d+)$', 'dynastie.views.preview', name='preview'), # url(r'^dynastie/', include('dynastie.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: diff --git a/views.py b/views.py index b5d4471..2a766ee 100644 --- a/views.py +++ b/views.py @@ -301,7 +301,7 @@ def edit_post(request, post_id): if form.is_valid(): # All validation rules pass if title != request.POST['title']: post.remove() - post.createPost(request.POST['content']) + post.createPost(request.POST['content']) form.save() # Process the data in form.cleaned_data # ... @@ -322,7 +322,8 @@ def edit_post(request, post_id): content = 'Empty post' return render(request, 'edit_post.html', { - 'form': form, 'post_id' : post_id, 'content' : content + 'form': form, 'post_id' : post_id, 'content' : content, + 'blog_id' : blog_id }) @login_required @@ -362,4 +363,46 @@ def generate(request, blog_id): c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report} - return render(request, 'generate.html', c) + return render(request, 'templates/generate.html', c) + +@login_required +def preview(request, blog_id): + from dynastie.generators import post + + values = {'title' : request.POST['title'], \ + 'author' : request.user.first_name + ' ' + request.user.last_name, \ + 'content' : request.POST['content'] + } + + if not request.user.is_superuser: + b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)[0] + else: + b = Blog.objects.get(pk=blog_id) + + if b is None: + raise Http404 + + b.create_paths() + + engine = globals()['post'] + + for name, obj in inspect.getmembers(engine): + if inspect.isclass(obj) and obj.__module__.startswith("dynastie") \ + and obj.__module__.endswith("post"): + e = obj() + content = e.preview(b.src_path, values) + break + + output = b.output_path + path = output + '/preview.html' + if os.path.exists(path): + os.unlink(path) + + f = open(path, 'wb') + f.write(content) + f.close() + + c = {'content' : content} + + return HttpResponseRedirect('http://' + b.name + '/preview.html') + # return HttpResponseRedirect('http://' + 'localhost:8080' + '/preview.html')