<commit_before>from flask import (
    Flask,
    flash,
    g,
    session,
    redirect,
    render_template,
    request,
)
from flask.ext.mongoengine import MongoEngine

import datetime
import hashlib
import os
import random


app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': 'corgiwrites'}
app.config["SECRET_KEY"] = os.urandom(12)
app.config["DEBUG"] = True

db = MongoEngine(app)

import models

@app.route('/')
def front():
    if user not logged_in:
        return """Welcome to Corgiwrites, your source for corgis and writing."""
    else:
        # redirect to the dashboard

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        # display the login form
    else:
        username = request.form.get('username', None)
        password = request.form.get('password', None)
        if username is not None and password is not None:
            # look up in the database if the password matches for the username
            user = models.User.objects.get(username=username)
            if user is None:
                # display an error and the login form
                flash.message = "User doesn't exist"
                return redirect('/login')
            if user.password == hashlib.sha256(password).hexdigest():
                # log the user in
                session['is_logged_in'] = True
                session['username'] = username
                return redirect('/dashboard')
            else:
                # display an error and the login form
                flash.message = "Wrong password"
                return redirect('/login')
        else:
            # display the login form
            flash.message = 'Both username and password are required'
            return redirect('/login')

@app.route('/logout')
def logout():
    # log the user out
    session['is_logged_in'] = False
    session['username'] = None
    return redirect('/')

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        # display the register form
    else:
        username = request.form.get('username', None)
        email = request.form.get('email', None)
        password = request.form.get('password', None)
        password_confirm = request.form.get('password_confirm', None)
        if username is None or email is None or password is None:
            flash.message = "All fields are required"
            return redirect('/register')
        if password != password_confirm:
            flash.message = "Please enter your password correctly both times"
            return redirect('/register')
        user_exists = models.User.objects.get(username=username)
        if user_exists is None:
            # start the registration process
            # create a user in the database with the provided username and password and email
            user = models.User()
            user.username = username
            user.email = email
            user.password = <PASSWORD>(password).<PASSWORD>()
            # save the user
            user.save()
            # send them to the login screen
            return redirect('/login')
        else:
            # Warn the user that the username is taken and start over
            flash.message = "That username already exists in the database!"
            return redirect('/register')

@app.route('/story/create', methods=['GET', 'POST'])
def create_story():
    if not session['is_logged_in']:
        return redirect('/login')
    user = models.User.objects.get(username=session['username'])
    if request.method == 'GET':
        # show them the story creation form
    else:
        # save the information to the database
        title = request.form.get('title', None)
        if title is None:
            flash.message = "Title is required"
            return redirect('/story/create')
        story = models.Story()
        story.title = title
        story.genre = request.form.get('genre', None)
        story.summary = request.form.get('summary', None)
        story.status = request.form.get('status', None)
        wordcount_text = request.form.get('wordcount', None)
        if wordcount_text is not None:
            wc_entry = models.WordCountEntry()
            wc_entry.wordcount = int(wordcount_text)
            story.wordcounts.append(wc_entry)
        story.save()
        user.stories.append(story)
        user.save()
        # send the user to the story page
        return redirect('/story/%s' % story.id)

@app.route('/story/view/<story_id>')
def view_story(story_id):
    if not session['is_logged_in']:
        return redirect('/login')
    story = models.Story.objects.get(id=story_id)
    if story is None:
        # TODO corgiw - find out how to show the user a 404 page
        # return a 404
    if story.owner.username is not session['username']:
        # return a 404
    # show the user the story

@app.route('/story/wordcount/update')
def update_story_wordcount():
    if user not logged_in:
        # redirect to the login page
        return
    pass

@app.route('/market/create', methods=['GET', 'POST'])
def create_market():
    if not session['is_logged_in']:
        return redirect('/login')
    if request.method == 'GET':<commit_msg>make some notes for corgi<commit_after>from flask import (
    Flask,
    flash,
    g,
    session,
    redirect,
    render_template,
    request,
)
from flask.ext.mongoengine import MongoEngine

import datetime
import hashlib
import os
import random


app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': 'corgiwrites'}
app.config["SECRET_KEY"] = os.urandom(12)
app.config["DEBUG"] = True

