Permalink
Browse files

Added delete page, delete button doesn't work because javascript is b…

…eing stupid
  • Loading branch information...
jrasky committed Oct 2, 2012
1 parent ef1007a commit 92e5f5f444a4c7eff14ff5128cb3142e0fc1a0a2
@@ -6,6 +6,7 @@ $(function($) {
'overlays/': 'listOverlays',
'overlays/new': 'newOverlay',
'overlay/:overlay_id': 'showOverlay',
+ 'overlay/:overlay_id/delete': 'deleteOverlay',
'': 'root'
},
@@ -32,6 +33,12 @@ $(function($) {
view.render();
},
+ deleteOverlay: function(overlay_id) {
+ console.log('Routed to deleteOverlay');
+ var view = new app.views.DeleteOverlayView({id: overlay_id});
+ view.render();
+ },
+
start: function() {
Backbone.history.start();
}
@@ -42,7 +42,7 @@ $(function($) {
'{{debug}}' +
'<ul>' +
'{{#each overlays }}' +
- '<li><a href="#overlay/{{key}}">{{name}}</a></li>' +
+ '<li><a href="#overlay/{{key}}">{{name}}</a><a href="#overlay/{{key}}/delete">delete</a></li>' +
'{{/each}}' +
'</ul>',
@@ -512,7 +512,7 @@ $(function($) {
});
var csrftoken = app.views.NewOverlayView.prototype.getCookie('csrftoken');
$.ajax({
- url: '/old/overlays/new.html',
+ url: '/overlays/new.json',
crossDomain: false,
beforeSend: function(xhr, settings) {
if (!app.views.NewOverlayView.prototype.csrfSafeMethod(settings.type)) {
@@ -540,10 +540,50 @@ $(function($) {
var overlay = new app.models.Overlay({key: json.id});
app.overlays.add(overlay);
overlay.fetch({ 'success': function() {
- app.router.navigate('overlay/'+json['id'], {trigger: true});
+ app.router.navigate('overlay/'+json['id']);
} });
}
}
});
+ app.views.DeleteOverlayView = app.views.View.extend({
+
+ template: '<form id="deleteOverlayForm"><h4>Are you sure you want to delete overlay {{name}}?</h4><br><input type="button" value="Delete" id="deleteOverlayFormSubmitButton" /><input type="button" value="Cancel" id="deleteOverlayFormCancelButton" />',
+
+ initialize: function() {
+ app.views.View.prototype.initialize.apply(this, arguments);
+ if (this.id && !this.model) {
+ this.model = app.overlays.get(this.id);
+ }
+ assert(this.model, 'Requires a model!');
+ this.context = this.model.toJSON();
+ },
+
+ afterRender: function() {
+ this.$('input#deleteOverlayFormSubmitButton').click(this.submitForm);
+ this.$('input#deleteOverlayFormCancelButton').click(this.cancel);
+ },
+
+ cancel: function() {
+ app.router.navigate('overlays/');
+ },
+
+ submitForm: function() {
+ var key = this.context['key']
+ $.ajax({
+ url: '/overlay/'+key+'/delete.html',
+ crossDomain: false,
+ cache: false,
+ contentType: false,
+ processData: false,
+ type: 'POST',
+ success: app.views.DeleteOverlayView.prototype.submitSuccess
+ });
+ },
+
+ submitSuccess: function(data) {
+ console.log("got data back");
+ app.router.navigate('overlays/');
+ },
+ });
});
View
@@ -12,9 +12,12 @@
'geocamTiePoint.views',
## New Workflow ##
- url(r'^overlays/', 'backbone',
+ url(r'^overlays/$', 'backbone',
{}, 'geocamTiePoint_backbone_overlays'),
+ url(r'^overlays/new.json$', 'overlayNewJSON',
+ {}, 'geocamTiePoint_overlayNew_JSON'),
+
## Urls to make current pages work with new workflow ##
url(r'^overlays/list.html$', lambda request: redirect('geocamTiePoint_backbone_overlays'),
{}, 'geocamTiePoint_overlayIndex'),
View
@@ -108,6 +108,64 @@ def overlayDelete(request, key):
overlay.delete()
return HttpResponseRedirect(reverse('geocamTiePoint_overlayIndex'))
+@transaction.commit_on_success
+def overlayNewJSON(request):
+ if request.method == 'POST':
+ form = forms.NewImageDataForm(request.POST, request.FILES)
+ if form.is_valid():
+ # create and save new empty overlay so we can refer to it
+ # this causes a ValueError if the user isn't logged in
+ overlay = models.Overlay(author=request.user)
+ overlay.save()
+
+ # save imageData
+ image = None
+ imageRef = form.cleaned_data['image']
+ imageData = models.ImageData(contentType=imageRef.content_type,
+ overlay=overlay)
+ if imageRef.content_type in PDF_MIME_TYPES:
+ # convert PDF to raster image
+ pngData = pdf.convertPdf(imageRef.file.read())
+ imageData.image.save('dummy.png', ContentFile(pngData), save=False)
+ imageData.contentType = 'image/png'
+ else:
+ bits = imageRef.file.read()
+ image = PIL.Image.open(StringIO(bits))
+ if image.mode != 'RGBA':
+ # add alpha channel to image for better
+ # transparency handling later
+ image = image.convert('RGBA')
+ out = StringIO()
+ image.save(out, format='png')
+ convertedBits = out.getvalue()
+ logging.info('converted image to RGBA, output length %s bytes'
+ % len(bits))
+ imageData.image.save('dummy.png', ContentFile(convertedBits),
+ save=False)
+ imageData.contentType = 'image/png'
+ else:
+ imageData.image.save('dummy.png', ContentFile(bits), save=False)
+ imageData.contentType = imageRef.content_type
+ imageData.save()
+
+ if image is None:
+ image = PIL.Image.open(imageData.image.file)
+
+ # fill in overlay info
+ overlay.name = imageRef.name
+ overlay.imageData = imageData
+ overlay.extras.points = []
+ overlay.extras.imageSize = image.size
+ overlay.save()
+
+ # generate initial quad tree
+ overlay.generateUnalignedQuadTree()
+
+ # respond with json
+ data = {'status': 'success', 'id': overlay.key}
+ return HttpResponse(json.dumps(data))
+ else:
+ return HttpResponseNotAllowed(('POST'))
@transaction.commit_on_success
def overlayNew(request):

0 comments on commit 92e5f5f

Please sign in to comment.