def post(self): """ Get fields from POST dict """ if not self.form.validate(): return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() last_name = self.form.last_name.data.strip() country = self.form.country.data tz = self.form.tz.data try: user_info = self.user_model.get_by_id(long(self.user_id)) try: message = '' # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username', 'email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if success: # free old uniques Unique.delete_multi( ['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username]) # The unique values were created, so we can save the user. user_info.username = username user_info.auth_ids[0] = 'own:%s' % username message += _('Your new username is <strong>{}</strong>').format(username) else: message += _( 'The username <strong>{}</strong> is already taken. Please choose another.').format( username) # At least one of the values is not unique. self.add_message(message, 'error') return self.get() user_info.name = name user_info.last_name = last_name user_info.country = country user_info.tz = tz user_info.put() message += " " + _('Thanks, your settings have been saved.') self.add_message(message, 'success') return self.get() except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ' + e) message = _('Unable to update profile. Please try again later.') self.add_message(message, 'error') return self.get() except (AttributeError, TypeError), e: login_error_message = _('Your session has expired.') self.add_message(login_error_message, 'error') self.redirect_to('login')
def set_email_to_pending(self): """ Changes the email address for the current user""" new_email = self.pending_change_email.lower() success = False if new_email != '': success, existing = \ Unique.create_multi(['Member.auth_id:%s'%new_email, 'Member.email_address:%s'%new_email]) if not success: logging.error('Unable to create user for email %s because of \ duplicate keys' % new_email) else: # delete the old unique values Unique.delete_multi(['Member.auth_id:%s'%self.email_address, 'Member.email_address:%s'%self.email_address]) self.email_address=new_email self.auth_ids=[new_email] self.pending_change_email = '' self.put() if success: return new_email else: return None
def forget_member_from_key(the_member_key): """ deletes a member, including all gig plans """ # first find all of the assocs to bands the_assocs = assoc.get_assocs_of_member_key(the_member_key=the_member_key, confirmed_only=False) # delete all plans & abdicate as contact for gigs for an_assoc in the_assocs: plan.delete_plans_for_member_key_for_band_key(the_member_key, an_assoc.band) gig.reset_gigs_for_contact_key(the_member_key, an_assoc.band) # now quit the bands the_assoc_keys = [a.key for a in the_assocs] ndb.delete_multi(the_assoc_keys) # delete the old unique values the_member = the_member_key.get() if the_member: Unique.delete_multi([ 'Member.auth_id:%s' % the_member.email_address, 'Member.email_address:%s' % the_member.email_address ]) # bye! the_member_key.delete()
def post(self): user_id = self.request.get('user_id') user = User.get_by_auth_id('%s|%s' % (self.module, user_id)) if user: Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids)) time.sleep(.25) user.key.delete() AppEventData(event=user_id, event_type='user', transaction='DEL', user=self.user.email).put() data_cache.set('events', None) data_cache.set('%s-users-tuple' % self.module, None) time.sleep(.25) return self.render_response('manage_users.html', success=True, message='User %s succesfully deleted' % user_id, form=forms.AddUserForm(), users=self.get_users()) self.redirect('/admin/manage_users')
def deleteRelationship(self): success, existing = Unique.create_multi([ 'AreaUser.uniqueAreaUserString.%s:%s' % (self.userKey, self.privateArea) ]) Unique.delete_multi(existing) self.key.delete()
def do_delete(self): if not self.is_logged_in: self.send_json_error('Not logged in') try: userid = self.session_user['user_id'] user = self.auth.store.user_model.get_by_id(userid) # delete all piskels for the current user piskels = models.get_piskels_for_user(userid) for piskel in piskels: piskel.delete() # force consistency db.get(piskel.key()) # logout current user self.auth.unset_session() # from webapp2_extras.appengine.auth.models.User # http://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#User # # def add_auth_id(self, auth_id): # ... # unique = '%s.auth_id:%s' % (self.__class__.__name__, auth_id) # ... Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids)) # delete user entry user.key.delete() return self.send_json_response({'status': 'ok'}) except Exception as error: return self.send_json_error(repr(error))
def post(self): email = self.request.get('user_id') user_id = ('%s|%s' % (self.module, email)) user = User.get_by_auth_id(user_id) if user: Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids) ) user.key.delete() time.sleep(.25) self.redirect('/super_admin/manage_users')
def post(self): """ Get fields from POST dict """ if not self.form.validate(): return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() last_name = self.form.last_name.data.strip() country = self.form.country.data try: user_info = models.User.get_by_id(long(self.user_id)) try: message='' # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username','email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if success: # free old uniques Unique.delete_multi(['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username]) # The unique values were created, so we can save the user. user_info.username=username user_info.auth_ids[0]='own:%s' % username message+= _('Your new username is ') + username + '.' else: message+= _('Username') + ": " + username + " " + _('is already taken. It is not changed.') # At least one of the values is not unique. # Make a list of the property names that failed. props = [name.split(':', 2)[-1] for name in uniques] raise ValueError(_('Properties %r are not unique.' % props)) user_info.name=name user_info.last_name=last_name user_info.country=country user_info.put() message+= " " + _('Your profile has been updated!') self.add_message(message,'success') return self.get() except (AttributeError, KeyError, ValueError), e: message = _('Unable to update profile!') logging.error('Unable to update profile: ' + e) self.add_message(message,'error') return self.get() except (AttributeError, TypeError), e: login_error_message = _('Sorry you are not logged in!') self.add_message(login_error_message,'error') self.redirect_to('login')
def update_username(self, username): if self.username is not None: uniqueness_key = 'User.username:'******'username', username) uniqueness_key = 'User.username:' + username.lower() unique = Unique.create(uniqueness_key) if not unique: raise DuplicateField( "There is already a user with username {}.".format(username)) return unique
def get(self): ageLimit = datetime.datetime.now() - datetime.timedelta(days=1) targetUsersQuery = UserModel.query(UserModel.updated < ageLimit) targetUsers = targetUsersQuery.iter() for targetUser in targetUsers: if not targetUser.verified: Unique.delete_multi( ["UserModel.auth_id:" + targetUser.auth_ids[0], "UserModel.email:" + targetUser.email]) targetUser.deleteAllMessages() targetUser.getKey()[0].key.delete() targetUser.key.delete() self.response.write(RESPONSE_OK)
def update_all_uniques(): the_members = get_all_members(order=False) logging.info('starting unique cleanup') m_list = [] t_list = [] for m in the_members: ea = m.email_address.lower() if ea != m.email_address: # found an upper-case email # first, make new auth_id and email_addresses Uniques newauth = Unique.create('Member.auth_id:%s' % ea) if newauth is False: logging.error( 'Unable to create unique auth_id for email {0}'.format(ea)) newemail = Unique.create('Member.email_address:%s' % ea) if newemail is False: logging.error( 'Unable to create unique email_address for email {0}'. format(ea)) if newauth and newemail: # delete the old unique values logging.info('deleting old tokens for {0}'.format( m.email_address)) Unique.delete_multi([ 'Member.auth_id:%s' % m.email_address, 'Member.email_address:%s' % m.email_address ]) else: logging.error('did not delete old tokens') m.email_address = ea m.auth_ids = [ea] m_list.append(m) else: # email address is fine, just make sure we have tokens for this guy t_list.append('Member.auth_id:%s' % ea) t_list.append('Member.email_address:%s' % ea) if m_list: ndb.put_multi(m_list) if t_list: Unique.create_multi(t_list) logging.info('unique cleanup done')
def rename(self, newUsername, preserveKeys=False): if Unique.create("UserModel.auth_id:" + newUsername): oldUsername = self.auth_ids[0] Unique.delete_multi(["UserModel.auth_id:" + oldUsername]) oldKey = self.getKey() newKey = UserKey(parent=UserKey.newKey(newUsername)) if preserveKeys: newKey.pubkey = oldKey.pubkey else: self.loginToken = "" self.auth_ids[0] = newUsername oldKey.key.delete() self.put() newKey.put() else: return RESPONSE_USER_NAME_EXISTS
def post(self, user_id): """ Get fields from POST dict """ user_data = self._data() user_info = models.User.get_by_id(long(user_id)) logging.info(user_data) if not user_info: raise Exception("User not found with id: " + user_id) username = user_data['username'] try: message = '' # update username if it has changed and it isn't already taken user_info.unique_properties = ['username', 'email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if not existing and not success: raise Exception("Error creating user") # free old uniques Unique.delete_multi( ['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username]) # The unique values were created, so we can save the user. user_info.username = username user_info.auth_ids[0] = 'own:%s' % username user_info.name = user_data.get('name', None) user_info.last_name = user_data.get('last_name', None) if user_data.get('password', None) is not None: # Password to SHA512 password = utils.hashing(user_data['password'], self.app.config.get('salt')) user_info.password = security.generate_password_hash(password, length=12) user_info.put() return True except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ') logging.exception(e) return False
def update_all_uniques(): the_members = get_all_members(order=False) logging.info('starting unique cleanup') m_list=[] t_list=[] for m in the_members: ea = m.email_address.lower() if ea != m.email_address: # found an upper-case email # first, make new auth_id and email_addresses Uniques newauth = Unique.create('Member.auth_id:%s'%ea) if newauth is False: logging.error('Unable to create unique auth_id for email {0}'.format(ea)) newemail = Unique.create('Member.email_address:%s'%ea) if newemail is False: logging.error('Unable to create unique email_address for email {0}'.format(ea)) if newauth and newemail: # delete the old unique values logging.info('deleting old tokens for {0}'.format(m.email_address)) Unique.delete_multi(['Member.auth_id:%s'%m.email_address, 'Member.email_address:%s'%m.email_address]) else: logging.error('did not delete old tokens') m.email_address=ea m.auth_ids=[ea] m_list.append(m) else: # email address is fine, just make sure we have tokens for this guy t_list.append('Member.auth_id:%s'%ea) t_list.append('Member.email_address:%s'%ea) if m_list: ndb.put_multi(m_list) if t_list: Unique.create_multi(t_list) logging.info('unique cleanup done')
def update_email(self, email): # Remove old email from unique keys so it can be re-used! old_email = self.email uniqueness_key = 'User.email:' + old_email Unique.delete_multi([uniqueness_key]) # Update to new email and check uniqueness setattr(self, 'email', email) uniqueness_key = 'User.email:' + email unique = Unique.create(uniqueness_key) if not unique: raise DuplicateField( "There is already a user with email {}.".format(email)) # Also need to update the user in our mailchimp mailchimp.unsubscribe(old_email) if self.receives_updates: subscribed = mailchimp.subscribe(email) # if subscription failed, they might need to resubscribe! if not subscribed: subscribed = mailchimp.resubscribe(email) return unique
def get(self): ageLimit = datetime.datetime.now() - datetime.timedelta(days=1) targetUsersQuery = UserModel.query(UserModel.updated < ageLimit) targetUsers = targetUsersQuery.iter() for targetUser in targetUsers: if not targetUser.verified: try: logging.info("Deleted user: "******"UserModel.auth_id:" + targetUser.auth_ids[0], "UserModel.email:" + targetUser.email]) try: targetUser.getKey().key.delete() except IndexError: logging.info("Missing key: " + str(targetUser.auth_ids)) targetUser.key.delete() except Exception as error: logging.error("Error " + str(type(error)) + " " + str(error) + " " + str(error.message)) self.response.write(RESPONSE_OK)
def forget_member_from_key(the_member_key): """ deletes a member, including all gig plans """ # first find all of the assocs to bands the_assocs = assoc.get_assocs_of_member_key(the_member_key=the_member_key, confirmed_only=False) # delete all plans & abdicate as contact for gigs for an_assoc in the_assocs: plan.delete_plans_for_member_key_for_band_key(the_member_key, an_assoc.band) gig.reset_gigs_for_contact_key(the_member_key, an_assoc.band) # now quit the bands the_assoc_keys=[a.key for a in the_assocs] ndb.delete_multi(the_assoc_keys) # delete the old unique values the_member=the_member_key.get() if the_member: Unique.delete_multi(['Member.auth_id:%s'%the_member.email_address, 'Member.email_address:%s'%the_member.email_address]) # bye! the_member_key.delete()
def post(self): emails_to_delete = self.request.get_all('email') emails_deleted = [] applicants_to_delete = [] applications_to_delete = [] uniques_to_delete = [] for email in emails_to_delete: applicant, application = query.get_application_by_email(email) if applicant: applicants_to_delete.append(applicant.key) emails_deleted.append(email) if application: applications_to_delete.append(application.key) # Delete auth ids which are just emails uniques_to_delete.append('User.auth_id:' + email) # Delete unique emails uniques_to_delete.append('User.email:' + email) ndb.delete_multi(applicants_to_delete + applications_to_delete) Unique.delete_multi(uniques_to_delete) self.response.write("Deleted:\n") self.response.write("\t- %d applicants\n" % len(applicants_to_delete)) self.response.write("\t- %d applications\n" % len(applications_to_delete)) self.response.write("\t- %d uniques\n" % len(uniques_to_delete)) logging.warning("Deleted applicants: %s", ', '.join(emails_deleted))
def do_delete(self): if not self.is_logged_in: self.send_json_error('Not logged in') try: userid = self.session_user['user_id'] user = self.auth.store.user_model.get_by_id(userid) # delete all piskels for the current user piskels = models.get_piskels_for_user(userid) for piskel in piskels: piskel.delete() # force consistency db.get(piskel.key()) # logout current user self.auth.unset_session() # from webapp2_extras.appengine.auth.models.User # http://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#User # # def add_auth_id(self, auth_id): # ... # unique = '%s.auth_id:%s' % (self.__class__.__name__, auth_id) # ... Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids) ) # delete user entry user.key.delete() return self.send_json_response({ 'status': 'ok' }) except Exception as error: return self.send_json_error(repr(error))
def post(self): """ Get fields from POST dict """ if not self.form.validate(): return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() last_name = self.form.last_name.data.strip() country = self.form.country.data try: user_info = models.User.get_by_id(long(self.user_id)) try: message = '' # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username', 'email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if success: # free old uniques Unique.delete_multi([ 'User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username ]) # The unique values were created, so we can save the user. user_info.username = username user_info.auth_ids[0] = 'own:%s' % username message += _('Your new username is ' ) + '<strong>' + username + '</strong>.' else: message += _( 'Username' ) + " <strong>" + username + "</strong> " + _( 'is already taken. It is not changed.') # At least one of the values is not unique. self.add_message(message, 'error') return self.get() user_info.name = name user_info.last_name = last_name user_info.country = country user_info.put() message += " " + _('Your profile has been updated!') self.add_message(message, 'success') return self.get() except (AttributeError, KeyError, ValueError), e: message = _('Unable to update profile!') logging.error('Unable to update profile: ' + e) self.add_message(message, 'error') return self.get() except (AttributeError, TypeError), e: login_error_message = _('Sorry you are not logged in!') self.add_message(login_error_message, 'error') self.redirect_to('login')
def remove_unique_properties(self): """Runs on delete to allow email and username to be reused.""" uniqueness_key_email = 'User.email:' + self.email uniqueness_key_username = '******' + self.username Unique.delete_multi([uniqueness_key_email, uniqueness_key_username])
def post(self): """ Get fields from POST dict """ if not self.form.validate(): return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() last_name = self.form.last_name.data.strip() country = self.form.country.data.strip() bio = self.form.bio.data.strip() twitter_widget_id = self.form.twitter_widget_id.data.strip() gravatar_url = self.form.gravatar_url.data.strip() google_plus_profile = self.form.google_plus_profile.data.strip() try: user_info = models.User.get_by_id(long(self.user_id)) try: message = '' # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username', 'email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if success: # free old uniques Unique.delete_multi([ 'User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username ]) # The unique values were created, so we can save the user. user_info.username = username user_info.auth_ids[0] = 'own:%s' % username message += _( 'Your new username is %s' % '<strong>{0:>s}</strong>'.format(username)) else: message += _( 'The username %s is already taken. Please choose another.' % '<strong>{0:>s}</strong>'.format(username)) # At least one of the values is not unique. self.add_message(message, 'error') return self.get() user_info.name = name user_info.last_name = last_name user_info.country = country user_info.bio = bio user_info.twitter_widget_id = twitter_widget_id user_info.gravatar_url = gravatar_url user_info.google_plus_profile = google_plus_profile user_info.put() message += " " + _( 'Thanks, your settings have been saved. You may now dance.' ) self.add_message(message, 'success') return self.get() except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ' + e) message = _( 'Unable to update profile. Please try again later.') self.add_message(message, 'error') return self.get() except (AttributeError, TypeError), e: login_error_message = _('Sorry you are not logged in.') self.add_message(login_error_message, 'error') self.redirect_to('login')
def deleteRelationship(self): success, existing = Unique.create_multi(['AreaUser.uniqueAreaUserString.%s:%s' % (self.userKey, self.privateArea)]) Unique.delete_multi(existing) self.key.delete();
def post(self): if not self.form.validate(): self.add_message("There were errors in subbitting the form.", "error") return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() email = self.form.email.data.strip() company = self.form.company.data.strip() country = self.form.country.data.strip() timezone = self.form.timezone.data.strip() user_info = User.get_by_id(long(self.user_id)) try: # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username'] uniques = ['User.username:%s' % username] # create the unique username and auth_id success, existing = Unique.create_multi(uniques) if success: # free old uniques and update user Unique.delete_multi(['User.username:%s' % user_info.username]) user_info.username = username self.add_message('Your new username is %s.' % format(username), 'success') else: # username not unique self.add_message('The username %s is already in use.' % format(username), 'error') return self.get() # update email if it has changed and it isn't already taken if email != user_info.email: user_info.unique_properties = ['email'] uniques = ['User.email:%s' % email] # create the unique username and auth_id success, existing = Unique.create_multi(uniques) if success: # free old uniques and update user Unique.delete_multi(['User.email:%s' % user_info.email]) user_info.email = email self.add_message('Your new email is %s.' % format(email), 'success') else: # user's email not unique self.add_message('That email address is already in use.', 'error') return self.get() # update database user_info.name = name user_info.company = company user_info.country = country user_info.timezone = timezone user_info.put() self.add_message("Your settings have been saved.", 'success') return self.get() except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ' + e) self.add_message('Unable to update profile. Please try again later.', 'error') return self.get()
def post(self): if not self.form.validate(): self.add_message("There were errors in subbitting the form.", "error") return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() email = self.form.email.data.strip() company = self.form.company.data.strip() country = self.form.country.data.strip() timezone = self.form.timezone.data.strip() user_info = User.get_by_id(long(self.user_id)) try: # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username'] uniques = ['User.username:%s' % username] # create the unique username and auth_id success, existing = Unique.create_multi(uniques) if success: # free old uniques and update user Unique.delete_multi( ['User.username:%s' % user_info.username]) user_info.username = username self.add_message( 'Your new username is %s.' % format(username), 'success') else: # username not unique self.add_message( 'The username %s is already in use.' % format(username), 'error') return self.get() # update email if it has changed and it isn't already taken if email != user_info.email: user_info.unique_properties = ['email'] uniques = ['User.email:%s' % email] # create the unique username and auth_id success, existing = Unique.create_multi(uniques) if success: # free old uniques and update user Unique.delete_multi(['User.email:%s' % user_info.email]) user_info.email = email self.add_message('Your new email is %s.' % format(email), 'success') else: # user's email not unique self.add_message('That email address is already in use.', 'error') return self.get() # update database user_info.name = name user_info.company = company user_info.country = country user_info.timezone = timezone user_info.put() self.add_message("Your settings have been saved.", 'success') return self.get() except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ' + e) self.add_message( 'Unable to update profile. Please try again later.', 'error') return self.get()
def post(self): """ Get fields from POST dict """ if not self.form.validate(): return self.get() username = self.form.username.data.lower() name = self.form.name.data.strip() last_name = self.form.last_name.data.strip() country = self.form.country.data.strip() bio = self.form.bio.data.strip() twitter_widget_id = self.form.twitter_widget_id.data.strip() gravatar_url = self.form.gravatar_url.data.strip() google_plus_profile = self.form.google_plus_profile.data.strip() try: user_info = models.User.get_by_id(long(self.user_id)) try: message='' # update username if it has changed and it isn't already taken if username != user_info.username: user_info.unique_properties = ['username','email'] uniques = [ 'User.username:%s' % username, 'User.auth_id:own:%s' % username, ] # Create the unique username and auth_id. success, existing = Unique.create_multi(uniques) if success: # free old uniques Unique.delete_multi(['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username]) # The unique values were created, so we can save the user. user_info.username=username user_info.auth_ids[0]='own:%s' % username message+= _('Your new username is %s' % '<strong>{0:>s}</strong>'.format(username) ) else: message+= _('The username %s is already taken. Please choose another.' % '<strong>{0:>s}</strong>'.format(username) ) # At least one of the values is not unique. self.add_message(message, 'error') return self.get() user_info.name=name user_info.last_name=last_name user_info.country=country user_info.bio=bio user_info.twitter_widget_id=twitter_widget_id user_info.gravatar_url=gravatar_url user_info.google_plus_profile=google_plus_profile user_info.put() message+= " " + _('Thanks, your settings have been saved. You may now dance.') self.add_message(message, 'success') return self.get() except (AttributeError, KeyError, ValueError), e: logging.error('Error updating profile: ' + e) message = _('Unable to update profile. Please try again later.') self.add_message(message, 'error') return self.get() except (AttributeError, TypeError), e: login_error_message = _('Sorry you are not logged in.') self.add_message(login_error_message, 'error') self.redirect_to('login')