Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added avatars/1013714_630706690284534_93067337_n.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avatars/Haircut.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avatars/ProfilePic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avatars/ProfilePic_RfZS9rz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avatars/dog_with_puppies.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added avatars/mr_bean.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions awp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@
STATIC_URL = '/static/'

LOGIN_URL = 'login'

MEDIA_URL = '/media/'
1 change: 1 addition & 0 deletions socialapp/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

admin.site.register(models.UserPost)
admin.site.register(models.UserPostComment)
admin.site.register(models.UserProfile)
15 changes: 14 additions & 1 deletion socialapp/forms.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)


29 changes: 29 additions & 0 deletions socialapp/migrations/0004_userprofile.py
Original file line number Diff line number Diff line change
@@ -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)),
],
),
]
20 changes: 20 additions & 0 deletions socialapp/migrations/0005_auto_20151203_0825.py
Original file line number Diff line number Diff line change
@@ -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(),
),
]
20 changes: 20 additions & 0 deletions socialapp/migrations/0006_auto_20151203_0903.py
Original file line number Diff line number Diff line change
@@ -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(),
),
]
20 changes: 20 additions & 0 deletions socialapp/migrations/0007_auto_20151205_0001.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
20 changes: 20 additions & 0 deletions socialapp/migrations/0008_userprofile_avatar.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
20 changes: 20 additions & 0 deletions socialapp/migrations/0009_auto_20151217_2104.py
Original file line number Diff line number Diff line change
@@ -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/'),
),
]
21 changes: 21 additions & 0 deletions socialapp/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)



9 changes: 9 additions & 0 deletions socialapp/templates/edit_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'layout.html' %}
{% block content %}
<h3>Edit your profile</h3>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
{% endblock %}
2 changes: 1 addition & 1 deletion socialapp/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3>
{% endif %}
</h3>
<p>
by <b><small>{{ post.author }}</small></b>
by <a href="{% url 'user_profile' post.author.username %}"><b><small>{{ post.author }}</small></b></a>
on <b><small>{{ post.date_added }}</small></b>
</p>
<a href="{% url 'post_details' post.pk %}"><small>See more</small></a>
Expand Down
2 changes: 2 additions & 0 deletions socialapp/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<body>
{% if user.is_authenticated %}
<a href="{% url 'logout' %}">Log out</a>
<br>
<a href="{% url 'user_profile' user.username %}">My profile</a>
{% endif %}
<h1>Welcome to my social network!</h1>
{% block content %}
Expand Down
4 changes: 2 additions & 2 deletions socialapp/templates/post_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% block content %}
<div class="user-post">
<h3>{{ post.text }}</h3>
by <b><small>{{ post.author }}</small></b>
by <b><small><a href="{% url 'user_profile' post.author.username %}"> {{ post.author }}</a></small></b>
on <b><small>{{ post.date_added }}</small></b>
</div>
<form method="post">
Expand All @@ -11,7 +11,7 @@ <h3>{{ post.text }}</h3>
<input type="submit" value="Post">
</form>
{% for comment in post.comments.all %}
<p><b>{{ comment.author }}</b>: {{ comment.text }}</p>
<p><b><a href="{% url 'user_profile' comment.author.username %}">{{ comment.author }}</a></b>: {{ comment.text }}</p>
{% endfor %}
<a href="{% url 'index' %}"><small>Back</small></a>
{% endblock %}
17 changes: 17 additions & 0 deletions socialapp/templates/user_profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends 'layout.html' %}
{% block content %}
<h2> {{ profile.user.username }}'s Profile </h2>
<div>
<img src={{ profile.avatar.url }} style="max-width: 200px; max-height: 200px;"/>
<p> First Name: {{ profile.first_name }} </p>
<p> Last Name: {{ profile.last_name }} </p>
<p> Birthday: {{ profile.birthday }} </p>
<p> Sex: {{ profile.gender }} </p>
</div>
{% if user == profile.user %}
<h5>
<a href="{% url 'edit_profile' user.username %}">Edit profile</a>
</h5>
{% endif %}
<a href="{% url 'index' %}"><small>Back</small></a>
{% endblock %}
6 changes: 5 additions & 1 deletion socialapp/urls.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -7,4 +9,6 @@
url(r'^post/(?P<pk>\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<username>[A-Za-z]+)/$', views.user_profile, name='user_profile'),
url(r'^edit_profile/(?P<username>[A-Za-z]+)/$', views.edit_profile, name='edit_profile')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
43 changes: 41 additions & 2 deletions socialapp/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)