def edit_posting(error=""):
    title = 'Edit Posting'
    if g.user is None:
        return redirect(url_for('login'))

    posting_info = database_helpers.get_post_id(request.args.get('postid'))
    if posting_info is None or g.user.userid != posting_info.userid:
        return redirect(url_for('user_home_screen'))

    if request.method == 'POST':
        [results, error
         ] = form_submissions.get_form_create_post(request.form, CATEGORIES)
        if len(error) == 0:
            print(results)
            database_helpers.modify_post_by_id(results, posting_info[0])
            return redirect(url_for('user_home_screen'))
    return render_template(
        'edit-posting-view.html',
        contact_options=CONTACT_METHOD,
        categories=CATEGORIES,
        js_file="tag-javascript.js",
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        user_id=g.user.userid,
        CURRENT_USER_ID=g.user.userid,
        page_title=title,
        error=error,
        css_file=helper_functions.generate_linked_files('create-posting-view'),
        post=posting_info)
def remove_posting_view(error=""):
    title = 'Remove Posting'
    if g.user is None:
        return redirect(url_for('login'))

    posting_info = database_helpers.get_post_id(request.args.get('postid'))
    if posting_info is None or g.user.userid != posting_info.userid:
        return redirect(url_for('user_home_screen'))

    if request.method == 'POST':
        posting_info_remove = Posting.query.filter_by(
            postid=request.args.get('postid')).first()
        db.session.delete(posting_info_remove)
        db.session.commit()
        return redirect(url_for('user_home_screen'))

    return render_template(
        'remove-posting-view.html',
        contact_options=CONTACT_METHOD,
        categories=CATEGORIES,
        js_file="tag-javascript.js",
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        user_id=g.user.userid,
        CURRENT_USER_ID=g.user.userid,
        page_title=title,
        error=error,
        css_file=helper_functions.generate_linked_files('create-posting-view'),
        post=posting_info)
def edit_account(error=""):
    if g.user is None:
        return redirect(url_for('login'))
    title = 'Edit Account'
    current_user = g.user
    error = ''
    if request.method == 'POST':
        [result, error] = form_submissions.get_modified_account_info(
            request.form, g.user.userid)
        if len(error) == 0 and check_password_hash(g.user.password,
                                                   str(result['oldpassword'])):
            if result['deleteaccount'] == "delete":
                database_helpers.delete_user(g.user.userid)
            if database_helpers.update_current_user(g.user, result):
                return redirect(url_for('login'))
    return render_template(
        'edit-account.html',
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        error=error,
        current_user=current_user,
        CURRENT_USER_ID=g.user.userid,
        page_title=title,
        css_file=helper_functions.generate_linked_files('create-account'),
    )
def users_account():
    if g.user is None:
        return redirect(url_for('login'))
    account_info = g.user
    title = "USER: "******"GET":
        userid = (request.args.get('userid'))
        account_info = User.query.filter_by(userid=userid).first()
        if account_info is None:
            return redirect(url_for('not_found_error_item'))
        postings = database_helpers.get_postings(account_info.userid)
        claims = database_helpers.get_claims(account_info.userid)
        sales = database_helpers.get_sales(account_info.userid)
        purchases = database_helpers.get_purchases(account_info.userid)
        title = "USER: " + account_info.username
    return render_template(
        'account-view.html',
        purchases=purchases,
        postings=postings,
        sales=sales,
        claims=claims,
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        user_id=g.user.userid,
        CURRENT_USER_ID=g.user.userid,
        page_title=title,
        css_file=helper_functions.generate_linked_files('account-view'),
        account=account_info)
def claim_completion(is_transaction_complete=False):
    if g.user is None:
        return redirect(url_for('login'))
    is_transaction_complete = request.args.get('is_transaction_complete')
    print(is_transaction_complete)
    return render_template(
        'claim-complete.html',
        is_transaction_complete=is_transaction_complete == "True",
        title="Claim Complete",
        error='',
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        css_file=helper_functions.generate_linked_files('claim'))
def full_posting_view():
    if g.user is None:
        return redirect(url_for('login'))
    #posting_info = {}
    if request.method == 'GET':
        [posting_info, user_info
         ] = database_helpers.get_posting_by_id(request.args.get('postid'))
        if posting_info is None:
            return redirect(url_for('not_found_error_item'))
        title = "POSTING: " + posting_info.title
    return render_template(
        'full-posting-view.html',
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        page_title=title,
        css_file=helper_functions.generate_linked_files('full-posting-view'),
        post=posting_info,
        poster_info=user_info)
def create_account(error=""):
    title = "Welcome to Craigversity!"
    CREATE_ERROR = "Need to fill in ALL fields marked with an '*'"
    errors = []
    if g.user:
        return redirect(url_for('user_home_screen'))
    if request.method == 'POST':
        [results,
         errors] = form_submissions.generate_new_account_form(request.form)
        if len(errors) == 0:
            added_successfully = database_helpers.add_user(results)
            if added_successfully:
                return redirect(url_for('login'))
            errors = "Could not process. Try again."
    return render_template(
        'create-account.html',
        current_user_is_auth=False,
        error=errors,
        page_title=title,
        css_file=helper_functions.generate_linked_files('create-account'),
    )
