-
Notifications
You must be signed in to change notification settings - Fork 1
Create user profile model #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b4d4e12
Create user profile model
naglepuff 11a88bd
Address factoryboy deprecation warning
naglepuff f141c3c
Add smell test for user profile creation
naglepuff 786e665
Make profile creation receiver private
naglepuff bea21b7
Clarify tests and factories
naglepuff a5ff6f3
Add user profiles to user admin
naglepuff 3475c24
Use `get` over `filter` for testing profile create
naglepuff 30c6aaf
Add verified filter to user admin
naglepuff 15ac317
Use the register decorator
naglepuff 8f4b611
Use double underscore for list filter
naglepuff 4b8676b
Add data migration to create user profiles
naglepuff bc1786a
Remove hasattr check for profile in user admin
naglepuff fdcab77
Remove unnecessary post gen hook
naglepuff 0640cfd
Squash migrations
naglepuff c438ed9
Link to ticket explaining `is_verified`
naglepuff 50c8594
Add trailing comma
naglepuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from django.contrib import admin | ||
| from django.contrib.auth.admin import UserAdmin as BaseUserAdmin | ||
| from django.contrib.auth.models import User | ||
|
|
||
| from bats_ai.core.models import UserProfile | ||
|
|
||
|
|
||
| class UserProfileInline(admin.StackedInline): | ||
| model = UserProfile | ||
| can_delete = False | ||
| extra = 0 | ||
|
|
||
|
|
||
| admin.site.unregister(User) | ||
|
|
||
|
|
||
| @admin.register(User) | ||
| class UserAdmin(BaseUserAdmin): | ||
| inlines = [UserProfileInline] | ||
| list_select_related = ['profile'] | ||
|
|
||
| # See https://code.djangoproject.com/ticket/36926#ticket | ||
| list_display = list(BaseUserAdmin.list_display) + ['is_verified'] | ||
| list_filter = list(BaseUserAdmin.list_filter) + ['profile__verified'] | ||
|
|
||
| @admin.display( | ||
| boolean=True, | ||
| description='Is Verified?', | ||
| ) | ||
| def is_verified(self, obj): | ||
| return obj.profile.verified | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Generated by Django 5.2.11 on 2026-02-12 20:10 | ||
|
|
||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
|
|
||
|
|
||
| def create_user_profiles(apps, schema_editor): | ||
| User = apps.get_model('auth', 'User') | ||
| UserProfile = apps.get_model('core', 'UserProfile') | ||
|
|
||
| for user in User.objects.all(): | ||
| UserProfile.objects.create(user=user) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('core', '0029_pulsemetadata_char_freq_pulsemetadata_curve_and_more'), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
brianhelba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| migrations.CreateModel( | ||
| name='UserProfile', | ||
| fields=[ | ||
| ( | ||
| 'id', | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name='ID' | ||
| ), | ||
| ), | ||
| ('verified', models.BooleanField(default=False)), | ||
| ( | ||
| 'user', | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name='profile', | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| migrations.RunPython(create_user_profiles, reverse_code=migrations.RunPython.noop), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from django.contrib.auth.models import User | ||
| from django.db import models | ||
| from django.db.models.signals import post_save | ||
| from django.dispatch import receiver | ||
|
|
||
|
|
||
| class UserProfile(models.Model): | ||
| user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile') | ||
| verified = models.BooleanField(default=False) | ||
|
|
||
|
|
||
| @receiver(post_save, sender=User, dispatch_uid='create_new_user_profile') | ||
| def _create_new_user_profile(sender, instance, created, **kwargs): | ||
| if created: | ||
| UserProfile.objects.create(user=instance) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from django.contrib.auth.models import User | ||
| import pytest | ||
|
|
||
| from bats_ai.core.models import UserProfile | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_profile_creation(): | ||
| # Use django model directly to test the signal receiver, | ||
| # not whether our factories are working as intended. | ||
| user = User.objects.create() | ||
naglepuff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| profile = UserProfile.objects.get(user=user) | ||
| assert not profile.verified | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing
profile__verifieddoesn't work?list_displaysupports this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It worked but I couldn't figure out how to display the checkmark/X icons. The value in the column would just be the string "True"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a bug with Django. Can you file a bug report: https://code.djangoproject.com/query
Obviously, it's fine to keep the extra code for now then. Could you add a code comment with a link to the bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c438ed9