feat: user profile model (for subscriptions) #526
No reviewers
Labels
No labels
automation
backend
bug
contributor experience
data
deployment
documentation
duplicate
good first issue
help wanted
nice to have
notifications
package maintainer
performance
skin
tech debt
user story
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: lix-community/nix-security-tracker#526
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "yannham/subscription-relation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is preliminary work for #173. The former mandates adding a
subscriptions
many-to-many relation between users and nixpkgs issues, but sinceUser
is a built-in model in Django, we have to do something special. Common approaches are:MyUser
model that extends the standard one, and configure Django to use that by defaultProfile
model in one to one relationship with usersI went for 2., which seems to be popular in Django when one just wants to add data and relationships to
User
that don't really relate to the core functions of authentication, passwords, access rights, etc. This PR thus adds a newProfile
model in one-to-one relationship withUser
, which stores the subscriptions, and the corresponding migration.Why not add a ManyToManyField to User on NixpkgsIssue with an appropriate
related_name
?This is a possibility I thought about, but while many-to-many relations can indeed be defined on both end of the relation, I still feel like there's a privileged model where it makes more sense to define it, because you'll want to get the subscriptions from a user, much less all user subscribed from an issue (I know it doesn't make a difference in practice at runtime, but if only for code discoverability and readability). Defining it on
NixpkgsIssue
instead felt like leaking a technical Django detail (User
is predefined so it's more annoying to extend).Another consideration is if we'll add more things to a user profile, defining the relationship on the other end is going to be even worse. If you're discovering the codebase and try to understand what is associated to a user, you have to either know it in advance or search all the other models. That being said, I don't know if we plan to add more relations to
User
.All of this made me lean toward the
Profile
approach (which is also a common one mentioned in Django docs), but it wasn't a clear-cut choice. Happy to reconsider if you think it's worth moving the relation toNixpkgsIssue
to get rid of the profile handling.We'll need the m2m on Users anyway if we want to notify all subscribers
Since
Profile
is in one-to-one relationship withUser
, it's entirely isomorphic to have a separate profile or to put the relationship directly onUser
(in that you can always achieve the same in both situations). I think the question is rather: do we optimize for code readability, where IMHO, I would expect the relationship to be defined onUser
orProfile
, but at the cost of a separate model, an additional DB indirection, and the boilerplate (albeit very small) of handling the case of aUser
that doesn't have yet a profile, etc. I think both approaches are reasonable; I slightly prefer the current one obviously, but once again, happy to go the other way if you think this is better.@ -2,0 +11,4 @@
class Profile(models.Model):
"""
Profile associated to a user, storing extra non-auth-related data such as
active issue subscriptions.
Alright, then please add the signal to auto-create a profile with each user here as well, so we don't leave opportunities for breakage.
I added a signal for newly created users, but that won't apply to existing users. Is that an issue at this point? Though we can just use
get_or_create
in place ofget
also and that shouldn't a problem.