Add a user profile
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Mar 2022 16:10:37 +0000 (11:10 -0500)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 4 Mar 2022 16:10:37 +0000 (11:10 -0500)
src/core/settings.py
src/core/urls.py
src/user_profile/__init__.py [new file with mode: 0644]
src/user_profile/admin.py [new file with mode: 0644]
src/user_profile/apps.py [new file with mode: 0644]
src/user_profile/migrations/0001_initial.py [new file with mode: 0644]
src/user_profile/migrations/__init__.py [new file with mode: 0644]
src/user_profile/models.py [new file with mode: 0644]
src/user_profile/tests.py [new file with mode: 0644]
src/user_profile/urls.py [new file with mode: 0644]
src/user_profile/views.py [new file with mode: 0644]

index c07823b..f338b7a 100644 (file)
@@ -40,6 +40,7 @@ INSTALLED_APPS = [
     'climbing',
     'nutrition',
     'training',
+    'user_profile',
     'core',
 ]
 
index 8eef687..068df05 100644 (file)
@@ -19,6 +19,7 @@ from django.urls import include, path
 from climbing import urls as climbing_urls
 from nutrition import urls as nutrition_urls
 from training import urls as training_urls
+from user_profile import urls as user_profile_urls
 
 from . import views
 
@@ -28,4 +29,5 @@ urlpatterns = (
     path('c/', include(climbing_urls)),
     path('n/', include(nutrition_urls)),
     path('t/', include(training_urls)),
+    path('u/', include(user_profile_urls)),
 )
diff --git a/src/user_profile/__init__.py b/src/user_profile/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/user_profile/admin.py b/src/user_profile/admin.py
new file mode 100644 (file)
index 0000000..8c38f3f
--- /dev/null
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/src/user_profile/apps.py b/src/user_profile/apps.py
new file mode 100644 (file)
index 0000000..fe0436b
--- /dev/null
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class UserProfileConfig(AppConfig):
+    default_auto_field = 'django.db.models.BigAutoField'
+    name = 'user_profile'
diff --git a/src/user_profile/migrations/0001_initial.py b/src/user_profile/migrations/0001_initial.py
new file mode 100644 (file)
index 0000000..524329a
--- /dev/null
@@ -0,0 +1,25 @@
+# Generated by Django 4.0.3 on 2022-03-04 16:08
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='UserProfile',
+            fields=[
+                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('notes', models.TextField(blank=True, null=True)),
+                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+    ]
diff --git a/src/user_profile/migrations/__init__.py b/src/user_profile/migrations/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/user_profile/models.py b/src/user_profile/models.py
new file mode 100644 (file)
index 0000000..a98a009
--- /dev/null
@@ -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)
+    notes = models.TextField(blank=True, null=True)
+
+    def __str__(self):
+        return self.user.username
+
+@receiver(post_save, sender=User)
+def create_profile(sender, instance, **kwargs):
+    profile, created = UserProfile.objects.get_or_create(user=instance)
diff --git a/src/user_profile/tests.py b/src/user_profile/tests.py
new file mode 100644 (file)
index 0000000..7ce503c
--- /dev/null
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/src/user_profile/urls.py b/src/user_profile/urls.py
new file mode 100644 (file)
index 0000000..bc2c6d8
--- /dev/null
@@ -0,0 +1,11 @@
+from django.urls import path
+
+from . import views
+
+urlpatterns = (
+    path(
+        '<str:username>',
+        views.user_profile_detail,
+        name='user-profile-detail',
+    ),
+)
diff --git a/src/user_profile/views.py b/src/user_profile/views.py
new file mode 100644 (file)
index 0000000..408880e
--- /dev/null
@@ -0,0 +1,8 @@
+from django.views.generic.detail import DetailView
+
+from . import models
+
+class UserProfileDetailView(DetailView):
+    model = models.UserProfile
+
+user_profile_detail = UserProfileDetailView.as_view()