Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On branch ratingFix/Darkice000 #167

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file added newsaggregator/core/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added newsaggregator/core/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion newsaggregator/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from core.models import Headline,Bookmark,Contact
from core.models import Headline, Bookmark, Contact
admin.site.register(Headline)
admin.site.register(Bookmark)
admin.site.register(Contact)
Expand Down
19 changes: 19 additions & 0 deletions newsaggregator/core/migrations/0004_alter_rating_headline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.6 on 2024-06-23 05:24

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


class Migration(migrations.Migration):

dependencies = [
('core', '0003_headline_average_rating_headline_rating_count_and_more'),
]

operations = [
migrations.AlterField(
model_name='rating',
name='headline',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ratings', to='core.headline'),
),
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 4 additions & 3 deletions newsaggregator/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Headline(models.Model):
average_rating = models.FloatField(default=0, null=True, blank=True)
rating_count = models.IntegerField(default=0)
def __str__(self):
return self.title
return f'{self.id} -> {self.title}'


class Bookmark(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Expand All @@ -39,11 +40,11 @@ def __str__(self):
# stores all the rating given by all the users
class Rating(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
headline = models.ForeignKey(Headline, on_delete=models.CASCADE)
headline = models.ForeignKey(Headline, on_delete=models.CASCADE, related_name="ratings")
rating = models.IntegerField(choices=[(i, str(i)) for i in range(1, 6)], null=True, blank=True)

def __str__(self):
return f"{self.user} rated {self.headline.title} as {self.rating}"
return f"{self.id} -> ({self.user}) rated {self.headline.id} ({self.headline.title}) as {self.rating}"



Expand Down
3 changes: 1 addition & 2 deletions newsaggregator/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ def rate_headline(request, headline_id):
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': 'success', 'average_rating': round(headline.average_rating, 2), 'rating_count': headline.rating_count})

return JsonResponse({'status': 'fail'}, status=400)

Expand Down
Binary file modified newsaggregator/db.sqlite3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 7 additions & 7 deletions newsaggregator/templates/components/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@

<div class="rating-container">
<div class="rate" id="rate-{{ record.id }}">
<input type="radio" id="star5-{{ record.id }}" name="rate-{{ record.id }}" value="5" onclick="setRating('{{ record.id }}', 5)" {% if record.user_rating == 5 %}checked{% endif %} />
<input type="radio" id="star5-{{ record.id }}" name="rate-{{ record.id }}" value="5" onclick="setRating('{{ record.id }}', 5)" {% for rt in record.ratings.all %}{% if rt.user == request.user and rt.rating == 5 %} checked{% endif %}{%endfor %} />
<label for="star5-{{ record.id }}"></label>
<input type="radio" id="star4-{{ record.id }}" name="rate-{{ record.id }}" value="4" onclick="setRating('{{ record.id }}', 4)" {% if record.user_rating == 4 %}checked{% endif %} />
<input type="radio" id="star4-{{ record.id }}" name="rate-{{ record.id }}" value="4" onclick="setRating('{{ record.id }}', 4)" {% for rt in record.ratings.all %}{% if rt.user == request.user and rt.rating == 4 %} checked{% endif %}{%endfor %}/>
<label for="star4-{{ record.id }}"></label>
<input type="radio" id="star3-{{ record.id }}" name="rate-{{ record.id }}" value="3" onclick="setRating('{{ record.id }}', 3)" {% if record.user_rating == 3 %}checked{% endif %} />
<input type="radio" id="star3-{{ record.id }}" name="rate-{{ record.id }}" value="3" onclick="setRating('{{ record.id }}', 3)" {% for rt in record.ratings.all %}{% if rt.user == request.user and rt.rating == 3 %} checked{% endif %}{%endfor %}/>
<label for="star3-{{ record.id }}"></label>
<input type="radio" id="star2-{{ record.id }}" name="rate-{{ record.id }}" value="2" onclick="setRating('{{ record.id }}', 2)" {% if record.user_rating == 2 %}checked{% endif %} />
<input type="radio" id="star2-{{ record.id }}" name="rate-{{ record.id }}" value="2" onclick="setRating('{{ record.id }}', 2)" {% for rt in record.ratings.all %}{% if rt.user == request.user and rt.rating == 2 %} checked{% endif %}{%endfor %}/>
<label for="star2-{{ record.id }}"></label>
<input type="radio" id="star1-{{ record.id }}" name="rate-{{ record.id }}" value="1" onclick="setRating('{{ record.id }}', 1)" {% if record.user_rating == 1 %}checked{% endif %} />
<input type="radio" id="star1-{{ record.id }}" name="rate-{{ record.id }}" value="1" onclick="setRating('{{ record.id }}', 1)" {% for rt in record.ratings.all %}{% if rt.user == request.user and rt.rating == 1 %} checked{% endif %}{%endfor %}/>
<label for="star1-{{ record.id }}"></label>
</div>
<div class="average-rating" id="average-rating-{{ record.id }}">
{% if record.average_rating %}
({{ record.average_rating }})
({{ record.average_rating | floatformat:2}})
{% endif %}
<button id="submit-btn-{{ record.id }}" style="font-size: 16px; display: flex; align-items: center; justify-content: center; min-width: 30px!important; height: 30px!important; border-radius: 50%; border: 2px solid var(--foreground-tertiary);" class="submit-btn" onclick="submitRating('{{ record.id }}')">
<span class="material-symbols-outlined">check</span>
Expand Down Expand Up @@ -74,7 +74,7 @@
<i style="font-size: 16px;" class="social-icon fa fa-bookmark"></i>
{% endif %}
</a>
<a style="display: flex; align-items: center; justify-content: center; border: 2px solid var(--foreground-tertiary); padding: 0; min-width: 30px!important; width!important: 30px; height: 30px;!important" class="btn btn-danger" data-toggle="modal" data-target="#reportModal" aria-label="Report">
<a style="display: flex; align-items: center; justify-content: center; border: 2px solid var(--foreground-tertiary); padding: 0; min-width: 30px!important; width: 30px !important; height: 30px !important;" class="btn btn-danger" data-toggle="modal" data-target="#reportModal" aria-label="Report">
<span style="font-size: 16px;" class="material-symbols-outlined"> report </span>
</a>
<a style="display: flex; align-items: center; justify-content: center; border: 2px solid var(--foreground-tertiary); padding: 0; min-width: 30px!important; width: 30px; height: 30px;" class="btn copy-btn" onclick="copyToClipboard('{{ record.url }}')" aria-label="Copy URL">
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading