From dc9f50feee63812676f52f9d76af221c8f7781dc Mon Sep 17 00:00:00 2001 From: Sanj Date: Sun, 15 May 2011 18:56:43 +0530 Subject: [PATCH] landed create new article from template feature --- edgware/editor/models.py | 31 ++++++++++++++++- edgware/editor/urls.py | 2 ++ edgware/editor/views.py | 46 ++++++++++++++++++++++-- edgware/templates/article_list.html | 2 ++ edgware/templates/create_article.html | 50 +++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 edgware/templates/create_article.html diff --git a/edgware/editor/models.py b/edgware/editor/models.py index af5ae6d..cb01783 100644 --- a/edgware/editor/models.py +++ b/edgware/editor/models.py @@ -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: diff --git a/edgware/editor/urls.py b/edgware/editor/urls.py index 0bd8176..547eceb 100644 --- a/edgware/editor/urls.py +++ b/edgware/editor/urls.py @@ -21,10 +21,12 @@ urlpatterns = patterns('', (r'^category/json/', views.category_json), (r'^save_page/$', views.canvas_save), (r'^article/(?P\d+)/$', views.edit_article), + (r'^create_article', views.create_article), (r'^view_page/(?P\d+)/$', views.view_page), (r'^view_article/(?P\d+)/$', views.view_article), (r'^print_article/$', views.article_pdf), (r'^article_frontend/(?P\d+)/(?P\d+)/$', views.article_frontend), + (r'^article_web/(?P\d+)/', views.article_webalone), (r'^issue/(?P\d+)/$', views.edit_issue), (r'^product/(?P\d+)/$', views.edit_product), (r'^new_issue/', views.new_issue), diff --git a/edgware/editor/views.py b/edgware/editor/views.py index 043891e..e9ee9a5 100644 --- a/edgware/editor/views.py +++ b/edgware/editor/views.py @@ -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'] diff --git a/edgware/templates/article_list.html b/edgware/templates/article_list.html index eb032dc..20b2cc0 100644 --- a/edgware/templates/article_list.html +++ b/edgware/templates/article_list.html @@ -5,4 +5,6 @@ {{ a.name }}
{% endfor %} +

+ Create Article {% endblock %} diff --git a/edgware/templates/create_article.html b/edgware/templates/create_article.html new file mode 100644 index 0000000..29b06b2 --- /dev/null +++ b/edgware/templates/create_article.html @@ -0,0 +1,50 @@ +{% extends 'base.html' %} + +{% block head %} + + + +{% endblock %} + +{% block body %} +
+ Instructions: 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! +
+
+
+ +
+
+

Templates:

+ {% for t in templates %} +
{{ t.name }}
+ {% endfor %} +

Other Articles:

+ {% for o in others %} +
{{ o.name }}
+ {% endfor %} +
+ +{% endblock %}