Commit c89b74a2 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

added user ratings pages

parent 02cc0db8
from django.conf.urls import patterns, url
from core.views import DetailStudent
from core.views import DetailStudent, StudentRatings
urlpatterns = patterns('',
url(r'^(?P<slug>[\w-]+)/$',
DetailStudent.as_view(), name='profile'),
url(r'^(?P<slug>[\w-]+)/ratings/$',
StudentRatings.as_view(), name='ratings'),
)
......@@ -3,7 +3,7 @@ from braces.views import LoginRequiredMixin
from core.models import Student
from lookouts.models import Lookout
from trades.models import Listing, Bid
from trades.models import Listing, Bid, Rating
class DetailStudent(LoginRequiredMixin, DetailView):
......@@ -13,7 +13,9 @@ class DetailStudent(LoginRequiredMixin, DetailView):
login_url = 'login'
def get_context_data(self, **kwargs):
context = super(DetailStudent, self).get_context_data(**kwargs)
# can be done with sum & filter only solds
def total_sales(listings):
sales = 0
for listing in listings:
......@@ -21,6 +23,7 @@ class DetailStudent(LoginRequiredMixin, DetailView):
sales = sales + 1
return sales
# can be done with sum & filter only solds
def total_proceeds(listings):
proceeds = 0
for listing in listings:
......@@ -33,9 +36,15 @@ class DetailStudent(LoginRequiredMixin, DetailView):
student_listings = Listing.objects.filter(seller=self.get_object().pk)
context = super(DetailStudent, self).get_context_data(**kwargs)
#context['listings'] = Listing.objects.filter(seller='2')
student_ratings = Rating.objects.filter(listing__seller__user=self.get_object())
if student_ratings:
student_stars = [int(rating.stars) for rating in student_ratings]
print student_stars
average_stars = sum(student_stars)/float((len(student_stars)))
else:
average_stars = None
context['avg_stars'] = average_stars
context['listings'] = student_listings
context['me'] = self.get_object().pk
context['lookouts'] = Lookout.objects.filter(owner=self.get_object())
......@@ -46,3 +55,29 @@ class DetailStudent(LoginRequiredMixin, DetailView):
context['bids'] = Bid.objects.filter(bidder=self.get_object())
return context
class StudentRatings(LoginRequiredMixin, DetailView):
model = Student
template_name = 'ratings.html'
context_object_name = 'student'
login_url = 'login'
def get_context_data(self, **kwargs):
context = super(StudentRatings, self).get_context_data(**kwargs)
student_ratings = Rating.objects.filter(listing__seller__user=self.get_object())
# copied code!
if student_ratings:
student_stars = [int(rating.stars) for rating in student_ratings]
print student_stars
average_stars = sum(student_stars)/float((len(student_stars)))
else:
average_stars = None
context['avg_stars'] = average_stars
context['student_ratings'] = student_ratings
context['student_ratings_num'] = student_ratings.count()
return context
......@@ -12,3 +12,26 @@ def isbn_name(isbn):
return data['title']
else:
return isbn
@register.filter(name='full_stars')
def full_stars(avg_stars):
return range(int(avg_stars))
@register.filter(name='half_stars')
def half_stars(avg_stars):
if (avg_stars % 1) >= .5:
return True
else:
return False
@register.filter(name='empty_stars')
def empty_stars(avg_stars):
if half_stars(avg_stars):
return range(4 - int(avg_stars))
else:
return range(5 - int(avg_stars))
@register.filter(name='int_maker')
def int_maker(num):
return int(num)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment