def clean_direct_channel(channel): channel = clean_instance(channel, Channel) # A direct channel can't have more than 2 participants if channel.type == CHANNEL_TYPE_DIRECT and channel.participants.count( ) > 2: channel.type = CHANNEL_TYPE_TOPIC channel.save()
def update_task_submit_milestone(task): task = clean_instance(task, Task) if task.deadline: days_before = task.fee > 150 and 2 or 1 submission_date = task.deadline - datetime.timedelta(days=days_before) defaults = {'due_at': submission_date, 'title': 'Submit final draft'} ProgressEvent.objects.update_or_create(task=task, type=PROGRESS_EVENT_TYPE_SUBMIT, defaults=defaults)
def send_contact_request_email(instance): instance = clean_instance(instance, ContactRequest) subject = "%s New Contact Request" % EMAIL_SUBJECT_PREFIX to = [CONTACT_REQUEST_EMAIL_RECIPIENT] ctx = {'email': instance.email} if send_mail(subject, 'tunga/email/email_contact_request_message', to, ctx): instance.email_sent_at = datetime.datetime.utcnow() instance.save()
def send_new_user_email(instance): instance = clean_instance(instance, get_user_model()) subject = "%s %s joined Tunga" % (EMAIL_SUBJECT_PREFIX, instance.display_name) to = TUNGA_STAFF_UPDATE_EMAIL_RECIPIENTS ctx = { 'user': instance, 'user_url': '%s/member/%s/' % (TUNGA_URL, instance.id) } send_mail(subject, 'tunga/email/email_new_user', to, ctx)
def send_contact_request_email(instance): instance = clean_instance(instance, ContactRequest) subject = "%s New Contact Request" % EMAIL_SUBJECT_PREFIX to = [CONTACT_REQUEST_EMAIL_RECIPIENT] ctx = {'email': instance.email} if send_mail(subject, 'tunga/email/email_contact_request_message', to, ctx): instance.email_sent_at = datetime.datetime.utcnow() instance.save()
def send_new_developer_email(instance): instance = clean_instance(instance, DeveloperApplication) subject = "%s %s has applied to become a Tunga developer" % ( EMAIL_SUBJECT_PREFIX, instance.display_name) to = TUNGA_STAFF_UPDATE_EMAIL_RECIPIENTS ctx = { 'application': instance, } send_mail(subject, 'tunga/email/email_new_developer_application', to, ctx)
def send_new_task_email(instance): instance = clean_instance(instance, Task) if instance.visibility in [VISIBILITY_DEVELOPER, VISIBILITY_MY_TEAM]: queryset = get_user_model().objects.filter(type=USER_TYPE_DEVELOPER) if instance.visibility == VISIBILITY_MY_TEAM: queryset = queryset.filter( my_connections_q_filter(instance.user) ) ordering = [] task_skills = instance.skills.all() if task_skills: when = [] for skill in task_skills: new_when = When( userprofile__skills=skill, then=1 ) when.append(new_when) queryset = queryset.annotate(matches=Sum( Case( *when, default=0, output_field=IntegerField() ) )) ordering.append('-matches') ordering.append('-tasks_completed') queryset = queryset.annotate( tasks_completed=Sum( Case( When( participation__task__closed=True, participation__user__id=F('id'), participation__accepted=True, then=1 ), default=0, output_field=IntegerField() ) ) ) queryset = queryset.order_by(*ordering) developers = None if queryset: developers = queryset[:15] subject = "%s New task created by %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) to = TUNGA_STAFF_UPDATE_EMAIL_RECIPIENTS bcc = [dev.email for dev in developers] if developers else None ctx = { 'owner': instance.user, 'task': instance, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.id) } send_mail(subject, 'tunga/email/email_new_task', to, ctx, bcc=bcc)
def send_new_task_application_email(instance): instance = clean_instance(instance, Application) subject = "%s New application from %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) to = [instance.task.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_application', to, ctx)
def send_new_task_application_applicant_email(instance): instance = clean_instance(instance, Application) subject = "%s You applied for a task: %s" % (EMAIL_SUBJECT_PREFIX, instance.task.summary) to = [instance.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_application_applicant', to, ctx)
def send_new_task_invitation_email(instance): instance = clean_instance(instance, Participation) subject = "%s Task invitation from %s" % (EMAIL_SUBJECT_PREFIX, instance.created_by.first_name) to = [instance.user.email] ctx = { 'inviter': instance.created_by, 'invitee': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_invitation', to, ctx)
def send_new_task_application_email(instance): instance = clean_instance(instance, Application) subject = "%s New application from %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) to = [instance.task.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_application', to, ctx)
def send_new_task_invitation_email(instance): instance = clean_instance(instance, Participation) subject = "%s Task invitation from %s" % (EMAIL_SUBJECT_PREFIX, instance.created_by.first_name) to = [instance.user.email] ctx = { 'inviter': instance.created_by, 'invitee': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_invitation', to, ctx)
def send_new_task_application_response_email(instance): instance = clean_instance(instance, Application) subject = "%s Task application %s" % (EMAIL_SUBJECT_PREFIX, instance.accepted and 'accepted' or 'rejected') to = [instance.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'accepted': instance.accepted, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_task_application_response', to, ctx)
def send_task_application_not_selected_email(instance): instance = clean_instance(instance, Task) rejected_applicants = instance.application_set.filter(responded=False) if rejected_applicants: subject = "%s Your application was not accepted for: %s" % (EMAIL_SUBJECT_PREFIX, instance.summary) to = [rejected_applicants[0].user.email] bcc = [dev.user.email for dev in rejected_applicants[1:]] if len(rejected_applicants) > 1 else None ctx = { 'task': instance, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.id) } send_mail(subject, 'tunga/email/email_task_application_not_selected', to, ctx, bcc=bcc)
def send_new_task_application_applicant_email(instance): instance = clean_instance(instance, Application) subject = "%s You applied for a task: %s" % (EMAIL_SUBJECT_PREFIX, instance.task.summary) to = [instance.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_new_task_application_applicant', to, ctx)
def send_new_task_application_response_email(instance): instance = clean_instance(instance, Application) subject = "%s Task application %s" % ( EMAIL_SUBJECT_PREFIX, instance.accepted and 'accepted' or 'rejected') to = [instance.user.email] ctx = { 'owner': instance.task.user, 'applicant': instance.user, 'accepted': instance.accepted, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_task_application_response', to, ctx)
def send_new_task_invitation_response_email(instance): instance = clean_instance(instance, Participation) subject = "%s Task invitation %s by %s" % ( EMAIL_SUBJECT_PREFIX, instance.accepted and 'accepted' or 'rejected', instance.user.first_name) to = [instance.created_by.email] ctx = { 'inviter': instance.created_by, 'invitee': instance.user, 'accepted': instance.accepted, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_task_invitation_response', to, ctx)
def send_developer_accepted_email(instance): instance = clean_instance(instance, DeveloperApplication) subject = "%s Your application to become a Tunga developer has been accepted" % EMAIL_SUBJECT_PREFIX to = [instance.email] ctx = { 'application': instance, 'invite_url': '%s/signup/developer/%s/' % (TUNGA_URL, instance.confirmation_key) } if send_mail(subject, 'tunga/email/email_developer_application_accepted', to, ctx): instance.confirmation_sent_at = datetime.datetime.utcnow() instance.save()
def send_new_task_invitation_response_email(instance): instance = clean_instance(instance, Participation) subject = "%s Task invitation %s by %s" % ( EMAIL_SUBJECT_PREFIX, instance.accepted and 'accepted' or 'rejected', instance.user.first_name) to = [instance.created_by.email] ctx = { 'inviter': instance.created_by, 'invitee': instance.user, 'accepted': instance.accepted, 'task': instance.task, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.task.id) } send_mail(subject, 'tunga/email/email_task_invitation_response', to, ctx)
def send_progress_event_reminder_email(instance): instance = clean_instance(instance, ProgressEvent) subject = "%s Upcoming Task Update" % (EMAIL_SUBJECT_PREFIX,) participants = instance.task.participation_set.filter(accepted=True) if participants: to = [participants[0].user.email] bcc = [participant.user.email for participant in participants[1:]] if participants.count() > 1 else None ctx = { 'owner': instance.task.user, 'event': instance, 'update_url': '%s/task/%s/event/%s/' % (TUNGA_URL, instance.task.id, instance.id) } if send_mail(subject, 'tunga/email/email_progress_event_reminder', to, ctx, bcc=bcc): instance.last_reminder_at = datetime.datetime.utcnow() instance.save()
def send_new_message_email(instance): instance = clean_instance(instance, Message) to = [] recipients = instance.channel.participants.exclude(id=instance.user.id) if recipients: to = [recipient.email for recipient in recipients] if to and isinstance(to, (list, tuple)): subject = "%s New message from %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) ctx = { 'sender': instance.user.first_name, 'subject': instance.channel.subject, 'channel': instance.channel, 'message': instance, 'message_url': '%s/channel/%s/' % (TUNGA_URL, instance.channel.id) } send_mail(subject, 'tunga/email/email_new_message', to, ctx)
def send_new_message_email(instance): instance = clean_instance(instance, Message) to = [] recipients = instance.channel.participants.exclude(id=instance.user.id) if recipients: to = [recipient.email for recipient in recipients] if to and isinstance(to, (list, tuple)): subject = "%s New message from %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) ctx = { 'sender': instance.user.first_name, 'subject': instance.channel.subject, 'channel': instance.channel, 'message': instance, 'message_url': '%s/channel/%s/' % (TUNGA_URL, instance.channel.id) } send_mail(subject, 'tunga/email/email_new_message', to, ctx)
def send_task_application_not_selected_email(instance): instance = clean_instance(instance, Task) rejected_applicants = instance.application_set.filter(responded=False) if rejected_applicants: subject = "%s Your application was not accepted for: %s" % ( EMAIL_SUBJECT_PREFIX, instance.summary) to = [rejected_applicants[0].user.email] bcc = [dev.user.email for dev in rejected_applicants[1:] ] if len(rejected_applicants) > 1 else None ctx = { 'task': instance, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.id) } send_mail(subject, 'tunga/email/email_task_application_not_selected', to, ctx, bcc=bcc)
def update_task_periodic_updates(task): task = clean_instance(task, Task) if task.update_interval and task.update_interval_units: periodic_start_date = task.progressevent_set.filter( task=task, type=PROGRESS_EVENT_TYPE_PERIODIC ).aggregate(latest_date=Max('due_at'))['latest_date'] now = datetime.datetime.utcnow() if periodic_start_date and periodic_start_date > now: return if not periodic_start_date: periodic_start_date = task.participation_set.filter( task=task, accepted=True ).aggregate(start_date=Min('activated_at'))['start_date'] if periodic_start_date: period_map = { UPDATE_SCHEDULE_HOURLY: 'hours', UPDATE_SCHEDULE_DAILY: 'days', UPDATE_SCHEDULE_WEEKLY: 'weeks', UPDATE_SCHEDULE_MONTHLY: 'months', UPDATE_SCHEDULE_QUATERLY: {'months': 3}, UPDATE_SCHEDULE_ANNUALLY: 'years' } period_info = period_map.get(task.update_interval_units, None) if period_info: unit = isinstance(period_info, dict) and period_info.keys()[0] or period_info multiplier = isinstance(period_info, dict) and period_info.values()[0] or 1 delta = {unit: multiplier*task.update_interval_units} last_update_at = periodic_start_date while True: next_update_at = last_update_at + relativedelta(**delta) if not task.deadline or next_update_at < task.deadline: ProgressEvent.objects.update_or_create( task=task, type=PROGRESS_EVENT_TYPE_PERIODIC, due_at=next_update_at ) if next_update_at > now: break else: last_update_at = next_update_at
def send_new_task_email(instance): instance = clean_instance(instance, Task) if instance.visibility in [VISIBILITY_DEVELOPER, VISIBILITY_MY_TEAM]: queryset = get_user_model().objects.filter(type=USER_TYPE_DEVELOPER) if instance.visibility == VISIBILITY_MY_TEAM: queryset = queryset.filter(my_connections_q_filter(instance.user)) ordering = [] task_skills = instance.skills.all() if task_skills: when = [] for skill in task_skills: new_when = When(userprofile__skills=skill, then=1) when.append(new_when) queryset = queryset.annotate(matches=Sum( Case(*when, default=0, output_field=IntegerField()))) ordering.append('-matches') ordering.append('-tasks_completed') queryset = queryset.annotate(tasks_completed=Sum( Case(When(participation__task__closed=True, participation__user__id=F('id'), participation__accepted=True, then=1), default=0, output_field=IntegerField()))) queryset = queryset.order_by(*ordering) developers = None if queryset: developers = queryset[:15] subject = "%s New task created by %s" % (EMAIL_SUBJECT_PREFIX, instance.user.first_name) to = TUNGA_STAFF_UPDATE_EMAIL_RECIPIENTS bcc = [dev.email for dev in developers] if developers else None ctx = { 'owner': instance.user, 'task': instance, 'task_url': '%s/task/%s/' % (TUNGA_URL, instance.id) } send_mail(subject, 'tunga/email/email_new_task', to, ctx, bcc=bcc)
def send_progress_event_reminder_email(instance): instance = clean_instance(instance, ProgressEvent) subject = "%s Upcoming Task Update" % (EMAIL_SUBJECT_PREFIX, ) participants = instance.task.participation_set.filter(accepted=True) if participants: to = [participants[0].user.email] bcc = [participant.user.email for participant in participants[1:] ] if participants.count() > 1 else None ctx = { 'owner': instance.task.user, 'event': instance, 'update_url': '%s/task/%s/event/%s/' % (TUNGA_URL, instance.task.id, instance.id) } if send_mail(subject, 'tunga/email/email_progress_event_reminder', to, ctx, bcc=bcc): instance.last_reminder_at = datetime.datetime.utcnow() instance.save()
def clean_direct_channel(channel): channel = clean_instance(channel, Channel) # A direct channel can't have more than 2 participants if channel.type == CHANNEL_TYPE_DIRECT and channel.participants.count() > 2: channel.type = CHANNEL_TYPE_TOPIC channel.save()
def initialize_task_progress_events(task): task = clean_instance(task, Task) update_task_submit_milestone(task) update_task_periodic_updates(task)