def test_experience_form_submit(self): exp_form_context = {'title': self.experience_title} exp_form = ExperienceForm(exp_form_context, author=self.explorer) self.assertTrue(exp_form.is_valid()) experience = exp_form.save() self.assertTrue(experience in self.explorer.experiences.all()) self.assertTrue(experience.author == self.explorer)
def add_experience(request): status = '' if request.method == 'POST': data = request.POST if not len(data['title']): status = 'You must enter a title for the experience, otherwise how can people know how to find you?' elif len(data['title']) > 140: status = 'Title cannot be longer than 140 characters' else: pass if not len(data['description']): status = 'Please enter a description, so people can get a sense of the experience you are offering' if len(data['description']) > 2000: status = 'Please limit description to 2000 characters, to improve the chances that people will actually read it' # if 'category' not in data: # status = 'You must choose one category' if data.get('use_saved_address') == 'on': pass # here I should assign the merchant's address to the experience... # not sure how to do it b/c the experience is still not in created in Mongo... elif not len(data['address']): # are these supposed to be elif? status = 'Please enter a valid address' # elif len(data['city']) == 0: # status = 'Please enter city' # elif len(data['state']) == 0: # status = 'Please select state' # elif len(data['zipcode']) == 0: # status = 'Please enter zip-code' # elif data['country'] is None: # status = 'You must select a country' # else: # pass if not len(data['price']): status = 'Please enter price' #elif data['price'] is not a number: # status = 'Price must be a number' #elif data['price'] > 500000: # status = 'Price must be between $1 and $500,000. If you want to list a really expensive experience please contact us at [email protected]' if len(request.FILES) > 0: data['merchant'] = request.merchant # experience = Experience(**data) # experience.update_location(float(request.POST.get('lat',0.0)), float(request.POST.get('lng',0.0))) # experience.save() # experience = create_experience(experience_collection, **data) # for k, v in request.FILES.iteritems(): # add_image(v, experience) else: status = 'you must upload at least one image' new_object = Experience(merchant=request.merchant) form = ExperienceForm(request.POST,request.FILES,instance=new_object) if form.is_valid() and status == '': new_object = form.save() return redirect(reverse(experience_profile,kwargs = {'id':new_object.slug_id})) else: return render_to_response('experiences/add_experience.html',context_instance=RequestContext(request, {'form':form, 'status':status})) else: form = ExperienceForm() return render_to_response('experiences/add_experience.html',context_instance=RequestContext(request, {'form':form}))
def edit_experience(request, slug_id): exp = Experience.objects.get(slug_id=slug_id) if request.method == 'GET': form = ExperienceForm(instance=exp) return render_to_response('merchants/edit_experience.html', context_instance=RequestContext(request,{'form' : form}) ) else: form = ExperienceForm(request.POST,request.FILES,instance=exp) if form.is_valid(): form.save() return redirect(reverse('merchant_experiences')) else: return render_to_response('merchants/edit_experience.html', context_instance=RequestContext(request,{'form' : form}) )
def step_two(request): request.session['signing_up'] = 0 if request.method == 'POST': exp_form = ExperienceForm(request.POST) if exp_form.is_valid(): # Store the data in case they wish to peruse for a bit request.session['experience_title'] = exp_form.cleaned_data['title'] request.session['signing_up'] = 1 reg_form = RegistrationForm() return render( request, 'registration/step_two.html', { 'experience_title': exp_form.cleaned_data['title'], 'form': reg_form } ) return redirect('/') return redirect(request.META['HTTP_REFERER'])
def create(request): if request.method == 'POST': form = ExperienceForm(request.POST, request=request, author=request.user) if form.is_valid(): new_experience = form.save() if form.cleaned_data['make_feature']: request.user.featured_experience = form.instance request.user.save() messages.success( request, _('Your featured experience is now {0}'.format( form.instance)) ) elif form.cleaned_data['unfeature']: request.user.featured_experience = None request.user.save() # Notify me so I can congratulate them personally! to = settings.ADMINS[0][1] from_email = '*****@*****.**' subject = 'New Experience!' text_content = '{0} has created a new experience: {1}!'.format(request.user, new_experience) message = EmailMultiAlternatives(subject, text_content, from_email, [to]) message.send() if 'ajax' in request.POST: html = '<hr />' html += render_to_string( 'experiences/snippets/dash.html', { 'experience': new_experience, 'user': request.user, 'STATIC_URL': settings.STATIC_URL } ) data = {'html': html} return HttpResponse(json.dumps(data)) messages.success(request, _('Experience successfully added')) return redirect(reverse('experience', args=(new_experience.id,))) else: form = ExperienceForm() return render(request, 'experiences/create.html', {'form': form})
def edit(request, experience_id): experience = get_object_or_404(Experience, pk=experience_id) if request.user in experience.explorers.all(): if request.method == 'POST': form = ExperienceForm( request.POST, instance=experience, request=request ) if form.is_valid(): if form.cleaned_data['make_feature']: request.user.featured_experience = form.instance request.user.save() messages.success( request, _('Your featured experience is now {0}'.format(form.instance))) elif form.cleaned_data['unfeature']: request.user.featured_experience = None request.user.save() if 'is_public' in request.POST: if experience.author != request.user: raise PermissionDenied form.save() messages.success( request, _('Experience has been successfully edited') ) for comrade in experience.comrades(exclude=request.user): notify.send(sender=request.user, recipient=comrade, target=experience, verb='has edited your shared experience') return redirect(reverse('experience', args=(experience.id,))) else: form = ExperienceForm(instance=experience) return render(request, 'experiences/edit.html', {'form': form, 'experience': experience}) else: raise PermissionDenied