Example #1
0
def create_tableaux_for_location(request, loc_code):
    """ Create the factory-based tableaux for the given location.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect(reverse(settings.ADMIN_HOME_VIEW))

    if request.method == "GET":
        # Ask the user to confirm the operation.
        return render_to_response("shared/templates/confirm.html",
                                  {'title'   : "Whenua Admin",
                                   'heading' : "Create Factory Tableaux",
                                   'message' : "Are you sure you want to " +
                                               "create a new set of factory " +
                                               "tableaux for this location?" +
                                               "  If you proceed, the " +
                                               "existing factory tableaux " +
                                               "will be overwritten.",
                                  },
                                  context_instance=RequestContext(request))
    elif request.POST['confirm'] != "1":
        # The user cancelled -> go back to the main tableau editor page.
        return HttpResponseRedirect(reverse("tableau_editor.views.select",
                                            args=[loc_code]))

    # If we get here, we're ready to create the factory tableau for this
    # location.  Load the location object into memory.

    try:
        location = Location.objects.get(code=loc_code)
    except Location.DoesNotExist:
        # Should never happen.
        return HttpResponseRedirect(reverse("tableau_editor.views.select",
                                            args=[loc_code]))

    # Build a list of tableau factories we need to run.

    factories = factory_generator.calc_factories_for_location(location)

    # Create each tableau in turn, keeping track of any errors which occur
    # along the way.

    results = []
    for factory in factories:
        err_msg = factory_generator.create_tableau(factory, location)
        results.append({'factory'  : factory,
                        'location' : location,
                        'err_msg'  : err_msg})

    # Finally, display the results to the caller.

    menu_html = menus.generate(request, "Tableau Editor",
                               "tableau_editor", "create_factory")

    return render_to_response("tableau_editor/templates/" +
                              "create_factory_results.html",
                              {'menu_html' : menu_html,
                               'results'   : results,
                               'loc_code'  : loc_code,
                              },
                              context_instance=RequestContext(request))
Example #2
0
def create_factory_tableau(request):
    """ Create a single tableau for a given location and tableau factory.

        This private API call is called by the daemon to generate each
        requested tableau in turn.

        Note that we simply return the string "OK" back to the caller, even if
        an error occurs.  Any errors which we encounter are added to the
        TableauFactoryGenerationError table for further processing.
    """
    err_msg = None

    if request.method == "GET":
        params = request.GET
    elif request.method == "POST":
        params = request.POST
    else:
        params = {}

    factory_id = params.get("factory_id")
    loc_code   = params.get("loc_code")

    try:
        factory = TableauFactory.objects.get(id=factory_id)
    except TableauFactory.DoesNotExist:
        err_msg = "Missing tableau factory."

    if err_msg == None:
        try:
            location = Location.objects.get(code=loc_code)
        except Location.DoesNotExist:
            err_msg = "Missing location."

    if err_msg == None:
        err_msg = factory_generator.create_tableau(factory, location)

    if err_msg != None:
        # Remember this error so we can display it later.
        error = TableauFactoryGenerationError()
        error.factory_id = int(factory_id)
        error.loc_code   = loc_code
        error.err_msg    = err_msg
        error.save()

    # Whether or not there was an error, tell our caller that we processed the
    # request.

    return HttpResponse("OK", mimetype="text/plain")