views.py 2.4 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
        student_listings = Listing.objects.filter(poster=self.get_object().pk)
22

23
        total_exchanges = student_listings.filter(exchanged=True).count()
24

25
        total_proceeds = Bid.objects.filter(listing__poster__user=self.get_object()).filter(listing__exchanged=True).aggregate(Sum('price'))['price__sum']
26

27
        student_ratings = Rating.objects.filter(listing__poster__user=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
28
29
30
31
32
33
        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
Daniel W Bond's avatar
Daniel W Bond committed
34

Daniel W Bond's avatar
Daniel W Bond committed
35
        context['avg_stars'] = average_stars
36
        context['listings'] = student_listings
37
        context['lookouts'] = Lookout.objects.filter(owner=self.get_object())
38

39
        context['proceeds'] = total_proceeds
40
        context['exchanges'] = total_exchanges
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

Daniel W Bond's avatar
Daniel W Bond committed
46

Daniel W Bond's avatar
Daniel W Bond committed
47
48
49
50
51
52
53
54
55
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)

56
        student_ratings = Rating.objects.filter(listing__poster__user=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
57
58
59
60
61
62
63
64

        # 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
Daniel W Bond's avatar
Daniel W Bond committed
65

Daniel W Bond's avatar
Daniel W Bond committed
66
67
68
69
70
71
        context['avg_stars'] = average_stars

        context['student_ratings'] = student_ratings
        context['student_ratings_num'] = student_ratings.count()

        return context