views.py 4.42 KB
Newer Older
1
2
3
4
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic import CreateView, UpdateView, DeleteView
from django.views.generic import ListView, DetailView
from django.urls import reverse_lazy
5
6
from submission.models import Submission
from submission.forms import SubmissionForm
7
from degree.models import Degree
8
from submission.analysis import main as submission_analysis
9
10
11
12
13
14
from educator.models import Educator
from django.contrib.auth.models import User
from guardian.shortcuts import assign_perm
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.db.models import Q
Bruno Meyer's avatar
Bruno Meyer committed
15

16
17
18


@method_decorator(login_required, name='dispatch')
19
20
21
22
23
24
class SubmissionCreate(SuccessMessageMixin, CreateView):
    model = Submission
    form_class = SubmissionForm
    template_name = 'submission_create.html'
    success_url = reverse_lazy('dashboard')
    success_message = "Relatório enviado com suceso"
25

26
    def get_context_data(self, **kwargs):
27
        user = self.request.user
28
        context = super().get_context_data(**kwargs)
29
        context["users"] = User.objects.filter(~Q(username=user.username))
30
31
        context["permissions"] = [x[1] for x in Submission._meta.permissions]
        context["perms"] = [x[0] for x in Submission._meta.permissions]
32
        context["hide_navbar"] = True
33
34
        context["degree_options"] = user.educator.degree.all()

35
        return context
36

37
38
39
40
41
42
43
44
    def form_valid(self, form):
        # muda nomes dos arquivos
        form.instance.historico.name = "historico.xls"
        form.instance.matricula.name = "matricula.xls"
        # adiciona a form.instance.usuario e curso
        form.instance.author = self.request.user.educator
        degree = Degree.objects.get(code=form.instance.degree.code)
        form.instance.degree = degree
Bruno Meyer's avatar
Bruno Meyer committed
45

46
        response = super(SubmissionCreate, self).form_valid(form)
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        # trata permissoes
        data = self.request.POST.copy()
        users = {}
        for i in data.keys():
            # gets only permission data
            if i.startswith("perm-"):
                perm_str = i.split('-')
                # cache user
                if perm_str[1] not in users:
                    users[perm_str[1]] = User.objects.get(username=perm_str[1])

                # assing permission perm_str[2] to user perm_str[1] for
                # submission self.object
                assign_perm(perm_str[2], users[perm_str[1]], self.object)

        # assing all permission for self user
        for perm in Submission._meta.permissions:
            assign_perm(perm[0], self.request.user, self.object)

67
68
69
        submission_analysis.analyze(self.object, debug=False)

        return response
70

71

72
@method_decorator(login_required, name='dispatch')
73
74
75
76
77
78
class SubmissionUpdate(UpdateView):
    model = Submission
    template_name = 'submission_update.html'
    context_object_name = 'submission'
    success_url = reverse_lazy('dashboard')
    fields = [
79
80
81
82
83
84
        'historico',
        'matricula',
        'relative_year',
        'relative_semester',
        'semester_status']

85
86
87
88
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
        return context
Bruno Meyer's avatar
Bruno Meyer committed
89

90
91

@method_decorator(login_required, name='dispatch')
92
93
94
95
class SubmissionDelete(DeleteView):
    model = Submission
    template_name = 'submission_delete.html'
    success_url = reverse_lazy('dashboard')
Bruno Meyer's avatar
Bruno Meyer committed
96

97
98
99
100
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
        return context
101

102
103

@method_decorator(login_required, name='dispatch')
104
105
106
107
class SubmissionList(ListView):
    model = Submission
    template_name = 'submission_list.html'
    context_object_name = 'submission'
108
109
110



111
112
113
114
115
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
        # context["degree_id"] = self.kwargs["degree_id"]
        return context
116

117
118
119
    def get_queryset(self):
        educator = self.request.user.educator
        return self.model.objects.filter(author=educator)
120

Bruno Meyer's avatar
Bruno Meyer committed
121

122
@method_decorator(login_required, name='dispatch')
123
124
125
126
class SubmissionDetail(DetailView):
    model = Submission
    template_name = 'submission_detail.html'
    context_object_name = 'submission'
Bruno Meyer's avatar
Bruno Meyer committed
127

128
129
130
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
131
        return context