Add preview
This commit is contained in:
parent
c60cd7e3f8
commit
3ac4540db4
|
@ -196,4 +196,3 @@ class Index(DynastieGenerator):
|
|||
self.addReport('Nothing changed')
|
||||
|
||||
return self.report
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -2,36 +2,13 @@
|
|||
|
||||
{% block head %}
|
||||
<script type="text/javascript" src="/static/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
|
||||
<script type="text/javascript">
|
||||
tinyMCE.init({
|
||||
// General options
|
||||
mode : "textareas",
|
||||
theme : "advanced",
|
||||
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
|
||||
editor_selector : "mceAdvanced",
|
||||
encoding : "raw",
|
||||
entities : "",
|
||||
entity_encoding : "raw",
|
||||
// Theme options
|
||||
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
|
||||
theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
|
||||
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,charmap,emotions,iespell,media,advhr",
|
||||
// theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
|
||||
theme_advanced_toolbar_location : "top",
|
||||
theme_advanced_toolbar_align : "left",
|
||||
theme_advanced_statusbar_location : "bottom",
|
||||
theme_advanced_resizing : true,
|
||||
width: "100%",
|
||||
height: "400"
|
||||
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" src="/static/js/dynastie.js"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form action="/post/add/{{ blog_id }}" method="post">{% csrf_token %}
|
||||
<form id="previewForm" action="/post/add/{{ blog_id }}" method="post">{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<textarea name="content" class="mceAdvanced"></textarea><br/><br/>
|
||||
<input type="submit" name="add" value="Add" /><input type="submit" name="preview" value="Preview" /><input type="submit" name="cancel" value="Cancel" />
|
||||
<input type="submit" name="add" value="Add" /><input type="button" name="preview" value="Preview" onClick="previewPost();"/><input type="submit" name="cancel" value="Cancel" />
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -2,34 +2,13 @@
|
|||
|
||||
{% block head %}
|
||||
<script type="text/javascript" src="/static/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
|
||||
<script type="text/javascript">
|
||||
tinyMCE.init({
|
||||
// General options
|
||||
mode : "textareas",
|
||||
theme : "advanced",
|
||||
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
|
||||
editor_selector : "mceAdvanced",
|
||||
|
||||
// Theme options
|
||||
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
|
||||
theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
|
||||
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,charmap,emotions,iespell,media,advhr",
|
||||
// theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
|
||||
theme_advanced_toolbar_location : "top",
|
||||
theme_advanced_toolbar_align : "left",
|
||||
theme_advanced_statusbar_location : "bottom",
|
||||
theme_advanced_resizing : true,
|
||||
width: "100%",
|
||||
height: "400"
|
||||
|
||||
});
|
||||
</script>
|
||||
<script type="text/javascript" src="/static/js/dynastie.js"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form action="/post/edit/{{ post_id }}" method="post">{% csrf_token %}
|
||||
<form id="previewForm" action="/post/edit/{{ blog_id }}" method="post">{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<textarea name="content" class="mceAdvanced">{{ content }}</textarea>
|
||||
<input type="submit" name="edit" value="Edit" /><input type="submit" name="preview" value="Preview" /><input type="submit" name="cancel" value="Cancel" />
|
||||
<input type="submit" name="edit" value="Edit" /><input type="button" name="preview" value="Preview" onClick="previewPost();"/><input type="submit" name="cancel" value="Cancel" />
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
1
urls.py
1
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:
|
||||
|
|
47
views.py
47
views.py
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue
Block a user