Skip to content

Commit

Permalink
On branch ratingFix/Darkice000
Browse files Browse the repository at this point in the history
 Your branch is up to date with 'origin/ratingFix/Darkice000'.

 Changes to be committed:
	modified:   core/models.py
	modified:   core/views.py
	modified:   templates/components/news.html

Fixes:

1. style attribute in news.html,type error.
2. Rating overflows after some long decimal places number encounters
3. Rating does not appearing from database for the user if he rated
   previously, but not showing when reloging. If he rate then shows but
   when relogin all disappears.
Not Fixed:

1. Rating button still not showing in green after being rated.
  • Loading branch information
Aarav Kumar authored and Aarav Kumar committed Jun 23, 2024
1 parent 9070c23 commit 1bede38
Show file tree
Hide file tree
Showing 31 changed files with 32 additions and 13 deletions.
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.

0 comments on commit 1bede38

Please sign in to comment.