diff --git a/account/admin.py b/account/admin.py index acde1b7..1c64565 100755 --- a/account/admin.py +++ b/account/admin.py @@ -2,4 +2,4 @@ from .models import Profile # Register your models here. -admin.site.register(Profile) \ No newline at end of file +admin.site.register(Profile) diff --git a/account/models.py b/account/models.py index 56ae7f5..4f10b53 100755 --- a/account/models.py +++ b/account/models.py @@ -1,7 +1,11 @@ +from django.conf import settings from django.db import models # 유저 정보 import -from django.conf import settings + class Profile(models.Model): - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - image = models.ImageField(upload_to='profile/', blank=True) \ No newline at end of file + user = models.ForeignKey( + settings.AUTH_USER_MODEL, + on_delete=models.CASCADE, + ) + image = models.ImageField(upload_to='profile/', blank=True) diff --git a/account/tests.py b/account/tests.py index 7ce503c..601fc86 100755 --- a/account/tests.py +++ b/account/tests.py @@ -1,3 +1,2 @@ -from django.test import TestCase - +# from django.test import TestCase # Create your tests here. diff --git a/account/urls.py b/account/urls.py index 2fac5b8..a654c16 100755 --- a/account/urls.py +++ b/account/urls.py @@ -1,4 +1,5 @@ from django.urls import path + from . import views urlpatterns = [ @@ -7,4 +8,4 @@ path('logout/', views.logout, name='logout'), path('userpage/', views.userpage, name='userpage'), path('changeprofile/', views.change_profile, name='change_profile'), -] \ No newline at end of file +] diff --git a/account/views.py b/account/views.py index b917a4e..b6c8a07 100755 --- a/account/views.py +++ b/account/views.py @@ -1,24 +1,28 @@ -from django.shortcuts import render, redirect, get_object_or_404 -from django.http import HttpResponse -from django.contrib.auth.models import User -from django.contrib import auth -from page.models import Post -from account.models import Profile - -# make circle image -from PIL import Image, ImageOps, ImageDraw +import os from io import BytesIO + +from django.contrib import auth +from django.contrib.auth.models import User from django.core.files.uploadedfile import InMemoryUploadedFile +from django.http import HttpResponse +from django.shortcuts import redirect +from django.shortcuts import render +from PIL import Image +from PIL import ImageDraw +from PIL import ImageOps +from account.models import Profile +from page.models import Post +# make circle image # default image path -import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + def register(request): if request.method == 'POST': if request.POST['pw'] == request.POST['confirm-pw']: user = User.objects.create_user( - request.POST['id'], password=request.POST['pw'] + request.POST['id'], password=request.POST['pw'], ) auth.login(request, user) profile = Profile() @@ -29,6 +33,7 @@ def register(request): else: return render(request, 'register.html') + def login(request): if request.method == 'POST': username = request.POST['id'] @@ -36,27 +41,34 @@ def login(request): user = auth.authenticate(request, username=username, password=password) if user is not None: auth.login(request, user) - return redirect ('home') + return redirect('home') else: return HttpResponse('입력 정보를 확인하세요.') else: return render(request, 'login.html') + def logout(request): auth.logout(request) return redirect('home') + def userpage(request, author_id): # 유저 객체를 user에 저장 account = User.objects.get(username=author_id) # 해당 유저의 포스트만 불러오기 posts = Post.objects.filter(author=account) profile = Profile.objects.filter(user=account) - return render(request, 'userpage.html', {'posts':posts, 'account':account, 'profile':profile}) + return render( + request, 'userpage.html', { + 'posts': posts, + 'account': account, 'profile': profile, + }, + ) def change_profile(request): - # if profile_image exist, delete this image + # if profile_image exist, delete this image if Profile.objects.filter(user=request.user): Profile.objects.filter(user=request.user).delete() @@ -65,17 +77,17 @@ def change_profile(request): # save profile_image if 'image' in request.FILES: im = Image.open(request.FILES['image']) - im = im.resize((1920, 1920)); + im = im.resize((1920, 1920)) bigsize = (im.size[0] * 3, im.size[1] * 3) mask = Image.new('L', bigsize, 0) - draw = ImageDraw.Draw(mask) + draw = ImageDraw.Draw(mask) draw.ellipse((0, 0) + bigsize, fill=255) mask = mask.resize(im.size, Image.ANTIALIAS) im.putalpha(mask) output = ImageOps.fit(im, mask.size, centering=(0.5, 0.5)) output.putalpha(mask) - + buffer = BytesIO() output.save(buffer, format='png') @@ -90,4 +102,4 @@ def change_profile(request): profile.image = file profile.save() - return redirect('/account/userpage/'+str(profile.user)) \ No newline at end of file + return redirect('/account/userpage/'+str(profile.user)) diff --git a/comment/admin.py b/comment/admin.py index f567340..899a017 100755 --- a/comment/admin.py +++ b/comment/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin + from .models import Comment # Register your models here. -admin.site.register(Comment) \ No newline at end of file +admin.site.register(Comment) diff --git a/comment/forms.py b/comment/forms.py index ea4cbcb..244e13b 100755 --- a/comment/forms.py +++ b/comment/forms.py @@ -1,7 +1,9 @@ from django import forms + from .models import Comment + class CommentForm(forms.ModelForm): class Meta: model = Comment - fields = ['content'] \ No newline at end of file + fields = ['content'] diff --git a/comment/models.py b/comment/models.py index d61033d..72fd5d5 100755 --- a/comment/models.py +++ b/comment/models.py @@ -1,15 +1,22 @@ -from django.db import models -# 유저 정보 import from django.conf import settings +from django.db import models + from page.models import Post +# 유저 정보 import # Create your models here. + + class Comment(models.Model): # related_name - title = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments') - author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + title = models.ForeignKey( + Post, on_delete=models.CASCADE, related_name='comments', + ) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, on_delete=models.CASCADE, + ) pub_date = models.DateTimeField('publish') content = models.CharField(max_length=200) def __str__(self): - return self.content \ No newline at end of file + return self.content diff --git a/comment/tests.py b/comment/tests.py index 7ce503c..601fc86 100755 --- a/comment/tests.py +++ b/comment/tests.py @@ -1,3 +1,2 @@ -from django.test import TestCase - +# from django.test import TestCase # Create your tests here. diff --git a/comment/urls.py b/comment/urls.py index b30ce30..1553d24 100755 --- a/comment/urls.py +++ b/comment/urls.py @@ -1,7 +1,11 @@ from django.urls import path + from . import views urlpatterns = [ path('new/', views.comment_new, name='comment_new'), - path('delete//', views.comment_delete, name='comment_delete'), -] \ No newline at end of file + path( + 'delete//', + views.comment_delete, name='comment_delete', + ), +] diff --git a/comment/views.py b/comment/views.py index cde25b5..49f3679 100755 --- a/comment/views.py +++ b/comment/views.py @@ -1,10 +1,13 @@ -from django.shortcuts import render, redirect, get_object_or_404 +from django.shortcuts import get_object_or_404 +from django.shortcuts import redirect from django.utils import timezone -from .forms import CommentForm + from .models import Comment from page.models import Post # Create your views here. + + def comment_new(request, post_id): post = get_object_or_404(Post, pk=post_id) @@ -17,6 +20,7 @@ def comment_new(request, post_id): comment.save() return redirect('post_detail', post_id) + def comment_delete(request, post_id, comment_id): comment = get_object_or_404(Comment, pk=comment_id) diff --git a/lionstagram/settings.py b/lionstagram/settings.py index a2cf505..087e974 100755 --- a/lionstagram/settings.py +++ b/lionstagram/settings.py @@ -1,35 +1,22 @@ -""" -Django settings for lionstagram project. - -Generated by 'django-admin startproject' using Django 2.1.5. - -For more information on this file, see -https://docs.djangoproject.com/en/2.1/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.1/ref/settings/ -""" - import os +import dj_database_url + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ - # SECURITY WARNING: keep the secret key used in production secret! # SECRET_KEY = 'mnu!)lvd7rkqcs21@esw7st%($-%*-k62-wrzhbjv*u^2#4_6s' -SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'mnu!)lvd7rkqcs21@esw7st%($-%*-k62-wrzhbjv*u^2#4_6s') +SECRET_KEY = os.environ.get( + 'DJANGO_SECRET_KEY', 'mnu!)lvd7rkqcs21@esw7st%($-%*-k62-wrzhbjv*u^2#4_6s', +) # SECURITY WARNING: don't run with debug turned on in production! # DEBUG = True -DEBUG = bool( os.environ.get('DJANGO_DEBUG', True) ) +DEBUG = bool(os.environ.get('DJANGO_DEBUG', True)) ALLOWED_HOSTS = ['*'] - # Application definition INSTALLED_APPS = [ @@ -75,7 +62,6 @@ WSGI_APPLICATION = 'lionstagram.wsgi.application' - # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases @@ -83,29 +69,31 @@ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } + }, } - # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + 'NAME': 'django.contrib.auth.password_validation.' + 'UserAttributeSimilarityValidator', }, { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + 'NAME': 'django.contrib.auth.password_validation.' + 'MinimumLengthValidator', }, { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + 'NAME': 'django.contrib.auth.password_validation.' + 'CommonPasswordValidator', }, { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + 'NAME': 'django.contrib.auth.password_validation.' + 'NumericPasswordValidator', }, ] - # Internationalization # https://docs.djangoproject.com/en/2.1/topics/i18n/ @@ -119,7 +107,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ @@ -135,6 +122,5 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Heroku: Update database configuration from $DATABASE_URL. -import dj_database_url db_from_env = dj_database_url.config(conn_max_age=500) DATABASES['default'].update(db_from_env) diff --git a/lionstagram/urls.py b/lionstagram/urls.py index dde5534..12ae734 100755 --- a/lionstagram/urls.py +++ b/lionstagram/urls.py @@ -13,12 +13,14 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings +from django.conf.urls.static import static from django.contrib import admin -from django.urls import path, include +from django.urls import include +from django.urls import path + import page.views # media 경로 -from django.conf import settings -from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), @@ -26,4 +28,4 @@ path('post/', include('page.urls')), path('account/', include('account.urls')), path('comment/', include('comment.urls')), -] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/lionstagram/wsgi.py b/lionstagram/wsgi.py index 4f0ea4d..fac4f4f 100755 --- a/lionstagram/wsgi.py +++ b/lionstagram/wsgi.py @@ -6,7 +6,6 @@ For more information on this file, see https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/ """ - import os from django.core.wsgi import get_wsgi_application diff --git a/manage.py b/manage.py index 183c658..cc4a00f 100755 --- a/manage.py +++ b/manage.py @@ -10,6 +10,6 @@ raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" + "forget to activate a virtual environment?", ) from exc execute_from_command_line(sys.argv) diff --git a/page/admin.py b/page/admin.py index f1d71c2..5b3d61b 100755 --- a/page/admin.py +++ b/page/admin.py @@ -1,6 +1,8 @@ from django.contrib import admin -from .models import Post, Like + +from .models import Like +from .models import Post # Register your models here. admin.site.register(Post) -admin.site.register(Like) \ No newline at end of file +admin.site.register(Like) diff --git a/page/models.py b/page/models.py index 9aec7b3..0f592a5 100755 --- a/page/models.py +++ b/page/models.py @@ -1,11 +1,20 @@ +from django.conf import settings from django.db import models # 유저 정보 import -from django.conf import settings # Create your models here. + + class Post(models.Model): - author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name='likes', through='Like') + author = models.ForeignKey( + settings.AUTH_USER_MODEL, on_delete=models.CASCADE, + ) + likes = models.ManyToManyField( + settings.AUTH_USER_MODEL, + blank=True, + related_name='likes', + through='Like', + ) pub_date = models.DateTimeField('publish') image = models.ImageField(upload_to='images/', blank=True) content = models.TextField() @@ -20,6 +29,10 @@ def summary(self): def likes_count(self): return self.likes.count() + class Like(models.Model): - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - post = models.ForeignKey(Post, on_delete=models.CASCADE) \ No newline at end of file + user = models.ForeignKey( + settings.AUTH_USER_MODEL, + on_delete=models.CASCADE, + ) + post = models.ForeignKey(Post, on_delete=models.CASCADE) diff --git a/page/templatetags/post_extras.py b/page/templatetags/post_extras.py index ab4ea09..568d127 100644 --- a/page/templatetags/post_extras.py +++ b/page/templatetags/post_extras.py @@ -3,6 +3,7 @@ register = template.Library() + @register.filter def in_profiles(profiles, author): - return profiles.filter(user=author) \ No newline at end of file + return profiles.filter(user=author) diff --git a/page/tests.py b/page/tests.py index 7ce503c..601fc86 100755 --- a/page/tests.py +++ b/page/tests.py @@ -1,3 +1,2 @@ -from django.test import TestCase - +# from django.test import TestCase # Create your tests here. diff --git a/page/urls.py b/page/urls.py index f84a2e9..041084b 100755 --- a/page/urls.py +++ b/page/urls.py @@ -1,4 +1,5 @@ from django.urls import path + from . import views urlpatterns = [ @@ -7,4 +8,4 @@ path('delete/', views.post_delete, name='post_delete'), path('edit/', views.post_edit, name='post_edit'), path('like/', views.post_like, name='post_like'), -] \ No newline at end of file +] diff --git a/page/views.py b/page/views.py index 9f333f5..0d440a4 100755 --- a/page/views.py +++ b/page/views.py @@ -1,29 +1,33 @@ -from django.shortcuts import render, redirect, get_object_or_404 -from django.utils import timezone -from .models import Post, Like -from django.conf import settings -#Like module import -from django.http import HttpResponse import json +from io import BytesIO + from django.contrib.auth.decorators import login_required +from django.core.files.uploadedfile import InMemoryUploadedFile +from django.http import HttpResponse +from django.shortcuts import get_object_or_404 +from django.shortcuts import redirect +from django.shortcuts import render +from django.utils import timezone from django.views.decorators.http import require_POST +from PIL import Image + +from .models import Like +from .models import Post from account.models import Profile -from PIL import Image, ExifTags -from PIL.ExifTags import TAGS -from io import BytesIO -from django.core.files.uploadedfile import InMemoryUploadedFile def home(request): posts = Post.objects profiles = Profile.objects - return render(request, 'home.html', {'posts': posts, 'profiles':profiles}) + return render(request, 'home.html', {'posts': posts, 'profiles': profiles}) def post_detail(request, post_id): post = get_object_or_404(Post, pk=post_id) profile = Profile.objects.filter(user=post.author) - return render(request, 'post_detail.html', {'post': post, 'profile':profile}) + return render( + request, 'post_detail.html', {'post': post, 'profile': profile}, + ) def post_new(request): @@ -36,15 +40,15 @@ def post_new(request): if 'image' in request.FILES: image = Image.open(request.FILES['image']) exif = image._getexif() - orientation_key = 274 # cf ExifTags - + orientation_key = 274 # cf ExifTags + if exif and orientation_key in exif: orientation = exif[orientation_key] rotate_values = { 3: Image.ROTATE_180, 6: Image.ROTATE_270, - 8: Image.ROTATE_90 + 8: Image.ROTATE_90, } if orientation in rotate_values: @@ -69,6 +73,7 @@ def post_new(request): else: return render(request, 'post_new.html') + def post_delete(request, post_id): post = get_object_or_404(Post, pk=post_id) if post.author == request.user: @@ -96,6 +101,8 @@ def post_edit(request, post_id): return redirect('home') # 좋아요 구현 + + @login_required @require_POST def post_like(request): @@ -103,12 +110,14 @@ def post_like(request): post = get_object_or_404(Post, pk=pk) # 해당 포스트 # Like create - post_like, post_like_created = Like.objects.get_or_create(user=request.user, post=post) + post_like, post_like_created = Like.objects.get_or_create( + user=request.user, post=post, + ) if not post_like_created: post_like.delete() # Like count likes_count = Like.objects.filter(post=post, post_id=pk).count() - content = {'likes_count': likes_count} + content = {'likes_count': likes_count} return HttpResponse(json.dumps(content))