Skip to content

Commit

Permalink
190416| Insta - Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunwoo-song committed Apr 16, 2019
1 parent f8d3afe commit 7cc6903
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 22 deletions.
5 changes: 4 additions & 1 deletion django/insta/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.contrib import admin

from .models import Profile
# Register your models here.

admin.site.register(Profile)

20 changes: 20 additions & 0 deletions django/insta/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django import forms
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from django.contrib.auth import get_user_model
from .models import Profile

class CustomUserCreationForm(UserCreationForm):
class Meta:
model= get_user_model
fields = UserCreationForm.Meta.fields

class CustomUserChangeForm(UserCreationForm):
class Meta:
model =


class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['nickname', 'introduction', 'image']

17 changes: 17 additions & 0 deletions django/insta/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
from django.db import models
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill
from django.conf import settings

# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete =models.CASCADE)
nickname = models.CharField(max_length=40, blank = True)
introduction = models.TextField(blank = True)
image = ProcessedImageField(
blank = True,
upload_to='profile/images', # 저장 위치
processors=[ResizeToFill(300,300)], # 처리할 작업 목록
format='JPEG', # 저장 포맷
options={'quality':90}, # 옵션
)

class User(AbstractUser):
followers = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name= 'followers')
15 changes: 15 additions & 0 deletions django/insta/accounts/templates/accounts/people.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html' %}

{% block container %}


<div class ='container'>
<h1>{{ people.username }}</h1>
<div class="row">
{% for post in people.post_set.all %}
<div class="col-4">
<img src="{{ post.image.url }}" class="img-fluid"/>
</div>
{% endblock %}
</div>
</div>
13 changes: 13 additions & 0 deletions django/insta/accounts/templates/accounts/profile_update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'base.html' %}

{% block container %}

<h1>Profile Edit</h1>

<form method = "POST">
{% csrf_token %}
{{% bootstrap_form profile_form %}}
<input type="submit" value="Submit"/>
</form>

{% endblock %}
7 changes: 7 additions & 0 deletions django/insta/accounts/templates/accounts/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'base.html' %}

{% block container %}

<h1>User Edit</h1>

{% endblock %}
3 changes: 3 additions & 0 deletions django/insta/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
path('signup/', views.signup, name='signup'),
path('login/', views.login, name= 'login'),
path('logout/', views.logout, name='logout'),
path('update/', views.update, name='update'),
path('profile/update/', views.profile_update, name= 'profile_update'),

]

29 changes: 25 additions & 4 deletions django/insta/accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout
from django.contrib.auth import get_user_model
from .forms import CustomUserChangeForm, ProfileForm
from .models import Profile

# Create your views here.
def signup(request):
if request.user.is_authenticated:
return redirect('posts:list')

if request.method == 'POST':
signup_form = UserCreationForm(request.POST)
signup_form = CustomUserCreationForm(request.POST)
if signup_form.is_valid():
user = signup_form.save()
Profile.objects.create(user=user) # User의 Profile 생성
auth_login(request, user)
return redirect('posts:list')

else:
signup_form = UserCreationForm()
signup_form = CustomUserCreationForm()
return render(request, 'accounts/signup.html', {'signup_form':signup_form})

def login(request):
Expand All @@ -34,4 +38,21 @@ def login(request):

def logout(request):
auth_logout(request)
return redirect('posts:list')
return redirect('posts:list')

def people(request, username):
people = get_object_or_404(get_user_model(), username=username)
return render(request, 'accounts/people.html', {'people':people})

def update(request):
user_change_form = CustomUserChangeForm(instance=request.user)
return render(request, 'accounts/update.html', {'user_change_form':user_change_form})


def profile_update(request):
profile = request.user.profile
if request.method = "POST":
profile_form = ProfileForm(request instance=profile)
return render(request, 'accounts/profile_update.html', {
'profile_form':profile_form,
})
6 changes: 5 additions & 1 deletion django/insta/insta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,9 @@

STATIC_URL = '/static/'

# Media
MEDIA_URL='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Auth User Model
AUTH_USER_MODEL = 'accounts.User'
2 changes: 2 additions & 0 deletions django/insta/insta/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from accounts import views as accounts_views

urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls')),
path('accounts/', include('accounts.urls')),
path('<str:username>/', accounts_views.people, name='people'),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
18 changes: 16 additions & 2 deletions django/insta/posts/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
from django import forms
from .models import Post
from .models import Post, Comment, Image

class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['content','image',]
fields = ['content',]

