def edit_offer(request, offer_id): offer = get_object_or_404(Offer, id=offer_id) if offer.offerer != request.user: request.user.message_set.create(message="You cannot edit offers that are not yours") return HttpResponseRedirect(reverse("offer_list_yours")) return_to = request.GET["returnto"] if request.method == "POST": if request.POST["action"] == "update": offer_form = OfferForm(request.POST, instance=offer) if offer_form.is_valid(): offer = offer_form.save(commit=False) offer.save() if notification: for swap in offer.proposed_swaps.filter(state=1): notification.send( [swap.responding_offer.offerer], "swaps_proposing_offer_changed", { "creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer, }, ) for swap in offer.responding_swaps.filter(state=1): notification.send( [swap.proposing_offer.offerer], "swaps_responding_offer_changed", { "creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer, }, ) request.user.message_set.create(message=_("Successfully updated offer '%s'") % offer.short_description) return HttpResponseRedirect(reverse(return_to)) else: offer_form = OfferForm(instance=offer) else: offer_form = OfferForm(instance=offer) return render_to_response( "swaps/edit_offer.html", {"offer_form": offer_form, "offer": offer}, context_instance=RequestContext(request) )
def edit_offer(request, offer_id): def is_valid(formsets): valid = True for name, formset in formsets: valid = valid and formset.is_valid() return valid offer = get_object_or_404(Offer, id=offer_id) if offer.offerer != request.user: request.user.message_set.create(message="You cannot edit offers that are not yours") return HttpResponseRedirect(reverse("offer_list_yours")) return_to = request.GET['returnto'] formsets = [] if request.method == "POST": if request.POST["action"] == "update": offer_form = OfferForm(request.POST, instance=offer) for name, formset_class in OfferForm.inlines: formsets+=[(name, formset_class(request.POST, request.FILES, instance=offer)),] formsets_valid = is_valid(formsets) offer_form.inlines = formsets if offer_form.is_valid() and formsets_valid: offer = offer_form.save(commit=False) offer.save() for name, formset_class in OfferForm.inlines: f = formset_class(request.POST, request.FILES, instance=offer) if f.is_valid(): f.save() if notification: for swap in offer.proposed_swaps.filter(state=1): notification.send([swap.responding_offer.offerer,], "swaps_proposing_offer_changed", {"creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer}) for swap in offer.responding_swaps.filter(state=1): notification.send([swap.proposing_offer.offerer,], "swaps_responding_offer_changed", {"creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer}) request.user.message_set.create(message=_("Successfully updated offer '%s'") % offer.short_description) return HttpResponseRedirect(reverse(return_to)) else: offer_form = OfferForm(instance=offer) for name, formset_class in OfferForm.inlines: formsets+=[(name, formset_class(None, None, instance=offer)),] else: offer_form = OfferForm(instance=offer) for name, formset_class in OfferForm.inlines: formsets+=[(name, formset_class(None, None, instance=offer)),] offer_form.inlines = formsets return render_to_response("swaps/edit_offer.html", { "offer_form": offer_form, "offer": offer, }, context_instance=RequestContext(request))
def new(request): if request.method == "POST": if request.POST["action"] == "create": offer_form = OfferForm(request.POST) if offer_form.is_valid(): offer = offer_form.save(commit=False) offer.offerer = request.user offer.save() request.user.message_set.create(message=_("Successfully saved offer '%s'") % offer.short_description) # if notification: # if friends: # @@@ might be worth having a shortcut for sending to all friends # notification.send((x['friend'] for x in Friendship.objects.friends_for_user(offer.offerer)), "offer_friend_post", {"post": blog}) return HttpResponseRedirect(reverse("offer_list_yours")) else: offer_form = OfferForm() else: offer_form = OfferForm() return render_to_response( "swaps/new_offer.html", {"offer_form": offer_form}, context_instance=RequestContext(request) )
def new(request): if request.method == "POST": if request.POST["action"] == "create": offer_form = OfferForm(request.POST) if offer_form.is_valid(): offer = offer_form.save(commit=False) offer.offerer = request.user offer.save() request.user.message_set.create(message=_("Successfully saved offer '%s'") % offer.short_description) #if notification: # if friends: # @@@ might be worth having a shortcut for sending to all friends # notification.send((x['friend'] for x in Friendship.objects.friends_for_user(offer.offerer)), "offer_friend_post", {"post": blog}) return HttpResponseRedirect(reverse("offer_list_yours")) else: offer_form = OfferForm() else: offer_form = OfferForm() return render_to_response("swaps/new_offer.html", { "offer_form": offer_form }, context_instance=RequestContext(request))
def edit_offer(request, offer_id): offer = get_object_or_404(Offer, id=offer_id) if offer.offerer != request.user: request.user.message_set.create(message="You cannot edit offers that are not yours") return HttpResponseRedirect(reverse("offer_list_yours")) return_to = request.GET['returnto'] if request.method == "POST": if request.POST["action"] == "update": offer_form = OfferForm(request.POST, instance=offer) if offer_form.is_valid(): offer = offer_form.save(commit=False) offer.save() if notification: for swap in offer.proposed_swaps.filter(state=1): notification.send([swap.responding_offer.offerer,], "swaps_proposing_offer_changed", {"creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer}) for swap in offer.responding_swaps.filter(state=1): notification.send([swap.proposing_offer.offerer,], "swaps_responding_offer_changed", {"creator": request.user, "swap": swap, "proposing_offer": swap.proposing_offer, "responding_offer": swap.responding_offer}) request.user.message_set.create(message=_("Successfully updated offer '%s'") % offer.short_description) return HttpResponseRedirect(reverse(return_to)) else: offer_form = OfferForm(instance=offer) else: offer_form = OfferForm(instance=offer) return render_to_response("swaps/edit_offer.html", { "offer_form": offer_form, "offer": offer, }, context_instance=RequestContext(request))
def new(request): def is_valid(formsets): valid = True for name, formset in formsets: valid = valid and formset.is_valid() return valid formsets = [] if request.method == "POST": if request.POST["action"] == "create": for name, formset_class in OfferForm.inlines: formsets+=[(name, formset_class(request.POST, request.FILES)),] offer_form = OfferForm(request.POST, request.FILES) formsets_valid = is_valid(formsets) offer_form.inlines = formsets if offer_form.is_valid() and formsets_valid: offer = offer_form.save(commit=False) offer.offerer = request.user offer.save() for name, formset_class in OfferForm.inlines: f = formset_class(request.POST, request.FILES, instance=offer) if f.is_valid(): f.save() request.user.message_set.create(message=_("Successfully saved offer '%s'") % offer.short_description) #if notification: # if friends: # @@@ might be worth having a shortcut for sending to all friends # notification.send((x['friend'] for x in Friendship.objects.friends_for_user(offer.offerer)), "offer_friend_post", {"post": blog}) return HttpResponseRedirect(reverse("offer_list_yours")) else: offer_form = OfferForm() else: offer_form = OfferForm() return render_to_response("swaps/new_offer.html", { "offer_form": offer_form }, context_instance=RequestContext(request))