Skip to content

Commit

Permalink
Merge pull request #204 from bounswe/kaan-image-test
Browse files Browse the repository at this point in the history
images available for profile and posts
  • Loading branch information
kaanguneyli authored May 11, 2024
2 parents 614bd81 + c433b79 commit 82d9bdd
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 20 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions backend/nba_app/migrations/0005_merge_20240511_1240.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Django 5.0.4 on 2024-05-11 12:40

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('nba_app', '0003_comment'),
('nba_app', '0003_follow_user_following'),
('nba_app', '0004_alter_post_image'),
]

operations = [
]
18 changes: 18 additions & 0 deletions backend/nba_app/migrations/0006_comment_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-05-11 14:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('nba_app', '0005_merge_20240511_1240'),
]

operations = [
migrations.AddField(
model_name='comment',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='comment_images/'),
),
]
17 changes: 17 additions & 0 deletions backend/nba_app/migrations/0007_remove_comment_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.4 on 2024-05-11 14:56

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('nba_app', '0006_comment_image'),
]

operations = [
migrations.RemoveField(
model_name='comment',
name='image',
),
]
1 change: 0 additions & 1 deletion backend/nba_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField(max_length=300, blank=False)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f'{self.user.username} on {self.post.post_id}: {self.content}'

Expand Down
6 changes: 3 additions & 3 deletions backend/nba_app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User #
fields = ['user_id', 'username', 'email', 'bio', 'image']
#
model = User
fields = ['user_id', 'username', 'email', 'bio', 'profile_picture']


class PostSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
2 changes: 1 addition & 1 deletion backend/nba_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
path('player/', views.player, name='player'),
path('csrf_token/', views.csrf_token, name='csrf_token'),
path('session/', views.session, name='session'),
path('log_out/', views.log_out, name='log_out')
path('log_out/', views.log_out, name='log_out'),
path('post/<int:post_id>/', views.post_detail, name='post_detail'),
path('post/<int:post_id>/comment/', views.create_comment, name='create_comment')
]
Expand Down
28 changes: 13 additions & 15 deletions backend/nba_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.urls import reverse
from .models import User, Post, Comment, Follow
import requests
from django.db import models
import os

def sign_up(request):
if request.method == "POST":
Expand All @@ -16,9 +16,7 @@ def sign_up(request):
username = request.POST.get("username")
email = request.POST.get("email")
password = request.POST.get("password")
#
profile_picture = request.FILES.get("image")
#
#profile_picture = request.FILES.get("profile_picture")
print("username: ", username, "email: ", email, "password: ", password)

if User.objects.filter(email=email).exists():
Expand All @@ -29,9 +27,9 @@ def sign_up(request):
# Return an error httpresponse if username is already taken
return HttpResponse("Username already taken.", status=400)

# Create and save user #
user = User.objects.create_user(username=username, email=email, password=password, profile_picture=profile_picture)
print("user created and saved: ", user) #
# Create and save user
user = User.objects.create_user(username=username, email=email, password=password)
print("user created and saved: ", user)

login(request, user)
print("user_logged in: ", user)
Expand All @@ -57,6 +55,7 @@ def log_in(request):

return render(request, 'login.html')


def log_out(request):
if request.method == "GET":
logout(request)
Expand All @@ -70,28 +69,26 @@ def post(request):
user = request.user
content = request.POST.get("content")
image = request.FILES.get("image")
#image_binary = image.read()
post = Post.objects.create(user=user, content=content, image=image)
#text = request.POST.get("post")
return HttpResponseRedirect(f'/post/{post.post_id}/')
return render(request, 'post.html')


@login_required
def create_comment(request, post_id):
if request.method == "POST":
user = request.user
content = request.POST.get("content")
post = Post.objects.get(post_id=post_id)

if post:
Comment.objects.create(user=user, content=content, post=post)
return HttpResponseRedirect(f'/post/{post_id}/')
else:
return HttpResponse("Post not found", status=404)

return render(request, 'comment.html', {'post_id': post_id})



def post_detail(request, post_id):
post = Post.objects.get(post_id=post_id)
comments = Comment.objects.get(post = post) #post.comments.all()
Expand Down Expand Up @@ -132,6 +129,7 @@ def profile_view_edit(request):

# Update profile picture if provided
if new_profile_picture:
os.remove(user.profile_picture.path)
user.profile_picture = new_profile_picture

# Update bio if provided
Expand All @@ -156,15 +154,15 @@ def profile_view_edit(request):
'username': user.username,
'email': user.email,
'bio': user.bio,
#'profile_picture': user.profile_picture.url if user.profile_picture else None,
'profile_picture': user.profile_picture.url if user.profile_picture else None,
# Add any other fields you want to include in the response
'following_count': following_count,
'followers_count': followers_count,
'profile_picture': user.profile_picture.url if user.profile_picture else None,
'posts': [{'content': post.content, 'created_at': post.created_at} for post in posts]
}
return JsonResponse(data, status=200)


return render(request, 'profile_view_edit.html', data)
#return JsonResponse(data, status=200)


def reset_password(request):
Expand Down
Binary file modified backend/requirements.txt
Binary file not shown.
50 changes: 50 additions & 0 deletions backend/templates/profile_view_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
</head>
<body>
<h1>User Profile</h1>
<div>
<h2>{{ username }}</h2>
<p>Email: {{ email }}</p>
<p>Bio: {{ bio }}</p>
{% if profile_picture %}
<img src="{{ profile_picture }}" alt="Profile Picture">
{% else %}
<p>No profile picture available</p>
{% endif %}
<p>Following: {{ following_count }}</p>
<p>Followers: {{ followers_count }}</p>
</div>

<h2>Posts</h2>
<ul>
{% for post in posts %}
<li>
<p>{{ post.content }}</p>
<p>Created At: {{ post.created_at }}</p>
</li>
{% endfor %}
</ul>

<!-- Add form for editing profile information -->
<h2>Edit Profile</h2>
<form action="{% url 'profile_view_edit' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" id="username"><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email"><br>
<label for="profile_picture">Profile Picture:</label>
<input type="file" name="profile_picture" id="profile_picture"><br>
<label for="bio">Bio:</label>
<textarea name="bio" id="bio"></textarea><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password"><br>
<input type="submit" value="Update">
</form>
</body>
</html>
1 change: 1 addition & 0 deletions backend/templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h1>Sign Up</h1>
<input type="password" id="password" name="password"><br>
<label for="confirm_password">Confirm Password:</label><br>
<input type="password" id="confirm_password" name="confirm_password"><br>
<input type="file" name="profile_picture"><br>
<input type="submit" value="Sign Up">
</form>
</body>
Expand Down

0 comments on commit 82d9bdd

Please sign in to comment.