views.py 2.82 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
6
# imports from your apps
from .models import Student
7
from lookouts.models import Lookout
Daniel W Bond's avatar
Daniel W Bond committed
8
from trades.models import Listing, Bid, Rating
9
10


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

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

Daniel W Bond's avatar
Daniel W Bond committed
20
        # can be done with sum & filter only solds
21
22
23
24
25
26
27
        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
28
        # can be done with sum & filter only solds
29
30
31
32
        def total_proceeds(listings):
            proceeds = 0
            for listing in listings:
                if listing.sold:
Daniel W Bond's avatar
Daniel W Bond committed
33
34
35
36
                    try:
                        proceeds = proceeds + listing.final_price()
                    except:
                        pass
37
38
39
40
            return proceeds

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

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

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

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

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

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