def campaign_edit(request, object_id): #done if request.user.has_perm('agents.super'): campaign = Campaign.objects.get(pk=object_id) # gets the campaign idea that you want to edit if request.method == 'POST': form = CampaignForm(request.POST, instance=campaign) # if the request method is post, gets the form data from the campaign and updates it with the new data if form.is_valid(): form.save() # saves the form return HttpResponseRedirect('/campaign/'+object_id) # redirects back to the object that was juse edited else: form = CampaignForm(instance=campaign) # passes the campaign into the form maker template = 'campaign/edit.html' context = RequestContext(request, {'form':form, 'object_id':object_id, 'campaign':campaign}) response = render_to_response(template, context) return response else: currentUrl = request.get_full_path() template = 'general/badpermissions.html' context = RequestContext(request, {'currenturl':currentUrl}) response = render_to_response(template, context) return response
def create_edit(request, campaign_id=None): 'Create a new campaign or edit an existing one' geuser = request.user.get_profile() campaign = None if campaign_id: campaign = get_object_or_404(Campaign, pk=campaign_id) form = CampaignForm(geuser.organization, instance=campaign) if request.method == 'POST': form = CampaignForm(geuser.organization, request.POST, instance=campaign) if form.is_valid(): form.save() if campaign: request.user.message_set.create(message='Campaign updated') else: request.user.message_set.create(message='New campaign created') return HttpResponseRedirect( reverse('campaign_list') ) return render_to_response( 'campaign/form.html', {'form': form, 'campaign': campaign}, context_instance = RequestContext(request))
def campaignEdit(request, campaign_id): """Edit a Campaign instance.""" campaign = get_object_or_404(Campaign, pk=campaign_id) if campaign.owner_id != request.user.pk: messages.error(request, "You cannot edit a campaign you do not own.") return HttpResponseRedirect(reverse('home')) if request.method == 'POST': address_form = AddressForm(request.POST, instance=campaign.address) campaign_form = CampaignForm(request.POST, instance=campaign) if address_form.is_valid() and campaign_form.is_valid(): campaign_form.instance.address = address_form.get_or_create() campaign_form.save() messages.success(request, 'You updated "{0}."'.format(campaign.name)) return HttpResponseRedirect( reverse('campaign_edit', args=[campaign_id])) else: address_form = AddressForm(instance=campaign.address) campaign_form = CampaignForm(instance=campaign) return render( request, 'campaign/edit.html', { 'address_form': address_form, 'campaign_form': campaign_form, 'campaign': campaign })
def campaignCreate(request): """Create a new Campaign instance if the user does not already own a campaign.""" if request.method == 'POST': login_form = AuthenticationForm(request, data=request.POST) campaign_form = CampaignForm(request.POST) address_form = AddressForm(request.POST) if login_form.is_valid(): # The credentials are valid, and the user is active. AuthenticationForm.clean() calls # 'authenticate' and 'confirm_login_allowed'. It saves the authenticated user as the # 'user_cache' attribute. if getattr(login_form.user_cache, 'campaign', None): messages.error(request, 'You may not create more than one campaign.') return HttpResponseRedirect(reverse('login')) if campaign_form.is_valid() and address_form.is_valid(): campaign_form.instance.address = address_form.get_or_create() campaign_form.instance.owner = login_form.user_cache campaign_form.save() messages.success(request, 'You created a new campaign.') login(request, login_form.user_cache) return HttpResponseRedirect(reverse('home')) else: login_form = AuthenticationForm(request) campaign_form = CampaignForm() address_form = AddressForm() return render( request, 'campaign/create.html', { 'login_form': login_form, 'campaign_form': campaign_form, 'address_form': address_form })
def campaign_new(request): if request.method == 'POST': campaign_form = CampaignForm(data=request.POST) image_form = CampaignImageForm(data=request.POST, files=request.FILES) files = request.FILES.getlist('images') if campaign_form.is_valid() and image_form.is_valid(): campaign = campaign_form.save(commit=False) campaign.author = request.user campaign.save() for file in files: CampaignImage.objects.create(image=file, campaign=campaign) messages.success( request, f'Congratulations! Your campaign \"{campaign.name}\" successfully created!' ) return redirect('campaign:campaign_detail', campaign.author_slug, campaign.name_slug) else: campaign_form = CampaignForm() image_form = CampaignImageForm() return render(request, 'campaign/new.html', { 'campaign_form': campaign_form, 'image_form': image_form })
def campaign_edit(request, author_slug, name_slug): campaign = get_object_or_404(Campaign, author_slug=author_slug, name_slug=name_slug) campaign_images = campaign.images.all() if campaign.author != request.user and not request.user.is_superuser: return HttpResponseForbidden() # raise Http404() campaign_form = CampaignForm(data=request.POST or None, instance=campaign) if request.method == 'POST': image_form = CampaignImageForm(data=request.POST, files=request.FILES) if campaign_form.is_valid() and image_form.is_valid(): campaign_form.save() files = request.FILES.getlist('images') for file in files: CampaignImage.objects.create(image=file, campaign=campaign) return redirect('campaign:campaign_detail', author_slug=campaign.author_slug, name_slug=campaign.name_slug) else: images_dict = {'images': [image.image for image in campaign_images]} image_form = CampaignImageForm(data=None, files=MultiValueDict(images_dict)) return render( request, 'campaign/new.html', { 'campaign_form': campaign_form, 'image_form': image_form, 'campaign': campaign, 'campaign_images': campaign_images })
def campaign_add(request): #done if request.user.has_perm('agents.super'): if request.method == 'POST': form = CampaignForm(request.POST) if form.is_valid(): form.save() #saves the returned campaign add form return HttpResponseRedirect('/campaign/') else: form = CampaignForm() template = 'campaign/add.html' context = RequestContext(request, {'form':form}) response = render_to_response(template, context) return response else: currentUrl = request.get_full_path() template = 'general/badpermissions.html' context = RequestContext(request, {'currenturl':currentUrl}) response = render_to_response(template, context) return response
def CampaignNew(request): if request.method == 'POST': form = CampaignForm(request.POST) if form.is_valid(): campaign = form.save(commit=False) campaign.set_user(request.user) campaign.save() return HttpResponseRedirect(reverse('campaign_csv')) else: form = CampaignForm() return render(request,'campaign_new.html',{'form':form})
def CampaignEdit(request): id = request.GET.get('camp_id','') campaign = Campaign.objects.get(id=id) if campaign.get_user() == request.user: if request.method == 'POST': form = CampaignForm(request.POST) if form.is_valid(): campaign.set_data(form.save(commit=False)) return HttpResponseRedirect(reverse('campaign_csv',kwargs={'campaign_id':campaign.id})) else: form = CampaignForm(initial=campaign) return render(request,'campaign_new.html',{'form':form}) else: raise Http404
def test_campaignForm_invalid(self): form = CampaignForm(invalid_data) self.assertFalse(form.is_valid())
def test_campaignForm_valid(self): form = CampaignForm(valid_data) self.assertTrue(form.is_valid())