db = MongoEngine(app)

import models

@app.route('/')
def front():
    if user not logged_in:
        return """Welcome to Corgiwrites, your source for corgis and writing."""
    else:
        # redirect to the dashboard

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        # display the login form
    else:
        username = request.form.get('username', None)
        password = request.form.get('password', None)
        if username is not None and password is not None:
            # look up in the database if the password matches for the username
            user = models.User.objects.get(username=username)
            if user is None:
                # display an error and the login form
                flash.message = "User doesn't exist"
                return redirect('/login')
            if user.password == hashlib.sha256(password).hex<PASSWORD>():
                # log the user in
                session['is_logged_in'] = True
                session['username'] = username
                return redirect('/dashboard')
            else:
                # display an error and the login form
                flash.message = "Wrong password"
                return redirect('/login')
        else:
            # display the login form
            flash.message = 'Both username and password are required'
            return redirect('/login')

@app.route('/logout')
def logout():
    # log the user out
    session['is_logged_in'] = False
    session['username'] = None
    return redirect('/')

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        # display the register form
    else:
        username = request.form.get('username', None)
        email = request.form.get('email', None)
        password = request.form.get('password', None)
        password_confirm = request.form.get('password_confirm', None)
        if username is None or email is None or password is None:
            flash.message = "All fields are required"
            return redirect('/register')
        if password != password_confirm:
            flash.message = "Please enter your password correctly both times"
            return redirect('/register')
        user_exists = models.User.objects.get(username=username)
        if user_exists is None:
            # start the registration process
            # create a user in the database with the provided username and password and email
            user = models.User()
            user.username = username
            user.email = email
            user.password = <PASSWORD>(<PASSWORD>()
            # save the user
            user.save()
            # send them to the login screen
            return redirect('/login')
        else:
            # Warn the user that the username is taken and start over
            flash.message = "That username already exists in the database!"
            return redirect('/register')

@app.route('/story/create', methods=['GET', 'POST'])
def create_story():
    if not session['is_logged_in']:
        return redirect('/login')
    user = models.User.objects.get(username=session['username'])
    if request.method == 'GET':
        # show them the story creation form
    else:
        # save the information to the database
        title = request.form.get('title', None)
        if title is None:
            flash.message = "Title is required"
            return redirect('/story/create')
        story = models.Story()
        story.title = title
        story.genre = request.form.get('genre', None)
        story.summary = request.form.get('summary', None)
        story.status = request.form.get('status', None)
        wordcount_text = request.form.get('wordcount', None)
        if wordcount_text is not None:
            wc_entry = models.WordCountEntry()
            wc_entry.wordcount = int(wordcount_text)
            story.wordcounts.append(wc_entry)
        story.save()
        user.stories.append(story)
        user.save()
        # send the user to the story page
        return redirect('/story/%s' % story.id)

@app.route('/story/view/<story_id>')
def view_story(story_id):
    if not session['is_logged_in']:
        return redirect('/login')
    story = models.Story.objects.get(id=story_id)
    if story is None:
        # TODO corgiw - find out how to show the user a 404 page
        # return a 404
    if story.owner.username is not session['username']:
        # return a 404
    # show the user the story

# First thing to do, make this route only take POST requests
@app.route('/story/wordcount/update')
def update_story_wordcount():
    if user not logged_in:
        # redirect to the login page
        return
    # fetch two things from the form:
    #   the story id
    #   the next wordcount
    # both are required, make sure they're not None, throw an error if so
    # fetch the story from the database using the story id
    # create a new wordcount object
    # set the wordcount on that object
    # append the object to the story's wordcount list
    # save the story
    # redirect to the story's page


@app.route('/market/create', methods=['GET', 'POST'])
def create_market():
    if not session['is_logged_in']:
        return redirect('/login')
    if request.method == 'GET':
{'fraction_non_alphanumeric': 0.10654767325380202, 'fraction_numerical': 0.0025498588470995354, 'mean_word_length': 2.282127913927077, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1946031', 'n_tokens_mistral': 2972, 'n_tokens_neox': 2899, 'n_words': 959}