def receive_function_args(request, *args, **kwargs): if can_execute and not permissions.check_group_or_permission( request, '{}.{}'.format(model._meta.app_label, function.__name__)): return HttpResponseRedirect('/admin/login/') f_return = function(request, *args, **kwargs) if 'title' not in f_return: f_return['title'] = title if type(f_return) == dict: if 'pdf' in style: request.GET._mutable = True request.GET['pdf'] = 1 request.GET._mutable = False from datetime import datetime from djangoplus.admin.models import Settings app_settings = Settings.default() f_return[ 'logo'] = app_settings.logo_pdf and app_settings.logo_pdf or app_settings.logo f_return['project_name'] = app_settings.initials f_return['project_description'] = app_settings.name f_return['today'] = datetime.now() template_list = [ '{}.html'.format(function.__name__), 'report.html' ] return PdfResponse( render_to_string(template_list, f_return, request=request)) else: template_list = [ '{}.html'.format(function.__name__), 'default.html' ] return render(request, template or template_list, f_return) else: return f_return
def return_response(f_return, request, title, style, template_name, raise_response=False, ignore_pdf=False): from datetime import datetime from django.shortcuts import render from djangoplus.admin.models import Settings from djangoplus.ui import ComponentHasResponseException if type(f_return) == dict: if title and 'title' not in f_return: f_return['title'] = title for key in f_return: if hasattr(f_return[key], 'process_request'): f_return[key].process_request() if 'pdf' in style and not ignore_pdf: request.GET._mutable = True request.GET['pdf'] = 1 request.GET._mutable = False app_settings = Settings.default() f_return['logo'] = app_settings.logo_pdf and app_settings.logo_pdf or app_settings.logo f_return['project_name'] = app_settings.initials f_return['project_description'] = app_settings.name f_return['today'] = datetime.now() f_return['default_template'] = 'report.html' template_list = [template_name, 'report.html'] landscape = 'landscape' in style response = PdfResponse(render_to_string(template_list, f_return, request=request), landscape=landscape) else: template_list = [template_name, 'default.html'] response = render(request, template_list, f_return) else: response = f_return if raise_response: raise ComponentHasResponseException(response) else: return response
def __init__(self, title, request, objects, landscape=False, template='report.html'): from djangoplus.admin.models import Settings app_settings = Settings.default() logo = app_settings.logo_pdf and app_settings.logo_pdf or app_settings.logo project_name = app_settings.initials project_description = app_settings.name unit_or_organization = request.session.get('scope') context = dict(objects=objects, title=title, today=datetime.date.today(), logo=logo, project_name=project_name, project_description=project_description, unit_or_organization=unit_or_organization) html = render_to_string([template], context, request=request) super(ReportResponse, self).__init__(html, landscape)
def context_processor(request): executing_tests = 'test' in sys.argv app_settings = Settings.default() alerts = [] menu = None if request.user.is_authenticated: menu = Menu(request, app_settings) for model in loader.subsets: icon = get_metadata(model, 'icon', 'fa-warning') title = get_metadata(model, 'verbose_name_plural') app_label = get_metadata(model, 'app_label') model_name = model.__name__.lower() for item in loader.subsets[model]: can_view = item['can_view'] alert = item['alert'] if alert and permissions.check_group_or_permission( request, can_view): attr_name = item['function'].__func__.__name__ qs = model.objects.all(request.user) qs = getattr(qs, attr_name)() count = qs.count() if count: url = '/list/{}/{}/{}/'.format(app_label, model_name, attr_name) description = item['verbose_name'].replace(title, '') item = dict(title=title, description=description, count=count, url=url, icon=icon) alerts.append(item) return dict(debug=settings.DEBUG, role_selector=RoleSelector(request), username_mask=settings.USERNAME_MASK, js_files=settings.EXTRA_JS, css_files=settings.EXTRA_CSS, settings=app_settings, menu=menu, alerts=alerts, display_emails=settings.DEBUG and not executing_tests and should_display(), display_fake_mouse=test.CACHE['RECORD'], executing_tests=executing_tests, default_template='default.html')
def receive_function_args(request, *args, **kwargs): without_permission = can_view and not permissions.check_group_or_permission( request, can_view) without_authentication = login_required and not request.user.is_authenticated if without_permission or without_authentication: return HttpResponseRedirect( '/admin/login/?next={}'.format(url)) f_return = function(request, *args, **kwargs) if 'title' not in f_return: f_return['title'] = title if type(f_return) == dict: for key in f_return: if hasattr(f_return[key], 'process_request'): f_return[key].process_request() if 'pdf' in style: request.GET._mutable = True request.GET['pdf'] = 1 request.GET._mutable = False from datetime import datetime from djangoplus.admin.models import Settings app_settings = Settings.default() f_return[ 'logo'] = app_settings.logo_pdf and app_settings.logo_pdf or app_settings.logo f_return['project_name'] = app_settings.initials f_return['project_description'] = app_settings.name f_return['today'] = datetime.now() template_list = [ '{}.html'.format(function.__name__), 'report.html' ] return PdfResponse( render_to_string(template_list, f_return, request=request)) else: template_list = [ template or '{}.html'.format(function.__name__), 'default.html' ] return render(request, template_list, f_return) return f_return
def send_mail(subject, message, send_to, reply_to=None, actions=()): from djangoplus.admin.models import Settings url = 'http://{}'.format(settings.HOST_NAME or 'localhost:8000') app_settings = Settings.default() context = dict() context['subject'] = subject context['project_url'] = url context['project_name'] = app_settings.initials context['project_description'] = app_settings.name context['project_logo'] = app_settings.logo and \ '{}/media/{}'.format(url, app_settings.logo) or '{}static/images/mail.png'.format(url) context['actions'] = actions context['message'] = message.replace('\n', '<br>').replace('\t', ' ' * 4) reply_to = reply_to and [reply_to] or None from_email = 'Não-Responder <{}>'.format(settings.SERVER_EMAIL) html = loader.render_to_string('mail.html', context) if settings.SENDGRID_KEY and 'test' not in sys.argv: import sendgrid from sendgrid.helpers.mail import Email, Content, Mail sg_client = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_KEY) from_email = Email(settings.SERVER_EMAIL) to_email = Email(send_to) content = Content("text/html", html) mail = Mail(from_email, subject, to_email, content) if reply_to: mail.reply_to = Email(reply_to) response = sg_client.client.mail.send.post(request_body=mail.get()) return response.status_code else: body = 'Mensagem em anexo.' email = EmailMultiAlternatives(subject, body, from_email, [send_to], reply_to=reply_to) email.attach_alternative(html, "text/html") return email.send()
def context_processor(request): app_settings = Settings.default() alerts = [] menu = None if request.user.is_authenticated: menu = Menu(request, app_settings) menu.load() for model in loader.subsets: icon = get_metadata(model, 'icon', u'fa-warning') title = get_metadata(model, 'verbose_name_plural') app_label = get_metadata(model, 'app_label') model_name = model.__name__.lower() for item in loader.subsets[model]: can_view = item['can_view'] alert = item['alert'] if alert and permissions.check_group_or_permission( request, can_view): attr_name = item['function'].im_func.func_name qs = model.objects.all(request.user) qs = getattr(qs, attr_name)() count = qs.count() if count: url = '/list/%s/%s/%s/' % (app_label, model_name, attr_name) item = dict(title=title, description=item['title'], count=count, url=url, icon=icon) alerts.append(item) return dict(js_files=settings.EXTRA_JS, css_files=settings.EXTRA_CSS, settings=app_settings, menu=menu, alerts=alerts)
def send_mail(subject, message, to, reply_to=None, actions=()): from djangoplus.admin.models import Settings url = 'http://{}'.format(settings.HOST_NAME or 'localhost:8000') app_settings = Settings.default() context = dict() context['subject'] = subject context['project_url'] = url context['project_name'] = app_settings.initials context['project_description'] = app_settings.name context['project_logo'] = app_settings.logo and '{}/media/{}'.format(url, app_settings.logo) or \ '{}/static/images/mail.png'.format(url) context['actions'] = actions context['message'] = message.replace('\n', '<br>').replace('\t', ' ' * 4) reply_to = reply_to and [reply_to] or None from_email = 'Não-Responder <{}>'.format(settings.SERVER_EMAIL) html = loader.render_to_string('mail.html', context) email = EmailMultiAlternatives(subject, 'Mensagem em anexo.', from_email, [to], reply_to=reply_to) email.attach_alternative(html, "text/html") return email.send()
def __init__(self, *args, **kwargs): kwargs['instance'] = Settings.default() super(SettingsForm, self).__init__(*args, **kwargs)
def public(request): app_settings = Settings.default() if not app_settings.background: return httprr(request, '/admin/') return locals()