views.py 3.98 KB
Newer Older
1
# core django imports
2
from django.views.generic import DetailView, FormView
3
from django.urls 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
    template_name = 'profile.html'
18
    login_url = 'login'
19
20

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

23
        student_listings = Listing.objects.filter(poster=self.get_object().pk)
24
25
        new_student_listings = [li for li in student_listings if not li.old()]
        old_student_listings = [li for li in student_listings if li.old()]
26

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

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

Daniel W Bond's avatar
Daniel W Bond committed
31
        student_ratings = Rating.objects.filter(listing__poster=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
32
33
        if student_ratings:
            student_stars = [int(rating.stars) for rating in student_ratings]
34
            print(student_stars)
Daniel W Bond's avatar
Daniel W Bond committed
35
36
37
            average_stars = sum(student_stars)/float((len(student_stars)))
        else:
            average_stars = None
Daniel W Bond's avatar
Daniel W Bond committed
38

39
40
41
42
        student_bids = Bid.objects.filter(bidder=self.get_object())
        new_bids = [bid for bid in student_bids if not bid.listing.old()]
        old_bids = [bid for bid in student_bids if bid.listing.old()]

Daniel W Bond's avatar
Daniel W Bond committed
43
        context['avg_stars'] = average_stars
44
45
        context['new_listings'] = new_student_listings
        context['old_listings'] = old_student_listings
46
        context['lookouts'] = Lookout.objects.filter(owner=self.get_object())
47

48
        context['proceeds'] = total_proceeds
49
        context['exchanges'] = total_exchanges
50

51
52
        context['new_bids'] = new_bids
        context['old_bids'] = old_bids
Daniel W Bond's avatar
Daniel W Bond committed
53

54
        return context
Daniel W Bond's avatar
Daniel W Bond committed
55

Daniel W Bond's avatar
Daniel W Bond committed
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
81
82
83
84
85
86
87
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
88
89
90
91
92
93
94
95
class StudentRatings(LoginRequiredMixin, DetailView):
    model = Student
    template_name = 'ratings.html'
    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
96
        student_ratings = Rating.objects.filter(listing__poster=self.get_object())
Daniel W Bond's avatar
Daniel W Bond committed
97
98
99
100

        # copied code!
        if student_ratings:
            student_stars = [int(rating.stars) for rating in student_ratings]
101
            print(student_stars)
Daniel W Bond's avatar
Daniel W Bond committed
102
103
104
            average_stars = sum(student_stars)/float((len(student_stars)))
        else:
            average_stars = None
Daniel W Bond's avatar
Daniel W Bond committed
105

Daniel W Bond's avatar
Daniel W Bond committed
106
107
108
109
110
111
        context['avg_stars'] = average_stars

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

        return context