diff --git a/avatars/1013714_630706690284534_93067337_n.jpg b/avatars/1013714_630706690284534_93067337_n.jpg new file mode 100644 index 0000000..870fc58 Binary files /dev/null and b/avatars/1013714_630706690284534_93067337_n.jpg differ diff --git a/avatars/Haircut.JPG b/avatars/Haircut.JPG new file mode 100644 index 0000000..aeee3be Binary files /dev/null and b/avatars/Haircut.JPG differ diff --git a/avatars/ProfilePic.jpg b/avatars/ProfilePic.jpg new file mode 100644 index 0000000..35dab6b Binary files /dev/null and b/avatars/ProfilePic.jpg differ diff --git a/avatars/ProfilePic_RfZS9rz.jpg b/avatars/ProfilePic_RfZS9rz.jpg new file mode 100644 index 0000000..35dab6b Binary files /dev/null and b/avatars/ProfilePic_RfZS9rz.jpg differ diff --git a/avatars/dog_with_puppies.jpg b/avatars/dog_with_puppies.jpg new file mode 100644 index 0000000..870fc58 Binary files /dev/null and b/avatars/dog_with_puppies.jpg differ diff --git a/avatars/mr_bean.jpg b/avatars/mr_bean.jpg new file mode 100644 index 0000000..524928f Binary files /dev/null and b/avatars/mr_bean.jpg differ diff --git a/awp/settings.py b/awp/settings.py index 54abddb..5f7acf1 100644 --- a/awp/settings.py +++ b/awp/settings.py @@ -103,3 +103,5 @@ STATIC_URL = '/static/' LOGIN_URL = 'login' + +MEDIA_URL = '/media/' diff --git a/socialapp/admin.py b/socialapp/admin.py index 7d62dbf..568fab5 100644 --- a/socialapp/admin.py +++ b/socialapp/admin.py @@ -5,3 +5,4 @@ admin.site.register(models.UserPost) admin.site.register(models.UserPostComment) +admin.site.register(models.UserProfile) diff --git a/socialapp/forms.py b/socialapp/forms.py index ff2cca3..557e162 100644 --- a/socialapp/forms.py +++ b/socialapp/forms.py @@ -1,4 +1,5 @@ -from django.forms import Form, CharField, Textarea, PasswordInput +from django.forms import Form, CharField, ChoiceField, DateField, \ + Textarea, PasswordInput, SelectDateWidget, ImageField class UserPostForm(Form): @@ -16,3 +17,15 @@ class UserPostCommentForm(Form): class UserLoginForm(Form): username = CharField(max_length=30) password = CharField(widget=PasswordInput) + + +class EditProfileForm(Form): + first_name = CharField(max_length=100, required=False) + last_name = CharField(max_length=100, required=False) + birthday = DateField(widget=SelectDateWidget( + empty_label=("Choose Year", "Choose Month", "Choose Day"), + ), required=False) + sex = ChoiceField(choices=(('M', 'Male'), ('F', 'Female')), required=False) + avatar = ImageField(required=False) + + diff --git a/socialapp/migrations/0004_userprofile.py b/socialapp/migrations/0004_userprofile.py new file mode 100644 index 0000000..f373fa1 --- /dev/null +++ b/socialapp/migrations/0004_userprofile.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-03 08:19 +from __future__ import unicode_literals + +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), + ('socialapp', '0003_auto_20151203_0623'), + ] + + operations = [ + migrations.CreateModel( + name='UserProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.TextField(max_length=500)), + ('last_name', models.TextField(max_length=500)), + ('birthday', models.DateTimeField()), + ('gender', models.CharField(choices=[(b'M', b'Male'), (b'F', b'Female')], default=b'M', max_length=1)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/socialapp/migrations/0005_auto_20151203_0825.py b/socialapp/migrations/0005_auto_20151203_0825.py new file mode 100644 index 0000000..b64b355 --- /dev/null +++ b/socialapp/migrations/0005_auto_20151203_0825.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-03 08:25 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('socialapp', '0004_userprofile'), + ] + + operations = [ + migrations.AlterField( + model_name='userpostcomment', + name='date_added', + field=models.DateField(), + ), + ] diff --git a/socialapp/migrations/0006_auto_20151203_0903.py b/socialapp/migrations/0006_auto_20151203_0903.py new file mode 100644 index 0000000..0c7c865 --- /dev/null +++ b/socialapp/migrations/0006_auto_20151203_0903.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-03 09:03 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('socialapp', '0005_auto_20151203_0825'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='birthday', + field=models.DateField(), + ), + ] diff --git a/socialapp/migrations/0007_auto_20151205_0001.py b/socialapp/migrations/0007_auto_20151205_0001.py new file mode 100644 index 0000000..c38fd94 --- /dev/null +++ b/socialapp/migrations/0007_auto_20151205_0001.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-05 00:01 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('socialapp', '0006_auto_20151203_0903'), + ] + + operations = [ + migrations.AlterField( + model_name='userpostcomment', + name='date_added', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/socialapp/migrations/0008_userprofile_avatar.py b/socialapp/migrations/0008_userprofile_avatar.py new file mode 100644 index 0000000..bc915d8 --- /dev/null +++ b/socialapp/migrations/0008_userprofile_avatar.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-05 15:12 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('socialapp', '0007_auto_20151205_0001'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='avatar', + field=models.ImageField(default=b'http://pethealthsupplements.com.au/wp-content/uploads/2015/06/ tumblr_static_mom-dog-with-puppies.jpg', upload_to=b'Desktop/Python/awp-project/pic-folder'), + ), + ] diff --git a/socialapp/migrations/0009_auto_20151217_2104.py b/socialapp/migrations/0009_auto_20151217_2104.py new file mode 100644 index 0000000..e127d73 --- /dev/null +++ b/socialapp/migrations/0009_auto_20151217_2104.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2015-12-17 21:04 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('socialapp', '0008_userprofile_avatar'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='avatar', + field=models.ImageField(default=b'avatars/dog_with_puppies.jpg', upload_to=b'avatars/'), + ), + ] diff --git a/socialapp/models.py b/socialapp/models.py index 6521cec..8f9c68b 100644 --- a/socialapp/models.py +++ b/socialapp/models.py @@ -1,5 +1,6 @@ from django.contrib.auth.models import User from django.db import models +from django.conf import settings class UserPost(models.Model): @@ -26,3 +27,23 @@ class Meta: def __unicode__(self): return u'{} @ {}'.format(self.author, self.date_added) + + +class UserProfile(models.Model): + user = models.OneToOneField( + settings.AUTH_USER_MODEL, + on_delete = models.CASCADE, + ) + first_name = models.TextField(max_length=500) + last_name = models.TextField(max_length=500) + birthday = models.DateField() + gender = models.CharField(max_length=1, + choices=(('M', 'Male'), ('F', 'Female')), default='M') + avatar = models.ImageField(upload_to='avatars/', + default='avatars/dog_with_puppies.jpg') + + def __unicode__(self): + return u'{} @ {}'.format(self.first_name, self.last_name) + + + diff --git a/socialapp/templates/edit_profile.html b/socialapp/templates/edit_profile.html new file mode 100644 index 0000000..65e84bf --- /dev/null +++ b/socialapp/templates/edit_profile.html @@ -0,0 +1,9 @@ +{% extends 'layout.html' %} +{% block content %} +

Edit your profile

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} \ No newline at end of file diff --git a/socialapp/templates/index.html b/socialapp/templates/index.html index 77f1201..7be3387 100644 --- a/socialapp/templates/index.html +++ b/socialapp/templates/index.html @@ -16,7 +16,7 @@

{% endif %}

- by {{ post.author }} + by {{ post.author }} on {{ post.date_added }}

See more diff --git a/socialapp/templates/layout.html b/socialapp/templates/layout.html index 5b12823..6caed3e 100644 --- a/socialapp/templates/layout.html +++ b/socialapp/templates/layout.html @@ -5,6 +5,8 @@ {% if user.is_authenticated %} Log out +
+ My profile {% endif %}

Welcome to my social network!

{% block content %} diff --git a/socialapp/templates/post_details.html b/socialapp/templates/post_details.html index 327c331..f339c5a 100644 --- a/socialapp/templates/post_details.html +++ b/socialapp/templates/post_details.html @@ -2,7 +2,7 @@ {% block content %}

{{ post.text }}

- by {{ post.author }} + by {{ post.author }} on {{ post.date_added }}
@@ -11,7 +11,7 @@

{{ post.text }}

{% for comment in post.comments.all %} -

{{ comment.author }}: {{ comment.text }}

+

{{ comment.author }}: {{ comment.text }}

{% endfor %} Back {% endblock %} diff --git a/socialapp/templates/user_profile.html b/socialapp/templates/user_profile.html new file mode 100644 index 0000000..d6d13eb --- /dev/null +++ b/socialapp/templates/user_profile.html @@ -0,0 +1,17 @@ +{% extends 'layout.html' %} +{% block content %} +

{{ profile.user.username }}'s Profile

+
+ +

First Name: {{ profile.first_name }}

+

Last Name: {{ profile.last_name }}

+

Birthday: {{ profile.birthday }}

+

Sex: {{ profile.gender }}

+
+ {% if user == profile.user %} +
+ Edit profile +
+ {% endif %} + Back +{% endblock %} \ No newline at end of file diff --git a/socialapp/urls.py b/socialapp/urls.py index 44aedd9..36baaf9 100644 --- a/socialapp/urls.py +++ b/socialapp/urls.py @@ -1,4 +1,6 @@ from django.conf.urls import url +from django.conf.urls.static import static +from django.conf import settings from socialapp import views @@ -7,4 +9,6 @@ url(r'^post/(?P\d+)/$', views.post_details, name='post_details'), url(r'^login/$', views.login_view, name='login'), url(r'^logout/$', views.logout_view, name='logout'), -] + url(r'^user_profile/(?P[A-Za-z]+)/$', views.user_profile, name='user_profile'), + url(r'^edit_profile/(?P[A-Za-z]+)/$', views.edit_profile, name='edit_profile') +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/socialapp/views.py b/socialapp/views.py index 44629a4..2646851 100644 --- a/socialapp/views.py +++ b/socialapp/views.py @@ -1,9 +1,10 @@ +from django.contrib.auth.models import User from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect -from socialapp.forms import UserPostForm, UserPostCommentForm, UserLoginForm -from socialapp.models import UserPost, UserPostComment +from socialapp.forms import UserPostForm, UserPostCommentForm, UserLoginForm, EditProfileForm +from socialapp.models import UserPost, UserPostComment, UserProfile @login_required @@ -68,3 +69,41 @@ def login_view(request): def logout_view(request): logout(request) return redirect('login') + + +@login_required +def user_profile(request, username): + if request.method == 'GET': + user = User.objects.filter(username=username) + profile = UserProfile.objects.filter(user=user).first() + context = {'profile': profile} + return render(request, 'user_profile.html', context) + + +@login_required +def edit_profile(request, username): + if request.method == 'GET': + form = EditProfileForm() + context = { + 'form': form, + } + return render(request, 'edit_profile.html', context) + elif request.method == 'POST': + form = EditProfileForm(request.POST, request.FILES) + user = User.objects.filter(username=username) + profile = UserProfile.objects.filter(user=user).first() + + if form.is_valid(): + if form.cleaned_data['first_name']: + profile.first_name = form.cleaned_data['first_name'] + if form.cleaned_data['last_name']: + profile.last_name = form.cleaned_data['last_name'] + if form.cleaned_data['birthday']: + profile.birthday = form.cleaned_data['birthday'] + if form.cleaned_data['sex']: + profile.gender = form.cleaned_data['sex'] + if form['avatar'].value(): + profile.avatar = form['avatar'].value() + profile.save() + + return redirect('user_profile', username=username)