Example #1
0
  def resolve_entity(self, analyzed_app):
    """Links the fields in the form dict to the actual model fields"""

    self.model = analyzed_app.models.get_by_name(self.entity)
    assert self.model is not None, "Model not found: %s" % self.entity

    # these are the fields from the form dict
    self.action = self.action
    f_manager = Manager(Field)
    # these are the fields from self.model
    f_manager._objects = self.model.fields
    for f in self.included_fields:
      f_check = f_manager.get_by_name(f.name)
      if f_check is None:
        assert f.name in ['password1','password2', 'password'], "ruh roh, field called %s is not an actual field in the model" % f.name
      f.model_field = f_check
Example #2
0
class AnalyzedApp:

  def __init__(self, app_state):
    # create "managers" which will help a dev find things inside lists
    logger.info("Converting app state to analyzed app.")
    self.models = Manager(Model)
    self.pages = Manager(Page)
    self.routes = Manager(Route)

    logger.debug("Adding user fields to the analyzed app.")
    base_user = { "name": "User" }
    if app_state['users']['local']:
      self.local_login = True
      base_user['fields'] = app_state['users']['fields']
      assert len([ f for f in base_user['fields'] if f['name'].lower() in ['username', 'email', 'First Name', 'Last Name']]) == 0, "Plz get rid of username field from user fields."
      base_user['fields'].append(simplejson.loads(r"""{"name": "username","required": true,"type": "text"}""")) # adds username
      base_user['fields'].append(simplejson.loads(r"""{"name": "email","required": true,"type": "text"}"""))
      base_user['fields'].append(simplejson.loads(r"""{"name": "First Name","required": true,"type": "text"}"""))
      base_user['fields'].append(simplejson.loads(r"""{"name": "Last Name","required": true,"type": "text"}"""))
      m = Model.create_user(base_user)
      self.models.add(m)

    if app_state['users']['facebook']:
      self.facebook_login = True

    logger.debug("Adding entities to the analyzed app.")
    for ent in app_state['entities']:
      m = Model.create(ent)
      self.models.add(m)
    logger.debug("Initing entities.")
    self.init_models()

    # create pages from app_state pages
    logger.debug("Adding pages to the analyzed app.")
    for p in app_state['pages']:
      page = Page(p)
      self.pages.add(page)
      logger.debug("Adding route for a page.")
      u = p['url']
      r = Route(u, page)
      self.routes.add(r)
      # this shouldn't be here, but who cares
      if page.navbar_brandname is None:
        logger.debug("Navbar set to app name.")
        page.navbar_brandname = app_state['name']
      # (ensures all navbar brandnames are strings)

    logger.debug("Resolving internal links in navbar.")
    for p in self.pages.each():
      p.resolve_navbar_pages(self)

    logger.debug("Resolving the models in URLs.")
    self.link_models_to_routes()
    logger.debug("Resolving the routes and pages.")
    self.link_routes_and_pages()
    logger.debug("Resolving links in nodes.")
    self.fill_in_hrefs()
    logger.debug("Resolving forms: entity, required fields, goto page.")
    self.init_forms()
    logger.debug("Resolving entities in the lists and tables.")
    self.init_queries(app_state)

  # link routes and models
  def link_models_to_routes(self):
    for r in self.routes.each():
      r.models = Manager(Model)

      for idx, u in enumerate(r.urlparts):
        # if this string resembles a model type thing, replace it with a model
        if utils.extract_from_brace(u) is not None:
          m = self.models.get_by_name(utils.extract_from_brace(u))
          r.urlparts[idx] = m
          r.models.add(m)

  # interlink routes and pages
  def link_routes_and_pages(self):
    for r in self.routes.each():
      r.page = self.pages.get_by_name(r.name)
      r.page.route = r

  def fill_in_hrefs(self):
    for p in self.pages.each():
      for uie in p.uielements:
        uie.resolve_links(self.pages)

  def init_models(self):
    for m in self.models.each():
        for f in m.fields:
          f.resolve_model_if_list_of_blah(self)

  def init_forms(self):
    self.forms = Manager(Form)
    for p in self.pages.each():
      for uie in p.uielements:
        if isinstance(uie, Form):
          uie.resolve_entity(self)
          uie.check_required_fields()
          uie.resolve_goto_page(self)

          # hack to make form names unique
          form_counter = 0
          def make_unique(name, level=0):
            if name != "" and self.forms.get_by_name(name) is None:
              return name
            elif level == 0:
              name += " " + uie.action
              return make_unique(name, level=1)
            elif level == 1:
              name += uie.entity
              return make_unique(name, level=2)
            elif level == 2:
              name += " on " + p.name + " page"
              return make_unique(name, level=3)
            else:
              name += str(form_counter)
              form_counter += 1
              return name

          uie.name = make_unique(uie.name)
          print "I just named my form "+  uie.name
          self.forms.add(uie)

  def init_queries(self, app_state):
    for p in self.pages.each():
      p.queries = Manager(QuerysetWrapper)
      for uie in p.uielements:
        if isinstance(uie, QuerysetWrapper):
          uie.resolve_entity(self)
          p.queries.add(uie)