def prepare_group_summary_data(group, from_date, to_date): new_users = group.members.filter( groupmembership__created_at__gte=from_date, groupmembership__created_at__lt=to_date, ).all() pickup_filters = { 'deleted': False, 'store__group': group, 'date__gte': from_date, 'date__lt': to_date, } pickups_done_count = PickupDate.objects.annotate( num_collectors=Count('collectors')).filter( **pickup_filters, num_collectors__gt=0, ).count() pickups_missed_count = PickupDate.objects.annotate( num_collectors=Count('collectors')).filter( **pickup_filters, num_collectors=0, ).count() feedbacks = Feedback.objects.filter( created_at__gte=from_date, created_at__lt=to_date, about__store__group=group, ) messages = ConversationMessage.objects.filter( conversation__target_type=ContentType.objects.get_for_model(Group), conversation__target_id=group.id, created_at__gte=from_date, created_at__lt=to_date, ) data = { # minus one second so it's displayed as the full day 'to_date': to_date - relativedelta(seconds=1), 'from_date': from_date, 'group': group, 'new_users': new_users, 'pickups_done_count': pickups_done_count, 'pickups_missed_count': pickups_missed_count, 'feedbacks': feedbacks, 'messages': messages, 'settings_url': group_settings_url(group), } data['has_activity'] = any(data[field] > 0 for field in ['pickups_done_count', 'pickups_missed_count']) or \ any(len(data[field]) > 0 for field in ['feedbacks', 'messages', 'new_users']) return data
def prepare_pickup_notification_email( user, group, tonight_date, tomorrow_date, tonight_user=None, tonight_empty=None, tonight_not_full=None, tomorrow_user=None, tomorrow_empty=None, tomorrow_not_full=None, ): has_pickups_tonight = any([ items is not None and len(items) > 0 for items in [ tonight_user, tonight_empty, tonight_not_full, ] ]) has_pickups_tomorrow = any([ items is not None and len(items) > 0 for items in [ tomorrow_user, tomorrow_empty, tomorrow_not_full, ] ]) return prepare_email( template='pickup_notification', user=user, context={ 'settings_url': group_settings_url(group), 'group': group, 'tonight_date': tonight_date, 'tomorrow_date': tomorrow_date, 'has_pickups_tonight': has_pickups_tonight, 'has_pickups_tomorrow': has_pickups_tomorrow, 'tonight_user': tonight_user, 'tonight_empty': tonight_empty, 'tonight_not_full': tonight_not_full, 'tomorrow_user': tomorrow_user, 'tomorrow_empty': tomorrow_empty, 'tomorrow_not_full': tomorrow_not_full, }, )
def prepare_new_application_notification_email(user, application): applicant = application.user conversation = Conversation.objects.get_for_target(application) reply_to_name = applicant.display_name local_part = make_local_part(conversation, user) reply_to = formataddr( (reply_to_name, '{}@{}'.format(local_part, settings.SPARKPOST_RELAY_DOMAIN))) from_email = formataddr( (applicant.display_name, settings.DEFAULT_FROM_EMAIL)) return prepare_email(template='new_application', from_email=from_email, user=user, reply_to=[reply_to], context={ 'applicant': applicant, 'group': application.group, 'questions': application.questions_rendered(), 'answers': application.answers_rendered(), 'conversation_url': group_application_url(application), 'mute_url': group_application_mute_url( application, conversation), 'settings_url': group_settings_url(application.group), 'group_applications_url': group_applications_url(application.group), 'group_edit_url': group_edit_url(application.group), })