def index(self): super(EventAttendee, self).index() user = self.user event = self.event # check if the user is switching events for the date the event is on current_event_id = user.get_attending_id(event) if current_event_id and current_event_id != event.id: EventAttendee({'event_id': current_event_id, 'user_id': user.id}).delete() with self.db.transaction(commit_on_select=False): # first update the global state of the event attendees_key = skey(event, 'attendees') self.db.sorted_set_add(attendees_key, user.id, epoch(self.created)) self.db.expire(attendees_key, event.ttl()) # now update the users view of the events # record the exact event the user is currently attending user.set_attending(event) # record current user as an attendee attendees_key = user_attendees_key(user, event) self.db.sorted_set_add(attendees_key, user.id, 'inf') self.db.expire(attendees_key, event.ttl()) # record the event into the events the user can see with self.db.transaction(commit_on_select=False): event.update_global_events() event.update_user_events(user)
def __get_events_keys(self): if self._model_class == EventMessage: query_class = EventMessage if self._user: return [ user_eventmessages_key(self._user, event, self._by_votes) for event in self._events ] else: return [ skey(event, 'messages', 'by_votes') if self._by_votes else skey(event, 'messages') for event in self._events ] elif self._model_class == EventAttendee: query_class = User if self._user: return [ user_attendees_key(self._user, event) for event in self._events ] else: return [skey(event, 'attendees') for event in self._events] else: raise ValueError('Invalid query')
def add_to_user_attending(self, user, attendee): # add to the users view of who is attending with self.db.transaction(commit_on_select=False): attendees_key = user_attendees_key(user, self) self.db.sorted_set_add(attendees_key, attendee.id, time()) self.db.expire(attendees_key, self.ttl()) # add to the users current events list self.update_user_events(user)
def delete_history(u, f): with wigo_db.transaction(commit_on_select=False): for message in EventMessage.select().key(skey(u, 'event_messages')): if message.user and message.event: message.remove_for_user(f) for event in Event.select().user(u): if wigo_db.sorted_set_is_member(user_attendees_key(f, event), u.id): event.remove_from_user_attending(f, u)
def remove_index(self): super(EventAttendee, self).remove_index() user = self.user event = self.event # the event may have been deleted if event: with self.db.transaction(commit_on_select=True): user.remove_attending(event) self.db.sorted_set_remove(skey(event, 'attendees'), user.id) self.db.sorted_set_remove(user_attendees_key(user, event), user.id) with self.db.transaction(commit_on_select=True): event.update_global_events() event.update_user_events(user) else: user.remove_attending(event)
def __get_by_event(self): if self._model_class == EventMessage: if self._user: key = user_eventmessages_key(self._user, self._event, self._by_votes) else: key = skey(self._event, 'messages', 'by_votes') if self._by_votes else skey( self._event, 'messages') return self.__get_page(key) elif self._model_class == EventAttendee: if self._user: key = user_attendees_key(self._user, self._event) else: key = skey(self._event, 'attendees') return self.model_class(User).key(key).execute() else: raise ValueError('Invalid query')
def update_user_events(self, user): events_key = skey(user, 'events') with self.db.transaction(commit_on_select=False): current_attending = user.get_attending_id(self) if current_attending and current_attending == self.id: self.db.sorted_set_add(events_key, self.id, get_score_key(self.expires, 0, 1000)) self.db.clean_old(events_key, self.TTL) else: num_attending = self.db.get_sorted_set_size(user_attendees_key(user, self)) num_messages = get_cached_num_messages(self.id, user.id) if self.is_expired else 10 if self.is_new is False and (num_attending == 0 or num_messages == 0): self.db.sorted_set_remove(events_key, self.id) else: distance = Location.getLatLonDistance((self.group.latitude, self.group.longitude), (user.group.latitude, user.group.longitude)) self.db.sorted_set_add(events_key, self.id, get_score_key(self.expires, distance, num_attending)) self.db.clean_old(events_key, self.TTL) user.track_meta('last_event_change')
def remove_from_user_attending(self, user, attendee): # remove from the users view of who is attending self.db.sorted_set_remove(user_attendees_key(user, self), attendee.id) # update the users event list for this event, removing if now empty self.update_user_events(user)
def is_attending(self, event): return self.db.sorted_set_is_member(user_attendees_key(self, event), self.id)