From 2fb3a9899c63262cdd32c84eef7e4d8868621ce5 Mon Sep 17 00:00:00 2001 From: mudassir-jmi Date: Mon, 29 Jul 2024 21:20:57 +0530 Subject: [PATCH 1/2] add delete post functionality and setup project --- chat/templates/chat/add_post.html | 103 ++++++++++------ chat/templates/chat/home.html | 191 ++++++++++++++++++------------ chat/urls.py | 6 +- chat/views.py | 96 +++++++++------ requirements.txt | 5 +- 5 files changed, 246 insertions(+), 155 deletions(-) diff --git a/chat/templates/chat/add_post.html b/chat/templates/chat/add_post.html index 29bb737..d7f0847 100644 --- a/chat/templates/chat/add_post.html +++ b/chat/templates/chat/add_post.html @@ -2,48 +2,75 @@ {% load widget_tweaks %} {% block title %} - New Post +New Post {% endblock %} {% block content %} -
-
-

New Post

-
- - {% if form.non_field_errors %} - - {% endif %} - -
- {% csrf_token %} - {% for field in form %} -
- {% if field.errors %} - - {% endif %} - - {{ field.label_tag }} - {{ field|add_class:'form-control' }} -
- {% endfor %} - -
+
+
+

New Post

+
+ + {% if form.non_field_errors %} + + {% endif %} + +
+ {% csrf_token %} + {% for field in form %} +
+ {% if field.errors %} + + {% endif %} + + {{ field.label_tag }} + {{ field|add_class:'form-control' }} +
+ {% endfor %} + +
-
-

Me

- {% include 'accounts/profile.html' %} -
+
+

Me

+ {% include 'accounts/profile.html' %} +
+
+ + {% endblock %} \ No newline at end of file diff --git a/chat/templates/chat/home.html b/chat/templates/chat/home.html index 9744e1c..2c7e10a 100644 --- a/chat/templates/chat/home.html +++ b/chat/templates/chat/home.html @@ -1,90 +1,133 @@ {% extends 'base.html' %} - {% load widget_tweaks %} - {% block title %} - Feeds +Feeds {% endblock %} {% block content %} -
-
-

News Feed

-
- {% for post in posts %} -
- {% if post.picture %} -
- -
+
+
+

+ News Feed + + + +

+ + +
+ +
+ +
+ {% for post in posts %} +
+ {% if post.picture %} +
+ +
+ {% endif %} +
+
+ {% if post.user == request.user %} + Me + {% else %} + + + {{ post.user.first_name }} {{ post.user.last_name }} + + + {% endif %} +
+
+ {{ post.text }} +
+
+ -
-

Me

- {% include 'accounts/profile.html' %} + {% endfor %}
+
+

Me

