| | 1 | |
| | 2 | How to set the current on the model instance using newforms admin. |
| | 3 | |
| | 4 | {{{ |
| | 5 | |
| | 6 | from django.db import models |
| | 7 | from django.http import Http404, HttpResponse, HttpResponseRedirect |
| | 8 | from django.utils.html import escape |
| | 9 | from django.utils.safestring import mark_safe |
| | 10 | from django.db import models, transaction |
| | 11 | from django.contrib.auth.models import User |
| | 12 | from django.utils.encoding import force_unicode |
| | 13 | from django.utils.translation import ugettext as _ |
| | 14 | |
| | 15 | class Post(models.Model): |
| | 16 | user = models.ForeignKey(User) |
| | 17 | content = models.TextField() |
| | 18 | |
| | 19 | from django.contrib import admin |
| | 20 | |
| | 21 | class PostModelAdmin(admin.ModelAdmin): |
| | 22 | |
| | 23 | fields= ('content',) |
| | 24 | |
| | 25 | def __call__(self, request, url): |
| | 26 | self.user = request.user |
| | 27 | return super(PostModelAdmin, self).__call__(request, url) |
| | 28 | |
| | 29 | def save_add(self, request, model, form, formsets, post_url_continue): |
| | 30 | """ |
| | 31 | Saves the object in the "add" stage and returns an HttpResponseRedirect. |
| | 32 | |
| | 33 | `form` is a bound Form instance that's verified to be valid. |
| | 34 | """ |
| | 35 | from django.contrib.admin.models import LogEntry, ADDITION |
| | 36 | from django.contrib.contenttypes.models import ContentType |
| | 37 | opts = model._meta |
| | 38 | new_object = form.save(commit=False) |
| | 39 | new_object.user = self.user |
| | 40 | new_object.save() |
| | 41 | form.save_m2m() |
| | 42 | |
| | 43 | if formsets: |
| | 44 | for formset in formsets: |
| | 45 | # HACK: it seems like the parent obejct should be passed into |
| | 46 | # a method of something, not just set as an attribute |
| | 47 | formset.instance = new_object |
| | 48 | formset.save() |
| | 49 | |
| | 50 | pk_value = new_object._get_pk_val() |
| | 51 | LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), ADDITION) |
| | 52 | msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object} |
| | 53 | # Here, we distinguish between different save types by checking for |
| | 54 | # the presence of keys in request.POST. |
| | 55 | if request.POST.has_key("_continue"): |
| | 56 | request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) |
| | 57 | if request.POST.has_key("_popup"): |
| | 58 | post_url_continue += "?_popup=1" |
| | 59 | return HttpResponseRedirect(post_url_continue % pk_value) |
| | 60 | if request.POST.has_key("_popup"): |
| | 61 | return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ |
| | 62 | # escape() calls force_unicode. |
| | 63 | (escape(pk_value), escape(new_object))) |
| | 64 | elif request.POST.has_key("_addanother"): |
| | 65 | request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) |
| | 66 | return HttpResponseRedirect(request.path) |
| | 67 | else: |
| | 68 | request.user.message_set.create(message=msg) |
| | 69 | # Figure out where to redirect. If the user has change permission, |
| | 70 | # redirect to the change-list page for this object. Otherwise, |
| | 71 | # redirect to the admin index. |
| | 72 | if self.has_change_permission(request, None): |
| | 73 | post_url = '../' |
| | 74 | else: |
| | 75 | post_url = '../../../' |
| | 76 | return HttpResponseRedirect(post_url) |
| | 77 | save_add = transaction.commit_on_success(save_add) |
| | 78 | |
| | 79 | def save_change(self, request, model, form, formsets=None): |
| | 80 | """ |
| | 81 | Saves the object in the "change" stage and returns an HttpResponseRedirect. |
| | 82 | |
| | 83 | `form` is a bound Form instance that's verified to be valid. |
| | 84 | |
| | 85 | `formsets` is a sequence of InlineFormSet instances that are verified to be valid. |
| | 86 | """ |
| | 87 | from django.contrib.admin.models import LogEntry, CHANGE |
| | 88 | from django.contrib.contenttypes.models import ContentType |
| | 89 | opts = model._meta |
| | 90 | new_object = form.save(commit=False) |
| | 91 | new_object.user = self.user |
| | 92 | new_object.save() |
| | 93 | form.save_m2m() |
| | 94 | pk_value = new_object._get_pk_val() |
| | 95 | |
| | 96 | if formsets: |
| | 97 | for formset in formsets: |
| | 98 | formset.save() |
| | 99 | |
| | 100 | # Construct the change message. TODO: Temporarily commented-out, |
| | 101 | # as manipulator object doesn't exist anymore, and we don't yet |
| | 102 | # have a way to get fields_added, fields_changed, fields_deleted. |
| | 103 | change_message = [] |
| | 104 | #if manipulator.fields_added: |
| | 105 | #change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and'))) |
| | 106 | #if manipulator.fields_changed: |
| | 107 | #change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and'))) |
| | 108 | #if manipulator.fields_deleted: |
| | 109 | #change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and'))) |
| | 110 | #change_message = ' '.join(change_message) |
| | 111 | if not change_message: |
| | 112 | change_message = _('No fields changed.') |
| | 113 | LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, force_unicode(new_object), CHANGE, change_message) |
| | 114 | |
| | 115 | msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object} |
| | 116 | if request.POST.has_key("_continue"): |
| | 117 | request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) |
| | 118 | if request.REQUEST.has_key('_popup'): |
| | 119 | return HttpResponseRedirect(request.path + "?_popup=1") |
| | 120 | else: |
| | 121 | return HttpResponseRedirect(request.path) |
| | 122 | elif request.POST.has_key("_saveasnew"): |
| | 123 | 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}) |
| | 124 | return HttpResponseRedirect("../%s/" % pk_value) |
| | 125 | elif request.POST.has_key("_addanother"): |
| | 126 | request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name)) |
| | 127 | return HttpResponseRedirect("../add/") |
| | 128 | else: |
| | 129 | request.user.message_set.create(message=msg) |
| | 130 | return HttpResponseRedirect("../") |
| | 131 | save_change = transaction.commit_on_success(save_change) |
| | 132 | |
| | 133 | admin.site.register(Post, PostModelAdmin) |
| | 134 | |
| | 135 | |
| | 136 | }}} |