Permalink
Browse files

Rewrite view to be more coherent

Part of this rewrite was renaming box.html to
_box.html to be in line with the convention of
naming partials.

In addition, the ajax portion will return a new
partial called _box_body.html. The templates that
ship with this are effectively the same as the
original box.html, however this separation gives
more flexibility in how ajax boxes can work.
  • Loading branch information...
1 parent 6a3fe89 commit 4414f1f8e6cef8303e325b28125d5c206cdc9141 @paltman paltman committed Feb 27, 2013
View
1 boxes/templates/boxes/_box.html
@@ -0,0 +1 @@
+{% include "boxes/_box_body.html" %}
View
2 boxes/templates/boxes/box.html → boxes/templates/boxes/_box_body.html
@@ -12,7 +12,7 @@
{{ form.content }}
</div>
<div class="form-actions">
- <button type="submit" class="btn btn-primary">Save changes</a>
+ <button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
View
2 boxes/templatetags/boxes_tags.py
@@ -8,7 +8,7 @@
register = template.Library()
-@register.inclusion_tag("boxes/box.html", takes_context=True)
+@register.inclusion_tag("boxes/_box.html", takes_context=True)
def box(context, label, show_edit=True, *args, **kwargs):
request = context["request"]
View
52 boxes/views.py
@@ -17,38 +17,32 @@
@require_POST
@permission_required("boxes.change_box", raise_exception=True)
def box_edit(request, label):
+ box, _ = Box.objects.get_or_create(label=label, defaults={
+ "created_by": request.user,
+ "last_updated_by": request.user
+ })
+ form = BoxForm(request.POST, instance=box, prefix=label)
if not form.is_valid():
return HttpResponseBadRequest() # not sure how this will ever happen
- next = request.GET.get("next")
-
- try:
- box = Box.objects.get(label=label)
- except Box.DoesNotExist:
- box = None
+ box = form.save(commit=False)
+ box.last_updated_by = request.user
+ box.last_updated = timezone.now()
+ box.save()
- form = BoxForm(request.POST, instance=box, prefix=label)
+ if not request.is_ajax():
+ return redirect(
+ request.POST.get("next", request.GET.get("next", request.path))
+ )
- if form.is_valid():
- if box is None:
- box = form.save(commit=False)
- box.label = label
- box.created_by = request.user
- box.last_updated_by = request.user
- box.last_updated = datetime.datetime.now()
- box.save()
- else:
- form.save()
-
- if request.is_ajax():
- data = {
- "html": render_to_string("boxes/box.html", {
- "label": label,
- "form": BoxForm(instance=box, prefix=label),
- "box": box,
- "form_action": reverse("box_edit", args=[label])
- }, context_instance=RequestContext(request))
- }
- return HttpResponse(json.dumps(data), mimetype="application/json")
- return redirect(next)
+ data = {
+ "html": render_to_string("boxes/_box_body.html", {
+ "label": label,
+ "form": BoxForm(instance=box, prefix=label),
+ "box": box,
+ "form_action": reverse("box_edit", args=[label]),
+ "saved": True
+ }, context_instance=RequestContext(request))
+ }
+ return HttpResponse(json.dumps(data), mimetype="application/json")

0 comments on commit 4414f1f

Please sign in to comment.