def edit_role(role_id, request): ''' Takes a role and a request. It updates the name and description of the Role based on the values in request.form, then it updates the set of Members who hold the role. :param role_id: :param request: :return: ''' # First, grab our pretty little Role... this_role = Role.query.get(role_id) # Next, get all of its new values... new_name = request.form['name'] new_description = request.form['description'] new_members = [Member.query.get(member_id) for member_id in request.form['members']] # Then you actually modify the Role... this_role.name = new_name this_role.description = new_description this_role.members = new_members # Save your work, and then you're done! db_session.add(this_role) db_session.commit() return this_role
def make_admin(self): if User.query.filter_by(codename='admin').first() is not None: admin = User(codename='admin',password='******', first_name="THE", last_name="ADMIN", email="*****@*****.**", phone='0123456789', bio='This is the administrator account.') db_session.add(admin) db_session.commit()
def edit_user(user_id, request): ''' INPUT A PasswordChangeForm, EmailChangeForm, or UserInfoChangeForm, along with the user's ID from the request. OUTPUT Modifies the user's row in the database as specified by whichever form was submitted ''' user = User.query.get(int(user_id)) if request.form['username']: username = request.form['username'] user.username = username if request.form['password']: password = request.form['password'] user.password = password if request.form['name']: name = request.form['name'] user.name = name if request.form['email']: email = request.form['email'] user.email = email if request.form['phone']: phone = request.form['phone'] user.phone = int(phone) if request.form['bio']: bio = request.form['bio'] user.bio = bio if request.form['photo']: photo = request.form['photo'] user.photo = photo db_session.add(user) db_session.commit() return True
def create_user(request): ''' INPUT User Form with the mandatory arguments username, password, and email. Optional arguments real/display name, phone number, and 160 character bio. REQUIREMENT Username and email must be unique. OUTPUT Creates a User row in the database with all of the provided information associated with it. ''' # Checks the optional fields with a ternary operator, uses the mandatory fields directly. print "request.form: ",request.form print "request.form['phone']: ",request.form['phone'] phone = request.form['phone'] if request.form['phone'] != '' else None bio = request.form.bio.data if request.form['bio'] != '' else None photo = request.form.photo.data if request.form['photo'] != '' else None newUser = User(codename=request.form['codename'], password=request.form['password'], first_name=request.form['first_name'], last_name=request.form['last_name'], email=request.form['email'], phone=phone, bio=bio, photo=photo) db_session.add(newUser) db_session.commit() return newUser
def build_roles(group, role_csv): # First, open up the .csv of role data and make a reader for it. with open(role_csv, 'rb') as role_file: role_data = csv.reader(role_file) # Make a list to store all the new Roles so we can find out how many we made later on. new_roles = [] # Pop off a row to get rid of the header, then start iterating through the file. role_data.next() for each_role in role_data: # Grab the data out of the row and make a new Role object out of it (role_name) = each_role new_role = Role(group_id=group.group_id, name=role_name) # Then add the role to the db_session, NEW_OBJECTS list, and new_roles list. db_session.add(new_role) NEW_OBJECTS.append(new_role) new_roles.append(new_role) # Finally, after having created all the Roles, commit and print something nice. db_session.commit() print "WHOA -- I just made " + str(len(new_roles)) + " new Roles.\n"
def create_tdc(): tdc = Group(human_name="Theta Delta Chi", codename="tdc-mit") db_session.add(tdc) NEW_OBJECTS.append(tdc) db_session.commit() print "Okay, just made a Group called " + tdc.codename return tdc
def add_users(user_csv): ''' Takes in a .csv of User information and then makes a bunch of Users out of it. Returns them at the end. :param user_csv: :return: ''' # First, make a list to hold all the Users we make so we can return it. new_users = [] # Open up the .csv and make a reader to iterate through its rows with open(user_csv, 'rb') as user_file: user_data = csv.reader(user_file) # Pop off a row to get rid of the header. user_data.next() # Now, iterate through each row and create a User object for each_user in user_data: # Get the info from the row and use it to make a fresh User (codename, password, first_name, last_name, email, phone, bio, photo) = each_user new_user = User(codename=codename, password=password, first_name=first_name, last_name=last_name, email=email, phone=phone) # Next, add 'em to the database session and the NEW_OBJECTS list. db_session.add(new_user) NEW_OBJECTS.append(new_user) new_users.append(new_user) # After that big loop, commit these Users so they have primary key IDs -- then return 'em. db_session.commit() print "Phew, just finished up making these " + str(len(new_users)) + " new Users over here." return new_users
def make_test_group(self): self.login() new_group = Group( codename='test_group', human_name='The Test Group', byline="Whatever it is, I'm testing it.", description="" ) db_session.add(new_group) db_session.commit()
def test_member_create(self): # We automatically have an admin user who is in the test group, but we need to make # up another one for the admin to invite into the test group. buddy = User( codename="test_user", password="******", ) db_session.add(buddy) db_session.commit() # With that done, let's move on to adding them into the test group with a Member join request. params = dict( user_codename='test_user' ) create_response = self.app.post('/groups/test_group/members/add', data=params) assert "Alright, you just added test_user to test_group!" in create_response assert len(Group.query.filter_by(group_codename='test_group').members) == 2
def create_role(group_id, request): ''' INPUT Takes a Group_ID, optional Member_ID, and RoleForm -- which has the mandatory argument of a role name and an optional argument of role description. OUTPUT Creates a Role within the specified Group that has the specified description. If a Member_ID is supplied, that Member automatically becomes the first person to hold the Role. ''' role_name = request.form['name'] description = request.form['description'] if request.form['description'] else None new_role = Role(group_id = group_id, name = role_name, description = description) db_session.add(new_role) db_session.commit() return new_role
def add_members(group, users): ''' Takes all the Users and makes them Members in the Group. :param group: :param users: :return: ''' # Okay -- to get started, iterate through all the Users for user in users: # Create a new Member object for each one new_member = Member(group_id=group.group_id, user_id=user.user_id, codename=user.codename) # Now add it into the db_session and the NEW_OBJECTS list. db_session.add(new_member) NEW_OBJECTS.append(new_member) # Finally, after having created all those Members, commit this ish. db_session.commit() print "Awesome, just made " + str(len(users)) + " Members in the " + group.codename + " group.\n"