django-lot icon indicating copy to clipboard operation
django-lot copied to clipboard

The LOT model should use Django's model validation

Open thedrow opened this issue 10 years ago • 9 comments

There are two options here:

  1. Instead of https://github.com/jespino/django-lot/blob/master/lot/models.py#L42 we should use clean and raise a validation error if the token has expired.
  2. Call verify inside clean and raise a validation error in order to prevent creating/updating an expired/invalid token.

thedrow avatar May 28 '14 14:05 thedrow

I prefer the second option because verify check that the token is valid in other way.

Use directly the clean methods have 2 problems, we use clean in an strange way, and we will use the Exception as a flow control structure.

jespino avatar May 28 '14 15:05 jespino

Exceptions are flow control structures in python. MyModel.DoesNotExist is a very good example of how Pythonic code behaves.

thedrow avatar May 28 '14 16:05 thedrow

Exceptions are for exceptional cases, and are less efficient than normal control structures.

jespino avatar May 28 '14 16:05 jespino

That's true for most languages. Python uses an exception to stop a generator from iterating. See https://docs.python.org/2/library/exceptions.html#exceptions.StopIteration

Django uses ValidationError regularly in order to report validation failures. It's more pythonic.

thedrow avatar May 28 '14 16:05 thedrow

Touché!

Anyway in this case i prefer the option 2.

jespino avatar May 29 '14 07:05 jespino

Model validation is for ensuring the values in the fields are valid. The LOT.valid method is for checking if the token is valid for login.

These are not the same action, so I don't agree with 1

I also don't see any specific harm in creating an invalid token. It will get cleaned up in general usage, and there's no need to restrict a case that is harmless and potentially useful to someone.

funkybob avatar May 29 '14 08:05 funkybob

Mmmm, I think one use case can be something like "you can directly login week days but no weekends" or other configuration not necesary valid on creation or modification but valid in the future.

jespino avatar May 29 '14 08:05 jespino

Oh, yeah... a more flexible way to test validity... perhaps a callable in the config dict? Should I open a separate ticket? :)

funkybob avatar May 29 '14 08:05 funkybob

Yes, I think so.

jespino avatar May 29 '14 08:05 jespino