class CommentForm(forms.ModelForm):
content = forms.CharField(label='', widget=forms.TextInput(attrs={'class':'form-control','placeholder':'댓글을 작성하세요'}))
class Meta:
model = Comment
fields = ['content',]

class ImageForm(forms.ModelForm):
class Meta:
model = Image
fields = ['file',]

ImageFormSet = forms.inlineformset_factory(Post, Image, form= ImageForm, extra=3)

25 changes: 25 additions & 0 deletions django/insta/posts/migrations/0005_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.1.8 on 2019-04-11 01:13

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),
('posts', '0004_auto_20190410_0510'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField()),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='posts.Post')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
20 changes: 20 additions & 0 deletions django/insta/posts/migrations/0006_post_like_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.1.8 on 2019-04-11 05:51

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('posts', '0005_comment'),
]

operations = [
migrations.AddField(
model_name='post',
name='like_user',
field=models.ManyToManyField(related_name='like_posts', to=settings.AUTH_USER_MODEL),
),
]
32 changes: 32 additions & 0 deletions django/insta/posts/migrations/0007_auto_20190415_0041.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 2.1.8 on 2019-04-15 00:41

from django.db import migrations, models
import django.db.models.deletion
import imagekit.models.fields
import posts.models


class Migration(migrations.Migration):

dependencies = [
('posts', '0006_post_like_user'),
]

operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', imagekit.models.fields.ProcessedImageField(upload_to=posts.models.post_image_path)),
],
),
migrations.RemoveField(
model_name='post',
name='image',
),
migrations.AddField(
model_name='image',
name='post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='posts.Post'),
),
]
14 changes: 12 additions & 2 deletions django/insta/posts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content = models.TextField()
# image = models.ImageField(blank=True)
image = ProcessedImageField(

like_user = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='like_posts')

class Image(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
file = ProcessedImageField(
upload_to=post_image_path, # 저장 위치
processors=[ResizeToFill(600,600)], # 처리할 작업 목록
format='JPEG', # 저장 포맷
options={'quality':90}, # 옵션
)
)

class Comment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField()
42 changes: 39 additions & 3 deletions django/insta/posts/templates/posts/_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
<span> {{ post.user.username }}</span>

</div>
{% if post.image %}
<img class="{{ post.image.url }}" class='card-img-top' alt="{{post.image}}">
{% endif %}
{% for image in post.image_set.all %}
<img class="{{ image.file.url }}" class='card-img-top' alt="{{ image.file }}">
{% endfor %}
<div class = "card-body">
<a href="{% url 'posts:like' post.id %}">
{% if user in post.like_user.all %}
<i class='fas fa-heart'></i>
{% else %}
<i class='far fa-heart'></i>
{% endif %}
</a>
<p class="card-text">
{{ post.like_user.count }}명이 좋아합니다.
</p>
</div>

<div class="card-body">
<h4 class="card-title">Card title</h4>
<p class="card-text">{{ post.content }}</p>
Expand All @@ -14,4 +27,27 @@ <h4 class="card-title">Card title</h4>
<a href="{% url 'posts:delete' post.id %}" class='btn btn-primary'>Edit</a>
{% endif %}
</div>
<div class="card-body">
{% for comment in post.comment_set.all %}
<div class="card-text">
<strong>{{ comment.user.username }}</strong> {{ comment.content }}
{% if comment.user == user %}
<a href="{% url 'post:comment_delete' post.id comment.id %}"></a>
{% endif %}
</div>
{% empty %}
<div class="card-text"> 댓글이 없습니다 </div>
{% endfor %}
</div>
{% if user.is_authenticated %}
<form action="{% url 'posts:comment_create' post.id %}" method="POST">
{% csrf_token %}
<div class='input-group'>
{{ comment_form }}
<div class="input-group-append">
<input type="submit" class="btn btn-primary"/>
</div>
</div>
</form>
{% endif %}
</div>
1 change: 1 addition & 0 deletions django/insta/posts/templates/posts/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ <h1>Post Form</h1>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form post_form %}
{{ image_formset.as_p }}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
Expand Down
3 changes: 3 additions & 0 deletions django/insta/posts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
path('create/', views.create, name='create'),
path('<int:post_id>/update/', views.update, name='update'),
path('<int:post_id>/delete/', views.delete, name='delete'),
path('<int:post_id>/comments/create/', views.comment_create, name='comment_create'),
path('<int:post_id>/comments/<int:comment_id>/delete/', views.comment_create, name='comment_create'),
path('<int:post_id>/like/', views.like, name='like'),
]
Loading

0 comments on commit 7cc6903

Please sign in to comment.