Ejemplo n.º 1
0
    def __get__(self, instance, instance_type):
        """??
        """
        if instance is None:
            return self

        cache = self.get_cache(instance)

        def get_obj(jeeves_id):
            """??
            """
            if jeeves_id is None:
                return None
            if jeeves_id not in cache:
                cache[jeeves_id] = self.field.to.objects.get(**{self.field.join_field.name: jeeves_id})
            return cache[jeeves_id]

        # TODO: Why do we need to facetMap this guy? If we know the viewer,
        # can we get rid of it?
        r = getattr(instance, self.field.get_attname())
        if not isinstance(JeevesLib.get_viewer(), FNull):
            robj = get_obj(r)
            if isinstance(robj, FObject):
                return robj.v
            else:
                return r
        else:
            return JeevesLib.facetMapper(fexpr_cast(r), get_obj)
Ejemplo n.º 2
0
  def delete(self, *args, **kw):
    if self.jeeves_id is None:
      return

    field_names = set()
    for field in self._meta.concrete_fields:
      if not field.primary_key and not hasattr(field, 'through'):
        field_names.add(field.attname)

    all_vars = []
    d = {}
    env = JeevesLib.jeevesState.pathenv.getEnv()
    for field_name in field_names:
      value = getattr(self, field_name)
      f = partialEval(fexpr_cast(value), env)
      all_vars.extend(v.name for v in f.vars())
      d[field_name] = f

    for p in powerset(all_vars):
      true_vars = list(p)
      false_vars = list(set(all_vars).difference(p))
      e = dict(env)
      e.update({tv : True for tv in true_vars})
      e.update({fv : False for fv in false_vars})

      delete_query = self.__class__._objects_ordinary.filter(jeeves_id=self.jeeves_id)
      for var_name, var_value in e:
        delete_query = delete_query.filter(jeeves_vars__contains =
              ';%s=%d;' % (var_name, var_value))
      delete_query.delete()
Ejemplo n.º 3
0
    def delete(self):
        if self.jeeves_id is None:
            return

        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, "through"):
                field_names.add(field.attname)

        all_vars = []
        field_dict = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            field_fexpr = fexpr_cast(value).partialEval(env)
            all_vars.extend(v.name for v in field_fexpr.vars())
            field_dict[field_name] = field_fexpr

        for var_set in JeevesModelUtils.powerset(all_vars):
            true_vars = list(var_set)
            false_vars = list(set(all_vars).difference(var_set))
            env_dict = dict(env)
            env_dict.update({tv: True for tv in true_vars})
            env_dict.update({fv: False for fv in false_vars})

            self.do_delete(env_dict)
Ejemplo n.º 4
0
  def delete(self, *args, **kw):
    if self.jeeves_id is None:
      return

    field_names = set()
    for field in self._meta.concrete_fields:
      if not field.primary_key and not hasattr(field, 'through'):
        field_names.add(field.attname)

    all_vars = []
    d = {}
    env = JeevesLib.jeevesState.pathenv.getEnv()
    for field_name in field_names:
      value = getattr(self, field_name)
      f = partialEval(fexpr_cast(value), env)
      all_vars.extend(v.name for v in f.vars())
      d[field_name] = f

    for p in powerset(all_vars):
      true_vars = list(p)
      false_vars = list(set(all_vars).difference(p))
      e = dict(env)
      e.update({tv : True for tv in true_vars})
      e.update({fv : False for fv in false_vars})

      self.do_delete(e)
Ejemplo n.º 5
0
    def delete(self, *args, **kw):
        if self.jeeves_id is None:
            return

        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, 'through'):
                field_names.add(field.attname)

        all_vars = []
        d = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            f = partialEval(fexpr_cast(value), env)
            all_vars.extend(v.name for v in f.vars())
            d[field_name] = f

        for p in powerset(all_vars):
            true_vars = list(p)
            false_vars = list(set(all_vars).difference(p))
            e = dict(env)
            e.update({tv: True for tv in true_vars})
            e.update({fv: False for fv in false_vars})

            self.do_delete(e)