+ {% include 'accounts/profile.html' %} +
+
+ + {% endblock %} \ No newline at end of file diff --git a/chat/urls.py b/chat/urls.py index e45f7de..8f8a7c8 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -1,5 +1,3 @@ -""" soMedia URL Configuration """ - from django.urls import path from . import views @@ -7,5 +5,7 @@ urlpatterns = [ path('', views.home, name='home'), path('posts/add', views.add_post, name='add_post'), - path('comments/add/', views.add_comment, name='add_comment') + path('comments/add/', views.add_comment, name='add_comment'), + path('posts/delete//', views.delete_post, name='delete_post'), + path('posts/search', views.search_posts, name='search_posts'), # New URL pattern for search ] diff --git a/chat/views.py b/chat/views.py index fd53195..1928b60 100644 --- a/chat/views.py +++ b/chat/views.py @@ -1,55 +1,77 @@ from django.contrib.auth.decorators import login_required -from django.shortcuts import redirect, render +from django.shortcuts import redirect, render, get_object_or_404 from django.urls import reverse +from django.http import JsonResponse from django.views.decorators.http import require_POST from .forms import CommentForm, PostForm from .models import Post - @login_required def home(request): - """ The home news feed page """ - - # Get users whose posts to display on news feed and add users account - _users = list(request.user.followers.all()) - _users.append(request.user) - - # Get posts from users accounts whose posts to display and order by latest - posts = Post.objects.filter(user__in=_users).order_by('-posted_date') - comment_form = CommentForm() - return render(request, 'chat/home.html', {'posts': posts, 'comment_form': comment_form}) + """The home news feed page.""" + # Get users whose posts to display on news feed and add user's account + _users = list(request.user.followers.all()) + _users.append(request.user) + # Get posts from users' accounts whose posts to display and order by latest + posts = Post.objects.filter(user__in=_users).order_by('-posted_date') + comment_form = CommentForm() + return render(request, 'chat/home.html', {'posts': posts, 'comment_form': comment_form}) @login_required def add_post(request): - """ create a new posts to user """ - # handle only POSTed Data - if request.method == 'POST': - form = PostForm(request.POST, request.FILES) - # validate form based on form definition - if form.is_valid(): - post = form.save(commit=False) - # add the post user to the existing form - # this method can be declared in the postForm easily by overiding the save() method - # and adding user before saving. - # See implementation of CommentForm - post.user = request.user - post.save() - return redirect('chat:home') - else: - form = PostForm() - return render(request, 'chat/add_post.html', {'form': form}) + """Create a new post for the user.""" + if request.method == 'POST': + form = PostForm(request.POST, request.FILES) + if form.is_valid(): + post = form.save(commit=False) + post.user = request.user + post.save() + return redirect('chat:home') + else: + # Handle form errors (e.g., display them on the template) + return render(request, 'chat/add_post.html', {'form': form}) + else: + form = PostForm() + return render(request, 'chat/add_post.html', {'form': form}) @login_required @require_POST def add_comment(request, post_id): - """ Add a comment to a post """ - - form = CommentForm(request.POST) - if form.is_valid(): - # pass the post id to the comment save() method which was overriden - # in the CommentForm implementation - comment = form.save(Post.objects.get(id=post_id), request.user) - return redirect(reverse('chat:home')) + """Add a comment to a post.""" + form = CommentForm(request.POST) + if form.is_valid(): + comment = form.save(Post.objects.get(id=post_id), request.user) + return redirect(reverse('chat:home')) + +@login_required +def delete_post(request, post_id): + """Delete a post.""" + post = get_object_or_404(Post, id=post_id, user=request.user) + post.delete() + return redirect('chat:home') + +@login_required +def search_posts(request): + query = request.GET.get('q', '') + user_followers = list(request.user.followers.all()) + user_followers.append(request.user) + + if query: + posts = Post.objects.filter(user__in=user_followers, text__icontains=query).order_by('-posted_date') + else: + posts = Post.objects.none() + + results = [] + for post in posts: + results.append({ + 'id': post.id, + 'text': post.text, + 'posted_date': post.posted_date.strftime('%Y-%m-%d %H:%M:%S'), + 'user': f'{post.user.first_name} {post.user.last_name}', + 'picture_url': post.picture.url if post.picture else '' + }) + + return JsonResponse({'results': results}) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index a19e7ab..e1469af 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -Django==2.2.13 -Pillow==6.0.0 -pkg-resources==0.0.0 +Django==3.2.5 +djangorestframework==3.12.4 pytz==2019.1 sqlparse==0.3.0 From 9d9c6e67d408d785872a69ee52ac28c91c530bda Mon Sep 17 00:00:00 2001 From: mudassir-jmi Date: Thu, 1 Aug 2024 23:43:16 +0530 Subject: [PATCH 2/2] add delete post functionaliy --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index bdbf787..1189e49 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # soMedia + Made with Django. ![ScreenShot](/libs/static/images/page-shot.png) A simple social Media Application for sharing images amongst users. This application was developed for teaching django to new learners and to expose them to the numerous functionalities of django. ## Features + - Registration - Login - Profile Editing @@ -21,8 +23,11 @@ A simple social Media Application for sharing images amongst users. This applica - View Other Users Profile ## Installation + ### Create a Virtualenv + - Windows + ```bash pip install virtualenv virtualenv .venv @@ -30,6 +35,7 @@ A simple social Media Application for sharing images amongst users. This applica ``` - Linux + ```bash sudo pip3 install virtualenv virtualenv .venv -p python3 @@ -37,16 +43,20 @@ A simple social Media Application for sharing images amongst users. This applica ``` ### Install Requirements + ```bash pip install -r requirements.txt ``` ### Start Up server + ```bash python manage.py runserver ``` + The application should be available at http://localhost:8000/ through your browser ## Todo + - More Documentation - More Tests