Skip to content

Commit

Permalink
Merge pull request #65 from anurag6569201/main
Browse files Browse the repository at this point in the history
paginator and bookmark #47 and #57
  • Loading branch information
SiddharthBahuguna authored May 21, 2024
2 parents ebe1b14 + 1a8a785 commit a58e120
Show file tree
Hide file tree
Showing 59 changed files with 532 additions and 646 deletions.
Binary file modified newsaggregator/core/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/forms.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/core/__pycache__/views.cpython-312.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions newsaggregator/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from news.models import Headline
from core.models import Headline,Bookmark

admin.site.register(Headline)
admin.site.register(Headline)
admin.site.register(Bookmark)
34 changes: 34 additions & 0 deletions newsaggregator/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.0.6 on 2024-05-21 11:05

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Headline',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('image', models.URLField(blank=True, null=True)),
('url', models.TextField()),
],
),
migrations.CreateModel(
name='Bookmark',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('headline', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.headline')),
],
),
]
Binary file not shown.
Binary file not shown.
15 changes: 14 additions & 1 deletion newsaggregator/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from django.db import models
from django.conf import settings

# Create your models here.
class Headline(models.Model):
title = models.CharField(max_length=200)#title char(200)
image = models.URLField(null=True, blank=True)#image
url = models.TextField()
def __str__(self):
return self.title

class Bookmark(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
headline = models.ForeignKey(Headline, on_delete=models.CASCADE)

def __str__(self):
return f"{self.user} - {self.headline.title}"
5 changes: 5 additions & 0 deletions newsaggregator/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
path('advertise/',views.advertise,name='advertise'),
path('privacy/',views.privacy,name='privacy'),
path('scrape/<str:name>', views.scrape, name="scrape"),

# bookmarking
path('bookmark/<int:headline_id>/', views.bookmark_article, name='bookmark_article'),
path('bookmarks/', views.view_bookmarks, name='view_bookmarks'),
path('remove_bookmark/<int:headline_id>/', views.remove_bookmark, name='remove_bookmark'),
]
67 changes: 58 additions & 9 deletions newsaggregator/core/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
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 news.models import Headline
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
# Create your views here.


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


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

def scrape(request, name):
Expand Down Expand Up @@ -51,21 +60,35 @@ def scrape(request, name):

@login_required(login_url='userauths:sign-in')
def news_list(request):
# Fetch all headlines
headlines = Headline.objects.all()[::-1] # store records in reverse order
# 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 = request.user.bookmark_set.values_list('headline_id', flat=True)
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,
"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')
def index(request):
Headline.objects.all().delete()
Expand Down Expand Up @@ -156,4 +179,30 @@ def privacy(request):
return render(request, "core/privacy.html", context)


@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')
context = {
'bookmarks': bookmarks,
'user_bookmarked_headline_ids': user_bookmarked_headline_ids,
}
return render(request, 'core/bookmarks.html', context)

@csrf_exempt
@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)
Bookmark.objects.get_or_create(user=request.user, headline=headline)
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)

@csrf_exempt
@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)
Binary file modified newsaggregator/db.sqlite3
Binary file not shown.
Empty file removed newsaggregator/news/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file removed newsaggregator/news/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file removed newsaggregator/news/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file removed newsaggregator/news/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file removed newsaggregator/news/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file not shown.
3 changes: 0 additions & 3 deletions newsaggregator/news/admin.py

This file was deleted.

6 changes: 0 additions & 6 deletions newsaggregator/news/apps.py

This file was deleted.

23 changes: 0 additions & 23 deletions newsaggregator/news/migrations/0001_initial.py

This file was deleted.

Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 0 additions & 6 deletions newsaggregator/news/migrations/__pycache__/urls.py

This file was deleted.

22 changes: 0 additions & 22 deletions newsaggregator/news/models.py

This file was deleted.

3 changes: 0 additions & 3 deletions newsaggregator/news/tests.py

This file was deleted.

12 changes: 0 additions & 12 deletions newsaggregator/news/urls.py

This file was deleted.

134 changes: 0 additions & 134 deletions newsaggregator/news/views.py

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file modified newsaggregator/newsaggregator/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file modified newsaggregator/newsaggregator/__pycache__/wsgi.cpython-312.pyc
Binary file not shown.
Loading

0 comments on commit a58e120

Please sign in to comment.