Sunday, June 12, 2022

Re: How to hash fields and detect changes in a record

On Sat, Jun 11, 2022 at 12:13:16AM +1000, Mike Dewhirst wrote:
> On 10/06/2022 11:24 pm, Ryan Nowakowski wrote:
> > On Fri, Jun 10, 2022 at 05:52:48PM +1000, Mike Dewhirst wrote:
> > > I think the solution might be to hash note.title and note.note into a new
> > > field note.hash on being auto-created. On subsequent saves, compare the
> > > latest hash with note.hash to decide whether to delete auto-inserted notes
> > > prior to generating the next set. Those subsequent saves could be months or
> > > years later.
> > Hashing is useful if you want to check that something has been
> > unexpectedly changed. I assume the note can only be changed through
> > your web app so you know when a user is changing a note.
>
> These are automatically generated notes which taken together constitute
> advice on how to deal with the analysis. Users can edit them. For example,
> someone might record some action taken regarding the advice. I don't want to
> delete that. If nothing has been edited, it is safe to delete.
>
> So how do I know it is the same as when originally generated - and safe to
> delete - except by storing a hash of the interesting fields.

Because when the user edits a note, during the form.save()(assuming
you're using Django forms), you'll set `altered_by_user` to True.

> And if that is the best approach, what sort of hashing will survive Python
> upgrades etc?

Pick a hash algorithm[1](ex: sha256). The output will remain the same
even with Python upgrades.

[1] https://docs.python.org/3/library/hashlib.html

> > Since you're
> > expecting users to change some of the notes and you know when they do,
> > hashing might be overkill. Instead, add a boolean `altered_by_user`
> > field to the note model. Initially when you automatically create the
> > note altered_by_user would be set to False. If a user changes the note,
> > set altered_by_user to True.
>
> Not sure this would work. Note creation and eventually automatic deletion is
> all driven from model methods executed on saving.

Why wouldn't this work? During note creation, altered_by_user would be
set to False automatically because that's the default. When
automatically deleting, do:

Note.objects.filter(altered_by_user=False).delete()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20220612210931.GA32625%40fattuba.com.

No comments:

Post a Comment