mirror of https://github.com/interlegis/sapl.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
5.2 KiB
192 lines
5.2 KiB
from datetime import date
|
|
from functools import wraps
|
|
|
|
import magic
|
|
from django.apps import apps
|
|
from django.contrib import admin
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
autor_label = '''
|
|
<div class="col-xs-12">
|
|
Autor: <span id="nome_autor" name="nome_autor">
|
|
{% if form.autor.value %}
|
|
{{form.autor.value}}
|
|
{% endif %}
|
|
</span>
|
|
</div>
|
|
'''
|
|
|
|
|
|
autor_modal = '''
|
|
<div id="modal_autor" title="Selecione o Autor" align="center">
|
|
<form>
|
|
<input id="q" type="text" />
|
|
<input id="pesquisar" type="submit" value="Pesquisar"
|
|
class="btn btn-primary btn-sm"/>
|
|
</form>
|
|
<div id="div-resultado"></div>
|
|
<input type="submit" id="selecionar" value="Selecionar"
|
|
hidden="true" />
|
|
</div>
|
|
'''
|
|
|
|
|
|
def register_all_models_in_admin(module_name):
|
|
appname = module_name.split('.')[0]
|
|
app = apps.get_app_config(appname)
|
|
for model in app.get_models():
|
|
class CustomModelAdmin(admin.ModelAdmin):
|
|
list_display = [f.name for f in model._meta.fields
|
|
if f.name != 'id']
|
|
|
|
if not admin.site.is_registered(model):
|
|
admin.site.register(model, CustomModelAdmin)
|
|
|
|
|
|
def xstr(s):
|
|
return '' if s is None else str(s)
|
|
|
|
|
|
def get_client_ip(request):
|
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
|
if x_forwarded_for:
|
|
ip = x_forwarded_for.split(',')[0]
|
|
else:
|
|
ip = request.META.get('REMOTE_ADDR')
|
|
return ip
|
|
|
|
|
|
def get_base_url(request):
|
|
# TODO substituir por Site.objects.get_current().domain
|
|
# from django.contrib.sites.models import Site
|
|
|
|
current_domain = request.get_host()
|
|
protocol = 'https' if request.is_secure() else 'http'
|
|
return "{0}://{1}".format(protocol, current_domain)
|
|
|
|
|
|
def create_barcode(value):
|
|
'''
|
|
creates a base64 encoded barcode PNG image
|
|
'''
|
|
from base64 import b64encode
|
|
from reportlab.graphics.barcode import createBarcodeDrawing
|
|
|
|
barcode = createBarcodeDrawing('Code128',
|
|
value=value,
|
|
barWidth=170,
|
|
height=50,
|
|
fontSize=2,
|
|
humanReadable=True)
|
|
data = b64encode(barcode.asString('png'))
|
|
return data.decode('utf-8')
|
|
|
|
|
|
YES_NO_CHOICES = [(True, _('Sim')), (False, _('Não'))]
|
|
|
|
|
|
def listify(function):
|
|
@wraps(function)
|
|
def f(*args, **kwargs):
|
|
return list(function(*args, **kwargs))
|
|
return f
|
|
|
|
UF = [
|
|
('AC', 'Acre'),
|
|
('AL', 'Alagoas'),
|
|
('AP', 'Amapá'),
|
|
('AM', 'Amazonas'),
|
|
('BA', 'Bahia'),
|
|
('CE', 'Ceará'),
|
|
('DF', 'Distrito Federal'),
|
|
('ES', 'Espírito Santo'),
|
|
('GO', 'Goiás'),
|
|
('MA', 'Maranhão'),
|
|
('MT', 'Mato Grosso'),
|
|
('MS', 'Mato Grosso do Sul'),
|
|
('MG', 'Minas Gerais'),
|
|
('PR', 'Paraná'),
|
|
('PB', 'Paraíba'),
|
|
('PA', 'Pará'),
|
|
('PE', 'Pernambuco'),
|
|
('PI', 'Piauí'),
|
|
('RJ', 'Rio de Janeiro'),
|
|
('RN', 'Rio Grande do Norte'),
|
|
('RS', 'Rio Grande do Sul'),
|
|
('RO', 'Rondônia'),
|
|
('RR', 'Roraima'),
|
|
('SC', 'Santa Catarina'),
|
|
('SE', 'Sergipe'),
|
|
('SP', 'São Paulo'),
|
|
('TO', 'Tocantins'),
|
|
('EX', 'Exterior'),
|
|
]
|
|
|
|
RANGE_ANOS = [(year, year) for year in range(date.today().year, 1889, -1)]
|
|
|
|
TIPOS_TEXTO_PERMITIDOS = (
|
|
'application/vnd.oasis.opendocument.text',
|
|
'application/x-vnd.oasis.opendocument.text',
|
|
'application/pdf',
|
|
'application/x-pdf',
|
|
'application/acrobat',
|
|
'applications/vnd.pdf',
|
|
'text/pdf',
|
|
'text/x-pdf',
|
|
'text/plain',
|
|
'application/txt',
|
|
'browser/internal',
|
|
'text/anytext',
|
|
'widetext/plain',
|
|
'widetext/paragraph',
|
|
'application/msword',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/xml',
|
|
'text/xml',
|
|
'text/html',
|
|
)
|
|
|
|
TIPOS_IMG_PERMITIDOS = (
|
|
'image/jpeg',
|
|
'image/jpg',
|
|
'image/jpe_',
|
|
'image/pjpeg',
|
|
'image/vnd.swiftview-jpeg',
|
|
'application/jpg',
|
|
'application/x-jpg',
|
|
'image/pjpeg',
|
|
'image/pipeg',
|
|
'image/vnd.swiftview-jpeg',
|
|
'image/x-xbitmap',
|
|
'image/bmp',
|
|
'image/x-bmp',
|
|
'image/x-bitmap',
|
|
'image/png',
|
|
'application/png',
|
|
'application/x-png',
|
|
)
|
|
|
|
|
|
def fabrica_validador_de_tipos_de_arquivo(lista, nome):
|
|
|
|
def restringe_tipos_de_arquivo(value):
|
|
mime = magic.from_buffer(value.read(), mime=True)
|
|
mime = mime.decode()
|
|
if mime not in lista:
|
|
raise ValidationError(_('Tipo de arquivo não suportado'))
|
|
# o nome é importante para as migrations
|
|
restringe_tipos_de_arquivo.__name__ = nome
|
|
return restringe_tipos_de_arquivo
|
|
|
|
restringe_tipos_de_arquivo_txt = fabrica_validador_de_tipos_de_arquivo(
|
|
TIPOS_TEXTO_PERMITIDOS, 'restringe_tipos_de_arquivo_txt')
|
|
restringe_tipos_de_arquivo_img = fabrica_validador_de_tipos_de_arquivo(
|
|
TIPOS_IMG_PERMITIDOS, 'restringe_tipos_de_arquivo_img')
|
|
|
|
|
|
def intervalos_tem_intersecao(a_inicio, a_fim, b_inicio, b_fim):
|
|
maior_inicio = max(a_inicio, b_inicio)
|
|
menor_fim = min(a_fim, b_fim)
|
|
return maior_inicio <= menor_fim
|
|
|