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 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) )