views.py 4.34 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
27
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
28
29
30
        context["users"] = User.objects.filter(~Q(username=self.request.user.username))
        context["permissions"] = [x[1] for x in Submission._meta.permissions]
        context["perms"] = [x[0] for x in Submission._meta.permissions]
31
32
        context["hide_navbar"] = True
        return context
33

34
35
36
37
38
39
40
41
    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
42

43
        response = super(SubmissionCreate, self).form_valid(form)
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        # 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)

64
65
66
        submission_analysis.analyze(self.object, debug=False)

        return response
67

68

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

82
83
84
85
    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
86

87
88

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

94
95
96
97
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
        return context
98

99
100

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



108
109
110
111
112
    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
113

114
115
116
    def get_queryset(self):
        educator = self.request.user.educator
        return self.model.objects.filter(author=educator)
117

Bruno Meyer's avatar
Bruno Meyer committed
118

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

125
126
127
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
128
        return context