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


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

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

24
        student_listings = Listing.objects.filter(poster=self.get_object().pk)
25

26
        total_exchanges = student_listings.filter(exchanged=True).count()
27

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

Daniel W Bond's avatar
Daniel W Bond committed
30
        student_ratings = Rating.objects.filter(listing__poster=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
31
32
33
34
35
36
        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
37

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

42
        context['proceeds'] = total_proceeds
43
        context['exchanges'] = total_exchanges
44

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

47
        return context
Daniel W Bond's avatar
Daniel W Bond committed
48

Daniel W Bond's avatar
Daniel W Bond committed
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class UpdateStudent(LoginRequiredMixin, FormView):
    template_name = 'update_student.html'
    form_class = UserNameForm
    login_url = 'login'

    def get_context_data(self, **kwargs):
        context = super(UpdateStudent, self).get_context_data(**kwargs)

        form = UserNameForm()

        form.fields['first_name'].initial = self.request.user.first_name
        form.fields['last_name'].initial = self.request.user.last_name

        context['form'] = form
        return context

    def form_valid(self, form):
        self.request.user.first_name = form.instance.first_name
        self.request.user.last_name = form.instance.last_name
        self.request.user.save()
        return super(UpdateStudent, self).form_valid(form)

    @ratelimit(key='user', rate='5/m', method='POST', block=True)
    @ratelimit(key='user', rate='10/day', method='POST', block=True)
    def post(self, request, *args, **kwargs):
        return super(UpdateStudent, self).post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse('profile',
                        kwargs={'slug': self.request.user.student.slug})

Daniel W Bond's avatar
Daniel W Bond committed
81
82
83
84
85
86
87
88
89
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)

Daniel W Bond's avatar
Daniel W Bond committed
90
        student_ratings = Rating.objects.filter(listing__poster=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
91
92
93
94
95
96
97
98

        # 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
99

Daniel W Bond's avatar
Daniel W Bond committed
100
101
102
103
104
105
        context['avg_stars'] = average_stars

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

        return context