Skip to content

Commit

Permalink
Merge pull request #131 from samarsajad/book2
Browse files Browse the repository at this point in the history
News displayed on home page
  • Loading branch information
SiddharthBahuguna authored Jun 6, 2024
2 parents 92454ad + 11c428f commit 5209a45
Show file tree
Hide file tree
Showing 12 changed files with 2,900 additions and 152 deletions.
13 changes: 13 additions & 0 deletions newsaggregator/core/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


from functools import wraps
from django.shortcuts import redirect
from django.urls import reverse

def custom_login_required(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
if not request.user.is_authenticated:
return redirect(reverse('core:index'))
return view_func(request, *args, **kwargs)
return _wrapped_view
3 changes: 3 additions & 0 deletions newsaggregator/core/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from django.urls import path
from core import views
from .views import submit_contact
# from .views import base_view
from .views import news_list

app_name='core'



urlpatterns=[
# path('base/', base_view, name='base'),
path('',views.news_list,name='index'),
path('about/', views.about, name='about'),
path('contact.html/',views.submit_contact,name='contact'),
Expand Down
218 changes: 74 additions & 144 deletions newsaggregator/core/views.py
Original file line number Diff line number Diff line change
@@ -1,201 +1,162 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

from django.shortcuts import render
import requests
from django.shortcuts import render, redirect
from bs4 import BeautifulSoup as BSoup
from core.models import Headline

from datetime import datetime

from core.models import Contact
from django.template.loader import render_to_string
from django.core.mail import send_mail
# your_app/views.py


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from core.models import Headline, Bookmark, Rating
from django.http import JsonResponse
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from .models import Headline, Bookmark, Rating, Contact
from django.contrib import messages
from .decorators import custom_login_required # Import the custom decorator
import requests
from bs4 import BeautifulSoup as BSoup
from django.views.decorators.csrf import csrf_exempt
import json

# def base_view(request):
# return render(request, 'core/base.html')

from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage
#view for scraping new

@custom_login_required
def scrape(request, name):
Headline.objects.all().delete() #remove all existing records from table
Headline.objects.all().delete()
session = requests.Session()
#useragent helps server to identify origin of request
#we are imitating request as google bot
session.headers = {"User-Agent": "Googlebot/2.1 (+http://www.google.com/bot.html)"}
#google bot is crawler program
#session.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}
url = f"https://www.theonion.com/{name}"
content = session.get(url).content
#print(content)#raw content
soup = BSoup(content, "html.parser")
#print(soup)
# finding all new div using common class
News = soup.find_all("div", {"class": "sc-cw4lnv-13 hHSpAQ"})
print(News)
for article in News:
#extracting news link,img url,title for each news
main = article.find_all("a", href=True)
linkx = article.find("a", {"class": "sc-1out364-0 dPMosf js_link"})
link = linkx["href"]
titlex = article.find("h2", {"class": "sc-759qgu-0 cvZkKd sc-cw4lnv-6 TLSoz"})
title = titlex.text
imgx = article.find("img")["data-src"]
#storing extracted data to model
new_headline = Headline()
new_headline.title = title
new_headline.url = link
new_headline.image = imgx
new_headline.save()
#saving details to table
return redirect("../")

@login_required(login_url='userauths:sign-in')
# @custom_login_required
# def news_list(request):
# headlines = Headline.objects.all().order_by('-id')
# swiper = Headline.objects.all()[:4]
# user_bookmarked_headline_ids = []
# if request.user.is_authenticated:
# user_bookmarked_headline_ids = request.user.bookmark_set.values_list('headline_id', flat=True)
# page = request.GET.get('page', 1)
# num_of_items = 9
# paginator = Paginator(headlines, num_of_items)
# try:
# headlines_obj = paginator.page(page)
# except PageNotAnInteger:
# headlines_obj = paginator.page(1)
# except EmptyPage:
# headlines_obj = paginator.page(paginator.num_pages)
# context = {
# "object_list": headlines_obj,
# "paginator": paginator,
# 'swiper': swiper,
# 'user_bookmarked_headline_ids': user_bookmarked_headline_ids,
# }
# return render(request, "core/index.html", context)
from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Headline

def news_list(request):
# Fetch all headlines in reverse order
headlines = Headline.objects.all().order_by('-id')
swiper = Headline.objects.all()[:4]


# Get the list of bookmarked headline IDs for the current user
user_bookmarked_headline_ids = []
if request.user.is_authenticated:
user_bookmarked_headline_ids = request.user.bookmark_set.values_list('headline_id', flat=True)

# Pagination logic
page = request.GET.get('page', 1)
num_of_items = 9
paginator = Paginator(headlines, num_of_items)

try:
headlines_obj = paginator.page(page)
except PageNotAnInteger:
headlines_obj = paginator.page(1)
except EmptyPage:
headlines_obj = paginator.page(paginator.num_pages)

context = {
"object_list": headlines_obj,
"paginator": paginator,
'swiper': swiper,
'user_bookmarked_headline_ids': user_bookmarked_headline_ids,
}
return render(request, "core/index.html", context)

@login_required(login_url='userauths:sign-in')

# @custom_login_required
def index(request):
Headline.objects.all().delete()
session = requests.Session()
session.headers = {"User-Agent": "Googlebot/2.1 (+http://www.google.com/bot.html)"}
url = f"https://www.theonion.com/latest"
content = session.get(url).content
soup = BSoup(content, "html.parser")

News = soup.find_all("div", {"class": "sc-cw4lnv-13 hHSpAQ"})
count=0
count = 0
for article in News:
count=count+1

if count<=8:
count += 1
if count <= 8:
main = article.find_all("a", href=True)

linkx = article.find("a", {"class": "sc-1out364-0 dPMosf js_link"})
link = linkx["href"]

titlex = article.find("h2", {"class": "sc-759qgu-0 cvZkKd sc-cw4lnv-6 TLSoz"})
title = titlex.text

imgx = article.find("img")["data-src"]

new_headline = Headline()
new_headline.title = title
new_headline.url = link
new_headline.image = imgx
new_headline.save() ##saving each record to news_headline

new_headline.save()
headlines = Headline.objects.all()[::-1]
context = {
"object_list": headlines,
}
return render(request, "core/index.html", context)

@login_required(login_url='userauths:sign-in')
# @custom_login_required
def about(request):
context={

}
context = {}
return render(request, "core/about.html", context)

@login_required(login_url='userauths:sign-in')
# @custom_login_required
def submit_contact(request):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
phone= request.POST.get('phone')
phone = request.POST.get('phone')
message = request.POST.get('message')
contact=Contact()
contact.name=name
contact.email=email
contact.phone=phone
contact.message=message
contact = Contact()
contact.name = name
contact.email = email
contact.phone = phone
contact.message = message
contact.save()
messages.success(request,"Thanks for contacting us")
messages.success(request, "Thanks for contacting us")
return redirect("/contact.html")
return render(request, "core/contact.html")


@login_required(login_url='userauths:sign-in')
# @custom_login_required
def advertise(request):
context={

}
context = {}
return render(request, "core/advertise.html", context)

@login_required(login_url='userauths:sign-in')
# @custom_login_required
def privacy(request):
context={

}
context = {}
return render(request, "core/privacy.html", context)


@login_required(login_url='userauths:sign-in')
# @custom_login_required
def view_bookmarks(request):
# Get the list of bookmarked headlines for the current user
bookmarks = Bookmark.objects.filter(user=request.user).select_related('headline')

if bookmarks.exists():
context = {'bookmarks': bookmarks}
else:
context = {'message': 'You have no bookmarks yet.'}


return render(request, 'core/bookmarks.html', context)
def view_bookmarks(request):
if request.user.is_authenticated:
# Get the list of bookmarked headlines for the current user
bookmarks = Bookmark.objects.filter(user=request.user).select_related('headline')

if bookmarks.exists():
context = {'bookmarks': bookmarks}
else:
context = {'message': 'You have no bookmarks yet.'}

return render(request, 'core/bookmarks.html', context)
bookmarks = Bookmark.objects.filter(user=request.user).select_related('headline')
if bookmarks.exists():
context = {'bookmarks': bookmarks}
else:
message = 'Sign in to view bookmarks.'
return render(request, 'core/index.html', {'message': message})
context = {'message': 'You have no bookmarks yet.'}
return render(request, 'core/bookmarks.html', context)

@csrf_exempt
@login_required(login_url='userauths:sign-in')
# @custom_login_required
def bookmark_article(request, headline_id):
if request.method == 'POST':
headline = get_object_or_404(Headline, id=headline_id)
Expand All @@ -204,74 +165,43 @@ def bookmark_article(request, headline_id):
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)

@csrf_exempt
@login_required(login_url='userauths:sign-in')
# @custom_login_required
def remove_bookmark(request, headline_id):
if request.method == 'POST':
Bookmark.objects.filter(user=request.user, headline_id=headline_id).delete()
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)

