|
|
|
from datetime import date
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from django.apps import apps
|
|
|
|
from django.contrib import admin
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
autor_label = '''
|
|
|
|
<div class="col-xs-12">
|
|
|
|
Autor: <span id="nome_autor"></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)]
|