landed create new article from template feature

This commit is contained in:
Sanj 2011-05-15 18:56:43 +05:30
parent 6a08812aff
commit dc9f50feee
5 changed files with 128 additions and 3 deletions

View File

@ -13,7 +13,7 @@ from settings import MEDIA_ROOT, PROJECT_PATH
from django.contrib.auth.models import User, Group
from tagging.fields import TagField
from tagging.models import Tag
from copy import deepcopy
def addPx(val):
'''
@ -209,6 +209,17 @@ class Article(models.Model):
return True
return False
def get_copy(self):
a = Article()
a.typ = self.typ
a.order = 0
a.name = "From Template " + self.name
a.save()
for p in Page.objects.filter(article=self):
new_page = p.get_copy(a)
return a
'''
Return all changes since revision_no.
'''
@ -355,6 +366,24 @@ class Page(models.Model):
b.save()
return self
def get_copy(self, new_article):
new_page = deepcopy(self)
new_page.id = None
new_page.article = new_article
new_page.save()
for i in ImageBox.objects.filter(page=self):
new_box = deepcopy(i)
new_box.id = None
new_box.page = new_page
new_box.save()
for t in TextBox.objects.filter(page=self):
new_box = deepcopy(t)
new_box.id = None
new_box.page = new_page
new_box.save()
return new_page
def deleteme(self):
pages_after = Page.objects.filter(article=self.article, page_no__gt=self.page_no)
for p in pages_after:

View File

@ -21,10 +21,12 @@ urlpatterns = patterns('',
(r'^category/json/', views.category_json),
(r'^save_page/$', views.canvas_save),
(r'^article/(?P<id>\d+)/$', views.edit_article),
(r'^create_article', views.create_article),
(r'^view_page/(?P<id>\d+)/$', views.view_page),
(r'^view_article/(?P<id>\d+)/$', views.view_article),
(r'^print_article/$', views.article_pdf),
(r'^article_frontend/(?P<product_id>\d+)/(?P<article_order>\d+)/$', views.article_frontend),
(r'^article_web/(?P<article_id>\d+)/', views.article_webalone),
(r'^issue/(?P<id>\d+)/$', views.edit_issue),
(r'^product/(?P<id>\d+)/$', views.edit_product),
(r'^new_issue/', views.new_issue),

View File

@ -88,8 +88,10 @@ def edit_article(request, id):
c = Category.objects.all()
a = Article.objects.get(pk=id)
p = a.product
frontend_url = "/edit/article_frontend/%d/%d/" % (p.id, a.order)
if p is not None:
frontend_url = "/edit/article_frontend/%d/%d/" % (p.id, a.order)
else:
frontend_url = "/edit/article_web/%d/" % a.id
rDict = {
'categories': c,
'article_id': id,
@ -514,6 +516,29 @@ def view_article(request, id):
page_height = article.view_size()[1]
return render_to_response("view_article.html", {'pages': d, 'm': m, 'width': addPx(page_width), 'height': addPx(page_height)})
@login_required
def create_article(request):
if request.POST:
template_article = int(request.POST['article_id'])
new_name = request.POST['article_name']
t = Article.objects.get(pk=template_article)
new_article = t.get_copy()
new_article.name = new_name
new_article.owner = request.user
new_article.save()
new_id = new_article.id
return HttpResponseRedirect("/edit/article/%d/" % new_id)
else:
templates = []
others = []
for a in Article.objects.all():
if a.name.upper().startswith('TEMPLATE'):
templates.append(a)
else:
others.append(a)
return render_to_response("create_article.html", {'templates': templates, 'others': others})
def article_frontend(request, product_id, article_order):
product = Product.objects.get(pk=product_id)
article = Article.objects.filter(product=product, order=article_order)[0]
@ -533,6 +558,23 @@ def article_frontend(request, product_id, article_order):
}
return render_to_response("article_frontend.html", {'pages': pages, 'width': width, 'height': height, 'm': 1})
def article_webalone(request, article_id):
article = Article.objects.get(pk=article_id)
width = article.view_size()[0]
height = article.view_size()[1]
pages = article.get_dict()
articles_before = []
articles_after = []
d = {
'pages': pages,
'height': height,
'width': width,
'm': 1,
'article': article,
'articles_before': [],
'articles_after': []
}
return render_to_response("article_frontend.html", d)
def poll_changes(request):
a_id = request.GET['article_id']

View File

@ -5,4 +5,6 @@
<a href="/edit/article/{{a.id}}/">{{ a.name }}</a><br />
{% endfor %}
<br /> <br />
<a href="create_article" title="Create New Article">Create Article</a>
{% endblock %}

View File

@ -0,0 +1,50 @@
{% extends 'base.html' %}
{% block head %}
<style type="text/css">
.templateArticle {
cursor: pointer;
font-family: sans-serif;
margin-bottom: 6px;
}
</style>
<script type="text/javascript">
$(function() {
$('.templateArticle').click(function() {
var id = $(this).attr("data-id");
alert(id);
$('#articleId').val(id);
if ($("#articleName").val().replace(" ", "") === '') {
alert("please enter a name for your article.");
} else {
$('#newArticleForm').submit();
}
});
});
</script>
{% endblock %}
{% block body %}
<div id="articleInstructions">
<b>Instructions:</b> Give your article a name and then click on a template (or other article) you would like to use as a starting point for your new article. Have fun!
</div>
<form id="newArticleForm" action="" method="POST">
<label for="article_name">Article Name:</label> <input type="text" id="articleName" name="article_name" /> <br />
<input type="hidden" id="articleId" name="article_id" />
</form>
<div id="templateArticles">
<h3>Templates:</h3>
{% for t in templates %}
<div class="templateArticle" data-id="{{ t.id }}">{{ t.name }}</div>
{% endfor %}
<h3>Other Articles:</h3>
{% for o in others %}
<div class="templateArticle" data-id="{{ o.id }}">{{ o.name }}</div>
{% endfor %}
</div>
{% endblock %}