-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from anurag6569201/main
fixed the issue fixes #142
- Loading branch information
Showing
14 changed files
with
181 additions
and
205 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django import forms | ||
|
||
|
||
class ContactForm(forms.Form): | ||
name = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'placeholder': 'Your Name'})) | ||
email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'Your Email'})) | ||
phone = forms.IntegerField(widget=forms.NumberInput(attrs={'placeholder': 'Your Phone Number'})) | ||
content = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Your Message'})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,197 @@ | ||
# your_app/views.py | ||
|
||
from django.shortcuts import render, redirect, get_object_or_404 | ||
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 | ||
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.forms import ContactForm | ||
from django.template.loader import render_to_string | ||
from django.core.mail import send_mail | ||
|
||
|
||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import render, redirect, get_object_or_404 | ||
from core.models import Headline, Bookmark | ||
from django.contrib import messages | ||
from django.views.decorators.csrf import csrf_exempt | ||
import json | ||
|
||
# def base_view(request): | ||
# return render(request, 'core/base.html') | ||
|
||
@custom_login_required | ||
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage | ||
#view for scraping new | ||
|
||
def scrape(request, name): | ||
Headline.objects.all().delete() | ||
Headline.objects.all().delete() #remove all existing records from table | ||
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("../") | ||
|
||
# @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 | ||
|
||
@login_required(login_url='userauths:sign-in') | ||
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) | ||
|
||
|
||
@custom_login_required | ||
@login_required(login_url='userauths:sign-in') | ||
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 += 1 | ||
if count <= 8: | ||
count=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() | ||
new_headline.save() ##saving each record to news_headline | ||
|
||
headlines = Headline.objects.all()[::-1] | ||
context = { | ||
"object_list": headlines, | ||
} | ||
return render(request, "core/index.html", context) | ||
|
||
# @custom_login_required | ||
|
||
def about(request): | ||
context = {} | ||
context={ | ||
|
||
} | ||
return render(request, "core/about.html", context) | ||
|
||
# @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') | ||
message = request.POST.get('message') | ||
contact = Contact() | ||
contact.name = name | ||
contact.email = email | ||
contact.phone = phone | ||
contact.message = message | ||
contact.save() | ||
messages.success(request, "Thanks for contacting us") | ||
return redirect("/contact.html") | ||
return render(request, "core/contact.html") | ||
|
||
# @custom_login_required | ||
def contact(request): | ||
today_date = datetime.now().strftime('%Y-%m-%d') | ||
if request.method == "POST": | ||
form = ContactForm(request.POST) | ||
|
||
if form.is_valid(): | ||
name = form.cleaned_data['name'] | ||
email = form.cleaned_data['email'] | ||
phone = form.cleaned_data['phone'] | ||
content = form.cleaned_data['content'] | ||
|
||
html = render_to_string('components/email.html', { | ||
'name': name, | ||
'email': email, | ||
'phone': phone, | ||
'content': content, | ||
}) | ||
|
||
send_mail("The contact form subject", 'this is the message', email, ['[email protected]'], html_message=html) | ||
messages.success(request, 'Form submitted successfully!') | ||
return redirect("core:index") | ||
else: | ||
form = ContactForm() | ||
|
||
context={ | ||
'today_date': today_date, | ||
'form': form, | ||
} | ||
return render(request,"core/contact.html",context) | ||
|
||
@login_required(login_url='userauths:sign-in') | ||
def advertise(request): | ||
context = {} | ||
context={ | ||
|
||
} | ||
return render(request, "core/advertise.html", context) | ||
|
||
# @custom_login_required | ||
@login_required(login_url='userauths:sign-in') | ||
def privacy(request): | ||
context = {} | ||
context={ | ||
|
||
} | ||
return render(request, "core/privacy.html", context) | ||
|
||
# @custom_login_required | ||
|
||
@login_required | ||
def view_bookmarks(request): | ||
# Get the list of bookmarked headline IDs for the current user | ||
user_bookmarked_headline_ids = request.user.bookmark_set.values_list('headline_id', flat=True) | ||
bookmarks = Bookmark.objects.filter(user=request.user).select_related('headline') | ||
if bookmarks.exists(): | ||
context = {'bookmarks': bookmarks} | ||
else: | ||
context = {'message': 'You have no bookmarks yet.'} | ||
context = { | ||
'bookmarks': bookmarks, | ||
'user_bookmarked_headline_ids': user_bookmarked_headline_ids, | ||
} | ||
return render(request, 'core/bookmarks.html', context) | ||
|
||
@csrf_exempt | ||
# @custom_login_required | ||
@login_required(login_url='userauths:sign-in') | ||
def bookmark_article(request, headline_id): | ||
if request.method == 'POST': | ||
headline = get_object_or_404(Headline, id=headline_id) | ||
|
@@ -165,15 +200,14 @@ def bookmark_article(request, headline_id): | |
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) | ||
|
||
@csrf_exempt | ||
# @custom_login_required | ||
@login_required(login_url='userauths:sign-in') | ||
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 | ||
# @custom_login_required | ||
|
||
@csrf_exempt | ||
def rate_headline(request, headline_id): | ||
if request.method == 'POST': | ||
|
@@ -205,3 +239,5 @@ def top_rated_articles(request): | |
top_rated_articles = Headline.objects.filter(average_rating__gte=3.5).order_by('-average_rating') | ||
paginator = Paginator(top_rated_articles, 9) | ||
page = request | ||
|
||
|
Oops, something went wrong.