示例#1
0
    def save(self, *args, **kwargs):
        try:
            # Get a copy of the old object from the db
            old = Cred.objects.get(id=self.id)

            # Reset the primary key so Django thinks its a new object
            old.id = None

            # Set the latest on the old copy to the new copy
            old.latest = self

            # Create it in the DB
            old.save()

            # Add the tags to the old copy now that it exists
            for t in self.tags.all():
                old.tags.add(t)

            # Add the groups
            for g in self.groups.all():
                old.groups.add(g)

            # Lets see what was changed
            oldcred = model_to_dict(old)
            newcred = model_to_dict(self)
            diff = DictDiffer(newcred, oldcred).changed()

            # Ohhhkaaay, so the attachment field looks like its changed every
            # time, so we do a deep comparison, if the files match we remove
            # it from the list of changed fields.
            for field in ('attachment', 'ssh_key'):
                if field_file_compare(oldcred[field], newcred[field]):
                    diff = diff - set((field, ))

            # Check if some non-metadata was changed
            chg = diff - set(Cred.METADATA)
            cred_changed = len(chg) > 0

            # If the creds were changed update the modify date
            if cred_changed:
                self.modified = now()
        except Cred.DoesNotExist:
            # This just means its new cred, set the initial modified time
            self.modified = now()

        super(Cred, self).save(*args, **kwargs)
示例#2
0
    def save(self, *args, **kwargs):
        try:
            # Get a copy of the old object from the db
            old = Cred.objects.get(id=self.id)

            # Reset the primary key so Django thinks its a new object
            old.id = None

            # Set the latest on the old copy to the new copy
            old.latest = self

            # Create it in the DB
            old.save()

            # Add the tags to the old copy now that it exists
            for t in self.tags.all():
                old.tags.add(t)

            # Add the groups
            for g in self.groups.all():
                old.groups.add(g)

            # Lets see what was changed
            oldcred = model_to_dict(old)
            newcred = model_to_dict(self)
            diff = DictDiffer(newcred, oldcred).changed()

            # Ohhhkaaay, so the attachment field looks like its changed every
            # time, so we do a deep comparison, if the files match we remove
            # it from the list of changed fields.
            for field in ('attachment', 'ssh_key'):
                if field_file_compare(oldcred[field], newcred[field]):
                    diff = diff - set((field, ))

            # Check if some non-metadata was changed
            chg = diff - set(Cred.METADATA)
            cred_changed = len(chg) > 0

            # If the creds were changed update the modify date
            if cred_changed:
                self.modified = now()
        except Cred.DoesNotExist:
            # This just means its new cred, set the initial modified time
            self.modified = now()

        super(Cred, self).save(*args, **kwargs)