def handle(self, *args, **options): # Filter onganizations with an active reminder mail organizations = Organization.objects.filter(mailtemplate__name='reminder', mailtemplate__is_active=True) for organization in organizations: memberships = organization.membership_set.filter(is_tender=True, is_active=True) now = timezone.now() events = organization.participates.filter(starts_at__gte=now, is_closed=False).order_by('starts_at', ) # Load template and settings try: mailtemplate = MailTemplate.objects.get(organization=organization, name="reminder", is_active=True) starts_before = now + datetime.timedelta(minutes=mailtemplate.send_at) if mailtemplate.send_at else None except MailTemplate.DoesNotExist: raise CommandError('MailTemplate "reminder" does not exist for %s' % organization) for membership in memberships: user = membership.user if not user.email: self.stderr.write('%s heeft geen e-mailadres.\n' % (user,)) continue missing_events = events.exclude(pk__in=user.event_set.values_list('id', flat=True)) if starts_before is not None: missing_events = missing_events.exclude(starts_at__gte=starts_before) addressees = [user] if len(missing_events) > 0: mail(settings.EMAIL_FROM, addressees, mailtemplate.subject, mailtemplate.template, extraattrs={'missing_events': missing_events, 'now': now})
def notify_tenders(sender, instance, **kwargs): from apps.scheduling.models import Event, MailTemplate mail_template = None if instance.pk is None: if not instance.is_closed: mail_template = "enrollopen" else: orig = Event.objects.get(pk=instance.pk) if orig.is_closed and not instance.is_closed: mail_template = "enrollopen" elif not orig.is_closed and instance.is_closed: mail_template = "enrollclosed" if mail_template: try: mt = MailTemplate.objects.get(organization=instance.organizer, name=mail_template) if mt.is_active: members = instance.organizer.membership_set.filter( is_tender=True).exclude(user__email="") addressees = [m.user for m in members] mail(settings.EMAIL_FROM, addressees, mt.subject, mt.template, extraattrs={'event': instance}) except MailTemplate.DoesNotExist: pass
def notify_tenders(sender, instance, **kwargs): from apps.scheduling.models import Event, MailTemplate mail_template = None if instance.pk is None: if not instance.is_closed: mail_template = "enrollopen" else: orig = Event.objects.get(pk=instance.pk) if orig.is_closed and not instance.is_closed: mail_template = "enrollopen" elif not orig.is_closed and instance.is_closed: mail_template = "enrollclosed" if mail_template: try: mt = MailTemplate.objects.get(organization=instance.organizer, name=mail_template) if mt.is_active: members = instance.organizer.membership_set.filter(is_tender=True).exclude(user__email="") addressees = [m.user for m in members] mail(settings.EMAIL_FROM, addressees, mt.subject, mt.template, extraattrs={'event': instance}) except MailTemplate.DoesNotExist: pass
def run_billing(emails="*****@*****.**", when=0): if not when: when = dt.now() date = '/'.join([str(when.month), str(when.day), str(when.year)]) print "Collecting output..." output = make_all_projects_output() save_output_to_file(output, when) print "Emailing results to %s" % emails if emails: mail.mail(emails, 'Trac Billing - %s ' % date, output, html=True, fromEmail='*****@*****.**') return output
def monitor_alert(): while True: r_time = (datetime.datetime.now() - datetime.timedelta(minutes=INTERVAL)).strftime("%Y-%m-%d %H:%M:%S") _, rt_list = dbutils.db_operating(SELECT_MONITOR_HOST, True, (r_time,)) rt_dict = {} for line in rt_list: rt_dict.setdefault(line[0], {'cpu' : 0, 'mem' : 0, 'disk' : 0}) for key, value in SELECT_MONITOR_HOST_COLUMNS.items(): if line[key] > policies[value]['ceil']: rt_dict[line[0]][value] += 1 for key in rt_dict: messages = [] for resource in SELECT_MONITOR_HOST_COLUMNS.values(): if rt_dict[key][resource] > policies[resource]['count']: messages.append('%s超过%s分钟内%s次超过阀值%s%%' % (resource, INTERVAL, policies[resource]['count'], policies[resource]['ceil'])) if messages: asset = model.get_asset_by_ip(key) admin = asset.get('admin', DEFAULT_ADMIN) # if admin != DEFAULT_ADMIN: # admin = [admin, DEFAULT_ADMIN] c_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") dbutils.db_operating(SQL_ALERT_CREATE, False, (key, ','.join(messages), admin, c_time)) RET = mail.mail(admin, '资源告警', key + '<br/>' + '<br/>'.join(messages)) print("start mail", RET) time.sleep(3)
def handle(self, *args, **options): # Filter onganizations with an active reminder mail organizations = Organization.objects.filter( mailtemplate__name='reminder', mailtemplate__is_active=True) for organization in organizations: memberships = organization.membership_set.filter(is_tender=True) now = timezone.now() events = organization.participates.filter( starts_at__gte=now, is_closed=False).order_by('starts_at', ) # Load template and settings try: mailtemplate = MailTemplate.objects.get( organization=organization, name="reminder", is_active=True) starts_before = now + datetime.timedelta( minutes=mailtemplate.send_at ) if mailtemplate.send_at else None except MailTemplate.DoesNotExist: raise CommandError( 'MailTemplate "reminder" does not exist for %s' % organization) for membership in memberships: user = membership.user if not user.email: self.stderr.write('%s heeft geen e-mailadres.\n' % (user, )) continue missing_events = events.exclude( pk__in=user.event_set.values_list('id', flat=True)) if starts_before is not None: missing_events = missing_events.exclude( starts_at__gte=starts_before) addressees = [user] if len(missing_events) > 0: mail(settings.EMAIL_FROM, addressees, mailtemplate.subject, mailtemplate.template, extraattrs={ 'missing_events': missing_events, 'now': now })
def save(self, organizer=None, *args, **kwargs): event = super(EventForm, self).save(*args, **kwargs) if not organizer: organizer = event.organizer # Mail voor standardreservations for reservation in StandardReservation.objects.occuring_at( self.cleaned_data['starts_at'], self.cleaned_data['ends_at'], ): if reservation.location in self.cleaned_data['location'] and \ not reservation.organization == organizer: try: mt = MailTemplate.objects.get(organization=reservation.organization, name="reservations") if mt.is_active: addressees = reservation.organization.managers.all() mail(settings.EMAIL_FROM, addressees, mt.subject, mt.template, extraattrs={'event': event}) except MailTemplate.DoesNotExist: pass # Dan maar geen mail... return event
def save(self, organizer=None, *args, **kwargs): event = super(EventForm, self).save(*args, **kwargs) if not organizer: organizer = event.organizer # Mail voor standardreservations for reservation in StandardReservation.objects.occuring_at( self.cleaned_data['starts_at'], self.cleaned_data['ends_at']): if reservation.location in self.cleaned_data['location'] and \ not reservation.organization == organizer: try: mt = MailTemplate.objects.get( organization=reservation.organization, name="reservations") if mt.is_active: addressees = reservation.organization.managers.all() mail(settings.EMAIL_FROM, addressees, mt.subject, mt.template, extraattrs={'event': event}) except MailTemplate.DoesNotExist: pass # Dan maar geen mail... return event