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

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

from core.models import Student

8
9
10
# stuff copied out of the website views

# 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
# USER home page -- rewrite as user homepage
Daniel W Bond's avatar
Daniel W Bond committed
31
#@login_required
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
75
76
77
78
79
80
81
82
83
84
85
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

    if request.method == 'POST':
        lookout_form = LookoutForm( request.POST )
        if lookout_form.is_valid():
            lookout = lookout_form.save(commit=False)
            lookout.ISBN = lookout.ISBN.strip()
            lookout.owner = request.user.seller
            lookout.save()
            return redirect( 'homepage' )

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

# User profile page
86
87
88
class DetailStudent(LoginRequiredMixin, DetailView):
    model = Student
    login_url = '/'
89

Daniel W Bond's avatar
Daniel W Bond committed
90
91
# 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