Ejemplo n.º 1
0
    def create_historical_record(self, instance, type):
        manager = getattr(instance, self.manager_name)
        attrs = {}
        for field in instance._meta.fields:
            if field.primary_key:
              attrs[field.attname] = instance
            elif isinstance(field, KeyToHistoryMarker):
                related_instance = getattr(instance, field.attname)
                result = None
                if related_instance is not None:
                    history_manager = getattr(related_instance, type(
                        related_instance)._meta.custom_history_manager_name)
                    result = history_manager.most_recent()
                attrs[field.attname] = result
            else:
                attrs[field.attname] = getattr(instance, field.attname)

        attrs['current'] = True
        manager.create(history_type=type, **attrs)
Ejemplo n.º 2
0
    def create_historical_record(self, instance, type):
        manager = getattr(instance, self.manager_name)
        # If they set track_changes to False
        # then we don't auto-create a revision here.
        if not instance._track_changes:
            return
        attrs = {}
        for field in instance._meta.fields:
            if isinstance(field, models.fields.related.ForeignKey):
                is_fk_to_self = (
                    field.related.parent_model == instance.__class__)
                if is_versioned(field.related.parent_model) or is_fk_to_self:
                    if field.rel.parent_link:
                        # Concrete model inheritance and the parent is
                        # versioned.  In this case, we subclass the
                        # parent historical model and use that parent
                        # related field instead.
                        continue

                    # If the FK field is versioned, set it to the most
                    # recent version of that object.
                    # The object the FK id refers to may have been
                    # deleted so we can't simply do Model.objects.get().
                    fk_hist_model = field.rel.to.history.model
                    fk_id_name = field.rel.field_name
                    fk_id_val = getattr(instance, field.attname)
                    fk_objs = fk_hist_model.objects.filter(
                        **{fk_id_name: fk_id_val})

                    if fk_objs:
                        attrs[field.name] = fk_objs[0]  # most recent version
                    else:
                        attrs[field.name] = None
                    continue

            attrs[field.attname] = getattr(instance, field.attname)

        attrs.update(self._get_save_with_attrs(instance))
        manager.create(history_type=type, **attrs)
Ejemplo n.º 3
0
    def create_historical_record(self, instance, type):
        manager = getattr(instance, self.manager_name)
        # If they set track_changes to False
        # then we don't auto-create a revision here.
        if not instance._track_changes:
            return
        attrs = {}
        for field in instance._meta.fields:
            if isinstance(field, models.fields.related.ForeignKey):
                is_fk_to_self = field.related.parent_model == instance.__class__
                if is_versioned(field.related.parent_model) or is_fk_to_self:
                    if field.rel.parent_link:
                        # Concrete model inheritance and the parent is
                        # versioned.  In this case, we subclass the
                        # parent historical model and use that parent
                        # related field instead.
                        continue

                    # If the FK field is versioned, set it to the most
                    # recent version of that object.
                    # The object the FK id refers to may have been
                    # deleted so we can't simply do Model.objects.get().
                    fk_hist_model = field.rel.to.history.model
                    fk_id_name = field.rel.field_name
                    fk_id_val = getattr(instance, field.attname)
                    fk_objs = fk_hist_model.objects.filter(**{fk_id_name: fk_id_val})

                    if fk_objs:
                        attrs[field.name] = fk_objs[0]  # most recent version
                    else:
                        attrs[field.name] = None
                    continue

            attrs[field.attname] = getattr(instance, field.attname)

        attrs.update(self._get_save_with_attrs(instance))
        manager.create(history_type=type, **attrs)
Ejemplo n.º 4
0
 def create(self):
     return create(self)
Ejemplo n.º 5
0
	def _add(self):
		""" Add a node or information to a node """
		try:
			if self._actions[1] == 'meta':
				_results									= []
				try:
					for _node in self._values[0]:
						try:
							_subresults						= []
							for _meta in self._values[0][_node]:
								_subresults.append(manager.call(_node,'addMeta',_meta))
							_results.append(_subresults)
						except IndexError:
							_results.append(False)
					return _results
				except:
					return False
			elif self._actions[1] == 'links':
				_results									= []
				try:
					# For each node we want to add new links to
					for _node in self._values[0]:
						try:
							_subresults						= []
							# For each destination node
							for _dest in self._values[0][_node].keys():
								_ssubresults				= []
								# For each link type
								for _type in self._values[0][_node][_dest]:
									_ssubresults.append(manager.call(_node,'addLink',_dest,_type))
								_subresults.append(_ssubresults)
							_results.append(_subresults)
						except IndexError:
							_results.append(False)
					return _results
				except:
					return False
			elif self._actions[1] == 'data':
				_results									= []
				try:
					for _node in self._values[0].keys():
						try:
							_subresults						= []
							for _data in self._values[0][_node]:
								_subresults.append(manager.call(_node,'addData',_data))
							_results.append(_subresults)
						except IndexError:
							_results.append(False)
					return _results
				except:
					return False
			else:
				return False
		except:
			# We are adding nodes to the system, loop through the list
			_ids											= []
			for i in self._values[0]:
				_id											= manager.create()
				# Add the meta data
				try:
					for meta in i[0]:
						manager.call(_id, 'addMeta', meta)
				except:
					pass
				# Add links
				try:
					for _dest in i[1].keys():
						for _type in i[1][_dest]:
							manager.call(_id, 'addLink', _dest, _type)
				except:
					pass
				# Add Data
				result										= manager.call(_id, 'addData', i[2])
				# Commit the node
				_ids.append(_id)
			return _ids