public void generate(Project _p, PrintWriter _out, Options _o, LangManager _l)
      throws IOException, PythonDomainGenerationException {
    p = _p;
    m = p.getStaticModel();
    l = _l;
    o = _o;
    out = _out;
    out.println(
        nestedString()
            + commentSymbol()
            + l.getString("heading.python.admin.script")
            + p.getName()
            + commentSymbolEnd());
    out.println();
    Vector<Class> allClasses = m.getAllPersistentClasses();

    // importing the admin module

    out.println(nestedString() + "from django.contrib import admin" + commentSymbolEnd());

    // Generation of the admin information for each class

    // first iteration is used to generate the imports

    Iterator<Class> itClImp = allClasses.iterator();

    while (itClImp.hasNext()) {
      Class cl = itClImp.next();
      out.println(
          nestedString() + "from " + p.getName().toLowerCase() + ".models import " + cl.getName());
    }

    // second to generate the admin info for each class
    out.println();
    Iterator<Class> itCl = allClasses.iterator();
    while (itCl.hasNext()) {
      Class cl = itCl.next();
      generateClass(cl, out);
    }
    out.flush();
    out.close();
  }
  protected void generateClass(Class c, PrintWriter out)
      throws IOException, PythonDomainGenerationException {

    try {
      String nameAdminClass = c.getName() + "Admin";
      out.println(nestedString() + "class " + nameAdminClass + "(admin.ModelAdmin):");
      incNested();
      int numFieldsDisplay = new Integer(o.getProperty("django.admin.list.display")).intValue();
      if (numFieldsDisplay == 0) {
        out.println(nestedString() + "pass"); // To take all default options
      } else {
        String listFields = "list_display = (";
        Iterator<Attribute> itAt = c.getAtt().iterator();
        if (!(new Boolean(o.getProperty("django.admin.list.display.id")).booleanValue()))
          itAt.next();
        int i = 0;
        for (i = 0; i < numFieldsDisplay && itAt.hasNext(); i++) {
          listFields = listFields + "'" + itAt.next().getName() + "',";
        }
        if (i == 0)
          listFields = "pass"; // we simply ignore this parameter since there´s nothing to show
        if (i == 1) // Django expects a list so if it´s a single value we need to add with ",)";
        listFields = listFields + ")";
        if (i > 1) listFields = listFields.substring(0, listFields.length() - 1) + ")";
        out.println(nestedString() + listFields);
      }

      out.println();
      decNested();
      out.println(
          nestedString() + "admin.site.register(" + c.getName() + ", " + nameAdminClass + ")");
      out.println();
    } catch (Exception e) {
      throw new PythonDomainGenerationException("Error when creating the admin schema file");
    }
  }
Ejemplo n.º 3
0
  protected void processPackage(Package pack, Menu menu) {
    if (pack.getAllClasses().size() > 0) {
      MenuItem menuPack =
          new MenuItem(
              o.getProperty("app.menu.item.prefix")
                  + "Pack"
                  + pack.getName()
                  + o.getProperty("app.menu.item.suffix"));
      menuPack.setCaption(pack.getName());
      Iterator<Class> c = pack.getAllPersistentClasses().iterator();
      while (c.hasNext()) {
        Class class1 = (Class) c.next();
        // General item for all the forms associated to the class
        MenuItem mi =
            new MenuItem(
                o.getProperty("app.menu.item.prefix")
                    + class1.getName()
                    + o.getProperty("app.menu.item.suffix"));
        mi.setCaption(class1.getName());

        // Specific forms (and menu items) for each individual form
        MenuItem miVis =
            new MenuItem(
                o.getProperty("app.menu.item.prefix")
                    + "Vis"
                    + class1.getName()
                    + o.getProperty("app.menu.item.suffix"));
        Form fVis =
            new Form(
                o.getProperty("app.form.prefix")
                    + "Vis"
                    + class1.getName()
                    + o.getProperty("app.form.suffix"));
        fVis.setCaption(l.getString("app.caption.form.visualization") + class1.getName());
        fVis.setActionKind(FormActionKind.FA_VISUALIZE);
        fVis.setBaseClass(class1);
        d.addForm(fVis);
        mi.addItem(miVis);
        miVis.setDestination(fVis);
        miVis.setCaption(l.getString("app.caption.menu.item.visualize"));
        if (!class1
            .isReadOnly()) // For non-read only classes we create a form for inserting data and
                           // another for updating it
        {
          Form fIns =
              new Form(
                  o.getProperty("app.form.prefix")
                      + "Ins"
                      + class1.getName()
                      + o.getProperty("app.form.suffix"));
          fIns.setCaption(l.getString("app.caption.form.insert") + class1.getName());
          fIns.setActionKind(FormActionKind.FA_INSERT);
          fIns.setBaseClass(class1);
          d.addForm(fIns);
          fVis.addRelatedForm(fIns);
          if (o.getProperty("app.menu.item.insert")
              .equals("true")) // We create a separate menu entry for the insert form
          {
            MenuItem miIns =
                new MenuItem(
                    o.getProperty("app.menu.item.prefix")
                        + "Ins"
                        + class1.getName()
                        + o.getProperty("app.menu.item.suffix"));
            mi.addItem(miIns);
            miIns.setDestination(fIns);
            miIns.setCaption(l.getString("app.caption.menu.item.insert"));
          }

          Form fUpd =
              new Form(
                  o.getProperty("app.form.prefix")
                      + "Upd"
                      + class1.getName()
                      + o.getProperty("app.form.suffix"));
          fUpd.setCaption(l.getString("app.caption.form.update") + class1.getName());
          fUpd.setActionKind(FormActionKind.FA_UPDATE);
          fUpd.setBaseClass(class1);
          d.addForm(fUpd);
          fVis.addRelatedForm(fUpd);
        } else // if it is read only we just create a form to visualize the details of the row
        {
          Form fDet =
              new Form(
                  o.getProperty("app.form.prefix")
                      + "Det"
                      + class1.getName()
                      + o.getProperty("app.form.suffix"));
          fDet.setCaption(l.getString("app.caption.form.visdetails") + class1.getName());
          fDet.setActionKind(FormActionKind.FA_VISDETAILS);
          fDet.setBaseClass(class1);
          d.addForm(fDet);
          fVis.addRelatedForm(fDet);
        }
        if (o.getProperty("app.menu.groupby.package").equals("true")
            && !s.getPackages().get(0).getName().equals(o.getProperty("db.package.default.name"))) {
          menuPack.addItem(mi);
        } // We create submenu for each package
        else {
          menu.addItem(mi);
        }
      }
      if (o.getProperty("app.menu.groupby.package").equals("true")
          && !s.getPackages().get(0).getName().equals(o.getProperty("db.package.default.name"))) {
        menu.addItem(menuPack);
      }
    }
    if (pack.getFirstLevelPackages().size() > 0) {
      Iterator<Package> p = pack.getFirstLevelPackages().iterator();
      while (p.hasNext()) {
        Package package1 = (Package) p.next();
        processPackage(package1, menu);
      }
    }
  }