def build_mail(self, year, user): print 'Building mail for %d - %s' % (year, user) # Sum stats stats = {} for m in range(1, 13): month = StatsMonth(user, year, m) data = month.fetch() if data: stats = self.merge(stats, data) # Load sports objects if stats and 'sports' in stats: for s_id, s in stats['sports'].items(): s['sport'] = Sport.objects.get(pk=s_id) # Build mail builder = MailBuilder('mail/new.year.html', user.language) data = { 'year' : year, 'stats' : stats, 'user' : user, } builder.subject = u'[RunReport] Bonne année !' builder.to = [user.email, ] mail = builder.build(data) mail.send()
def send_related_races_mail(): ''' Send mails about incoming friends and club members races ''' from users.models import Athlete from coach.mail import MailBuilder for a in Athlete.objects.all(): # List incoming races races = a.list_related_races() if not races: continue # Build and send email context = { 'user': a, 'races': races, } mb = MailBuilder('mail/related_races.html', a.language) mb.to = [ a.email, ] mb.subject = _('New friends races') try: mail = mb.build(context) mail.send() except Exception, e: print 'Mail for races failed: %s' % str(e)
def publish(self, membership, base_uri=None): ''' Publish this report ''' if self.published: raise Exception('This report is already published') # Build xls xls = open(self.build_xls(), 'r') xls_name = '%s_semaine_%d.xls' % (self.user.username, self.week+1) # Context for html context = { 'week_human' : self.week + 1, 'report': self, 'club': membership.club, 'days' : self.get_days_per_date(), 'base_uri' : base_uri, } # Build mail headers = {'Reply-To' : self.user.email,} mb = MailBuilder('mail/report.html') mb.subject = u'Séance de %s : du %s au %s' % (self.user, self.get_date_start(), self.get_date_end()) mb.to = [m.email for m in membership.trainers.all()] mb.cc = [self.user.email] mail = mb.build(context, headers) # Attach Xls & send mail.attach(xls_name, xls.read(), 'application/vnd.ms-excel') mail.send() self.published = True self.save()
def notify_friend_request(sender, recipient, accepted=False): ''' Notify a user he has a new or accepted friend request by sending him an email ''' from coach.mail import MailBuilder builder = MailBuilder('mail/friend.request.html') data = { 'sender' : sender, 'recipient' : recipient, 'accepted' : accepted, } if accepted: builder.language = sender.language builder.subject = u'[RunReport] Demande d\'ami acceptée' builder.to = [sender.email, ] else: builder.language = recipient.language builder.subject = u'[RunReport] Nouvelle demande d\'ami' builder.to = [recipient.email, ] mail = builder.build(data) mail.send()
def find_badges(self, save=False): ''' Find all the best badges for a user ''' from badges.models import BadgeCategory from coach.mail import MailBuilder all_added = [] for cat in BadgeCategory.objects.all(): badges, added = cat.find_badges(self, save) if added: all_added += added # Notify a user about new badges # in one mail if all_added: data = { 'user': self, 'badges': all_added, } mb = MailBuilder('mail/badges.html') mb.language = self.language mb.subject = _('New badges') mb.to = [ self.email, ] mail = mb.build(data) mail.send() return all_added
def send(self): ''' Send the invite by mail ''' if self.sent: raise Exception('Invite already sent') context = { 'invite_url' : self.get_absolute_url(), 'name' : self.name, 'club' : self.club, 'user' : self.user, } templates = { 'create' : 'mail/club_invite.html', 'join' : 'mail/subscription.html', } lang = self.user and self.user.language or 'fr' # Default to french mb = MailBuilder(templates[self.type], lang) mb.to = [self.recipient, ] mb.subject = _('RunReport Invite') mail = mb.build(context) mail.send() # Save sent date self.sent = datetime.now() self.save()
def find_badges(self, save=False): """ Find all the best badges for a user """ from badges.models import BadgeCategory from coach.mail import MailBuilder all_added = [] for cat in BadgeCategory.objects.all(): badges, added = cat.find_badges(self, save) if added: all_added += added # Notify a user about new badges # in one mail if all_added: data = {"user": self, "badges": all_added} mb = MailBuilder("mail/badges.html") mb.language = self.language mb.subject = _("New badges") mb.to = [self.email] mail = mb.build(data) mail.send() return all_added
def race_mail(*args, **kwargs): ''' Send a mail to all users having a race today ''' from sport.models import SportSession from datetime import date, timedelta from coach.mail import MailBuilder # Setup mail builder builder = MailBuilder('mail/race.html') # Load tommorow's race tmrw = date.today() + timedelta(days=1) races = SportSession.objects.filter(day__date=tmrw, type='race') # Build and Send all mails for race in races: user = race.day.week.user data = { 'race' : race, 'user' : user, } builder.language = user.language builder.subject = u'Votre course %s - RunReport' % (race.name,) builder.to = [user.email, ] mail = builder.build(data) mail.send()
def send_related_races_mail(): """ Send mails about incoming friends and club members races """ from users.models import Athlete from coach.mail import MailBuilder for a in Athlete.objects.all(): # List incoming races races = a.list_related_races() if not races: continue # Build and send email context = {"user": a, "races": races} mb = MailBuilder("mail/related_races.html", a.language) mb.to = [a.email] mb.subject = _("New friends races") try: mail = mb.build(context) mail.send() except Exception, e: print "Mail for races failed: %s" % str(e)
def athletes_daily_sessions(): ''' For all trainers, send a daily email with sessions about to be done today ''' from django.utils.translation import ugettext_lazy as _ from club.models import ClubMembership from sport.models import SportSession from coach.mail import MailBuilder from datetime import date today = date.today() memberships = ClubMembership.objects.filter(role='trainer', user__daily_trainer_mail=True) for m in memberships: # List athletes sessions for today users = [cm.user for cm in m.athletes] sessions = SportSession.objects.filter(day__date=today, day__week__user__in=users) if not sessions: continue # Build mail mb = MailBuilder('mail/sessions.html', m.user.language) mb.to = [ m.user.email, ] mb.subject = _('Your athlete\'s sessions today') context = { 'club': m.club, 'user': m.user, 'sessions': sessions, } mail = mb.build(context) mail.send()
def notify_friend_request(sender, recipient, accepted=False): ''' Notify a user he has a new or accepted friend request by sending him an email ''' from coach.mail import MailBuilder builder = MailBuilder('mail/friend.request.html') data = { 'sender': sender, 'recipient': recipient, 'accepted': accepted, } if accepted: builder.language = sender.language builder.subject = u'[RunReport] Demande d\'ami acceptée' builder.to = [ sender.email, ] else: builder.language = recipient.language builder.subject = u'[RunReport] Nouvelle demande d\'ami' builder.to = [ recipient.email, ] mail = builder.build(data) mail.send()
def athletes_daily_sessions(): ''' For all trainers, send a daily email with sessions about to be done today ''' from django.utils.translation import ugettext_lazy as _ from club.models import ClubMembership from sport.models import SportSession from coach.mail import MailBuilder from datetime import date today = date.today() memberships = ClubMembership.objects.filter(role='trainer', user__daily_trainer_mail=True) for m in memberships: # List athletes sessions for today users = [cm.user for cm in m.athletes] sessions = SportSession.objects.filter(day__date=today, day__week__user__in=users) if not sessions: continue # Build mail mb = MailBuilder('mail/sessions.html', m.user.language) mb.to = [m.user.email, ] mb.subject = _('Your athlete\'s sessions today') context = { 'club' : m.club, 'user' : m.user, 'sessions' : sessions, } mail = mb.build(context) mail.send()
def send(self): ''' Send the invite by mail ''' if self.sent: raise Exception('Invite already sent') context = { 'invite_url': self.get_absolute_url(), 'name': self.name, 'club': self.club, 'user': self.user, } templates = { 'create': 'mail/club_invite.html', 'join': 'mail/subscription.html', } lang = self.user and self.user.language or 'fr' # Default to french mb = MailBuilder(templates[self.type], lang) mb.to = [ self.recipient, ] mb.subject = _('RunReport Invite') mail = mb.build(context) mail.send() # Save sent date self.sent = datetime.now() self.save()
def mail_club(self): # Send mail to club manager # about a new prospect context = { 'manager': self.club.manager, 'club': self.club, 'user': self.user, } mb = MailBuilder('mail/club_prospect.html', self.club.manager.language) mb.to = [self.club.manager.email] mb.subject = 'Nouvelle inscription au club %s' % (self.club.name, ) mail = mb.build(context) mail.send()
def warn_sender(self): ''' Send a warning message to sender Used when the invite is asked ''' context = { 'invite': self, } mb = MailBuilder('mail/club_invite_asked.html', self.sender.language) mb.to = [self.sender.email] mb.subject = 'Demande Invitation RunReport.fr' mail = mb.build(context) mail.send()
def mail_club(self): # Send mail to club manager # about a new prospect context = { 'manager' : self.club.manager, 'club' : self.club, 'user' : self.user, } mb = MailBuilder('mail/club_prospect.html', self.club.manager.language) mb.to = [self.club.manager.email] mb.subject = 'Nouvelle inscription au club %s' % (self.club.name, ) mail = mb.build(context) mail.send()
def form_valid(self, form): """ Send email to all admins """ context = form.cleaned_data headers = {"Reply-To": context["email"]} mb = MailBuilder("mail/contact.html") mb.subject = _("New contact message") mb.to = [email for __, email in settings.ADMINS] mail = mb.build(context, headers) mail.send() return super(ContactView, self).form_valid(form)
def warn_sender(self): ''' Send a warning message to sender Used when the invite is asked ''' context = { 'invite' : self, } mb = MailBuilder('mail/club_invite_asked.html', self.sender.language) mb.to = [self.sender.email, ] mb.subject = 'Demande Invitation RunReport.fr' mail = mb.build(context) mail.send()
def mail_user(self, old_role): # Send mail to user # about a role evolution context = { 'club': self.club, 'user': self.user, 'old_role': old_role, 'new_role': self.role, } mb = MailBuilder('mail/user_role.html', self.user.language) mb.to = [self.user.email] mb.subject = 'Role dans le club %s' % (self.club.name, ) mail = mb.build(context) mail.send()
def mail_user(self, old_role): # Send mail to user # about a role evolution context = { 'club' : self.club, 'user' : self.user, 'old_role' : old_role, 'new_role' : self.role, } mb = MailBuilder('mail/user_role.html', self.user.language) mb.to = [self.user.email] mb.subject = 'Role dans le club %s' % (self.club.name, ) mail = mb.build(context) mail.send()
def form_valid(self, form): ''' Send email to all admins ''' context = form.cleaned_data headers = { 'Reply-To': context['email'], } mb = MailBuilder('mail/contact.html') mb.subject = _('New contact message') mb.to = [email for __, email in settings.ADMINS] mail = mb.build(context, headers) mail.send() return super(ContactView, self).form_valid(form)
def notify_message(message, user): ''' Notify a user he has a new message by sending him an email ''' from coach.mail import MailBuilder builder = MailBuilder('mail/message.html', user.language) data = { 'message' : message, 'user' : user, } builder.subject = u'[RunReport] Nouveau message' builder.to = [user.email, ] mail = builder.build(data) mail.send()
def notify_message(message, user): ''' Notify a user he has a new message by sending him an email ''' from coach.mail import MailBuilder builder = MailBuilder('mail/message.html', user.language) data = { 'message' : message, 'user' : user, } if message.conversation.type == 'comments_week': data['week'] = message.conversation.week builder.subject = u'[RunReport] Nouveau message' builder.to = [user.email, ] mail = builder.build(data) mail.send()
def notify_message(message, user): ''' Notify a user he has a new message by sending him an email ''' from coach.mail import MailBuilder builder = MailBuilder('mail/message.html', user.language) data = { 'message': message, 'user': user, } builder.subject = u'[RunReport] Nouveau message' builder.to = [ user.email, ] mail = builder.build(data) mail.send()
def send(self): ''' Send the invite by mail ''' if self.sent: raise Exception('Invite already sent') context = { 'invite_url': self.get_absolute_url(), 'name': self.name, } mb = MailBuilder('mail/club_invite.html', 'fr') # Default to french mb.to = [self.recipient] mb.subject = 'Invitation RunReport.fr' mail = mb.build(context) mail.send() # Save sent date self.sent = datetime.now() self.save()
def send(self): ''' Send the invite by mail ''' if self.sent: raise Exception('Invite already sent') context = { 'invite_url' : self.get_absolute_url(), 'name' : self.name, } mb = MailBuilder('mail/club_invite.html', 'fr') # Default to french mb.to = [self.recipient] mb.subject = 'Invitation RunReport.fr' mail = mb.build(context) mail.send() # Save sent date self.sent = datetime.now() self.save()
def notify_athlete(self, user, pdf): ''' Send an email to athlete with the plan attached ''' # Context for html context = { 'plan' : self, 'user' : user, } # Build mail mb = MailBuilder('mail/plan.html', user.language) mb.subject = _(u'Training plan : %s') % (self.name, ) mb.to = [user.email, ] mail = mb.build(context) # Attach Xls & send pdf_name = _('Training plan - %s.pdf') % self.name mail.attach(pdf_name, pdf, 'application/pdf') mail.send()
def notify_athlete(self, user, pdf): ''' Send an email to athlete with the plan attached ''' # Context for html context = { 'plan': self, 'user': user, } # Build mail mb = MailBuilder('mail/plan.html', user.language) mb.subject = _(u'Training plan : %s') % (self.name, ) mb.to = [ user.email, ] mail = mb.build(context) # Attach Xls & send pdf_name = _('Training plan - %s.pdf') % self.name mail.attach(pdf_name, pdf, 'application/pdf') mail.send()