Examples of ModelAdmin


Examples of javango.contrib.admin.api.ModelAdmin

    Map<String, List<ModelDefinition>> packageMap = new HashMap<String, List<ModelDefinition>>();   
   
    Iterator<Class> i = options.getClassMappings();
    while (i.hasNext()) {
      Class pc = i.next();
      ModelAdmin ma = options.getModelAdmin(pc);
      if (ma.isAuthorized(request.getUser())) {
        String className = pc.getName();
        String packageName =  className.substring(0,className.lastIndexOf("."));
        if (log.isDebugEnabled()) log.debug ("Adding class: " + className);
        if (packageMap.containsKey(packageName)) {
          List<ModelDefinition> appList = packageMap.get(packageName);
View Full Code Here

Examples of javango.contrib.admin.api.ModelAdmin

  public HttpResponse search(HttpRequest request, String model) throws HttpException {
    Class pc = options.getClassMapping(model);
    if (pc == null) {
      throw new Http404("Class not found");
    }
    ModelAdmin ma = options.getModelAdmin(pc);
   
    if (!ma.isReader(request.getUser())) {
      throw new Http404("Class not found, or not authorized");
    }
   
    String[] property_list = ma.getListSearchFields();     

    Form form = ma.getSearchForm();

    if (form == null) {
      String[] includeFields = property_list == null ? getIncludeFields(pc) : property_list;

      ModelForm<?> mForm = new ModelForm(fieldFactory, hibernateUtil, managers);
      mForm.setInclude(includeFields);
      mForm.setModel(pc);

      for (Field f : mForm.getFields().values()) {
        f.setRequired(false).setAllowNull(true);
        f.setEditable(true);
        if (f.getClass().equals(CharField.class)) {
          f.setName(f.getName() + "__ilike");
        } else if (f.getClass().equals(DateField.class)) {
          f.setName(f.getName() + "__date");
        } else {
          f.setName(f.getName() + "__eq");
        }
      }
     
      form = mForm;
    }
   

   
    if ("POST".equals(request.getMethod())) {
      form.bind(request.getParameterMap());
      if (form.isValid()) {
        // TODO better way to get the relative 'admin' part of the url
        Map<String, String> params = new HashMap<String, String>();
        generateSearchParams(request, params);
        return new HttpResponseRedirect("../", params);
      }
    }
   
    Map<String, Object> context = new HashMap<String, Object>();
    context.put("form", form);
    context.put("jquery", jqueryProvider.get().setBasePath(request.getContext()).forForm(form));

    updateContext(context, pc, ma);
   
    context.put("page_title", String.format("%s Search", findVerboseName(pc, ma)));
    return renderToResponse(ma.getSearchFormTemplate(), context);

  }
View Full Code Here

Examples of javango.contrib.admin.api.ModelAdmin

    try {
      Class pc = options.getClassMapping(model);
      if (pc == null) {
        throw new Http404("Class not found");
      }
      ModelAdmin ma = options.getModelAdmin(pc);
     
      if (!ma.isReader(request.getUser())) {
        throw new Http404("Class not found, or not authorized");
      }
     
      Manager<?> manager = findManager(pc, ma);

      String[] property_list = ma.getListDisplay();
      List<String> header_list = new ArrayList<String>();
      for(String property : property_list) {
        header_list.add(getChangeListLabel(pc, ma, property));
      }
     
      Map<String, String> searchParams = new HashMap<String, String>();
      for(Entry<String, String[]> e : request.getParameterMap().entrySet()) {
        if (e.getKey().contains("__")) {
          String value = e.getValue() == null ? null : e.getValue().length == 0 ? null : e.getValue()[0];
          searchParams.put(e.getKey(), value);
        }
      }
     
      QuerySet<?> qs = manager.filter(searchParams);
      String[] orderBy = ma.getOrderBy();
      if (orderBy == null) {
        qs = qs.orderBy(manager.getPkProperty());
      } else {
        qs = qs.orderBy();
      }
      String tool = request.getParameter("tool");
      // an action has been selected or clicked
      if (tool != null) {
        for (AdminAction action : ma.getListTools()) {
          if (tool.equals(action.getClass().getName())) {
            return action.execute(ma, request, qs);
          }
        }
      }
     
      QuerySetPaginator<?> paginator = new QuerySetPaginator(qs, 30);

      String query_string = generateSearchParams(request, searchParams);

      QuerySetPage<?> page = paginator.getPage(getPageNumber(request));
      List<?> object_list = page.getObjectList();
     
      Map<String, Object> context = new HashMap<String, Object>();
      context.put("header_list", header_list);
      context.put("property_list", property_list);
      context.put("object_list", object_list);
      context.put("key_property", manager.getPkProperty());
      context.put("link_property", ma.getListDisplayLinks() == null ? property_list[0] : ma.getListDisplayLinks());
      context.put("paginator", new PaginatorWidget(paginator, getPageNumber(request), query_string));
      context.put("query_string", query_string);
      context.put("list_tools", ma.getListTools());
      updateContext(context, pc, ma);
         
      context.put("page_title", String.format("Select %s to change", findVerboseName(pc, ma)));     
      return renderToResponse(ma.getChangeListTemplate(), context);
    } catch (ManagerException e) {
      throw new HttpException(e);
    }
  }
View Full Code Here

Examples of javango.contrib.admin.api.ModelAdmin

      // TODO Changet object_id to an Object and figure out the correct datatype from the dao. dont' think this is necessary
      Class pc = options.getClassMapping(model);
      if (pc == null) {
        throw new Http404("Class not found");
      }
      ModelAdmin ma = options.getModelAdmin(pc);
     
      if (object_id != null && !ma.isEditor(request.getUser())) {
        throw new Http404("Class not found, or not authorized");
      } else if (!ma.isAuthor(request.getUser())) {
        throw new Http404("Class not found, or not authorized");
      }
     
      Manager manager = findManager(pc, ma);
     
      Object object = null;

      if (object_id != null) {
        object = manager.get(object_id);
        if (object == null) {
          return new SimpleHttpResponse("Object not found");
        }
      }

      String[] property_list = ma.getFields();     

      Form form = ma.getForm();
     
      if (form == null) {
        String[] includeFields = property_list == null ? getIncludeFields(pc) : property_list;
 
        ModelForm mForm = new ModelForm(fieldFactory, hibernateUtil, managers);
        if (includeFields != null) mForm.setInclude(includeFields);
        mForm.setModel(pc);
        form = mForm;
      }
     
      if ("POST".equals(request.getMethod())) {
        // update the object
        if (object == null) object=injector.getInstance(pc);
       
        form.bind(request.getParameterMap());
        // form = modelFactory.form(pc.getMappedClass(), request.getParameterMap());
        if (form.isValid()) {
//          if (object_id == null) { // this is a new object
//            if (!bl.canCreate()) {
//              throw new HttpException("Unable to create");
//            }
//          } else {
//            if (!bl.canUpdate(object)) {
//              throw new HttpException("Unable to updte");
//            }
//          }
         
          form.clean(object);
          try {
            if (object_id == null) {
              manager.create(object);
            } else {
              manager.save(object);
            }
           
            if (request.getParameterMap().containsKey("_addanother")) {
              return new HttpResponseRedirect("../add");
            } else if (request.getParameterMap().containsKey("_continue")) {
              return new HttpResponseRedirect(String.format("../%s/", manager.getPk(object)));
            }
            return new HttpResponseRedirect("..");
          } catch (ManagerException e) {
            request.getSession().addError(e.getMessage());
          }
        }
      } else {
        // display the form
        if (object != null) form.setInitial(object);
      }
         
      Map<String, Object> context = new HashMap<String, Object>();
      context.put("property_list", property_list);
      context.put("object", object);
      context.put("form", form);
      context.put("object_id", object_id);
      context.put("jquery", jqueryProvider.get().setBasePath(request.getContext()).forForm(form));
      updateContext(context, pc, ma);
         
      context.put("page_title", String.format("Change %s", findVerboseName(pc, ma)));
      return renderToResponse(ma.getChangeFormTemplate(), context);
    } catch (Exception e) {
      log.error(e,e);
      throw new HttpException(e);
    }
  }
View Full Code Here

Examples of javango.contrib.admin.api.ModelAdmin

      Class pc = options.getClassMapping(model);
      if (pc == null) {
        throw new Http404("Class not found");
      }
      ModelAdmin ma = options.getModelAdmin(pc);
     
      Manager manager = findManager(pc, ma);

      Object object = manager.get(object_id);
      if (object == null) {
        throw new Http404("Not found"); // TODO 404
      }
 
//      if (!bl.canDelete(object)) {
//        throw new HttpException("Unauthorized to delete");
//      }
       
      if ("POST".equals(request.getMethod())) {
        try {
          manager.delete(object);
          return new HttpResponseRedirect("../..");
        } catch (ManagerException e) {
          request.getSession().addError(e.getMessage());
        }
      }
     
      Map<String, Object> context = new HashMap<String, Object>();
      context.put("object", object);
      updateContext(context, pc, ma);
     
      context.put("page_title", "Are you sure?");
      return renderToResponse(ma.getConfirmDeleteTemplate(), context);
    } catch (Exception e) {
      log.error(e,e);
      throw new HttpException(e);
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.