def post(self): """Update mailinglist subscription statuses. Request: `POST` `/users/mailinglists` {'avalanche': False, 'lawinen': True, 'valanghe': False} """ user_id = self.request.authenticated_userid user = DBSession.query(User).get(user_id) subscribed_lists = DBSession.query(Mailinglist). \ filter(Mailinglist.user_id == user_id).all() subscribed_lists = {list.listname: list for list in subscribed_lists} subscribed_listnames = set(subscribed_lists.keys()) lists_to_add = [] removed_lists = False data = self.request.validated['mailinglists'] for listname in data: subscribe_status = data.get(listname, False) if subscribe_status and listname not in subscribed_listnames: # Add list lists_to_add.append(Mailinglist( listname=listname, email=user.email, user_id=user_id, user=user )) elif not subscribe_status and listname in subscribed_listnames: # Remove list removed_lists = True DBSession.delete(subscribed_lists[listname]) if lists_to_add: DBSession.add_all(lists_to_add) if lists_to_add or removed_lists: DBSession.flush() return {}
def post(self): """Update mailinglist subscription statuses. Request: `POST` `/users/mailinglists` {'avalanche': False, 'lawinen': True, 'valanghe': False} """ user_id = self.request.authenticated_userid user = DBSession.query(User).get(user_id) subscribed_lists = DBSession.query(Mailinglist). \ filter(Mailinglist.user_id == user_id).all() subscribed_lists = {l.listname: l for l in subscribed_lists} subscribed_listnames = set(subscribed_lists.keys()) lists_to_add = [] removed_lists = False data = self.request.validated['mailinglists'] for listname in data: subscribe_status = data.get(listname, False) if subscribe_status and listname not in subscribed_listnames: # Add list lists_to_add.append(Mailinglist( listname=listname, email=user.email, user_id=user_id, user=user )) elif not subscribe_status and listname in subscribed_listnames: # Remove list removed_lists = True DBSession.delete(subscribed_lists[listname]) if lists_to_add: DBSession.add_all(lists_to_add) if lists_to_add or removed_lists: DBSession.flush() return {}
def post(self): user = self.get_user() request = self.request validated = request.validated result = {} # Before all, check whether the user knows the current password current_password = validated['currentpassword'] if not user.validate_password(current_password): request.errors.add('body', 'currentpassword', 'Invalid password') return sync_sso = False # update password if a new password is provided if 'newpassword' in validated: user.password = validated['newpassword'] # start email validation procedure if a new email is provided email_link = None if 'email' in validated and validated['email'] != user.email: user.email_to_validate = validated['email'] user.update_validation_nonce( Purpose.change_email, VALIDATION_EXPIRE_DAYS) email_service = get_email_service(self.request) nonce = user.validation_nonce settings = request.registry.settings link = settings['mail.validate_change_email_url_template'].format( '#', nonce) email_link = link result['email'] = validated['email'] result['sent_email'] = True sync_sso = True update_search_index = False if 'name' in validated: user.name = validated['name'] result['name'] = user.name update_search_index = True sync_sso = True if 'forum_username' in validated: user.forum_username = validated['forum_username'] result['forum_username'] = user.forum_username update_search_index = True sync_sso = True if 'is_profile_public' in validated: user.is_profile_public = validated['is_profile_public'] # Synchronize everything except the new email (still stored # in the email_to_validate attribute while validation is pending). if sync_sso: try: client = get_discourse_client(request.registry.settings) client.sync_sso(user) except: log.error('Error syncing with discourse', exc_info=True) raise HTTPInternalServerError('Error with Discourse') try: DBSession.flush() except: log.warning('Error persisting user', exc_info=True) raise HTTPInternalServerError('Error persisting user') if email_link: email_service.send_change_email_confirmation(user, link) if update_search_index: # when user name changes, the search index has to be updated notify_es_syncer(self.request.registry.queue_config) # also update the cache version of the user profile update_cache_version(user.profile) return result
def post(self): user = self.get_user() request = self.request validated = request.validated result = {} # Before all, check whether the user knows the current password current_password = validated['currentpassword'] if not user.validate_password(current_password): request.errors.add('body', 'currentpassword', 'Invalid password') return sync_sso = False # update password if a new password is provided if 'newpassword' in validated: user.password = validated['newpassword'] # start email validation procedure if a new email is provided email_link = None if 'email' in validated and validated['email'] != user.email: user.email_to_validate = validated['email'] user.update_validation_nonce(Purpose.change_email, VALIDATION_EXPIRE_DAYS) email_service = get_email_service(self.request) nonce = user.validation_nonce settings = request.registry.settings link = settings['mail.validate_change_email_url_template'].format( '#', nonce) email_link = link result['email'] = validated['email'] result['sent_email'] = True sync_sso = True update_search_index = False if 'name' in validated: user.name = validated['name'] result['name'] = user.name update_search_index = True sync_sso = True if 'forum_username' in validated: user.forum_username = validated['forum_username'] result['forum_username'] = user.forum_username update_search_index = True sync_sso = True if 'is_profile_public' in validated: user.is_profile_public = validated['is_profile_public'] # Synchronize everything except the new email (still stored # in the email_to_validate attribute while validation is pending). if sync_sso: try: client = get_discourse_client(request.registry.settings) client.sync_sso(user) except Exception: log.error('Error syncing with discourse', exc_info=True) raise HTTPInternalServerError('Error with Discourse') try: DBSession.flush() except Exception: log.warning('Error persisting user', exc_info=True) raise HTTPInternalServerError('Error persisting user') if email_link: email_service.send_change_email_confirmation(user, link) if update_search_index: # when user name changes, the search index has to be updated notify_es_syncer(self.request.registry.queue_config) # also update the cache version of the user profile update_cache_version(user.profile) return result