Skip to content

Commit

Permalink
Merge pull request #166 from CelineBoudier/add-cron1
Browse files Browse the repository at this point in the history
adding new users notification
  • Loading branch information
mikebryant committed Mar 21, 2016
2 parents 913799a + ee4e91e commit 687f983
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
49 changes: 49 additions & 0 deletions portal/tests/test_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# Code for Life
#
# Copyright (C) 2015, Ocado Innovation Limited
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ADDITIONAL TERMS – Section 7 GNU General Public Licence
#
# This licence does not grant any right, title or interest in any “Ocado” logos,
# trade names or the trademark “Ocado” or any other trademarks or domain names
# owned by Ocado Innovation Limited or the Ocado group of companies or any other
# distinctive brand features of “Ocado” as may be secured from time to time. You
# must not distribute any modification of this program using the trademark
# “Ocado” or claim any affiliation or association with Ocado or its employees.
#
# You are not authorised to use the name Ocado (or any of its trade names) or
# the names of any author or contributor in advertising or for publicity purposes
# pertaining to the distribution of this program, without the prior written
# authorisation of Ocado.
#
# Any propagation, distribution or conveyance of this program must include this
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from django.test import Client
from unittest import TestCase
from django.core.urlresolvers import reverse
from django.core import mail


class EmailTest(TestCase):
def test_send_new_users_numbers_email(self):
client = Client()
response = client.get(reverse('send_new_users_report'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(mail.outbox), 1)
mail.outbox = []
3 changes: 3 additions & 0 deletions portal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
teacher_print_reminder_cards, teacher_accept_student_request, teacher_reject_student_request, \
teacher_class_password_reset
from portal.views.teacher.home import teacher_home
from portal.views.email import send_new_users_report

js_info_dict = {
'packages': ('conf.locale',),
Expand Down Expand Up @@ -97,6 +98,8 @@
url(r'^admin/map/$', schools_map, name='map'),
url(r'^admin/data/$', aggregated_data, name='aggregated_data'),

url(r'^mail/weekly', send_new_users_report, name='send_new_users_report'),

url(r'^locked_out/$', TemplateView.as_view(template_name='portal/locked_out.html'),
name='locked_out'),
url(r'^logout/$', logout_view, name='portal/logout'),
Expand Down
14 changes: 12 additions & 2 deletions portal/views/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@

from django.utils import timezone
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages as messages

from portal.models import EmailVerification
from portal.models import EmailVerification, UserProfile
from portal.helpers.email import send_email, NOTIFICATION_EMAIL
from portal.app_settings import CONTACT_FORM_EMAILS


def verify_email(request, token):
verifications = EmailVerification.objects.filter(token=token)
Expand Down Expand Up @@ -75,3 +78,10 @@ def verify_email(request, token):

# default to homepage if something goes wrong
return HttpResponseRedirect(reverse_lazy('home'))


def send_new_users_report(request):
new_profiles_count = UserProfile.objects.filter(user__date_joined__gte=timezone.now() - timedelta(days=7)).count()
send_email(NOTIFICATION_EMAIL, CONTACT_FORM_EMAILS, "new users",
"There are %d new users this week!" % new_profiles_count)
return HttpResponse('success')

0 comments on commit 687f983

Please sign in to comment.