Created
May 22, 2020 20:15
-
-
Save linikerunk/b69a7f818012f326398f28a61a509070 to your computer and use it in GitHub Desktop.
Ticket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from django.forms import ModelForm, RadioSelect | |
| from .widgets import TesteInput | |
| from .models import Ticket, Categoria | |
| from perfil.models import Funcionario, Unidade | |
| from django import forms | |
| BOOL_CHOICES = ((True, 'Sim'), (False, 'Não')) | |
| class TicketForm(forms.ModelForm): | |
| unidade = forms.ModelChoiceField( | |
| label="Unidade : ", | |
| queryset=Unidade.objects.all()) | |
| categoria = forms.ModelChoiceField( | |
| label="Categoria : ", | |
| queryset = Categoria.objects.all()) | |
| funcionario = forms.ModelChoiceField( | |
| label="RE : ", | |
| queryset=Funcionario.objects.all(), | |
| to_field_name="re_funcionario", | |
| widget=forms.TextInput()) | |
| upload_arquivo = forms.FileField(widget=forms.ClearableFileInput( | |
| attrs={'multiple': True}), required=False, label="Anexar Arquivo : ") | |
| class Meta: | |
| model = Ticket | |
| fields = ['unidade', 'categoria', 'funcionario', 'texto', 'nome', | |
| 'upload_arquivo'] | |
| labels = { | |
| 'nome': 'Funcionário : ', | |
| 'categoria': 'Categoria : ', | |
| 'texto': 'Descrição : ', | |
| 'upload_arquivo': "Anexar arquivos : " | |
| } | |
| widgets={ | |
| 'texto': forms.Textarea( | |
| attrs={'placeholder': 'Informe um telefone e/ou e-mail para retorno do chamado', | |
| 'rows': 5}), | |
| } | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self.fields['categoria'].queryset = Categoria.objects.none() | |
| def clean_funcionario(self): | |
| print(self.cleaned_data) | |
| unidade = self.cleaned_data['unidade'] | |
| funcionario = self.cleaned_data['funcionario'] | |
| if funcionario.unidade != unidade: | |
| raise forms.ValidationError('Funcionário não está vinculado à essa unidade') | |
| return funcionario | |
| class TicketUpdateForm(forms.ModelForm): | |
| funcionario = forms.ModelChoiceField( | |
| label="RE : ", | |
| queryset=Funcionario.objects.all(), | |
| to_field_name="re_funcionario", | |
| widget=forms.TextInput()) | |
| upload_arquivo = forms.FileField(widget=forms.ClearableFileInput( | |
| attrs={'multiple': True}), required=False, label="Anexar Arquivo : ") | |
| class Meta: | |
| model = Ticket | |
| fields = ['categoria', 'funcionario', 'nome', 'texto', | |
| 'resposta', 'data_finalizada', 'finalizado', 'upload_arquivo'] | |
| labels = { | |
| 'nome': 'Funcionário : ', | |
| 'categoria': 'Categoria : ', | |
| 'texto': 'Descrição : ', | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import os | |
| from datetime import datetime | |
| from django.db import models | |
| from django.utils import timezone | |
| from django.core.validators import MaxValueValidator, MinValueValidator | |
| from django.conf import settings | |
| from perfil.models import Funcionario | |
| BOOL_CHOICES = ((True, 'Sim'), (False, 'Não')) | |
| def update_filename(instance, filename): | |
| path=f'documents/{instance.funcionario.unidade}/{instance.funcionario.re_funcionario}/' | |
| #instance.funcionario.perfil.unidade | |
| output = "" | |
| for i in range(len(filename)): | |
| if ord(filename[i]) < 127: | |
| output += (filename[i]) | |
| filename = output | |
| path = path.replace('ç', 'c').replace('ã', 'a').replace('´', '') | |
| return os.path.join(path, filename) | |
| class Categoria(models.Model): | |
| nome = models.CharField(max_length=50, unique=True) | |
| categoria = models.ForeignKey('self', related_name='subcategorias', on_delete=models.CASCADE, blank=True, null=True) | |
| def __str__(self): | |
| return self.nome | |
| class Ticket(models.Model): | |
| nome = models.CharField(max_length=55, verbose_name="Funcionário : ") | |
| texto = models.TextField(blank=False, verbose_name="Descrição : ") | |
| resposta = models.TextField(blank=True, null=True) | |
| data = models.DateTimeField(auto_now_add=True) | |
| data_finalizada = models.DateTimeField(null=True, blank=True, verbose_name="Data Finalizada : ") | |
| finalizado = models.BooleanField(default=False, choices=BOOL_CHOICES) | |
| upload_arquivo = models.FileField(blank=True, upload_to=update_filename, verbose_name="Anexar Arquivos : ") | |
| funcionario = models.ForeignKey(Funcionario, related_name="tickets", on_delete=models.PROTECT) | |
| categoria = models.ForeignKey(Categoria, related_name='tickets', on_delete=models.CASCADE) | |
| def save(self, *args, **kwargs): | |
| if self.finalizado: | |
| if not self.data_finalizada: | |
| self.data_finalizada = timezone.now() | |
| return super(Ticket, self).save(*args, **kwargs) | |
| def tempo_aberto(self): | |
| tempo = timezone.now() - self.data | |
| tempo = tempo.days | |
| if tempo == 0: #Ajuste no tempo | |
| tempo = 1 | |
| print(tempo) | |
| return tempo | |
| def tempo_finalizado(self): | |
| print(self.data_finalizada) | |
| if self.data_finalizada: | |
| tempo_finalizado = self.data_finalizada - self.data | |
| if abs(tempo_finalizado.days) == 0: | |
| tempo_finalizada = 1 | |
| return abs(tempo_finalizada) | |
| return abs(tempo_finalizado.days) | |
| return 1 | |
| def __str__(self): | |
| return f'Ticket Número {self.id}, Re funcionário {self.funcionario.re_funcionario}, data {self.data}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends 'base.html' %} | |
| {% load crispy_forms_tags %} | |
| {% block titulo %} Abrir um novo chamado {% endblock %} | |
| {% block conteudo %} | |
| <div class="row"> | |
| <div class="col-12"> | |
| <h3 class="">Abrir um chamado</h3> | |
| <hr> | |
| <form method="POST" enctype="multipart/form-data" content='text/html; charset=UTF-8' accept-charset="UTF-8"> | |
| {% csrf_token %} | |
| <div class="form-row"> | |
| <div class="form-group offset-md-1 col-md-3 mb-0"> | |
| {{ form.unidade|as_crispy_field }} | |
| </div> | |
| <div class="form-group col-md-3 mb-0"> | |
| {{ form.categoria|as_crispy_field }} | |
| </div> | |
| <div class="form-group col-md-4 mb-0"> | |
| <label> Subcategoria </label> | |
| <select name="subcategoria" class="select form-control form-control" required="" id="subcategoria"> | |
| <option value="" selected="">---------</option> | |
| </select> | |
| </div> | |
| <div class="form-group offset-md-1 col-md-3 mb-0"> | |
| {{ form.funcionario|as_crispy_field }} | |
| </div> | |
| <div class="form-group col-md-7 mb-0"> | |
| {{ form.nome|as_crispy_field }} | |
| </div> | |
| <div class="form-group offset-md-1 col-md-10 mb-0"> | |
| <input type="email" class="form-control" id="email" name="email" readonly hidden> | |
| </div> | |
| <div class="form-group offset-md-1 col-md-10 mb-0"> | |
| {{ form.texto|as_crispy_field }} | |
| </div> | |
| <div class="form-group offset-md-1 col-md-10 mb-0"> | |
| {{ form.upload_arquivo|as_crispy_field }} | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <button type="reset" class="btn btn-warning float-right w-25">Limpar</button> | |
| <button type="submit" class="btn btn-success mr-2 float-right w-50">Enviar</button> | |
| </div> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def carregar_subcategorias(request): | |
| categoria = request.GET.get('categoria') | |
| subcategoria = Categoria.objects.filter(categoria).order_by('name') | |
| response = {'subcategoria': subcategoria} | |
| return JsonResponse(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment