def send_emails(self, request, form_for_form, form, entry, attachments): subject = form.email_subject if not subject: subject = "%s - %s" % (form.title, entry.entry_time) fields = [] for (k, v) in form_for_form.fields.items(): value = form_for_form.cleaned_data[k] if isinstance(value, list): value = ", ".join([i.strip() for i in value]) fields.append((v.label, value)) context = { "fields": fields, "message": form.email_message, "request": request, } email_from = form.email_from or settings.DEFAULT_FROM_EMAIL email_to = form_for_form.email_to() if email_to and form.send_email: send_mail_template(subject, "form_response", email_from, email_to, context=context, fail_silently=EMAIL_FAIL_SILENTLY) headers = None if email_to: headers = {"Reply-To": email_to} email_copies = split_choices(form.email_copies) if email_copies: send_mail_template(subject, "form_response_copies", email_from, email_copies, context=context, attachments=attachments, fail_silently=EMAIL_FAIL_SILENTLY, headers=headers)
def form_detail(request, slug, template="forms/form_detail.html"): """ Display a built form and handle submission. """ published = Form.objects.published(for_user=request.user) form = get_object_or_404(published, slug=slug) if form.login_required and not request.user.is_authenticated(): return redirect("%s?%s=%s" % (settings.LOGIN_URL, REDIRECT_FIELD_NAME, urlquote(request.get_full_path()))) request_context = RequestContext(request) args = (form, request_context, request.POST or None, request.FILES or None) form_for_form = FormForForm(*args) if request.method == "POST": if not form_for_form.is_valid(): form_invalid.send(sender=request, form=form_for_form) else: entry = form_for_form.save() subject = form.email_subject if not subject: subject = "%s - %s" % (form.title, entry.entry_time) fields = [] for (k, v) in form_for_form.fields.items(): value = form_for_form.cleaned_data[k] if isinstance(value, list): value = ", ".join([i.strip() for i in value]) fields.append((v.label, value)) context = { "fields": fields, "message": form.email_message, "request": request, } email_from = form.email_from or settings.DEFAULT_FROM_EMAIL email_to = form_for_form.email_to() if email_to and form.send_email: send_mail_template(subject, "form_response", email_from, email_to, context=context, fail_silently=settings.DEBUG) email_copies = split_choices(form.email_copies) if email_copies: if email_to and SEND_FROM_SUBMITTER: # Send from the email entered. email_from = email_to attachments = [] for f in form_for_form.files.values(): f.seek(0) attachments.append((f.name, f.read())) send_mail_template(subject, "form_response", email_from, email_copies, context=context, attachments=attachments, fail_silently=settings.DEBUG) form_valid.send(sender=request, form=form_for_form, entry=entry) return redirect(reverse("form_sent", kwargs={"slug": form.slug})) context = {"form": form} return render_to_response(template, context, request_context)
def database_usage(context={}): LOG.info("Notifying Database usage with context %s" % context) subject = _("[DBAAS] Database is almost full") template = "database_notification" addr_from = Configuration.get_by_name("email_addr_from") team = context.get("team") if team and team.email: addr_to = [ team.email, Configuration.get_by_name("new_user_notify_email") ] else: addr_to = Configuration.get_by_name("new_user_notify_email") context['domain'] = get_domain() database = context['database'] context['database_url'] = get_database_url(database.id) send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context)
def schedule_task_notification(database, scheduled_task, is_new): subject = _('[DBaaS] Automatic Task {} for Database {}'.format( 'created' if is_new else 'updated', database.name, )) template = "schedule_task_notification" domain = get_domain() context = { 'database': database, 'ssl_expire_at': database.infra.earliest_ssl_expire_at, 'scheduled_for': scheduled_task.scheduled_for, 'database_url': "{}{}".format( domain, reverse('admin:logical_database_maintenance', kwargs={'id': database.id})), 'is_new': is_new, 'domain': domain } send_mail_template(subject, template, email_from(), email_to(database.team), fail_silently=False, attachments=None, context=context)
def notify_team_change_for(user=None): LOG.info("Notifying team change for user %s" % user) subject = _("[DBAAS] your team has been updated!") template = "team_change_notification" addr_from = Configuration.get_by_name("email_addr_from") if user.email: addr_to = [user.email] context = {} context['user'] = user domain = get_domain() context['url'] = domain context['teams'] = [team.name for team in user.team_set.all()] if user and addr_from and addr_to: send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context) else: LOG.warning("could not send email for team change") else: LOG.warning( "user %s has no email set and therefore cannot be notified!")
def disk_resize_notification(database, new_disk, usage_percentage): LOG.info('Notifying disk resize {} - {}'.format(database, new_disk)) subject = _('[DBaaS] Database {} auto disk resize to {}') if new_disk.is_last_auto_resize_offering: subject = _('[DBaaS] Database {} final auto disk resize to {}') subject = subject.format(database.name, new_disk) template = "disk_auto_resize_notification" context = { 'domain': get_domain(), 'database': database, 'usage_percentage': usage_percentage, 'new_disk_offering': new_disk, 'database_url': get_database_url(database.id) } send_mail_template(subject, template, email_from(), email_to(database.team), fail_silently=False, attachments=None, context=context)
def form_detail(request, slug, template="forms/form_detail.html", success_url=None): """ Display a built form and handle submission. """ published = Form.objects.published(for_user=request.user) form = get_object_or_404(published, slug=slug) if form.login_required and not request.user.is_authenticated(): return redirect("%s?%s=%s" % (settings.LOGIN_URL, REDIRECT_FIELD_NAME, urlquote(request.get_full_path()))) request_context = RequestContext(request) args = (form, request_context, request.POST or None, request.FILES or None) form_for_form = FormForForm(*args) if request.method == "POST": if not form_for_form.is_valid(): form_invalid.send(sender=request, form=form_for_form) else: entry = form_for_form.save() subject = form.email_subject if not subject: subject = "%s - %s" % (form.title, entry.entry_time) fields = [] for (k, v) in form_for_form.fields.items(): value = form_for_form.cleaned_data[k] if isinstance(value, list): value = ", ".join([i.strip() for i in value]) fields.append((v.label, value)) context = { "fields": fields, "message": form.email_message, "request": request, } email_from = form.email_from or settings.DEFAULT_FROM_EMAIL email_to = form_for_form.email_to() if email_to and form.send_email: send_mail_template(subject, "form_response", email_from, email_to, context=context, fail_silently=settings.DEBUG) email_copies = [e.strip() for e in form.email_copies.split(",") if e.strip()] if email_copies: if email_to and SEND_FROM_SUBMITTER: # Send from the email entered. email_from = email_to attachments = [] for f in form_for_form.files.values(): f.seek(0) attachments.append((f.name, f.read())) send_mail_template(subject, "form_response", email_from, email_copies, context=context, attachments=attachments, fail_silently=settings.DEBUG) form_valid.send(sender=request, form=form_for_form, entry=entry) if success_url is None: success_url = reverse("form_sent", kwargs={"slug": form.slug}) # Check to see if the POST data overrides the view's success_url argument. success_url = request.POST.get("success_url", success_url) return redirect(success_url) context = {"form": form} return render_to_response(template, context, request_context)
def form_detail(request, slug, template="forms/form_detail.html"): """ Display a built form and handle submission. """ use_email = False # emailing forms isn't supported in MAPLE published = Form.objects.published(for_user=request.user) form = get_object_or_404(published, slug=slug) if form.login_required and not request.user.is_authenticated(): return redirect("%s?%s=%s" % (settings.LOGIN_URL, REDIRECT_FIELD_NAME, urlquote(request.get_full_path()))) request_context = RequestContext(request) args = (form, request_context, request.POST or None, request.FILES or None) form_for_form = FormForForm(*args) if request.method == "POST": if not form_for_form.is_valid(): form_invalid.send(sender=request, form=form_for_form) else: entry = form_for_form.save() subject = None # form.email_subject if not subject: subject = "%s - %s" % (form.title, entry.entry_time) fields = [] for (k, v) in form_for_form.fields.items(): value = form_for_form.cleaned_data[k] if isinstance(value, list): value = ", ".join([i.strip() for i in value]) fields.append((v.label, value)) context = { "fields": fields, "message": '', #form.email_message, "request": request, } email_from = None #form.email_from or settings.DEFAULT_FROM_EMAIL email_to = None # form_for_form.email_to() if email_to and use_email: #form.send_email: send_mail_template(subject, "form_response", email_from, email_to, context=context, fail_silently=settings.DEBUG) headers = None if email_to: # Add the email entered as a Reply-To header headers = {'Reply-To': email_to} email_copies = None # split_choices(form.email_copies) if email_copies: attachments = [] for f in form_for_form.files.values(): f.seek(0) attachments.append((f.name, f.read())) send_mail_template(subject, "form_response", email_from, email_copies, context=context, attachments=attachments, fail_silently=settings.DEBUG, headers=headers) form_valid.send(sender=request, form=form_for_form, entry=entry) return redirect(reverse("form_sent", kwargs={"slug": form.slug})) context = {"form": form} return render_to_response(template, context, request_context)
def send_subscription_success(from_addr, to_addr, template): context = {'email': to_addr} send_mail_template( "New Subscription", template, from_addr, to_addr, fail_silently=False, context=context,)
def databaseinfra_ending(context={}): LOG.info("Notifying DatabaseInfra ending with context %s" % context) subject = _("[DBAAS] DatabaseInfra is almost full") template = "infra_notification" addr_from = Configuration.get_by_name("email_addr_from") addr_to = Configuration.get_by_name_as_list("new_user_notify_email") context['domain'] = get_domain() send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context)
def detail(request, category, id): template_name = 'catalog/view_detail.html' try: model = MODELS[category]['model'] except KeyError as e: raise Http404(e) object = get_object_or_404(model, id=id) context = { 'title': object.name, 'category_name': model._meta.verbose_name_plural, 'object': object, 'related_list': model.objects.all().exclude(id=id).order_by('?')[:12] } form = RequestForm(request.POST or None) context.update(form=form) if form.is_valid(): inst = form.save(commit=False) inst.url = "http://%s%s" % (request.get_host(), request.get_full_path()) inst.save() email = form.cleaned_data.get("email") context.update(user_email=email, site=request.get_host(), phone=form.cleaned_data.get('phone'), text=form.cleaned_data.get('text'), request=request) subject = _(u"%(site)s Карточка товара %(title)s") % context if True: send_mail_template(subject, 'truck_card', settings.EMAIL_HOST_USER, email, context=context) if True and config.SEND_PRODUCT_CARD_COPY: send_mail_template(subject, 'truck_card_copy', settings.EMAIL_HOST_USER, config.FEEDBACK_EMAIL.replace(' ', '').split(','), context=context) return JsonResponse(dict(success=1)) # get template for maol # Send email if request.GET.get("print"): template_name = "email_extras/truck_card.html" response = TemplateResponse(request, template_name, context) return response
def send_email(self, subject, email_to, email_from, context, form_for_form): attachments = [] for f in form_for_form.files.values(): f.seek(0) attachments.append((f.name, f.read())) send_mail_template(subject, self.email_template(), email_from, email_to, context=context, attachments=attachments, fail_silently=settings.DEBUG)
def notify_new_user_creation(user=None): subject=_("[DBAAS] a new user has just been created: %s" % user.username) template="new_user_notification" addr_from=Configuration.get_by_name("email_addr_from") addr_to=Configuration.get_by_name_as_list("new_user_notify_email") context={} context['user'] = user domain = get_domain() context['url'] = domain + reverse('admin:account_team_changelist') LOG.debug("user: %s | addr_from: %s | addr_to: %s" % (user, addr_from, addr_to)) if user and addr_from and addr_to: send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context) else: LOG.warning("could not send email for new user creation")
def databaseinfra_ending(plan,environment,used,capacity,percent): LOG.info("Notifying DatabaseInfra ending") subject=_("[DBAAS] DatabaseInfra is almost full") template="infra_notification" addr_from=Configuration.get_by_name("email_addr_from") addr_to=Configuration.get_by_name_as_list("new_user_notify_email") context={} context['domain'] = get_domain() context['plan'] = plan context['environment'] = environment context['used'] = used context['capacity'] = capacity context['percent'] = percent send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context)
def database_usage(context={}): LOG.info("Notifying Database usage with context %s" % context) subject = _("[DBAAS] Database is almost full") template = "database_notification" addr_from = Configuration.get_by_name("email_addr_from") team = context.get("team") if team and team.email: addr_to = [ team.email, Configuration.get_by_name("new_user_notify_email")] else: addr_to = Configuration.get_by_name("new_user_notify_email") context['domain'] = get_domain() send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context)
def database_analyzing(context={}): LOG.info("Notifying Database alayzing with context %s" % context) subject = _("[DBAAS] Database overestimated") template = "analyzing_notification" addr_from = Configuration.get_by_name("email_addr_from") send_email = Configuration.get_by_name("send_analysis_email") team = context.get("team") if team and team.email and send_email: addr_to = [ team.email, Configuration.get_by_name("new_user_notify_email")] else: addr_to = Configuration.get_by_name("new_user_notify_email") context['domain'] = get_domain() send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context)
def notify_new_user_creation(user=None): subject = _("[DBAAS] a new user has just been created: %s" % user.username) template = "new_user_notification" addr_from = Configuration.get_by_name("email_addr_from") addr_to = Configuration.get_by_name_as_list("new_user_notify_email") context = {} context['user'] = user domain = get_domain() context['url'] = domain + reverse('admin:account_team_changelist') LOG.debug("user: %s | addr_from: %s | addr_to: %s" % (user, addr_from, addr_to)) if user and addr_from and addr_to: send_mail_template( subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context ) else: LOG.warning("could not send email for new user creation")
def notify_team_change_for(user=None): LOG.info("Notifying team change for user %s" % user) subject=_("[DBAAS] your team has been updated!") template="team_change_notification" addr_from=Configuration.get_by_name("email_addr_from") if user.email: #addr_to=Configuration.get_by_name_as_list("new_user_notify_email") + [user.email] addr_to=[user.email] context={} context['user'] = user domain = get_domain() context['url'] = domain context['teams'] = [team.name for team in user.team_set.all()] if user and addr_from and addr_to: send_mail_template(subject, template, addr_from, addr_to, fail_silently=False, attachments=None, context=context) else: LOG.warning("could not send email for team change") else: LOG.warning("user %s has no email set and therefore cannot be notified!")
def send_mail(self): """ Sends out the mail for this post """ #gpg = gnupg.GPG(gnupghome='/tmp/sd-gpg/') #gpg.encoding = 'utf-8' #data = mail_template.render(Context({'post': self, 'MEDIA_URL': settings.MEDIA_URL})) #pgp_signature = gpg.sign(data, keyid = '0x95CAF8BC', detach = True).data #data = gpg.encrypt(data, '*****@*****.**', always_trust = True, sign = '0x95CAF8BC').data send_mail_template( _('A chunk of your past - Here\'s what you did last year ({0})!').format(self.date), 'post', 'Shortdiary Robot <*****@*****.**>', ['{0} <{1}>'.format(self.author.username, self.author.email)], context = {'post': self} ) self.sent = True self.save()
def send_mail(cls, subject, template_name, template_context, action, database=None, fail_silently=False, attachments=None): action_extra_explanation = cls.extra_explanation_action_map.get(action) if (template_context.get('extra_explanation') is None and action_extra_explanation): template_context['extra_explanation'] = action_extra_explanation task_warning_title = cls.task_warning_title_action_map.get(action) if task_warning_title: template_context['task_warning_title'] = task_warning_title.format( database.name) send_mail_template(subject, template_name, cls.email_from(), cls.email_to(database), fail_silently=fail_silently, attachments=attachments, context=template_context)
def disk_resize_notification(database, new_disk, usage_percentage): LOG.info( 'Notifying disk resize {} - {}'.format(database, new_disk) ) subject = _('[DBaaS] Database {} auto disk resize to {}') if new_disk.is_last_auto_resize_offering: subject = _('[DBaaS] Database {} final auto disk resize to {}') subject = subject.format(database.name, new_disk) template = "disk_auto_resize_notification" context = { 'domain': get_domain(), 'database': database, 'usage_percentage': usage_percentage, 'new_disk_offering': new_disk, 'database_url': get_database_url(database.id) } send_mail_template( subject, template, email_from(), email_to(database.team), fail_silently=False, attachments=None, context=context )