Ejemplo n.º 6
0
    def delete(self):
        if self.jeeves_id is None:
            return

        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, 'through'):
                field_names.add(field.attname)

        all_vars = []
        field_dict = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            field_fexpr = fexpr_cast(value).partialEval(env)
            all_vars.extend(v.name for v in field_fexpr.vars())
            field_dict[field_name] = field_fexpr

        for var_set in JeevesModelUtils.powerset(all_vars):
            true_vars = list(var_set)
            false_vars = list(set(all_vars).difference(var_set))
            env_dict = dict(env)
            env_dict.update({tv: True for tv in true_vars})
            env_dict.update({fv: False for fv in false_vars})

            self.do_delete(env_dict)
Ejemplo n.º 7
0
    def __get__(self, instance, instance_type):
        """??
        """
        if instance is None:
            return self

        cache = self.get_cache(instance)

        def get_obj(jeeves_id):
            """??
            """
            if jeeves_id is None:
                return None
            if jeeves_id not in cache:
                cache[jeeves_id] = self.field.to.objects.get(
                    **{self.field.join_field.name: jeeves_id})
            return cache[jeeves_id]

        # TODO: Why do we need to facetMap this guy? If we know the viewer,
        # can we get rid of it?
        r = getattr(instance, self.field.get_attname())
        if not isinstance(JeevesLib.get_viewer(), FNull):
            robj = get_obj(r)
            if isinstance(robj, FObject):
                return robj.v
            else:
                return r
        else:
            return JeevesLib.facetMapper(fexpr_cast(r), get_obj)
Ejemplo n.º 8
0
 def __set__(self, instance, value):
   cache = self.get_cache(instance)
   def getID(obj):
     if obj is None:
       return None
     obj_jid = getattr(obj, self.field.join_field.name)
     if obj_jid is None:
       raise Exception("Object must be saved before it can be attached via JeevesForeignKey.")
     cache[obj_jid] = obj
     return obj_jid
   ids = JeevesLib.facetMapper(fexpr_cast(value), getID)
   setattr(instance, self.field.get_attname(), ids)
Ejemplo n.º 9
0
  def __get__(self, instance, instance_type):
    if instance is None:
      return self

    cache = self.get_cache(instance)
    def getObj(jeeves_id):
      if jeeves_id is None:
        return None
      if jeeves_id not in cache:
        cache[jeeves_id] = self.field.to.objects.get(**{self.field.join_field.name:jeeves_id})
      return cache[jeeves_id]
    if instance is None:
      return self
    return JeevesLib.facetMapper(fexpr_cast(getattr(instance, self.field.get_attname())), getObj)
Ejemplo n.º 10
0
 def __set__(self, instance, value):
     cache = self.get_cache(instance)
     def get_id(obj):
         """Gets the ID associated with an object.
         """
         if obj is None:
             return None
         obj_jid = getattr(obj, self.field.join_field.name)
         if obj_jid is None:
             raise Exception("Object must be saved before it can be \
                 attached via JeevesForeignKey.")
         cache[obj_jid] = obj
         return obj_jid
     ids = JeevesLib.facetMapper(fexpr_cast(value), get_id)
     setattr(instance, self.field.get_attname(), ids)
Ejemplo n.º 11
0
    def __get__(self, instance, instance_type):
        if instance is None:
            return self

        cache = self.get_cache(instance)

        def getObj(jeeves_id):
            if jeeves_id is None:
                return None
            if jeeves_id not in cache:
                cache[jeeves_id] = self.field.to.objects.get(
                    **{self.field.join_field.name: jeeves_id})
            return cache[jeeves_id]

        if instance is None:
            return self
        return JeevesLib.facetMapper(
            fexpr_cast(getattr(instance, self.field.get_attname())), getObj)
