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