def test_delete_gcm_course_alert(self): created_timestamp = 1396710772 expiry_timestamp = 1496710772 alert = m.GcmCourseAlert( registration_id='neverheardsilencequitethisloud', course_id='sci238', created_date=datetime.datetime.utcfromtimestamp(created_timestamp), expiry_date=datetime.datetime.utcfromtimestamp(expiry_timestamp), ) alert.save() self.assertEqual(m.GcmCourseAlert.objects.count(), 1) headers = self.get_csrf_token_header() resp = self.app.delete( '/api/v1/alerts/course/gcm/%s' % alert.id, headers=headers) self.assertResponseOk(resp) self.assertJsonResponse(resp, { 'gcm_course_alert': { 'registration_id': 'neverheardsilencequitethisloud', 'user_id': None, 'term_id': '', 'section_type': '', 'expiry_date': expiry_timestamp * 1000, 'created_date': created_timestamp * 1000, 'course_id': 'sci238', 'section_num': '', 'id': str(alert.id), } }) self.assertEqual(m.GcmCourseAlert.objects.count(), 0)
def add_gcm_course_alert(): """Adds an alert to notify when a seat opens up in a course/section via GCM. GCM is used to send push notifications to our Android app. Requires the following parameters: registration_id: Provided by GCM to identify the device-app pair course_id: ID of the course to alert on Optional parameters: created_date: Timestamp in millis expiry_date: Timestamp in millis. Defaults to 1 year later term_id: e.g. "2014_01" section_type: e.g. "LEC" section_num: e.g. "001" user_id: ID of the logged in user """ params = flask.request.form created_date = datetime.datetime.now() expiry_date_param = params.get('expiry_date') if expiry_date_param: expiry_date = datetime.datetime.fromtimestamp(int(expiry_date_param)) else: expiry_date = created_date + datetime.timedelta(days=365) try: alert_dict = { 'registration_id': params['registration_id'], 'course_id': params['course_id'], 'created_date': created_date, 'expiry_date': expiry_date, 'term_id': params.get('term_id'), 'section_type': params.get('section_type'), 'section_num': params.get('section_num'), 'user_id': params.get('user_id'), } except KeyError as e: raise api_util.ApiBadRequestError('Missing required parameter: %s' % e.message) alert = m.GcmCourseAlert(**alert_dict) try: alert.save() except me.NotUniqueError as e: raise api_util.ApiBadRequestError( 'Alert with the given parameters already exists.') return api_util.jsonify({ 'gcm_course_alert': alert.to_dict(), })