Ejemplo n.º 12
0
  def save(self, *args, **kw):
    if not self.jeeves_id:
      self.jeeves_id = get_random_jeeves_id()

    if kw.get("update_field", None) is not None:
      raise NotImplementedError("Partial saves not supported.")

    field_names = set()
    for field in self._meta.concrete_fields:
      if not field.primary_key and not hasattr(field, 'through'):
        field_names.add(field.attname)

    all_vars = []
    d = {}
    env = JeevesLib.jeevesState.pathenv.getEnv()
    for field_name in field_names:
      value = getattr(self, field_name)
      f = partialEval(fexpr_cast(value), env)
      all_vars.extend(v.name for v in f.vars())
      d[field_name] = f

    for p in powerset(all_vars):
      true_vars = list(p)
      false_vars = list(set(all_vars).difference(p))
      e = dict(env)
      e.update({tv : True for tv in true_vars})
      e.update({fv : False for fv in false_vars})

      delete_query = self.__class__._objects_ordinary.filter(jeeves_id=self.jeeves_id)
      for var_name, var_value in e:
        delete_query = delete_query.filter(jeeves_vars__contains =
              ';%s=%d;' % (var_name, var_value))
      delete_query.delete()

      klass = self.__class__
      obj_to_save = klass(**{
        field_name : fullEval(field_value, env)
        for field_name, field_value in d.iteritems()
      })
      obj_to_save.jeeves_vars = serialize_vars(e)
      super(JeevesModel, obj_to_save).save(*args, **kw)
Ejemplo n.º 13
0
    def save(self, *args, **kw):
        """Saves elements with the appropriate faceted labels.
        """

        def full_eval(val, env):
            """Evaluating a value in the context of an environment.
            """
            eval_expr = val.partialEval(env)
            return eval_expr.v

        # TODO: OMG why is this so long.
        if not self.jeeves_id:
            self.jeeves_id = JeevesModelUtils.get_random_jeeves_id()

        if kw.get("update_field", None) is not None:
            raise NotImplementedError("Partial saves not supported.")

        # Go through fields and do something. TODO: Figure out what.
        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, "through"):
                field_names.add(field.attname)

        # Go through labels and create facets.
        for label_name, field_name_list in self._jeeves_labels.iteritems():
            label = self.acquire_label(label_name)
            for field_name in field_name_list:
                public_field_value = getattr(self, field_name)
                private_field_value = getattr(self, "jeeves_get_private_" + field_name)(self)
                faceted_field_value = JeevesLib.mkSensitive(label, public_field_value, private_field_value).partialEval(
                    JeevesLib.jeevesState.pathenv.getEnv()
                )
                setattr(self, field_name, faceted_field_value)

        all_vars = []
        field_dict = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            field_val = fexpr_cast(value).partialEval(env)
            all_vars.extend(v.name for v in field_val.vars())
            field_dict[field_name] = field_val
        all_vars = list(set(all_vars))

        for cur_vars in JeevesModelUtils.powerset(all_vars):
            true_vars = list(cur_vars)
            false_vars = list(set(all_vars).difference(cur_vars))
            env_dict = dict(env)
            env_dict.update({tv: True for tv in true_vars})
            env_dict.update({fv: False for fv in false_vars})

            self.do_delete(env_dict)

            klass = self.__class__
            obj_to_save = klass(
                **{field_name: full_eval(field_value, env_dict) for field_name, field_value in field_dict.iteritems()}
            )

            all_jid_objs = list(klass._objects_ordinary.filter(jeeves_id=obj_to_save.jeeves_id).all())
            all_relevant_objs = [
                obj
                for obj in all_jid_objs
                if all(
                    field_name == "jeeves_vars" or getattr(obj_to_save, field_name) == getattr(obj, field_name)
                    for field_name in field_dict
                )
            ]

            # Optimization.
            # TODO: See how we can refactor this to shorten the function.
            while True:
                # check if we can collapse
                # if we can, repeat; otherwise, exit
                for i in xrange(len(all_relevant_objs)):
                    other_obj = all_relevant_objs[i]
                    diff_var = get_one_differing_var(env_dict, JeevesModelUtils.unserialize_vars(other_obj.jeeves_vars))
                    if diff_var is not None:
                        super(JeevesModel, other_obj).delete()
                        del env_dict[diff_var]
                        break
                else:
                    break

            obj_to_save.jeeves_vars = JeevesModelUtils.serialize_vars(env_dict)
            super(JeevesModel, obj_to_save).save(*args, **kw)
