def rack_edit(request,rack_id): rack = get_object_or_404(Rack, id=rack_id) form = RackForm() if rack.locked: flash_error('This rack is locked and cannot be edited.', request) elif request.method == 'POST': # For now, preserve the original creator. request.POST[u'email'] = rack.email request.POST[u'user'] = rack.user _preprocess_rack_form(request.POST) form = RackForm(request.POST, request.FILES, instance=rack) if form.is_valid(): x = form.save() flash('Your changes have been saved.', request) return HttpResponseRedirect( urlresolvers.reverse(rack_edit, kwargs={'rack_id': rack.id})) else: flash_error('Please correct the following errors.', request) # Who created this rack? if rack.user == request.user.username or rack.email == request.user.email: creator = rack.user else: # Don't reveal email address to other users. # Instead show a username, or a truncated address if submitted # anonymously. creator = rack.user or "anonymous" # (%s@...)" % (rack.email.split('@', 1)[0])) return render_to_response('update_rack.html', {"rack": rack, "form": form , "creator": creator, }, context_instance=RequestContext(request))
def _add_comment(request, rack): # Simplified and hacked comment post function to change various things: # # 0. Skip all the model checking stuff as we assume we're just # working with racks. # # 1. get client IP address into the POST data, to make recaptcha happy. if '__recaptcha_ip' in request.POST: return HttpResponseBadRequest() data = request.POST.copy() data['__recaptcha_ip'] = request.META['REMOTE_ADDR'] # 2. Don't use a separate preview form to display errors. # Just validate and leave display up to the calling function. if request.user.is_authenticated(): # We must first prepopulate logged-in user data; # copy-pasted from post_comment() if not data.get('name', ''): data["name"] = request.user.get_full_name() or request.user.username if not data.get('email', ''): data["email"] = request.user.email form = ReCaptchaCommentForm(rack, data, need_captcha= not request.user.is_authenticated()) if form.is_valid(): flash(u"Your comment has been saved", request) comment = form.get_comment_object() comment.save() else: flash_error(u"Please correct errors in your comment", request) return form
def bulk_order_add_form(request): cb = None form = BulkOrderForm() if request.method == 'POST': cb_gid = request.POST.get('cb_gid') post = request.POST.copy() post[u'communityboard'] = cb_gid post[u'user'] = request.user.pk form = BulkOrderForm(post) if form.is_valid(): bulk_order = NYCDOTBulkOrder.objects.filter(communityboard=cb_gid) if bulk_order: flash_error("There is already a bulk order for this CB.", request) else: bulk_order = form.save() if request.user.has_perm('bmabr.add_nycdotbulkorder'): flash("Bulk order created! Follow the directions below to complete it.", request) bulk_order.approve() bulk_order.save() return HttpResponseRedirect( urlresolvers.reverse(bulk_order_edit_form, kwargs={'bo_id': bulk_order.id})) else: flash("Thanks for your request. " "The site admins will check out your request and " "get back to you soon!", request ) subject = 'New bulk order request needs approval' approval_uri = request.build_absolute_uri( urlresolvers.reverse( bulk_order_approval_form, args=[bulk_order.id])) body = """ A new bulk order has been submitted. To approve this user and this order, go to: %s """ % approval_uri send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, settings.BULK_ORDER_APPROVAL_EMAIL, fail_silently=False) return HttpResponseRedirect(urlresolvers.reverse(blank_page)) else: flash_error('Please correct the following errors.', request) return render_to_response( 'bulk_order_add_form.html', {'request': request, 'cb': cb, 'form': form, }, context_instance=RequestContext(request) )
def newrack_form(request): if request.method == 'POST': _preprocess_rack_form(request.POST) result = _newrack(request.POST, request.FILES) form = result['form'] if not result['errors']: message = '''<h2>Thank you for your suggestion!</h2><p>Racks can take six months or more for the DOT to install, but we\'ll be in touch about its progress.</p><a href="/racks/new/">Add another rack</a> or continue to see other suggestions.''' flash(message, request) return HttpResponseRedirect(urlresolvers.reverse(racks_index)) else: flash_error('Please correct the following errors.', request) else: form = RackForm() return render_to_response('newrack.html', { 'form': form, }, context_instance=RequestContext(request))
def bulk_order_edit_form(request, bo_id): if not request.user.has_perm('bmabr.add_nycdotbulkorder'): error = """Only approved users can edit bulk orders. Please <a href="/contact/">contact us</a> to ask for approval.""" flash_error(error, request) url = '%s?%s' % ( urlresolvers.reverse('django.contrib.auth.views.login'), urllib.urlencode({'next': request.get_full_path()})) return HttpResponseRedirect(url) bulk_order = get_object_or_404(NYCDOTBulkOrder, id=bo_id) cb = bulk_order.communityboard form = BulkOrderForm() if request.method == 'POST': post = request.POST.copy() next_state = post.get('next_state') if next_state == 'completed': # The DOT has apparently completed building this order. Yay!! flash(u'Marking bulk order as completed.', request) bulk_order.racks.update(status=next_state) bulk_order.status = next_state bulk_order.save() else: post[u'communityboard'] = post.get('cb_gid') post[u'user'] = request.user.pk form = BulkOrderForm(post) if form.is_valid(): form.save() else: flash_error('Please correct the following errors.', request) return render_to_response( 'bulk_order_edit_form.html', {'request': request, 'bulk_order': bulk_order, 'cb': cb, 'form': form, 'status': dict(form.fields['status'].choices).get(bulk_order.status), }, context_instance=RequestContext(request) )