def login(error=""):
    title = "Login to Craigversity!"
    LOGIN_ERROR = "Invalid information was submitted. Please try again!"
    error = ''
    if g.user:
        return redirect(url_for('user_home_screen'))
    if request.method == "POST":
        error = LOGIN_ERROR
        email = form_submissions.get_email(request.form['email'])
        if not email == False:
            user = User.query.filter_by(email=email).first()
            if form_submissions.verify_password(user,
                                                request.form['password']):
                session['userid'] = user.userid
                return redirect(url_for('user_home_screen'))
    return render_template(
        'login.html',
        current_user_is_auth=False,
        error=error,
        page_title=title,
        css_file=helper_functions.generate_linked_files('login'))
def new_posting_submission(error=""):
    title = "Submit a New Posting!"
    error = []
    if g.user is None:
        return redirect(url_for('login'))
    if request.method == 'POST':
        [results, error
         ] = form_submissions.get_form_create_post(request.form, CATEGORIES)
        if len(error) == 0:
            database_helpers.add_new_post(results, g.user)
            return redirect(url_for('login'))
    return render_template(
        'create-posting-view.html',
        contact_options=CONTACT_METHOD,
        categories=CATEGORIES,
        current_user_id=g.user.userid,
        js_file="tag-javascript.js",
        current_user_is_auth=(g.user.userid > 0),
        page_title=title,
        error=error,
        css_file=helper_functions.generate_linked_files('create-posting-view'))
def claim_submission():
    if g.user is None:
        return redirect(url_for('login'))
    title = "Claim Submission"
    error = ''
    IsSeller = False
    post_info = {'title': '', 'postid': '', 'username': ''}
    try:
        [posting_info, poster_info
         ] = database_helpers.get_posting_by_id(request.args.get('postid'))
        if posting_info is None:
            return redirect(url_for('error'))
        post_info = helper_functions.get_post_info_claims(
            posting_info, poster_info)
        isSeller = (posting_info.userid == g.user.userid)
        if isSeller:
            title = "Seller " + title
        else:
            title = "Buyer " + title
    except Exception as e:
        error = "Please Try Resubmitting. Something went Wrong."
        return render_template(
            'claim.html',
            error=error,
            post=post_info,
            isSeller=False,
            current_user_id=g.user.userid,
            current_user_is_auth=(g.user.userid > 0),
            page_title=title,
            css_file=helper_functions.generate_linked_files('claim'))

    if request.method == 'POST':
        [submitted,
         error] = form_submissions.get_new_claims_form(request.form, g.user,
                                                       post_info['postid'])
        if len(error) == 0:
            [completed_claim,
             claim] = database_helpers.add_claim(submitted,
                                                 post_info['postid'], g.user)
            if completed_claim:
                is_transaction_completed = database_helpers.check_for_transaction(
                    claim)
                if is_transaction_completed:
                    db.session.commit()
                    return redirect(
                        url_for('claim_completion',
                                is_transaction_complete=True))
                elif is_transaction_completed is not None:
                    db.session.commit()
                    return redirect(
                        url_for('claim_completion',
                                is_transaction_complete=False))
            error = "Please resubmit your claim. There was an issue. You may have entered invalid data."
    return render_template(
        'claim.html',
        error=error,
        post=post_info,
        isSeller=isSeller,
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        page_title=title,
        css_file=helper_functions.generate_linked_files('claim'))
def user_home_screen():
    title = "Search and Filter Postings!"
    page = 1
    if g.user is None:
        return redirect(url_for('login'))
    if request.method == 'GET':
        [submitted,
         randomize] = form_submissions.get_filters(request.args, True)
        categoryIsAll = True if submitted['category'] == 'All' else False
        if randomize:
            postings = database_helpers.generate_random_postings()
        else:
            if submitted['search'] == '':
                postings = Posting.query.filter(
                    and_(
                        Posting.price >= float(submitted['minPrice']),
                        Posting.price <= float(submitted['maxPrice']),
                        or_(Posting.category.contains(submitted['category']),
                            categoryIsAll))).join(User).with_entities(
                                Posting.postid, Posting.userid, User.username,
                                Posting.title, Posting.price,
                                Posting.description, User.rating)
            else:
                search_elems = form_submissions.get_search_elems(
                    submitted['search'])
                postings = Posting.query.filter(
                    and_(
                        and_(Posting.tags.like(e) for e in search_elems),
                        Posting.price >= float(submitted['minPrice']),
                        Posting.price <= float(submitted['maxPrice']),
                        or_(Posting.category.contains(submitted['category']),
                            categoryIsAll))).join(User).with_entities(
                                Posting.postid, Posting.userid, User.username,
                                Posting.title, Posting.price,
                                Posting.description, User.rating)
    else:
        submitted = form_submissions.get_filters('', True)
        postings = database_helpers.generate_random_postings()

    page = form_submissions.get_page_number(submitted['page'],
                                            postings.count(),
                                            app.config['POSTS_PER_PAGE'])
    postings = postings.paginate(page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('user_home_screen',
                       search=submitted['search'],
                       category=submitted['category'],
                       minPrice=submitted['minPrice'],
                       maxPrice=submitted['maxPrice'],
                       page=postings.next_num) if postings.has_next else None
    prev_url = url_for('user_home_screen',
                       search=submitted['search'],
                       category=submitted['category'],
                       minPrice=submitted['minPrice'],
                       maxPrice=submitted['maxPrice'],
                       page=postings.prev_num) if postings.has_prev else None
    return render_template(
        'user-view.html',
        next_url=next_url,
        prev_url=prev_url,
        page=page,
        categories=CATEGORIES,
        able_to_filter=True,
        submitted=submitted,
        current_user_id=g.user.userid,
        current_user_is_auth=(g.user.userid > 0),
        page_title=title,
        css_file=helper_functions.generate_linked_files('user-view'),
        filtered_postings=postings.items)