class EventAttendeeListResource(WigoResource): model = EventAttendee @user_token_required @check_last_modified('group', 'last_event_change') @api.response(200, 'Success', model=EventAttendee.to_doc_list_model(api)) def get(self, event_id, headers): event = Event.find(event_id) if not g.user.can_see_event(event): abort(403, message='Can not see event') count, page, instances = self.select().event(event).secure(g.user).execute() return self.serialize_list(self.model, instances, count, page) @user_token_required @api.expect(EventAttendee.to_doc_list_model(api)) @api.response(200, 'Success', model=EventAttendee.to_doc_list_model(api)) def post(self, event_id): data = { 'user_id': g.user.id, 'event_id': event_id } attendee = self.create(data) return {'success': True} @user_token_required def delete(self, event_id): abort(501, message='Not implemented')
def test_private_event(): from server.models.event import Event, EventAttendee from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') make_friends(c, user1, user2) event_id = create_event(c, user1, 'test event 1', privacy='private') assert 1 == Event.select().count() assert 0 == Event.select().group(user1.group).count() assert 1 == Event.select().user(user1).count() assert 0 == Event.select().user(user2).count() event = Event.find(event_id) resp = api_post(c, user1, '/api/events/{}/invites'.format(event_id), {'invited_id': user2.id}) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 1 == Event.select().user(user2).count() count, page, results = EventAttendee.select().event(event).user( user2).execute() assert count == 1 assert results[0] == user1
def notify_on_friend_attending(event_id, user_id, friend_id): num_attending = get_num_attending(event_id, user_id) if num_attending < 5: return try: event = Event.find(event_id) user = User.find(user_id) except DoesNotExist: return if event.is_expired: return rl_key = 'notify:nofa:{}:{}'.format(event_id, user_id) with rate_limit(rl_key, event.expires) as limited: if not limited: friends = list(islice(EventAttendee.select().event(event).user(user).limit(6), 5)) if user in friends: friends.remove(user) num_attending -= 1 logger.info('notifying user {} of {} friends attending event {}'.format(user_id, num_attending, event_id)) if len(friends) >= 2: notification = Notification({ 'user_id': user.id, 'type': 'system', 'navigate': '/users/me/events/{}'.format(event_id), 'badge': 1, 'message': '{}, {}, and {} others are going to {}'.format( friends[0].full_name.encode('utf-8'), friends[1].full_name.encode('utf-8'), num_attending - 2, event.name.encode('utf-8')) }).save() send_notification_push.delay(notification.to_primitive())
def delete(self, event_id, user_id): EventAttendee({ 'user_id': g.user.id, 'event_id': event_id }).delete() return {'success': True}
def handle_already_exists_exception(self, e): event = e.instance if g.user.can_see_event(event) and not g.user.is_attending(event): EventAttendee({ 'user_id': g.user.id, 'event_id': e.instance.id }).save() return super(EventListResource, self).handle_already_exists_exception(e)
class UserEventAttendeeListResource(WigoResource): model = EventAttendee @user_token_required @check_last_modified('user', 'last_event_change') @api.response(200, 'Success', model=EventAttendee.to_doc_list_model(api)) def get(self, user_id, event_id, headers): event = Event.find(event_id) if not g.user.can_see_event(event): abort(403, message='Can not see event') count, page, instances = self.select().user(g.user).event(event).execute() return self.serialize_list(self.model, instances, count, page), 200, headers
def test_attending(): from server.models.event import Event, EventAttendee from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') create_event(c, user1, 'test event 1') event = Event.find(name='test event 1', group=user1.group) resp = api_post(c, user2, '/api/events/{}/attendees'.format(event.id), {}) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 2 == EventAttendee.select().event(event).count() assert 1 == EventAttendee.select().event(event).user(user1).count() assert 1 == EventAttendee.select().event(event).user(user2).count() make_friends(c, user1, user2) assert 2 == EventAttendee.select().event(event).user(user1).count()
def annotate_events(self, events): alimit = int(request.args.get('attendees_limit', 5)) mlimit = int(request.args.get('messages_limit', 5)) current_user = g.user user_context = current_user if '/users/' in request.path else None # fill in attending on each event query = EventAttendee.select().events(events).user(user_context).secure(g.user) count, page, attendees_by_event = query.limit(alimit).execute() if count: for event, attendees in zip(events, attendees_by_event): # make sure the event the current user is attending is in front if hasattr(event, 'current_user_attending'): count, attendees = attendees if attendees and attendees[0] != g.user: if g.user in attendees: attendees.remove(g.user) attendees.insert(0, g.user) attendees = (count, attendees) event.attendees = attendees event.num_attending = attendees[0] def capture_messages(events, query): count, page, messages_by_event = query.limit(mlimit).execute() if count: for event, messages in zip(events, messages_by_event): event.messages = messages base_query = EventMessage.select().user(user_context).secure(g.user) capture_messages(events, base_query.events(events)) # expired, current = partition(events, lambda e: e.is_expired) # base_query = EventMessage.select().user(user_context).secure(g.user) # capture_messages(current, base_query.events(current)) # capture_messages(expired, base_query.events(expired).by_votes()) filtered = [] for event in events: include_event = True if not hasattr(event, 'attendees') or len(event.attendees[1]) == 0: include_event = False if event.is_expired: if not hasattr(event, 'messages') or len(event.messages[1]) == 0: include_event = False if include_event: filtered.append(event) return filtered
def notify_on_eventmessage(message_id): try: message = EventMessage.find(message_id) except DoesNotExist: return user = message.user if message.event.is_expired: return type = 'video' if message.media_mime_type == 'video/mp4' else 'photo' for friend in EventAttendee.select().user(message.user).event(message.event): if friend == user: continue with rate_limit('notify:eventmessage:{}:{}:{}'.format(user.id, message.event.id, friend.id), timedelta(hours=2)) as limited: if limited: return message_text = '{name} posted a {type} in {event}'.format( name=user.full_name.encode('utf-8'), type=type, event=message.event.name.encode('utf-8')) notification = Notification({ 'user_id': friend.id, 'type': 'eventmessage.post', 'from_user_id': message.user_id, 'navigate': '/users/me/events/{}/messages/{}'.format(message.event_id, message.id), 'message': message_text }).save() send_notification_push.delay(notification.to_primitive())