summaryrefslogtreecommitdiff
path: root/app/sightings/views.py
blob: f65011c348e6fe10617d138fc86fc9dc8a282506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
from django.views.generic.detail import DetailView
from django.views.generic import ListView
from django.contrib.auth.models import User
from utils.views import PaginatedListView
from .models import AP, Sighting
from dal import autocomplete


class SightingListView(PaginatedListView):
    template_name = 'archives/sightings.html'

    def get_queryset(self):
        qs_ids = Sighting.objects.order_by('ap__id', '-pub_date').distinct('ap').values_list('id', flat=True)
        return Sighting.objects.filter(id__in=qs_ids).order_by('-pub_date')


class LifeListView(ListView):
    template_name = 'archives/life-list.html'

    def get_queryset(self):
        return Sighting.objects.filter(ap__apclass__kind=1).order_by('ap__id').distinct('ap')


class SightingListUserView(PaginatedListView):
    template_name = 'archives/sightings.html'

    def get_queryset(self):
        return Sighting.objects.filter(
            seen_by__username=self.kwargs['user']
        )

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SightingListUserView, self).get_context_data(**kwargs)
        context['user'] = User.objects.get(username=self.kwargs['user'])
        return context


class SightingDetailView(DetailView):
    model = AP
    template_name = "details/sighting.html"
    slug_field = "slug"

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SightingDetailView, self).get_context_data(**kwargs)
        #try:
        #    context['recording'] = SightingAudio.objects.get(
        #        ap__slug=self.kwargs['slug']
        #    )
        #except SightingAudio.DoesNotExist:
        #    pass
        try:
            context['sighting'] = Sighting.objects.filter(
                ap__slug=self.kwargs['slug']
            )
        except Sighting.DoesNotExist:
            pass
        return context


class APAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated:
            return AP.objects.none()
        qs = AP.objects.all()
        if self.q:
            qs = qs.filter(common_name__icontains=self.q)
        return qs