356 | | def save_add(self, request, model, form, formsets, post_url_continue): |
357 | | """ |
358 | | Saves the object in the "add" stage and returns an HttpResponseRedirect. |
359 | | |
360 | | `form` is a bound Form instance that's verified to be valid. |
361 | | """ |
| 356 | def save(self, request, form, formsets, original_obj, commit=True): |
| 357 | """ Save new or changed object and its inlines. """ |
| 358 | new_obj = form.save(commit=commit) |
| 359 | for formset in formsets: |
| 360 | # HACK: it seems like the parent object should be passed into |
| 361 | # a method of something, not just set as an attribute |
| 362 | formset.instance = new_obj |
| 363 | formset.save() |
| 364 | return new_obj |
| 365 | |
| 366 | def save_add(self, request, form, formsets, commit=True): |
| 367 | """ Save new object and its inlines. """ |
| 368 | return self.save(request, form, formsets, None, commit) |
| 369 | |
| 370 | def save_change(self, request, form, formsets, original_obj, commit=True): |
| 371 | """ Save changed object and its inlines. """ |
| 372 | return self.save(request, form, formsets, original_obj, commit) |
| 373 | |
| 374 | def log_add(self, request, form, formsets, new_obj): |
| 375 | """ Log saving of new object and its inlines. """ |
363 | | from django.contrib.contenttypes.models import ContentType |
364 | | opts = model._meta |
365 | | new_object = form.save(commit=True) |
366 | | |
367 | | if formsets: |
368 | | for formset in formsets: |
369 | | # HACK: it seems like the parent obejct should be passed into |
370 | | # a method of something, not just set as an attribute |
371 | | formset.instance = new_object |
372 | | formset.save() |
373 | | |
374 | | pk_value = new_object._get_pk_val() |
375 | | LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), ADDITION) |
376 | | msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object} |
377 | | # Here, we distinguish between different save types by checking for |
378 | | # the presence of keys in request.POST. |
379 | | if request.POST.has_key("_continue"): |
380 | | request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) |
381 | | if request.POST.has_key("_popup"): |
382 | | post_url_continue += "?_popup=1" |
383 | | return HttpResponseRedirect(post_url_continue % pk_value) |
384 | | if request.POST.has_key("_popup"): |
385 | | if type(pk_value) is str: # Quote if string, so JavaScript doesn't think it's a variable. |
386 | | pk_value = '"%s"' % pk_value.replace('"', '\\"') |
387 | | return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, %s, "%s");</script>' % \ |
388 | | (pk_value, str(new_object).replace('"', '\\"'))) |
389 | | elif request.POST.has_key("_addanother"): |
390 | | request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) |
391 | | return HttpResponseRedirect(request.path) |
392 | | else: |
393 | | request.user.message_set.create(message=msg) |
394 | | # Figure out where to redirect. If the user has change permission, |
395 | | # redirect to the change-list page for this object. Otherwise, |
396 | | # redirect to the admin index. |
397 | | if self.has_change_permission(request, None): |
398 | | post_url = '../' |
399 | | else: |
400 | | post_url = '../../../' |
401 | | return HttpResponseRedirect(post_url) |
402 | | |
403 | | def save_change(self, request, model, form, formsets=None): |
404 | | """ |
405 | | Saves the object in the "change" stage and returns an HttpResponseRedirect. |
406 | | |
407 | | `form` is a bound Form instance that's verified to be valid. |
408 | | |
409 | | `formsets` is a sequence of InlineFormSet instances that are verified to be valid. |
410 | | """ |
| 377 | user_id = request.user.id |
| 378 | content_type_id = ContentType.objects.get_for_model(self.model).id |
| 379 | object_id = new_obj._get_pk_val() |
| 380 | LogEntry.objects.log_action(user_id, content_type_id, object_id, force_unicode(new_obj), ADDITION) |
| 381 | |
| 382 | def log_change(self, request, form, formsets, original_obj, new_obj): |
| 383 | """ Log saving of changed object and its inlines. """ |
434 | | LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message) |
435 | | |
436 | | msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object} |
437 | | if request.POST.has_key("_continue"): |
438 | | request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) |
439 | | if request.REQUEST.has_key('_popup'): |
440 | | return HttpResponseRedirect(request.path + "?_popup=1") |
441 | | else: |
442 | | return HttpResponseRedirect(request.path) |
443 | | elif request.POST.has_key("_saveasnew"): |
444 | | request.user.message_set.create(message=_('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': opts.verbose_name, 'obj': new_object}) |
445 | | return HttpResponseRedirect("../%s/" % pk_value) |
446 | | elif request.POST.has_key("_addanother"): |
447 | | request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) |
448 | | return HttpResponseRedirect("../add/") |
449 | | else: |
450 | | request.user.message_set.create(message=msg) |
451 | | return HttpResponseRedirect("../") |
| 401 | LogEntry.objects.log_action(user_id, content_type_id, object_id, force_unicode(new_obj), CHANGE, change_message) |
| 402 | |
| 403 | def redirect_after_save(self, request, new_obj, add=False, change=False): |
| 404 | """ Redirect after saving of new or changed object. """ |
| 405 | opts = self.opts |
| 406 | pk_value = new_obj._get_pk_val() |
| 407 | |
| 408 | # popup |
| 409 | if request.POST.has_key('_popup'): |
| 410 | if isinstance(pk_value, str): # Quote if string, so JavaScript doesn't think it's a variable. |
| 411 | pk_value = '"%s"' % pk_value.replace('"', '\\"') |
| 412 | return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, %s, "%s");</script>' % \ |
| 413 | (pk_value, str(new_obj).replace('"', '\\"'))) |
| 414 | |
| 415 | # default message |
| 416 | if add: |
| 417 | msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_obj} |
| 418 | else: |
| 419 | msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_obj} |
| 420 | |
| 421 | # save as new |
| 422 | if request.POST.has_key('_saveasnew'): |
| 423 | if self.has_change_permission(request, new_obj): |
| 424 | msg += ' ' + _('You may edit it again below.') |
| 425 | redirect_url = '../%s/' % pk_value |
| 426 | elif self.has_change_permission(request, None): |
| 427 | redirect_url = '../' |
| 428 | else: |
| 429 | redirect_url = '../../../' |
| 430 | # save and add another |
| 431 | elif request.POST.has_key('_addanother'): |
| 432 | msg += ' ' + (_('You may add another %s below.') % opts.verbose_name) |
| 433 | if add: |
| 434 | redirect_url = request.path |
| 435 | else: |
| 436 | redirect_url = '../add/' |
| 437 | # save and continue editing |
| 438 | elif request.POST.has_key('_continue'): |
| 439 | if self.has_change_permission(request, new_obj): |
| 440 | msg += ' ' + _('You may edit it again below.') |
| 441 | if add: |
| 442 | redirect_url = '../%s/' % pk_value |
| 443 | else: |
| 444 | redirect_url = request.path |
| 445 | elif self.has_change_permission(request, None): |
| 446 | redirect_url = '../' |
| 447 | else: |
| 448 | redirect_url = '../../../' |
| 449 | # save |
| 450 | else: |
| 451 | if self.has_change_permission(request, None): |
| 452 | redirect_url = '../' |
| 453 | else: |
| 454 | redirect_url = '../../../' |
| 455 | |
| 456 | # create message and redirect |
| 457 | request.user.message_set.create(message=msg) |
| 458 | return HttpResponseRedirect(redirect_url) |