Ejemplo n.º 1
0
def signup_check():
    # check to see if there are any spaces or special characters in the requested username
    try:
        check_special_and_spaces(request.form['username'])
    except FormNotValidError as e:
        flash(e.args[0])
        return redirect(url_for('signup'))
    # checks to see if password has at least one capital and one number
    try:
        check_password(request.form['password'])
    except FormNotValidError as e:
        flash(e.args[0])
        return redirect(url_for('signup'))
    username = request.form['username']
    find_user = User.query.filter_by(username=username).first()
    if find_user == None:
        username, password = username, request.form['password']
        user = User(username, password)
        db.session.add(user)
        db.session.commit()
        if 'remember_me' in session:
            remember_me = session['remember_me']
        else:
            remember_me = None
        login_user(user)
        return redirect(url_for('user_profile'))
    else:
        flash("Username already taken. Please try again")
        return redirect(url_for('signup'))
Ejemplo n.º 2
0
def create_community():
    form = NewCommunityForm()
    filename = None
    if form.validate_on_submit():
        if request.files['FAQ']:
            # split the file into its extension and filename
            file = request.files['FAQ']
            extension = file.filename.split('.')[1]
            file_part = file.filename.split('.')[0]
            # user must submit a .txt file that has the same filename as the name of the community being created
            if extension == "txt" and file_part == request.form['name']:
                # secure and save the file to the directory FAQ_DIRECTORY so it can be retrieved later
                filename = secure_filename(file.filename)
                file.save(os.path.join(FAQ_DIRECTORY, filename))
            else:
                flash("Please follow instructions for submitting FAQ files")
                return redirect(url_for('create_community'))
        community = Community.query.filter_by(name=request.form['name']).first()
        if community is None:
            try:
                #check to make sure no spaces or special characters
                #check to make sure (if there is a password) that there is at least one capital and one number
                check_special_and_spaces(request.form['name'])
                check_special_and_spaces(request.form['password'])
            except FormNotValidError as e:
                flash(e.args[0])
                return redirect(url_for('create_community'))
            kwargs = {
                'name': request.form['name'],
                'password': request.form['password'],
                'founder': g.user,
                'FAQ': str(filename),
                'description': request.form['description']
            }
            c = Community(**kwargs)
            db.session.add(c)
            db.session.commit()
            communities = Community.query.all()
            return render_template('communities.html', communities=communities)
        else:
            flash("community already exists")
    return render_template('create_community.html', form=form)