import json
@login_required
# @custom_login_required
@csrf_exempt
def rate_headline(request, headline_id):
if request.method == 'POST':
headline = get_object_or_404(Headline, id=headline_id)

try:
data = json.loads(request.body)
print("Received data:", data) # Print the entire JSON payload for debugging
rating_value_str = data.get('rating')
# print("rating_value_str:", rating_value_str) # Add this line for debugging
except json.JSONDecodeError:
return JsonResponse({'status': 'fail', 'message': 'Invalid JSON'}, status=400)

if rating_value_str is not None:
try:
rating_value = int(rating_value_str)
except ValueError:
return JsonResponse({'status': 'fail', 'message': 'Invalid rating value'}, status=400)
else:
return JsonResponse({'status': 'fail', 'message': 'Rating value is missing'}, status=400)

# Check if the user has already rated this headline
rating, created = Rating.objects.get_or_create(user=request.user, headline=headline)
rating.rating = rating_value
rating.save()

# Update headline average rating and rating count
ratings = Rating.objects.filter(headline=headline).exclude(rating__isnull=True)
headline.rating_count = ratings.count()
headline.average_rating = sum(r.rating for r in ratings) / headline.rating_count if headline.rating_count > 0 else 0
headline.save()

return JsonResponse({'status': 'success', 'average_rating': headline.average_rating, 'rating_count': headline.rating_count})
return JsonResponse({'status': 'fail'}, status=400)
from django.shortcuts import render
from .models import Headline

# @custom_login_required
def top_rated_articles(request):
if request.user.is_authenticated:
top_rated_articles = Headline.objects.filter(average_rating__gte=3.5).order_by('-average_rating')
else:
top_rated_articles = Headline.objects.none()

paginator = Paginator(top_rated_articles, 9) # 9 items per page

page = request.GET.get('page')
try:
top_rated_articles_obj = paginator.page(page)
except PageNotAnInteger:
top_rated_articles_obj = paginator.page(1)
except EmptyPage:
top_rated_articles_obj = paginator.page(paginator.num_pages)

context = {
'object_list': top_rated_articles_obj,
'paginator': paginator
}
return render(request, 'core/index.html', context)
from django.shortcuts import render

def about(request):
return render(request, 'core/about.html')

top_rated_articles = Headline.objects.filter(average_rating__gte=3.5).order_by('-average_rating')
paginator = Paginator(top_rated_articles, 9)
page = request
Loading

0 comments on commit 5209a45

Please sign in to comment.