views.py 2.97 KB
Newer Older
Jean Michel Rouly's avatar
Jean Michel Rouly committed
1
2
from django.shortcuts import render

3
4
5
6
from django.views.generic import DetailView
from braces.views import LoginRequiredMixin

from core.models import Student
7
8
from lookouts.models import Lookout
from trades.models import Listing
9
10

# seller's rating
11
"""def ratingsAverage(seller):
12
13
14
15
16
17
18
19
    sellerRating = Seller.objects.filter(user__username=seller)
    ratingNumber = 0
    ratingTotal = 0
    ratingAverage = 0
    for rating in sellerRating:
        ratingNumber += 1
        ratingTotal += rating
    ratingAverage = ratingTotal/ratingNumber
20
    return ratingNumber"""
21
22
23

### VIEWS ###

Daniel W Bond's avatar
Daniel W Bond committed
24
25
26
27
28
29
def privacy_opt_out(request):
    # merely forms
    return render(request, 'privacy_opt_out.html', {
   },
   )

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
def index(request):

    lookout_form = LookoutForm()
    lookouts = Lookout.objects.filter(
        owner__user__username = request.user.username )

    # This unwieldy double forloop grabs the pk of each listing that shows
    # up for your lookout, and adds it to a list.
    listing_pks = []
    for lookout in lookouts:
        lookout_listings = lookout.get_listings()
        for lookout_listing in lookout_listings:
            listing_pks.append( lookout_listing.pk )

    # The list of pks is then used to create a queryset, ordered by newest
    # listing first.
    listings = Listing.objects.filter(pk__in=listing_pks).order_by('-date_created')

    # Listings will be shown in 3 columns and 2 rows, for a total of 6
    # entries per page.
    paginator = Paginator(listings, 6) # Show 6 listings per page

    page = request.GET.get('page')
    try:
        listings = paginator.page(page)
    except PageNotAnInteger:
        # if page is NaN, deliver the first page
        listings = paginator.page(1)
    except EmptyPage:
        # if the page is empty, deliver the last page
        listings = paginator.page(paginator.num_pages)

    # the rows variable is >= 1, and is determined by the number of
    # entries on this page. this is intended to cause the listing
    # previews to fill in rows first, rather than columns.
    rows = int(math.ceil( len(listings) / 3.0 )) or 1

    return render(request, 'index.html', {
        'listings' : listings,
        'rows' : rows,
        'CreateLookout_form': lookout_form,
    },
    )

# User profile page
75
76
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
77
78
79
80
81
82
83
84
85

    def get_context_data(self, **kwargs):
        context = super(DetailStudent, self).get_context_data(**kwargs)
        #context['listings'] = Listing.objects.filter(seller='2')
        context['listings'] = Listing.objects.filter(seller=self.get_object().pk)
        context['me'] = self.get_object().pk
        context['lookouts'] = Lookout.objects.filter(owner=self.get_object().user)
        return context

86
    login_url = '/'
87

Daniel W Bond's avatar
Daniel W Bond committed
88
89
# manage all listings -- close your listings, delete your listings, see your bids, remove your bids, close your bids, etc -- on both sides of the transactions, and the ratings
# other students see all of your previous transactions but without the editing options ofc