def export_translations(request, language):
"""
Vista de exportación de las traducciones
"""
FieldTranslation.delete_orphan_translations()
translations = FieldTranslation.objects.filter(lang=language)
for trans in translations:
trans.source_text = trans.source_text.replace("'","\'").replace("\"","\\\"")
trans.translation = trans.translation.replace("'","\'").replace("\"","\\\"")
replacements = {"translations":translations, "lang":language}
if len(settings.ADMINS)>0:
replacements["last_translator"] = settings.ADMINS[0][0]
replacements["last_translator_email"] = settings.ADMINS[0][1]
if settings.WEBSITE_NAME:
replacements["website_name"] = settings.WEBSITE_NAME
response = render(request=request, template_name='modeltranslation/admin/export_translations.po', dictionary=replacements, context_instance=RequestContext(request), content_type="text/x-gettext-translation")
response['Content-Disposition'] = 'attachment; filename="{0}.po"'.format(language)
return response
########################################################################
########################################################################
## Actualizar las traducciones
python类ADMINS的实例源码
def export_translations(request, language):
"""
Export translations view.
"""
FieldTranslation.delete_orphan_translations()
translations = FieldTranslation.objects.filter(lang=language)
for trans in translations:
trans.source_text = trans.source_text.replace("'","\'").replace("\"","\\\"")
trans.translation = trans.translation.replace("'","\'").replace("\"","\\\"")
replacements = {"translations":translations, "lang":language}
if len(settings.ADMINS)>0:
replacements["last_translator"] = settings.ADMINS[0][0]
replacements["last_translator_email"] = settings.ADMINS[0][1]
if settings.WEBSITE_NAME:
replacements["website_name"] = settings.WEBSITE_NAME
response = render(request=request, template_name='modeltranslation/admin/export_translations.po', dictionary=replacements, context_instance=RequestContext(request), content_type="text/x-gettext-translation")
response['Content-Disposition'] = 'attachment; filename="{0}.po"'.format(language)
return response
########################################################################
########################################################################
## Update translations
def send_error_report(url, exception, trace):
"""Send email when an error occurs."""
if settings.DEBUG is False:
email_context = {
'site': url,
'exception': exception,
'traceback': trace
}
message = get_template('scheduler_core/error_report_parsing.html').render(email_context)
error_email = EmailMessage('[MovieScheduler] Parsing Error Report',
message,
settings.SERVER_EMAIL,
settings.ADMINS)
error_email.content_subtype = 'html'
error_email.send(fail_silently=False)
else:
print("Exception: " + str(exception))
print("Traceback: ")
print(trace)
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
default_plan = Plan.objects.get(pk=Plan.DEFAULT_PK)
up = Profile(user=user, plan=default_plan)
up.save()
try:
for tg in TalkGroupAccess.objects.filter(default_group=True):
up.talkgroup_access.add(tg)
except OperationalError:
pass
try:
new_user_email = SiteOption.objects.get(name='SEND_ADMIN_EMAIL_ON_NEW_USER')
if new_user_email.value_boolean_or_string() == True:
send_mail(
'New {} User {}'.format(settings.SITE_TITLE, user.username),
'New User {} {} Username {} Email {} just registered'.format(user.first_name, user.last_name, user.username, user.email),
settings.SERVER_EMAIL,
[ mail for name, mail in settings.ADMINS],
fail_silently=False,
)
except (SiteOption.DoesNotExist, OperationalError):
pass
def mail_admins(subject, message, fail_silently=False, priority="medium"):
from django.utils.encoding import force_unicode
from django.conf import settings
from mailer.models import Message
priority = PRIORITY_MAPPING[priority]
subject = settings.EMAIL_SUBJECT_PREFIX + force_unicode(subject)
message = force_unicode(message)
if len(subject) > 100:
subject = u"%s..." % subject[:97]
for name, to_address in settings.ADMINS:
Message(to_address=to_address,
from_address=settings.SERVER_EMAIL,
subject=subject,
message_body=message,
priority=priority).save()
def request_extension(request, primary_key):
"""Send email to admins to request :model:`library.Lendable` extension.
After renewing a lendable the max available (max_renewals) times
a user may request an extension. An email is sent to the site
ADMINS as found in the settings for this request.
Redirect:
:view:`library.index`
"""
admin_path_for_lendable = reverse(
'admin:library_lendable_change',
args=(primary_key,)
)
admin_url_for_lendable = settings.PRIMARY_URL + admin_path_for_lendable
try:
send_mail(
'openbare: request to extend due_date of PK#%s' % primary_key,
'Message from %s:\n%s\n\n%s' % (
request.user.username,
request.POST['message'],
admin_url_for_lendable
),
request.user.email,
_admin_emails()
)
except Exception as e:
messages.error(request, e)
else:
messages.success(
request,
'Your request was sent to the openbare Admins' +
' and will be evaluated.'
)
return redirect(reverse('library:index'))
def _admin_emails():
return ["%s <%s>" % (admin[0], admin[1]) for admin in settings.ADMINS]
def __init__(self, subject, message, fail_silently=False, html_message=None, connection=None):
self.connection = connection
self.subject = subject
self.message = message
self.recipient_list = [a[1] for a in settings.ADMINS]
self.from_email = settings.SERVER_EMAIL
self.fail_silently = fail_silently
self.html_message = html_message
threading.Thread.__init__(self)
def run(self):
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, self.subject),
self.message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=self.connection)
if self.html_message:
mail.attach_alternative(self.html_message, 'text/html')
try:
mail.send(fail_silently=self.fail_silently)
except Exception as e:
logger.exception(e)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def send_email(self, request=None):
emails = [EmailMultiAlternatives(**{
'subject' : 'Contact Form: ' + str(self.instance.name),
'to' : [mkemail(*a) for a in settings.ADMINS],
'reply_to' : [mkemail(str(self.instance.name), self.cleaned_data['email'])],
'body' : str().join('{0:15s} : {1}\n'.format(
self.fields[f].label, self.cleaned_data[f])
for f in self._meta.fields),
})]
emails[0].attach_alternative(
render_to_string('contact/email.html',
context={
'name' : str(self.instance.name),
'phone' : self.cleaned_data['phone'],
'email' : self.cleaned_data['email'],
'comment' : self.cleaned_data['comment'],
},
request=request
), 'text/html'
)
if self.cleaned_data['cc_myself']:
emails.append(copy.copy(emails[0]))
emails[1].to = emails[0].reply_to
emails[1].reply_to = [settings.DEFAULT_REPLY_ADDR]
try:
with get_connection() as con:
con.send_messages(emails)
con.close()
except BadHeaderError as e:
return HttpResponse(' '.join(
['BadHeaderError:'] + list(e.args)
))
except Exception as e: # pragma: no cover
print(e)
return False
return True
def handle(self, *args, **options):
github = github_login(token=settings.GITHUB_TOKEN)
for index, package in enumerate(Project.objects.iterator()):
logging.info("{} ...".format(package.name))
print("{} ...".format(package.name))
# Simple attempt to deal with Github rate limiting
while True:
if github.ratelimit_remaining < 50:
sleep(120)
break
try:
try:
package.fetch_metadata(fetch_pypi=False)
package.fetch_commits()
except Exception as e:
raise PackageUpdaterException(e, package.name)
except PackageUpdaterException:
pass # We've already caught the error so let's move on now
sleep(5)
message = "TODO - load logfile here" # TODO
send_mail(
subject="Package Updating complete",
message=message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[x[1] for x in settings.ADMINS]
)
def send_email_to_admins(subject, message):
"""Asynchronously sends an email alert to all ADMINs in settings
"""
tasks.send_email.delay(
subject=subject, message=message,
from_email=settings.MAIL_DEFAULT_SENDER,
recipient_list=[email for name, email in settings.ADMINS])
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def contact(self):
subject = self.cleaned_data['subject']
from_email = self.cleaned_data['email']
message = self.cleaned_data['body']
try:
return send_mail(subject, message, from_email, settings.ADMINS)
except SMTPResponseException:
return False
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives(
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), message,
settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection,
)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
def mail_admins(subject, message, fail_silently=False, connection=None,
html_message=None):
"""Sends a message to the admins, as defined by the ADMINS setting."""
if not settings.ADMINS:
return
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
connection=connection)
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)