Ejemplo n.º 14
0
  def save(self, *args, **kw):
    if not self.jeeves_id:
      self.jeeves_id = get_random_jeeves_id()

    if kw.get("update_field", None) is not None:
      raise NotImplementedError("Partial saves not supported.")

    field_names = set()
    for field in self._meta.concrete_fields:
      if not field.primary_key and not hasattr(field, 'through'):
        field_names.add(field.attname)

    for label_name, field_name_list in self._jeeves_labels.iteritems():
      label = self.acquire_label(label_name)
      for field_name in field_name_list:
        public_field_value = getattr(self, field_name)
        private_field_value = getattr(self, 'jeeves_get_private_' + field_name)(self)
        faceted_field_value = partialEval(
          JeevesLib.mkSensitive(label, public_field_value, private_field_value),
          JeevesLib.jeevesState.pathenv.getEnv()
        )
        setattr(self, field_name, faceted_field_value)

    all_vars = []
    d = {}
    env = JeevesLib.jeevesState.pathenv.getEnv()
    for field_name in field_names:
      value = getattr(self, field_name)
      f = partialEval(fexpr_cast(value), env)
      all_vars.extend(v.name for v in f.vars())
      d[field_name] = f
    all_vars = list(set(all_vars))

    for p in powerset(all_vars):
      true_vars = list(p)
      false_vars = list(set(all_vars).difference(p))
      e = dict(env)
      e.update({tv : True for tv in true_vars})
      e.update({fv : False for fv in false_vars})

      self.do_delete(e)

      klass = self.__class__
      obj_to_save = klass(**{
        field_name : fullEval(field_value, e)
        for field_name, field_value in d.iteritems()
      })

      all_jid_objs = list(klass._objects_ordinary.filter(jeeves_id=obj_to_save.jeeves_id).all())
      all_relevant_objs = [obj for obj in all_jid_objs if
            all(field_name == 'jeeves_vars' or 
                getattr(obj_to_save, field_name) == getattr(obj, field_name)
                for field_name in d)]
      while True:
        # check if we can collapse
        # if we can, repeat; otherwise, exit
        for i in xrange(len(all_relevant_objs)):
          other_obj = all_relevant_objs[i]
          diff_var = get_one_differing_var(e, unserialize_vars(other_obj.jeeves_vars))
          if diff_var is not None:
            super(JeevesModel, other_obj).delete()
            del e[diff_var]
            break
        else:
          break

      obj_to_save.jeeves_vars = serialize_vars(e)
      super(JeevesModel, obj_to_save).save(*args, **kw)
Ejemplo n.º 15
0
    def save(self, *args, **kw):
        """Saves elements with the appropriate faceted labels.
        """
        def full_eval(val, env):
            """Evaluating a value in the context of an environment.
            """
            eval_expr = val.partialEval(env)
            return eval_expr.v

        # TODO: OMG why is this so long.
        if not self.jeeves_id:
            self.jeeves_id = JeevesModelUtils.get_random_jeeves_id()

        if kw.get("update_field", None) is not None:
            raise NotImplementedError("Partial saves not supported.")

        # Go through fields and do something. TODO: Figure out what.
        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, 'through'):
                field_names.add(field.attname)

        # Go through labels and create facets.
        for label_name, field_name_list in self._jeeves_labels.iteritems():
            label = self.acquire_label(label_name)
            for field_name in field_name_list:
                public_field_value = getattr(self, field_name)
                private_field_value = getattr(self
                                        , 'jeeves_get_private_' + \
                                            field_name)(self)
                faceted_field_value = JeevesLib.mkSensitive(label
                                        , public_field_value
                                        , private_field_value).partialEval(
                                            JeevesLib.jeevesState.pathenv. \
                                                getEnv())
                setattr(self, field_name, faceted_field_value)

        all_vars = []
        field_dict = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            field_val = fexpr_cast(value).partialEval(env)
            all_vars.extend(v.name for v in field_val.vars())
            field_dict[field_name] = field_val
        all_vars = list(set(all_vars))

        for cur_vars in JeevesModelUtils.powerset(all_vars):
            true_vars = list(cur_vars)
            false_vars = list(set(all_vars).difference(cur_vars))
            env_dict = dict(env)
            env_dict.update({tv: True for tv in true_vars})
            env_dict.update({fv: False for fv in false_vars})

            self.do_delete(env_dict)

            klass = self.__class__
            obj_to_save = klass(
                **{
                    field_name: full_eval(field_value, env_dict)
                    for field_name, field_value in field_dict.iteritems()
                })

            all_jid_objs = list(
                klass._objects_ordinary.filter(
                    jeeves_id=obj_to_save.jeeves_id).all())
            all_relevant_objs = [
                obj for obj in all_jid_objs
                if all(field_name == 'jeeves_vars' or getattr(
                    obj_to_save, field_name) == getattr(obj, field_name)
                       for field_name in field_dict)
            ]

            # Optimization.
            # TODO: See how we can refactor this to shorten the function.
            while True:
                # check if we can collapse
                # if we can, repeat; otherwise, exit
                for i in xrange(len(all_relevant_objs)):
                    other_obj = all_relevant_objs[i]
                    diff_var = get_one_differing_var(
                        env_dict,
                        JeevesModelUtils.unserialize_vars(
                            other_obj.jeeves_vars))
                    if diff_var is not None:
                        super(JeevesModel, other_obj).delete()
                        del env_dict[diff_var]
                        break
                else:
                    break

            obj_to_save.jeeves_vars = JeevesModelUtils.serialize_vars(env_dict)
            super(JeevesModel, obj_to_save).save(*args, **kw)
