views.py 4.67 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
    def form_valid(self, form):
        # muda nomes dos arquivos
39 40 41 42 43 44 45 46 47 48
        if ".csv" in form.instance.historico.name:
            form.instance.historico.name = "historico.csv"
        else:
            form.instance.historico.name = "historico.xls"

        if ".csv" in form.instance.matricula.name:
            form.instance.matricula.name = "matricula.csv"
        else:
            form.instance.matricula.name = "matricula.xls"

49 50 51 52
        # 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
53

54
        response = super(SubmissionCreate, self).form_valid(form)
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        # 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)

75 76 77
        submission_analysis.analyze(self.object, debug=False)

        return response
78

79

80
@method_decorator(login_required, name='dispatch')
81 82 83 84 85 86
class SubmissionUpdate(UpdateView):
    model = Submission
    template_name = 'submission_update.html'
    context_object_name = 'submission'
    success_url = reverse_lazy('dashboard')
    fields = [
87 88 89 90 91 92
        'historico',
        'matricula',
        'relative_year',
        'relative_semester',
        'semester_status']

93 94 95 96
    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
97

98 99

@method_decorator(login_required, name='dispatch')
100 101 102 103
class SubmissionDelete(DeleteView):
    model = Submission
    template_name = 'submission_delete.html'
    success_url = reverse_lazy('dashboard')
Bruno Meyer's avatar
Bruno Meyer committed
104

105 106 107 108
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
        return context
109

110 111

@method_decorator(login_required, name='dispatch')
112 113 114 115
class SubmissionList(ListView):
    model = Submission
    template_name = 'submission_list.html'
    context_object_name = 'submission'
116 117 118



119 120 121 122 123
    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
124

125 126 127
    def get_queryset(self):
        educator = self.request.user.educator
        return self.model.objects.filter(author=educator)
128

Bruno Meyer's avatar
Bruno Meyer committed
129

130
@method_decorator(login_required, name='dispatch')
131 132 133 134
class SubmissionDetail(DetailView):
    model = Submission
    template_name = 'submission_detail.html'
    context_object_name = 'submission'
Bruno Meyer's avatar
Bruno Meyer committed
135

136 137 138
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["hide_navbar"] = True
139
        return context