def post(request, slug, id_): # pylint: disable=unused-argument """Method resposible for saving changed offer. :param request: WSGIRequest instance :param slug: string Offer title slugified :param id_: int Offer database unique identifier (primary key) """ offer = Offer.objects.get(id=id_) if request.POST.get('submit') == 'save_image' and request.FILES: form = OfferImageForm(request.POST, request.FILES) if form.is_valid(): offer.save_offer_image(form.save(commit=False), form.cleaned_data['is_main']) messages.success(request, "Dodano zdjęcie do galerii.") else: messages.error( request, "Problem w trakcie dodawania grafiki: {}".format( "<br />".join(form.errors))) return redirect( reverse('offers_edit', args=[slugify(offer.title), offer.id])) elif request.POST.get('close_offer') == 'close': offer.close_offer() return redirect( reverse('offers_view', args=[slugify(offer.title), offer.id])) elif request.POST.get('status_flag') == 'change_status': if request.POST.get('status') == 'published': offer.publish() if request.user.userprofile.is_administrator: return redirect('offers_reorder', offer.id) elif request.POST.get('status') == 'rejected': offer.reject() return redirect('offers_list') form = CreateOfferForm(request.POST, instance=offer) if form.is_valid(): offer = form.save() offer.unpublish() offer.save() save_history(request, offer, action=CHANGE) messages.success(request, "Oferta została zmieniona.") else: messages.error( request, "Formularz zawiera niepoprawnie wypełnione pola: {}".format( "<br />".join(form.errors))) if offer.id or request.user.userprofile.is_administrator: organizations = [offer.organization] else: organizations = request.user.userprofile.organizations.all() return render( request, 'offers/offer_form.html', { 'offer': offer, 'form': form, 'organizations': organizations, 'offer_image_form': OfferImageForm(), })
def get(request, slug, id_): # pylint: disable=unused-argument """Method responsible for rendering form for offer to be changed. :param request: WSGIRequest instance :param slug: string Offer title slugified :param id_: int Offer database unique identifier (primary key) """ offer = Offer.objects.get(id=id_) if offer.id or request.user.userprofile.is_administrator: organizations = [offer.organization] else: organizations = request.user.userprofile.organizations.all() return render( request, 'offers/offer_form.html', { 'offer': offer, 'offer_form': CreateOfferForm(), 'organization': offer.organization, 'organizations': organizations, 'offer_image_form': OfferImageForm(), 'images': OfferImage.objects.filter(offer=offer).all(), 'MEDIA_URL': settings.MEDIA_URL, })
def post(request, slug, id_): # pylint: disable=unused-argument u"""Method resposible for saving changed offer. :param request: WSGIRequest instance :param slug: string Offer title slugified :param id_: int Offer database unique identifier (primary key) """ offer = Offer.objects.get(id=id_) if request.POST.get('submit') == 'save_image' and request.FILES: form = OfferImageForm(request.POST, request.FILES) if form.is_valid(): offer.save_offer_image( form.save(commit=False), request.user.userprofile, form.cleaned_data['is_main'] ) messages.success(request, u"Dodano zdjęcie do galerii.") else: messages.error( request, u"Problem w trakcie dodawania grafiki: {}".format( '<br />'.join(form.errors) ) ) return redirect( reverse( 'offers_edit', args=[slugify(offer.title), offer.id] ) ) elif request.POST.get('close_offer') == 'close': offer.close_offer() return redirect( reverse( 'offers_view', args=[slugify(offer.title), offer.id] ) ) elif request.POST.get('status_flag') == 'change_status': if request.POST.get('status') == 'published': offer.publish() if request.user.userprofile.is_administrator: return redirect('offers_reorder', offer.id) elif request.POST.get('status') == 'rejected': offer.reject() return redirect('offers_list') form = CreateOfferForm(request.POST, instance=offer) if form.is_valid(): offer = form.save() offer.unpublish() offer.save() save_history(request, offer, action=CHANGE) messages.success(request, u"Oferta została zmieniona.") else: messages.error( request, u"Formularz zawiera niepoprawnie wypełnione pola: {}".format( '<br />'.join(form.errors) ) ) if offer.id or request.user.userprofile.is_administrator: organizations = [offer.organization] else: organizations = request.user.userprofile.organizations.all() return render( request, 'offers/offer_form.html', { 'offer': offer, 'form': form, 'organizations': organizations, 'offer_image_form': OfferImageForm(), } )