Ejemplo n.º 16
0
    def save(self, *args, **kw):
        if not self.jeeves_id:
            self.jeeves_id = get_random_jeeves_id()

        if kw.get("update_field", None) is not None:
            raise NotImplementedError("Partial saves not supported.")

        field_names = set()
        for field in self._meta.concrete_fields:
            if not field.primary_key and not hasattr(field, 'through'):
                field_names.add(field.attname)

        for label_name, field_name_list in self._jeeves_labels.iteritems():
            label = self.acquire_label(label_name)
            for field_name in field_name_list:
                public_field_value = getattr(self, field_name)
                private_field_value = getattr(
                    self, 'jeeves_get_private_' + field_name)(self)
                faceted_field_value = partialEval(
                    JeevesLib.mkSensitive(label, public_field_value,
                                          private_field_value),
                    JeevesLib.jeevesState.pathenv.getEnv())
                setattr(self, field_name, faceted_field_value)

        all_vars = []
        d = {}
        env = JeevesLib.jeevesState.pathenv.getEnv()
        for field_name in field_names:
            value = getattr(self, field_name)
            f = partialEval(fexpr_cast(value), env)
            all_vars.extend(v.name for v in f.vars())
            d[field_name] = f
        all_vars = list(set(all_vars))

        for p in powerset(all_vars):
            true_vars = list(p)
            false_vars = list(set(all_vars).difference(p))
            e = dict(env)
            e.update({tv: True for tv in true_vars})
            e.update({fv: False for fv in false_vars})

            self.do_delete(e)

            klass = self.__class__
            obj_to_save = klass(
                **{
                    field_name: fullEval(field_value, e)
                    for field_name, field_value in d.iteritems()
                })

            all_jid_objs = list(
                klass._objects_ordinary.filter(
                    jeeves_id=obj_to_save.jeeves_id).all())
            all_relevant_objs = [
                obj for obj in all_jid_objs
                if all(field_name == 'jeeves_vars' or getattr(
                    obj_to_save, field_name) == getattr(obj, field_name)
                       for field_name in d)
            ]
            while True:
                # check if we can collapse
                # if we can, repeat; otherwise, exit
                for i in xrange(len(all_relevant_objs)):
                    other_obj = all_relevant_objs[i]
                    diff_var = get_one_differing_var(
                        e, unserialize_vars(other_obj.jeeves_vars))
                    if diff_var is not None:
                        super(JeevesModel, other_obj).delete()
                        del e[diff_var]
                        break
                else:
                    break

            obj_to_save.jeeves_vars = serialize_vars(e)
            super(JeevesModel, obj_to_save).save(*args, **kw)