views.py 2.75 KB
Newer Older
1
2
3
4
from django.views.generic import DetailView
from braces.views import LoginRequiredMixin

from core.models import Student
5
from lookouts.models import Lookout
Daniel W Bond's avatar
Daniel W Bond committed
6
from trades.models import Listing, Bid, Rating
7
8


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

    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
16
        context = super(DetailStudent, self).get_context_data(**kwargs)
17

Daniel W Bond's avatar
Daniel W Bond committed
18
        # can be done with sum & filter only solds
19
20
21
22
23
24
25
        def total_sales(listings):
            sales = 0
            for listing in listings:
                if listing.sold:
                    sales = sales + 1
            return sales

Daniel W Bond's avatar
Daniel W Bond committed
26
        # can be done with sum & filter only solds
27
28
29
30
        def total_proceeds(listings):
            proceeds = 0
            for listing in listings:
                if listing.sold:
Daniel W Bond's avatar
Daniel W Bond committed
31
32
33
34
                    try:
                        proceeds = proceeds + listing.final_price()
                    except:
                        pass
35
36
37
38
            return proceeds

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

Daniel W Bond's avatar
Daniel W Bond committed
39
40
41
42
43
44
45
46
47
        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
48
        context['listings'] = student_listings
49
        context['me'] = self.get_object().pk
50
        context['lookouts'] = Lookout.objects.filter(owner=self.get_object())
51
52
53
54

        context['proceeds'] = total_proceeds(student_listings)
        context['sales'] = total_sales(student_listings)

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

57
        return context
Daniel W Bond's avatar
Daniel W Bond committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

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