开发者

Proper way to define "remaining time off" for a Django User

开发者 https://www.devze.com 2023-01-02 03:13 出处:网络
I\'ve implemented a 开发者_C百科UserProfile model (as the Django 1.2 docs say is the proper way to save additional data about a User) which has a \'remaining_vacation_hours\' field.

I've implemented a 开发者_C百科UserProfile model (as the Django 1.2 docs say is the proper way to save additional data about a User) which has a 'remaining_vacation_hours' field.

In our current system, when a user fills out a Time Off Request, the remaining hours available should be checked to see that they have enough vacation to use, or otherwise be warned that they are asking for more than they have.

Of course, vacation hours are annually replenished, so it would be appropriate for the system to check if the user would have additional vacation accrued for the dates they're asking off.

Simply creating a get_remaining_vacation_hours() method would suffice, because other calculations or business logic that might need to be added in the future could be added to or called from that method.

My question is, does it sound correct that the get_remaining_vacation_hours() method be added to the UserProfile model?

It seems to make sense, but I wanted to verify with the community that I wasn't overlooking a better practice for this type of thing. Any ideas or suggestions are welcome.


First, I would suggest the solution of making a different method (as you've already suggested), but on a different class and pass the user instance as a parameter. That way, your user isn't characterized by something that might not apply to everyone at that level of abstraction. Consider, for example, potential employees, auditors, partners, contractors, or other types of users.

If you're system will never have users as such (or least very few), then I echo lazerscience's answer. In my experience, even well-seasoned programmers with great design experience tend to vary on what kind of information and behavior should go into the user profile. Trust your gut. If it "feels" like it might not be a source of pain or confusion then go for it. See also: Jeff Atwood's advice in matters such as these.


If you do not want to modify/inherit from the original User model I'd say it's totally ok if the method is added to your UserProfile!

0

精彩评论

暂无评论...
验证码 换一张
取 消