def send_activation_email(self): email_subject = 'Welcome to PANDA, please activate your account!' email_body = 'Hello there, the administrator of your organization\'s PANDA has signed you up for an account.\n\nTo activate your account, click this link:\n\nhttp://%s/#activate/%s' % (config_value('DOMAIN', 'SITE_DOMAIN'), self.activation_key) send_mail(email_subject, email_body, [self.user.email])
def on_user_post_save(sender, instance, created, **kwargs): """ When a User is created, create an API key for them, add them to the panda_user group and send them an activation email. When a User is saved, update their Datasets' metadata in Solr. """ # Setup activation if created and not kwargs.get('raw', False): ApiKey.objects.get_or_create(user=instance) panda_users = Group.objects.get(name='panda_user') instance.groups.add(panda_users) salt = sha.new(str(random.random())).hexdigest()[:5] activation_key = sha.new(salt + instance.username).hexdigest() user_profile = UserProfile.objects.create( user=instance, activation_key=activation_key ) email_subject = 'Welcome to PANDA, please activate your account!' email_body = 'Hello there, the administrator of your organization\'s PANDA has signed you up for an account.\n\nTo activate your account, click this link:\n\nhttp://%s/#activate/%s' % (config_value('DOMAIN', 'SITE_DOMAIN'), user_profile.activation_key) send_mail(email_subject, email_body, [instance.email])
def forgot_password(request): """ PANDA user password reset and notification. """ if request.method == 'POST': try: user = UserProxy.objects.get(email=request.POST.get('email')) except UserProfile.DoesNotExist: return JSONResponse( {'__all__': 'Unknown or inactive email address.'}, status=400) if not user.is_active: return JSONResponse( {'__all__': 'Unknown or inactive email address.'}, status=400) user_profile = user.get_profile() user_profile.generate_activation_key() user_profile.save() email_subject = 'Forgotten password' email_body = 'PANDA received a request to change your password.\n\nTo set your new password follow this link:\n\nhttp://%s/#reset-password/%s\n\nIf you did not request this email you should notify your adminstrator.' % ( config_value('DOMAIN', 'SITE_DOMAIN'), user_profile.activation_key) send_mail(email_subject, email_body, [user.email]) # Success return JSONResponse(make_user_login_response(user)) else: # Invalid request return JSONResponse(None, status=400)
def send_activation_email(self): email_subject = _("Welcome to PANDA, please activate your account!") email_body = _( "Hello there, the administrator of your organization's PANDA has signed you up for an account.\n\nTo activate your account, click this link:\n\nhttp://%(site_domain)s/#activate/%(activation_key)s" ) % {"site_domain": config_value("DOMAIN", "SITE_DOMAIN"), "activation_key": self.activation_key} send_mail(email_subject, email_body, [self.user.email])
def forgot_password(request): """ PANDA user password reset and notification. """ if request.method == 'POST': try: user = UserProxy.objects.get(email=request.POST.get('email')) except UserProfile.DoesNotExist: return JSONResponse({ '__all__': 'Unknown or inactive email address.' }, status=400) if not user.is_active: return JSONResponse({ '__all__': 'Unknown or inactive email address.' }, status=400) user_profile = user.get_profile() user_profile.generate_activation_key() user_profile.save() email_subject = 'Forgotten password' email_body = 'PANDA received a request to change your password.\n\nTo set your new password follow this link:\n\nhttp://%s/#reset-password/%s\n\nIf you did not request this email you should notify your adminstrator.' % (config_value('DOMAIN', 'SITE_DOMAIN'), user_profile.activation_key) send_mail(email_subject, email_body, [user.email]) # Success return JSONResponse(make_user_login_response(user)) else: # Invalid request return JSONResponse(None, status=400)
def send_activation_email(self): email_subject = _('Welcome to PANDA, please activate your account!') email_body = _('Hello there, the administrator of your organization\'s PANDA has signed you up for an account.\n\nTo activate your account, click this link:\n\nhttp://%(site_domain)s/#activate/%(activation_key)s') \ % {'site_domain': config_value('DOMAIN', 'SITE_DOMAIN'), 'activation_key': self.activation_key} send_mail(email_subject, email_body, [self.user.email])
def send_notifications(self, dataset, retval, einfo): """ Send user notifications this task has finished. """ from panda.models import Notification task_status = dataset.current_task if einfo: task_status.exception("Import failed", u"\n".join([einfo.traceback, unicode(retval)])) email_subject = "Import failed: %s" % dataset.name email_message = "Import failed: %s:\n\nhttp://%s/#dataset/%s" % ( dataset.name, config_value("DOMAIN", "SITE_DOMAIN"), dataset.slug, ) notification_message = "Import failed: <strong>%s</strong>" % dataset.name notification_type = "Error" elif self.is_aborted(): email_subject = "Import aborted: %s" % dataset.name email_message = "Import aborted: %s:\n\nhttp://%s/#dataset/%s" % ( dataset.name, config_value("DOMAIN", "SITE_DOMAIN"), dataset.slug, ) notification_message = "Import aborted: <strong>%s</strong>" % dataset.name notification_type = "Info" else: task_status.complete("Import complete") email_subject = "Import complete: %s" % dataset.name email_message = "Import complete: %s:\n\nhttp://%s/#dataset/%s" % ( dataset.name, config_value("DOMAIN", "SITE_DOMAIN"), dataset.slug, ) notification_message = "Import complete: <strong>%s</strong>" % dataset.name notification_type = "Info" if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=dataset, message=notification_message, type=notification_type, ) send_mail(email_subject, email_message, [task_status.creator.username])
def after_return(self, status, retval, task_id, args, kwargs, einfo): """ Save final status, results, etc. """ from panda.models import Dataset, Notification dataset = Dataset.objects.get(slug=args[0]) task_status = dataset.current_task if einfo: self.task_exception( task_status, 'Import failed', u'\n'.join([einfo.traceback, unicode(retval)]) ) email_subject = 'Import failed: %s' % dataset.name email_message = 'Import failed: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) notification_message = 'Import failed: <strong>%s</strong>' % dataset.name notification_type = 'Error' elif self.is_aborted(): email_subject = 'Import aborted: %s' % dataset.name email_message = 'Import aborted: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) notification_message = 'Import aborted: <strong>%s</strong>' % dataset.name notification_type = 'Info' else: self.task_complete(task_status, 'Import complete') email_subject = 'Import complete: %s' % dataset.name email_message = 'Import complete: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) notification_message = 'Import complete: <strong>%s</strong>' % dataset.name notification_type = 'Info' if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=dataset, message=notification_message, type=notification_type ) send_mail(email_subject, email_message, [task_status.creator.username]) # If import failed, clear any data that might be staged if task_status.status == 'FAILURE': solr.delete(settings.SOLR_DATA_CORE, 'dataset_slug:%s' % args[0], commit=True)
def send_notifications(self, dataset, retval, einfo): """ Send user notifications this task has finished. """ from panda.models import Notification task_status = dataset.current_task if einfo: task_status.exception( 'Reindex failed', u'\n'.join([einfo.traceback, unicode(retval)]) ) email_subject = 'Reindex failed: %s' % dataset.name email_message = 'Reindex failed: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) notification_message = 'Reindex failed: <strong>%s</strong>' % dataset.name notification_type = 'Error' elif self.is_aborted(): email_subject = 'Reindex aborted: %s' % dataset.name email_message = 'Reindex aborted: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) notification_message = 'Reindex aborted: <strong>%s</strong>' % dataset.name notification_type = 'Info' else: task_status.complete('Reindex complete') email_subject = 'Reindex complete: %s' % dataset.name email_message = 'Reindex complete: %s:\n\nhttp://%s/#dataset/%s' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN'), dataset.slug) type_summary = retval.summarize() if type_summary: email_message += '\n\n' + type_summary notification_message = 'Reindex complete: <strong>%s</strong>' % dataset.name notification_type = 'Info' if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=dataset, message=notification_message, type=notification_type ) send_mail(email_subject, email_message, [task_status.creator.username])
def after_return(self, status, retval, task_id, args, kwargs, einfo): """ Save final status, results, etc. """ from panda.models import Dataset, Export, Notification dataset = Dataset.objects.get(slug=args[0]) task_status = dataset.current_task if einfo: error_detail = u'\n'.join([einfo.traceback, unicode(retval)]) self.task_exception( task_status, 'Export failed', error_detail) email_subject = 'Export failed: %s' % dataset.name email_message = 'Export failed: %s:\n%s' % (dataset.name, error_detail) notification_message = 'Export failed: <strong>%s</strong>' % dataset.name notification_type = 'Error' else: self.task_complete(task_status, 'Export complete') export = Export.objects.create( filename=retval, original_filename=retval, size=os.path.getsize(os.path.join(settings.EXPORT_ROOT, retval)), creator=task_status.creator, creation_date=task_status.start, dataset=dataset) email_subject = 'Export complete: %s' % dataset.name email_message = 'Export complete: %s. Download your results:\n\nhttp://%s/api/1.0/export/%i/download/' % (dataset.name, config_value('DOMAIN', 'SITE_DOMAIN', export.id), export.id) notification_message = 'Export complete: <strong>%s</strong>' % dataset.name notification_type = 'Info' if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=dataset, message=notification_message, type=notification_type ) send_mail(email_subject, email_message, [task_status.creator.username])
def send_notifications(self, query, task_status, retval, einfo): """ Send user notifications this task has finished. """ from panda.models import Export, Notification if einfo: if isinstance(einfo, tuple): traceback = '\n'.join(format_tb(einfo[2])) else: traceback = einfo.traceback error_detail = u'\n'.join([traceback, unicode(retval)]) task_status.exception('Export failed', error_detail) email_subject = 'Export failed: %s' % query email_message = 'Export failed: %s:\n%s' % (query, error_detail) notification_message = 'Export failed: <strong>%s</strong>' % (query) notification_type = 'Error' else: task_status.complete('Export complete') export = Export.objects.create( filename=retval, original_filename=retval, size=os.path.getsize(os.path.join(settings.EXPORT_ROOT, retval)), creator=task_status.creator, creation_date=task_status.start, dataset=None) email_subject = 'Export complete: %s' % query email_message = 'Export complete: %s. Download your results:\n\nhttp://%s/api/1.0/export/%i/download/' % (query, config_value('DOMAIN', 'SITE_DOMAIN', export.id), export.id) notification_message = 'Export complete: <strong>%s</strong>' % query notification_type = 'Info' if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=None, message=notification_message, type=notification_type ) send_mail(email_subject, email_message, [task_status.creator.username])
def notify(recipient, template_prefix, note_type, url=None, extra_context={}): """ Notify a user of an event using the Notification system and email. """ from panda.models import Notification context = Context({ 'recipient': recipient, 'type': note_type, 'url': url, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) context.update(extra_context) message = get_message_template(template_prefix).render(context) Notification.objects.create( recipient=recipient, message=message, type=note_type, url=url ) # Don't HTML escape plain-text emails context.autoescape = False try: email_subject = get_email_subject_template(template_prefix).render(context) except TemplateDoesNotExist: email_subject = message try: email_message = get_email_body_template(template_prefix).render(context) except TemplateDoesNotExist: email_message = message send_mail(email_subject.strip(), email_message, [recipient.username])
def notify(recipient, template_prefix, note_type, url=None, extra_context={}): """ Notify a user of an event using the Notification system and email. """ from panda.models import Notification context = Context({ 'recipient': recipient, 'type': note_type, 'url': url, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) context.update(extra_context) message = get_message_template(template_prefix).render(context) Notification.objects.create(recipient=recipient, message=message, type=note_type, url=url) # Don't HTML escape plain-text emails context.autoescape = False try: email_subject = get_email_subject_template(template_prefix).render( context) except TemplateDoesNotExist: email_subject = message try: email_message = get_email_body_template(template_prefix).render( context) except TemplateDoesNotExist: email_message = message send_mail(email_subject.strip(), email_message, [recipient.username])
def run(self, *args, **kwargs): from panda.models import UserProxy log = logging.getLogger(self.name) log.info('Running admin alerts') # Disk space root_disk = os.stat('/').st_dev upload_disk = os.stat(settings.MEDIA_ROOT).st_dev indices_disk = os.stat(settings.SOLR_DIRECTORY).st_dev root_disk_total = get_total_disk_space('/') root_disk_free = get_free_disk_space('/') root_disk_percent_used = 100 - (float(root_disk_free) / root_disk_total * 100) if upload_disk != root_disk: upload_disk_total = get_total_disk_space(settings.MEDIA_ROOT) upload_disk_free = get_free_disk_space(settings.MEDIA_ROOT) upload_disk_percent_used = 100 - (float(upload_disk_free) / upload_disk_total * 100) else: upload_disk_total = None upload_disk_free = None upload_disk_percent_used = None if indices_disk != root_disk: indices_disk_total = get_total_disk_space(settings.SOLR_DIRECTORY) indices_disk_free = get_free_disk_space(settings.SOLR_DIRECTORY) indices_disk_percent_used = 100 - (float(indices_disk_free) / indices_disk_total * 100) else: indices_disk_total = None indices_disk_free = None indices_disk_percent_used = None notify = False for free in (root_disk_free, upload_disk_free, indices_disk_free): if free is None: continue if free < settings.PANDA_AVAILABLE_SPACE_WARN: notify = True if notify: context = Context({ 'root_disk': root_disk, 'upload_disk': upload_disk, 'indices_disk': indices_disk, 'root_disk_total': root_disk_total, 'root_disk_free': root_disk_free, 'root_disk_percent_used': root_disk_percent_used, 'upload_disk_total': upload_disk_total, 'upload_disk_free': upload_disk_free, 'upload_disk_percent_used': upload_disk_percent_used, 'indices_disk_total': indices_disk_total, 'indices_disk_free': indices_disk_free, 'indices_disk_percent_used': indices_disk_percent_used, 'settings': settings, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) # Don't HTML escape plain-text emails context.autoescape = False email_subject = get_email_subject_template( 'disk_space_alert').render(context) email_message = get_email_body_template('disk_space_alert').render( context) recipients = UserProxy.objects.filter(is_superuser=True, is_active=True) send_mail(email_subject.strip(), email_message, [r.email for r in recipients]) log.info('Finished running admin alerts')
def run(self, *args, **kwargs): from panda.models import UserProxy log = logging.getLogger(self.name) log.info('Running admin alerts') # Disk space root_disk = os.stat('/').st_dev upload_disk = os.stat(settings.MEDIA_ROOT).st_dev indices_disk = os.stat(settings.SOLR_DIRECTORY).st_dev root_disk_total = get_total_disk_space('/') root_disk_free = get_free_disk_space('/') root_disk_percent_used = 100 - (float(root_disk_free) / root_disk_total * 100) if upload_disk != root_disk: upload_disk_total = get_total_disk_space(settings.MEDIA_ROOT) upload_disk_free = get_free_disk_space(settings.MEDIA_ROOT) upload_disk_percent_used = 100 - (float(upload_disk_free) / upload_disk_total * 100) else: upload_disk_total = None upload_disk_free = None upload_disk_percent_used = None if indices_disk != root_disk: indices_disk_total = get_total_disk_space(settings.SOLR_DIRECTORY) indices_disk_free = get_free_disk_space(settings.SOLR_DIRECTORY) indices_disk_percent_used = 100 - (float(indices_disk_free) / indices_disk_total * 100) else: indices_disk_total = None indices_disk_free = None indices_disk_percent_used = None notify = False for free in (root_disk_free, upload_disk_free, indices_disk_free): if free is None: continue if free < settings.PANDA_AVAILABLE_SPACE_WARN: notify = True if notify: context = Context({ 'root_disk': root_disk, 'upload_disk': upload_disk, 'indices_disk': indices_disk, 'root_disk_total': root_disk_total, 'root_disk_free': root_disk_free, 'root_disk_percent_used': root_disk_percent_used, 'upload_disk_total': upload_disk_total, 'upload_disk_free': upload_disk_free, 'upload_disk_percent_used': upload_disk_percent_used, 'indices_disk_total': indices_disk_total, 'indices_disk_free': indices_disk_free, 'indices_disk_percent_used': indices_disk_percent_used, 'settings': settings, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) # Don't HTML escape plain-text emails context.autoescape = False email_subject = get_email_subject_template('disk_space_alert').render(context) email_message = get_email_body_template('disk_space_alert').render(context) recipients = UserProxy.objects.filter(is_superuser=True, is_active=True) send_mail(email_subject.strip(), email_message, [r.email for r in recipients]) log.info('Finished running admin alerts')
def send_notifications(self, dataset, query, retval, einfo): """ Send user notifications this task has finished. """ from panda.models import Export, Notification task_status = dataset.current_task dataset_name = unquote(dataset.name) if einfo: error_detail = u"\n".join([einfo.traceback, unicode(retval)]) task_status.exception("Export failed", error_detail) if query: email_subject = 'Export failed: "%s" in %s' % (query, dataset_name) email_message = 'Export failed: "%s" in %s:\n%s' % (query, dataset_name, error_detail) notification_message = 'Export failed: <strong>"%s" in %s</strong>' % (query, dataset_name) else: email_subject = "Export failed: %s" % dataset_name email_message = "Export failed: %s:\n%s" % (dataset_name, error_detail) notification_message = "Export failed: <strong>%s</strong>" % dataset_name notification_type = "Error" else: task_status.complete("Export complete") export = Export.objects.create( filename=retval, original_filename=retval, size=os.path.getsize(os.path.join(settings.EXPORT_ROOT, retval)), creator=task_status.creator, creation_date=task_status.start, dataset=dataset, ) if query: email_subject = 'Export complete: "%s" in %s' % (query, dataset_name) email_message = ( 'Export complete: "%s" in %s. Download your results:\n\nhttp://%s/api/1.0/export/%i/download/' % (query, dataset_name, config_value("DOMAIN", "SITE_DOMAIN", export.id), export.id) ) notification_message = 'Export complete: <strong>"%s" in %s</strong>' % (query, dataset_name) else: email_subject = "Export complete: %s" % dataset_name email_message = ( "Export complete: %s. Download your results:\n\nhttp://%s/api/1.0/export/%i/download/" % (dataset_name, config_value("DOMAIN", "SITE_DOMAIN", export.id), export.id) ) notification_message = "Export complete: <strong>%s</strong>" % dataset_name notification_type = "Info" if task_status.creator: Notification.objects.create( recipient=task_status.creator, related_task=task_status, related_dataset=dataset, message=notification_message, type=notification_type, ) send_mail(email_subject, email_message, [task_status.creator.username])