views.py 1.96 KB
Newer Older
1
2
3
from lookouts.models import Lookout
from lookouts.forms import LookoutForm

Daniel W Bond's avatar
Daniel W Bond committed
4
from django.views.generic import CreateView, DetailView, UpdateView, DeleteView
5
from braces.views import LoginRequiredMixin
Jean Michel Rouly's avatar
Jean Michel Rouly committed
6

Daniel W Bond's avatar
Daniel W Bond committed
7
from django.contrib.auth.models import User
8
from core.models import Student
9
from django.http import Http404, HttpResponseForbidden
Daniel W Bond's avatar
Daniel W Bond committed
10

11
### VIEWS ###
12
13
class CreateLookout(LoginRequiredMixin, CreateView):
    model = Lookout
14
    fields = ['isbn',]
15
16
    context_object_name = 'lookout'
    template_name = 'create_lookout.html'
Daniel W Bond's avatar
Daniel W Bond committed
17
18
    login_url = '/'

19
    def form_valid(self, form):
20
        me = Student.objects.get(user=self.request.user)
21

22
23
        form.instance.owner = me
        return super(CreateLookout, self).form_valid(form)
24

25
26
    def get_context_data(self, **kwargs):
        context = super(CreateLookout, self).get_context_data(**kwargs)
27

28
        form = LookoutForm()
29
30
31
32
        context['my_form'] = form

        return context

Daniel W Bond's avatar
Daniel W Bond committed
33
34
35
class DetailLookout(LoginRequiredMixin, DetailView):
    model = Lookout
    context_object_name = 'lookout'
36
    template_name = 'detail_lookout.html'
37
    login_url = '/'
38

39
40
41
42
43
44
45
46
47
48
    def get_context_data(self, **kwargs):
        context = super(DetailLookout, self).get_context_data(**kwargs)

        me = Student.objects.get(user=self.request.user)
        lookout_student = self.get_object().owner

        if not(lookout_student == me):
            return HttpResponseForbidden()

        return context
49

Daniel W Bond's avatar
Daniel W Bond committed
50
# updating is not neccessary since it's just literally an isbn and a course
51

Daniel W Bond's avatar
Daniel W Bond committed
52
class DeleteLookout(LoginRequiredMixin, DeleteView):
53
54
55
    model = Lookout
    context_object_name = 'lookout'
    template_name = 'delete_lookout.html'
Daniel W Bond's avatar
Daniel W Bond committed
56
    success_url = '/'
Daniel W Bond's avatar
Daniel W Bond committed
57
58

    def get_context_data(self, **kwargs):
Daniel W Bond's avatar
Daniel W Bond committed
59
        context = super(DeleteLookout, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
60

61
62
        me = Student.objects.get(user=self.request.user)
        lookout_student = self.get_object().owner
Daniel W Bond's avatar
Daniel W Bond committed
63

64
        if not(lookout_student == me):
65
            return HttpResponseForbidden()
Daniel W Bond's avatar
Daniel W Bond committed
66

67
        return context