From 9d2d000e482746e7e3b18176ffb7844b095a2fed Mon Sep 17 00:00:00 2001 From: Mark Riedesel Date: Thu, 14 Nov 2013 00:54:32 -0600 Subject: [PATCH] Document model appears to fetch mostly properly Can fetch Documents, and then get pages triggers page retrieval and related field retrieval. --- admin.py | 31 ++++++-- static/js/pdfformfiller/pdfformfiller.js | 70 ++++++++++++++++++- .../pdfformfiller/document/change_form.html | 8 +++ .../admin/pdfformfiller/document/preview.html | 57 +++++++++++++++ views/api.py | 2 + 5 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 templates/admin/pdfformfiller/document/change_form.html create mode 100644 templates/admin/pdfformfiller/document/preview.html diff --git a/admin.py b/admin.py index 981dfb3..1bfbcad 100644 --- a/admin.py +++ b/admin.py @@ -7,9 +7,34 @@ import models class DocumentAdmin(admin.ModelAdmin): list_display = ['name', 'pdf', 'number_of_pages'] + class Media: + js = ( "lib/jquery.js", + "lib/underscore.js", + "lib/backbone.js", + "lib/backbone.marionette.js", + "js/pdfformfiller/pdfformfiller.js") + + def number_of_pages(self, instance): return instance.page_set.count() + def get_urls(self): + urls = super(DocumentAdmin, self).get_urls() + my_urls = patterns('', + url(r'^(?P.*)/preview/$', self.admin_site.admin_view(self.preview), name='preview'), + ) + return my_urls + urls + + def preview(self, request, pk): + context = {} + context['document'] = models.Document.objects.get(pk=pk) + context['media'] = self.media + + return TemplateResponse(request, + 'admin/pdfformfiller/document/preview.html', + context + ) + class PageAdmin(admin.ModelAdmin): list_display = ['document', 'name', 'image', 'page_num'] @@ -26,7 +51,6 @@ class PageAdmin(admin.ModelAdmin): urls = super(PageAdmin, self).get_urls() my_urls = patterns('', url(r'^(?P.*)/preview/$', self.admin_site.admin_view(self.preview), name='preview'), - url(r'^(?P.*)/json/$', self.admin_site.admin_view(self.json), name='json'), ) return my_urls + urls @@ -40,11 +64,6 @@ class PageAdmin(admin.ModelAdmin): context ) - def json(self, request, pk): - page = models.Page.get(pk=pk) - attribs = ['pos_x', 'pos_y', 'name', 'height', 'width', 'fieldtype', 'page'] - - class FormFieldAdmin(admin.ModelAdmin): list_display = ['name', 'page', 'fieldtype', 'page_number', 'pos_y'] diff --git a/static/js/pdfformfiller/pdfformfiller.js b/static/js/pdfformfiller/pdfformfiller.js index fee1a15..74781b1 100644 --- a/static/js/pdfformfiller/pdfformfiller.js +++ b/static/js/pdfformfiller/pdfformfiller.js @@ -11,7 +11,11 @@ Page, Field, PageCollection, - FieldCollection; + FieldCollection, + FieldData, + FieldDataCollection, + + DocumentView; if (typeof exports !== 'undefined') { PDFFormFiller = exports; @@ -25,6 +29,12 @@ model: Field }); + FieldData = PDFFormFiller.FieldData = Backbone.Model.extend({}); + + FieldDataCollection = PDFFormFiller.FieldDataCollection = Backbone.Collection.extend({ + model: FieldData + }); + Page = PDFFormFiller.Page = Backbone.Model.extend({ initialize: function (options) { var fields, @@ -42,16 +52,53 @@ fetch: function () { var self = this; - Backbone.Model.prototype.fetch.apply(this, arguments).then(function () { + return Backbone.Model.prototype.fetch.apply(this, arguments).then(function () { var model = self; // Auto-fetch the fields collection if (typeof model.get('fields') !== 'undefined') { - model.get('fields').fetch(); + return model.get('fields').fetch(); } }); }, }); + PageCollection = PDFFormFiller.PageCollection = Backbone.Collection.extend({ + model: Page + }); + + Document = PDFFormFiller.Document = Backbone.Model.extend({ + initialize: function (options) { + console.log(options); + + if (typeof options.pages === 'undefined') { + this.pages = new PageCollection(); + this.pages.url = this.url + '../page/'; + } + }, + + getPage: function (page_num) { + var page_ids = this.get('page_ids'), + page_id = page_ids[page_num], + page = this.pages.findWhere({id: page_id}), + defer = new $.Deferred(); + + if (typeof page === 'undefined') { + page = new Page(); + page.url = '/formfiller/api/page/' + page_id + '/'; + this.pages.add(page); + page.fetch() + .then(function () { + return defer.resolve(page); + }); + } else { + defer.resolve(page); + return defer.promise(); + } + + return defer.promise(); + } + }); + FieldItemPreviewView = PDFFormFiller.FieldItemPreviewView = Marionette.ItemView.extend({ /* template: _.template('
<%= name %>
'), */ @@ -131,5 +178,22 @@ }); + DocumentView = PDFFormFiller.DocumentView = Marionette.Layout.extend({ + template: _.template('
'), + regions: { + buttons: '.buttons', + editor: '.editor' + }, + + ui: { + next: ':contains("next")', + prev: ':contains("prev")' + }, + + initialize: function (options) { + console.log(this.ui.next); + } + + }); }).call(this); diff --git a/templates/admin/pdfformfiller/document/change_form.html b/templates/admin/pdfformfiller/document/change_form.html new file mode 100644 index 0000000..9c645cc --- /dev/null +++ b/templates/admin/pdfformfiller/document/change_form.html @@ -0,0 +1,8 @@ +{% extends 'admin/change_form.html' %} +{% load i18n admin_static admin_modify %} +{% load admin_urls %} +{% block object-tools-items %} +
  • Preview
  • +{{ block.super }} +{% endblock %} + diff --git a/templates/admin/pdfformfiller/document/preview.html b/templates/admin/pdfformfiller/document/preview.html new file mode 100644 index 0000000..413205e --- /dev/null +++ b/templates/admin/pdfformfiller/document/preview.html @@ -0,0 +1,57 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_static admin_modify %} +{% load admin_urls %} + +{% block extrahead %}{{ block.super }} +{{ media }} +{% endblock %} +{% block content %} + + + + +{% url "pdfformfiller_api_document_detail" id=document.id %} + +
    +
    +
    +{% endblock %} diff --git a/views/api.py b/views/api.py index 7bb55a4..23b2dbe 100644 --- a/views/api.py +++ b/views/api.py @@ -66,7 +66,9 @@ def document(request): def document_detail(request, id): document = models.Document.objects.get(pk=id) + pages = models.Page.objects.filter(document__pk=document.id).values('id','page_num') context = extract_fields(document) + context['page_ids'] = [p['id'] for p in pages] return json_response(context)