def post(self, event_id): #grab the event from the db event = Event.get(event_id) #make sure the event exists if event is None: raise HTTPError(404) #make sure the client is the event's owner if not event[u'creator'] == self.get_session()[u'username']: raise HTTPError(403) #make sure the request has the correct info in the body body = self.body_dict() if not body or (not u'users' in body and not u'contacts' in body): raise HTTPError(400) if u'users' in body or u'contacts' in body: users = body.get(u'users') #friends is a special case if users == u'friends': users = self.get_user().friends() out_of_numbers = event.invite(usernames=users, contacts=body.get(u'contacts')) if out_of_numbers is not None: self.write(json.dumps({'error': 'OUT_OF_NUMBERS', 'contacts': out_of_numbers, 'event_revision': event[u'revision']})) self.set_status(409)
def test_get_none(self): """ Tests boundary condition when a Event does not exists in the DB :return: """ _e = Event.get('dne', db) self.assertEqual(_e, None)
def get(self, event_id): event = Event.get({u'id': event_id}) if not event: raise HTTPError(404) if not event.user_can_access(self.get_user()): raise HTTPError(401) self.output({u'attendants': Attendant.find({u'event': event_id}).serializable(name=True)})
def test_get(self): """ Creates, saves a test Event data. Then fetches it from DB and asserts that both should be key-value pairwise exactly the same :return: """ e = self.create_event() key = e.save(db) _e = Event.get(key, db) self.assertEqual(e.to_dict(), _e.to_dict())
def send(self, user, client_id, message): from api.events.models import Event if message[u'type'] not in (u'invite', u'comment'): return False smsee = SMSRegister.get({u'event': message[u'event_id'], u'contact_number': client_id}) if smsee is None: return False event = Event.get(message[u'event_id']) if event is None: return False account = twilio.Account(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) texts = list() if message[u'type'] == u'invite': tz = smsee[u'tz'] or 'America/Toronto' t2 = ('%s shared a plan with you on ' 'Connectsy. ' % event[u'creator']) if event[u'where'] is not None: t2 += 'Where: %s' % event[u'where'] if event[u'when'] is not None and event[u'when'] != 0: t2 += ", " else: t2 += ". " if event[u'when'] is not None and event[u'when'] != 0: t2 += 'When: %s. ' % format_date(event[u'when'], tz=tz) t2 += 'Reply to send a comment, include #in to join.' texts.append(t2) texts.append('What\'s the plan: %(what)s' % {'username': event[u'creator'], 'what': event[u'what']}) elif message[u'type'] == u'comment': texts.append('%(commenter)s commented: %(comment)s' % message) if len(texts[-1]) > 160: texts[-1] = texts[-1][:157]+"..." try: for text in texts: account.request(SMS_OUTPUT_URL, 'POST', { 'To': smsee[u'contact_number'], 'Body': text, 'From': smsee[u'twilio_number'], }) return True except HTTPError, e: print e.read() return False
def delete(self, revision): ''' Deletes an event, if the current user it that event's owner. TODO - remove associated attendance ''' event = Event.get({u'revision': revision}) if event is None: raise HTTPError(404) if event[u'creator'] != self.get_user()[u'username']: raise HTTPError(403) db.objects.event.remove(event[u'id'], safe=True) self.finish()
def test_save(self): """ Saves the test image in the database and checks if the process is successful by comparing the dictionaries :return: """ e = self.create_event() incident_id = e.save(db) i = create_image(uuid='image uuid', name='image.png') i.save(incident_id, db) e.images.append(i.to_dict()) _e = Event.get(incident_id, db) self.assertEqual(e.to_dict(), _e.to_dict())
def get(self, revision): ''' TODO - enforce user restrictions ''' #grab the event from the database event = Event.get({u'revision': revision}) if event is None: raise HTTPError(404) if not event.user_can_access(self.get_user()): raise HTTPError(401) response = {'event': event.serializable()} #give the user the attendance info if they asked for it if self.request.arguments.get('attendants'): response['attendants'] = Attendant.find({u'event': event[u'id']}).serializable(name=True) self.output(response)
def post(self, event_id): ''' Updates an invitee's attendance ''' body = self.body_dict() username = self.get_session()[u'username'] user = User.get({u'username': username}) #make sure the event exists event = Event.get(event_id) if not event: raise HTTPError(404) #try to grab the user's existing attendance status att = Attendant.get({u'event': event[u'id'], u'user': user[u'id']}) #if the user isn't invited, we need to error out if broadcast is off if att is None: if event[u'broadcast']: att = Attendant(user=user[u'id'], event=event[u'id']) else: raise HTTPError(403) # create a list of acceptable statuses valid_statuses = [v for k, v in status.__dict__.iteritems() if k[0] != '_'] #make sure status is present and a correct value if body.get(u'status') not in valid_statuses: raise HTTPError(400) notify = (att[u'status'] == status.INVITED and body[u'status'] == status.ATTENDING) att[u'status'] = body[u'status'] att.save() if notify: #Send out the attendant notifications for uname in Attendant.to_notify(event, skip=[username]): notifications.send(uname, {u'type': 'attendant', u'event_revision': event[u'revision'], u'event_id': event[u'id'], u'attendant': username})
def post(self, event_id): #make sure the event exists event = Event.get(event_id) if not event: raise HTTPError(404) #grab the data body = self.body_dict() #comment body is required, and must have content if not u'comment' in body or len(body[u'comment']) == 0: raise HTTPError(400) #nonce is optional if u'nonce' in body: #if another comment exists with this nonce, it's a double-post if Comment.get({u'nonce': body[u'nonce'], u'event': event[u'id'], u'user': self.get_session()[u'username']}): raise HTTPError(409) commenter = self.get_session()[u'username'] #create the comment Comment(**{ u'comment': body[u'comment'], u'event': event[u'id'], u'username': commenter, }).save() #Send out the comment notifications usernames = Attendant.to_notify(event, skip=[commenter]) for username in usernames: notifications.send(username, {u'type': 'comment', u'event_revision': event[u'revision'], u'event_id': event[u'id'], u'comment': body[u'comment'], u'commenter': commenter})
def get(self, event_id): ''' Gets all comments for a given event. ''' #make sure the event exists if not Event.get(event_id): raise HTTPError(404) #show them comments! comments = Comment.find({u'event': event_id}) comments.sort(u'timestamp', pymongo.ASCENDING) ret_coms = [] for c in comments: user = User.get(c[u'user']) ret = c.__data__ ret[u'username'] = user.__data__.get(u'username', None) if ret[u'username'] is None: del ret[u'username'] ret[u'display_name'] = user.__data__.get(u'display_name', None) if ret[u'display_name'] is None: del ret[u'display_name'] ret_coms.append(ret) self.output({u'comments': ret_coms})