def create_meeting(form=None): """ Creates a new Meeting. """ if form is None: create_form = MeetingCreateForm(request.form) else: create_form = MeetingCreateForm(form) if not create_form.validate(): flash('error Could not Create New Meeting, Please Try Again.') return redirect(request.args.get('next') or url_for('meetings.home')) try: user = current_user._get_current_object() # generate the list of users that will be in the meeting emails = create_form.emails.data.split(" ") emails.append(user.email) # generate the list of valid emails query = User.objects(email__in=emails) valid_emails = [u.email for u in query] # display the invalid emails, cancel the creation request if len(emails) != len(emails): invalid_emails = list(set(emails) - set(valid_emails)) flash('error We Were Unable to Find User(s): {}'.format( invalid_emails)) return redirect(url_for('meetings.home')) # validate and create the meeting if create_form.nature.data == 'other': m = Meeting(name=create_form.name.data, members=query, owner=user, meeting_nature="", active=False).save() else: m = Meeting(name=create_form.name.data, members=query, owner=user, meeting_nature=create_form.nature.data, active=False).save() # insert the meeting in each user's list of meetings for u in query: u.meetings.append(m) u.meeting_count = u.meeting_count + 1 u.save() flash('success New Meeting Created with Member(s): {}'.format( ", ".join(valid_emails))) except Exception as e: flash('error An Error has occurred, Please Try Again. {}'.format(e)) return redirect(request.args.get('next') or url_for('meetings.home'))
def create_group_meeting(form=None): """ Creates a new meeting from the group landing page """ if form is None: flash("error Invalid Request to create meeting") return redirect(request.args.get('next') or url_for('groups.home')) create_form = MeetingCreateForm(form) if not create_form.validate(): flash("error Could not create new meeting, please try again") return redirect(request.args.get('next') or url_for('groups.home')) try: user = current_user._get_current_object() emails = create_form.emails.data.split(" ") emails.append(user.email) # generate list of valid emails - should be valid unless a user is deleted query = User.objects(email__in=emails) valid_emails = [u.email for u in query] # check if emails are complete, if not, display incorrect and cancel request if len(valid_emails) != len(emails): invalid_emails = list(set(emails) - set(valid_emails)) flash("error We were unable to find user(s): {}".format(invalid_emails)) return redirect(url_for('groups.home')) # validate and create meeting m = Meeting(name=create_form.name.data, members=query, owner=user, meeting_nature=create_form.nature.data, active=False).save() # insert meeting into each user's list of meetings for u in query: u.meetings.append(m) u.meeting_count = u.meeting_count + 1 u.save() # insert meeting into this group # query to find group associated g = Group.objects.get(members=query) g.meetings.append(m) g.save() flash("success Meeting successfully created with group {}".format(g.name)) return redirect(url_for('groups.get_group_by_id', group_id=g.id)) except Exception as e: flash("error An error has occcured, please try again: {}".format(e)) return redirect(url_for('groups.home'))
def signup(): """ Signs the Current User In """ if request.method == 'GET': return render_template('auth/signup.html', form=SignupForm()) form = SignupForm(request.form) if not form.validate(): flash('error Invalid Email or Password') return redirect(url_for('auth.signup')) name = form.name.data email = form.email.data password = form.password.data try: # validate that the user does not already exist if User.objects(email__exact=email).count() != 0: flash('error An Account Is Already Using That Email.') return redirect(url_for('auth.login')) # generate activation token activation_token = secrets.token_urlsafe(32) mail = SendGrid(app) # send registration email mail.send_email(from_email=app.config['SENDGRID_DEFAULT_FROM'], to_email=email, subject='Welcome to Quillio', html=activate_html(name, activation_token, email)) # add user to the database user_datastore.create_user( email=email, name=name, password=hash_password(password), activation_hash=hash_password(activation_token), active=True, authenticated=False) flash('Please Check Your Email For An Activation Request.') return redirect(url_for('auth.login')) except Exception as e: print(str(e)) flash('error An Error has Occured, Please Try Again!') return redirect(url_for('auth.signup'))
def create_group(form=None): """ Creates a new Group. """ if form is None: create_form = GroupCreateForm(request.form) else: create_form = GroupCreateForm(form) if not create_form.validate(): flash('error Could not Create New Group, Please Try Again.') return redirect(request.args.get('next') or url_for('groups.home')) try: user = current_user._get_current_object() # generate the list of requested member emails emails = create_form.emails.data.split(" ") emails.append(user.email) # generate the list of valid emails query = User.objects(email__in=emails) valid_emails = [u.email for u in query] # validate and create the new group if len(emails) == len(valid_emails): g = Group(name=create_form.name.data, members=query, admins=[user]).save() # add the group to each member's list of groups for u in query(): u.groups.append(g) u.save() flash('success New Group Created with Member(s): {}'.format( ", ".join(valid_emails))) return redirect(request.args.get('next') or url_for('groups.home')) else: # determine the invalid emails invalid_emails = list(set(emails) - set(valid_emails)) flash('error Could not Create New Group. Unable to Find User(s):' '{}'.format(invalid_emails)) except Exception as e: flash('error An Error has Occured, Please Try Again. ' '{}'.format(str(e))) return redirect(request.args.get('next') or url_for('groups.home'))
def update_meeting(form=None): """ Updates an Existing Meeting """ if form is None: flash('error Invalid Request to Create New Meeting.') return redirect(request.args.get('next') or url_for('meetings.home')) update_form = MeetingUpdateForm(form) if not update_form.validate(): flash('error Could not Update Meeting, Please Try Again.') return redirect(request.args.get('next') or url_for('meetings.home')) try: # extract the form data name = update_form.name.data emails_to_add_str = update_form.emails_to_add.data emails_to_remove_str = update_form.emails_to_remove.data # search for the meeting meeting = Meeting.objects.get(id=update_form.meeting_id.data) members = meeting.members # remove the undesired members if len(emails_to_remove_str) != 0: emails_to_remove = emails_to_remove_str.split(" ") members_to_remove = User.objects(email__in=emails_to_remove) # remove the meeting from each members list of meetings for member in members_to_remove: if meeting in member.meetings: member.meeting_count = member.meeting_count - 1 member.meetings.remove(meeting) member.save() # remove the members from the list members = list( filter(lambda x: x not in members_to_remove, members)) # add the new members if len(emails_to_add_str) != 0: emails_to_add = emails_to_add_str.split(" ") members_to_add = User.objects(email__in=emails_to_add) for member in members_to_add: # add member to the meeting's list of members if member not in members: members.append(member) # add meeting to the member's list of meetings if meeting not in member.meetings: member.meetings.append(meeting) member.save() # save the changes meeting.name = name meeting.members = members meeting.save() flash('success Meeting has been Successfully Updated.') except Exception as e: flash('error An Error has Occurred, Please Try Again. {}'.format(e)) return redirect(request.args.get('next') or url_for('meetings.home'))
def update_group_meeting(form=None): """ Update and existing meeting from the group landing page""" if form is None: flash("error Invalid Request to update meeting") return redirect(request.args.get('next') or url_for('groups.home')) update_form = MeetingUpdateForm(form) if not update_form.validate(): flash("error Could not update meeting, please try again") return redirect(request.args.get('next') or url_for('groups.home')) try: # extract form data name = update_form.name.data emails_to_add_str = update_form.emails_to_add.data emails_to_remove_str = update_form.emails_to_remove.data # search for the meeting meeting = Meeting.objects.get(id=update_form.meeting_id.data) members = meeting.members # remove the undesired members if len(emails_to_remove_str) != 0: emails_to_remove = emails_to_remove_str.split("") members_to_remove = User.objects(email__in=emails_to_remove) # remove the meeting from each members list of meetings for member in members_to_remove: if meeting in member.meetings: member.meeting_count = member.meeting_count - 1 member.meetings.remove(meeting) member.save() # remove members from the list members = list(filter( lambda x: x not in members_to_remove, members)) # add new members if len(emails_to_add_str) != 0: emails_to_add = emails_to_add_str.split(" ") members_to_add = User.objects(email__in=emails_to_add) for member in members_to_add: # add member to the meeting's list of members if member not in members: members.append(member) # add meeting to the member's list of meetings if meeting not in member.meetings: member.meetings.append(meeting) member.save() # save all the changes meeting.name = name meeting.members = members meeting.save() flash('success Meeting has been successfully updated.') except Exception as e: flash("error An error has occcured, please try again: {}".format(e)) return redirect(url_for('groups.home'))
def update_group(form=None): """ Updates an Existing Group """ if form is None: update_form = GroupUpdateForm(request.form) else: update_form = GroupUpdateForm(form) if not update_form.validate(): flash('error Could Not Update Group, Please Try Again.') return redirect(request.args.get('next') or url_for('groups.home')) try: # extract the form data name = update_form.name.data description = update_form.description.data emails_to_add_str = update_form.emails_to_add.data admin_emails_to_add_str = update_form.admin_emails_to_add.data emails_to_remove_str = update_form.emails_to_remove.data group = Group.objects.get(id=update_form.group_id.data) members = group.members admins = group.admins # remove the undesired members if len(emails_to_remove_str) != 0: emails_to_remove = emails_to_remove_str.split(" ") members_to_remove = User.objects( email__in=emails_to_remove) # remove the group from each members list of groups for member in members_to_remove: if group in member.groups: member.groups.remove(group) member.save() # remove the members from the list members = list(filter( lambda x: x not in members_to_remove, members)) # add the new members if len(emails_to_add_str) != 0: emails_to_add = emails_to_add_str.split(" ") members_to_add = User.objects(email__in=emails_to_add) for member in members_to_add: # add member to the group's list of members if member not in members: members.append(member) # add group to the member's list of groups if group not in member.groups: member.groups.append(group) member.save() # add the new admins if len(admin_emails_to_add_str) != 0: admin_emails_to_add = admin_emails_to_add_str.split(" ") admins_to_add = User.objects( email__in=admin_emails_to_add) for admin in admins_to_add: # add admin to the group's list of admins if admin not in admins: admins.append(admin) # add admin to the group's list of members if admin not in members: members.append(admin) # add group to the admin's list of groups if group not in admin.groups: admin.groups.append(group) admin.save() # update the description if len(description) != 0: group.description = description # save the changes group.name = name group.members = members group.admins = admins group.save() flash('success Group Successfully Updated') except Exception as e: flash('error Unable to Update Group at this Time, Please Try Again.' '{}'.format(str(e))) print(str(e)) return redirect(request.args.get('next') or url_for('groups.home'))