views.py 2.39 KB
Newer Older
1
# core django imports
2
from django.views.generic import DetailView
3
# third-party imports
4
from braces.views import LoginRequiredMixin
5
from django.db.models import Sum
6
7
# imports from your apps
from .models import Student
8
from lookouts.models import Lookout
Daniel W Bond's avatar
Daniel W Bond committed
9
from trades.models import Listing, Bid, Rating
10
11


12
13
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
14
15
    template_name = 'profile.html'
    context_object_name = 'student'
16
    login_url = 'login'
17
18

    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
19
        context = super(DetailStudent, self).get_context_data(**kwargs)
20
21
22

        student_listings = Listing.objects.filter(seller=self.get_object().pk)

23
24
25
26
        total_sales = student_listings.filter(sold=True).count()

        total_proceeds = Bid.objects.filter(listing__seller__user=self.get_object()).filter(listing__sold=True).aggregate(Sum('price'))['price__sum']

Daniel W Bond's avatar
Daniel W Bond committed
27
28
29
30
31
32
33
34
35
        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
36
        context['listings'] = student_listings
37
        context['lookouts'] = Lookout.objects.filter(owner=self.get_object())
38

39
40
        context['proceeds'] = total_proceeds
        context['sales'] = total_sales
41

42
        context['bids'] = Bid.objects.filter(bidder=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
43

44
        return context
Daniel W Bond's avatar
Daniel W Bond committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

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