diff --git a/forums/admin.py b/forums/admin.py index 55476cf..bb44d8f 100644 --- a/forums/admin.py +++ b/forums/admin.py @@ -1,9 +1,10 @@ from django.contrib import admin -from .models import Forum, Thread, Post, UpVote, Notification +from .models import Forum, Thread, Post, UserProfile, UpVote, Notification admin.site.register(Forum) admin.site.register(Thread) admin.site.register(Post) +admin.site.register(UserProfile) admin.site.register(UpVote) admin.site.register(Notification) diff --git a/forums/forms.py b/forums/forms.py index 8c518f3..904cc93 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -1,5 +1,22 @@ from django import forms +from .models import UserProfile + class SearchForm(forms.Form): q = forms.CharField(label='Search', max_length=200) + + +class UserProfileForm(forms.ModelForm): + class Meta: + model = UserProfile + fields = ( + 'first_name', + 'last_name', + 'bio', + 'location', + 'gender', + 'web_site', + 'github_url', + 'signature', + ) diff --git a/forums/migrations/0008_userprofile.py b/forums/migrations/0008_userprofile.py new file mode 100644 index 0000000..4453445 --- /dev/null +++ b/forums/migrations/0008_userprofile.py @@ -0,0 +1,29 @@ +# Generated by Django 2.2.8 on 2019-12-07 18:24 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('forums', '0007_auto_20191203_0820'), + ] + + operations = [ + migrations.CreateModel( + name='UserProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('bio', models.TextField(blank=True, max_length=1000)), + ('location', models.CharField(blank=True, max_length=50)), + ('gender', models.TextField(choices=[('M', 'Male'), ('F', 'Female'), ('O', 'Other')], default='N', max_length=1)), + ('web_site', models.URLField(blank=True)), + ('github_url', models.URLField(blank=True)), + ('signature', models.TextField(blank=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/forums/migrations/0009_auto_20191207_1835.py b/forums/migrations/0009_auto_20191207_1835.py new file mode 100644 index 0000000..b6a08be --- /dev/null +++ b/forums/migrations/0009_auto_20191207_1835.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.8 on 2019-12-07 18:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('forums', '0008_userprofile'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='gender', + field=models.TextField(choices=[('N', 'NotProvided'), ('M', 'Male'), ('F', 'Female'), ('O', 'Other')], default='N', max_length=1), + ), + ] diff --git a/forums/migrations/0010_auto_20191207_1914.py b/forums/migrations/0010_auto_20191207_1914.py new file mode 100644 index 0000000..a799536 --- /dev/null +++ b/forums/migrations/0010_auto_20191207_1914.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.8 on 2019-12-07 19:14 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('forums', '0009_auto_20191207_1835'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='user', + field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/forums/migrations/0011_auto_20191209_1624.py b/forums/migrations/0011_auto_20191209_1624.py new file mode 100644 index 0000000..e46a7a6 --- /dev/null +++ b/forums/migrations/0011_auto_20191209_1624.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.8 on 2019-12-09 16:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('forums', '0010_auto_20191207_1914'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='first_name', + field=models.CharField(blank=True, max_length=100), + ), + migrations.AddField( + model_name='userprofile', + name='last_name', + field=models.CharField(blank=True, max_length=100), + ), + ] diff --git a/forums/models.py b/forums/models.py index e1af7c7..1566439 100644 --- a/forums/models.py +++ b/forums/models.py @@ -1,6 +1,5 @@ -from django.db import models - from django.contrib.auth import get_user_model +from django.db import models class Forum(models.Model): @@ -44,6 +43,28 @@ class Meta: ordering = ['added'] +class UserProfile(models.Model): + NOTPROVIDED = 'N' + MALE = 'M' + FEMALE = 'F' + OTHER = 'O' + GENDER_CHOICES = ( + (NOTPROVIDED, 'NotProvided'), + (MALE, 'Male'), + (FEMALE, 'Female'), + (OTHER, 'Other'), + ) + user = models.OneToOneField(get_user_model(), related_name='profile', on_delete=models.CASCADE, ) + first_name = models.CharField(max_length=100, blank=True) + last_name = models.CharField(max_length=100, blank=True) + bio = models.TextField(max_length=1000, blank=True) + location = models.CharField(max_length=50, blank=True) + gender = models.TextField(max_length=1, choices=GENDER_CHOICES, default=NOTPROVIDED) + web_site = models.URLField(blank=True) + github_url = models.URLField(blank=True) + signature = models.TextField(blank=True) + + class UpVote(models.Model): post = models.ForeignKey(Post, on_delete=models.CASCADE) user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, ) diff --git a/forums/signals.py b/forums/signals.py index e61d330..66d9d38 100644 --- a/forums/signals.py +++ b/forums/signals.py @@ -1,8 +1,9 @@ from django.core.mail import EmailMultiAlternatives from django.db.models.signals import post_save from django.dispatch import receiver - -from .models import Post, Notification +from django.contrib.auth import get_user_model +from users.models import CustomUser +from .models import Post, Notification, UserProfile @receiver(post_save, sender=Post, dispatch_uid="notify_thread_subscribers") @@ -29,3 +30,18 @@ def send_notification(sender, created, **kwargs): msg = EmailMultiAlternatives(subject=subject, body=text_content, from_email=from_email, bcc=bcc) msg.send() + + +@receiver(post_save, sender=CustomUser, dispatch_uid="create_user_profile") +def create_user_profile(sender, instance, created, **kwargs): + if created: + UserProfile.objects.create(user=instance) + + +@receiver(post_save, sender=get_user_model(), dispatch_uid="save_user_profile") +def save_user_profile(sender, instance, **kwargs): + user_profile = UserProfile.objects.filter(user=instance) + if not user_profile: + UserProfile.objects.create(user=instance) + instance.profile.save() + diff --git a/project/urls.py b/project/urls.py index 1aeb8b7..f451505 100644 --- a/project/urls.py +++ b/project/urls.py @@ -11,7 +11,7 @@ path('admin/', include('admin_honeypot.urls', namespace='admin_honeypot')), # user management path('accounts/', include('allauth.urls')), - path('user_profile/', views.UserUpdate.as_view(), name='user_edit'), + path('user_profile/', views.UserProfileUpdate.as_view(), name='user_profile_edit'), # local apps path('', include('pages.urls')), path('forums/', include('forums.urls')), diff --git a/project/views.py b/project/views.py index f18f1ef..9b08544 100644 --- a/project/views.py +++ b/project/views.py @@ -1,10 +1,11 @@ -from django.views.generic.edit import UpdateView from django.urls import reverse_lazy +from django.views.generic.edit import UpdateView -from django.contrib.auth import get_user_model +from forums.forms import UserProfileForm +from forums.models import UserProfile -class UserUpdate(UpdateView): - model = get_user_model() - fields = ['first_name', 'last_name'] +class UserProfileUpdate(UpdateView): + model = UserProfile success_url = reverse_lazy('home') + form_class = UserProfileForm diff --git a/templates/_base.html b/templates/_base.html index ad808a5..eaaecd9 100644 --- a/templates/_base.html +++ b/templates/_base.html @@ -40,7 +40,7 @@ diff --git a/templates/users/customuser_form.html b/templates/forums/userprofile_form.html similarity index 50% rename from templates/users/customuser_form.html rename to templates/forums/userprofile_form.html index 453bc42..d77da5c 100644 --- a/templates/users/customuser_form.html +++ b/templates/forums/userprofile_form.html @@ -2,12 +2,20 @@ {% load crispy_forms_tags %} {% block title %}Edit Profile{% endblock title %} - +Hello {% block content %}

Edit Profile

+
+
+
+ +
+
+
+ {% csrf_token %} {{ form|crispy }} - +
{% endblock content %} \ No newline at end of file