<commit_before>import os
import urlparse
import smtplib
import werkzeug
import urllib
from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException
from werkzeug.wsgi import SharedDataMiddleware
from jinja2 import Environment, FileSystemLoader
from email.mime.text import MIMEText
from conf import EMAIL, TOKN, CEILING
from validate_email import validate_email
from datetime import datetime

"""
WSGI Application

This application listens to a form. When the form is submitted, this
application takes the information submitted, formats it into a python
dictiononary, then emails it to a specified email
"""
class Forms(object):
    """
    This class listens for a form submission, checks that the data is valid, and
    sends the form data in a formatted message to the email specified in conf.py
    """

    def __init__(self, rater):
        # Sets up the path to the template files
        template_path = os.path.join(os.path.dirname(__file__), 'templates')
        self.rater = rater
        self.error = None
        # Initiates jinja environment
        self.jinja_env = Environment(loader=FileSystemLoader(template_path),
                                     autoescape=True)
        # When the browser is pointed at the root of the website, call
        # on_form_page
        self.url_map = Map([Rule('/', endpoint='form_page')])

    # Renders a webpage based on a template
    def render_template(self, template_name, status, **context):
        # Render template
        t = self.jinja_env.get_template(template_name)
        # Returns response object with rendered template
        return Response(t.render(context), mimetype='text/html', status=status)

    # Really important. Handles deciding what happens
    def dispatch_request(self, request):
        adapter = self.url_map.bind_to_environ(request.environ)
        try:
            endpoint, values = adapter.match()
            return getattr(self, 'on_' + endpoint)(request, **values)
        except HTTPException, e:
            return e

    # Starts wsgi_app by creating a Request and Response based on the Request
    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)

    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    # Sets up and sends the email
    def send_email(self, msg):
        # Format the message
        msg_send = MIMEText(str(msg))
        # Sets up a temporary mail server to send from
        s = smtplib.SMTP('localhost')
        # Attempts to send the mail to EMAIL, with the message formatted as a
        # string
        try:
            s.sendmail('theform', EMAIL, msg_send.as_string())
            s.quit()
        except:
            s.quit()

    # Checks for valid form data, calls send_email, returns a redirect
    def on_form_page(self, request):
        self.error = None
        self.rater.increment_rate()
        message = None
        error_number = 0
        if request.method == 'POST':
            if not is_valid_email(request):
                self.error = 'Invalid Email'
                message = None
                error_number = 1
            elif not validate_name(request):
                self.error = 'Invalid Name'
                message = None
                error_number = 2
            elif (not is_hidden_field_empty(request)
                  or not is_valid_token(request)):
                self.error = 'Improper Form Submission'
                message = None
                error_number = 3
            elif self.rater.is_rate_violation():
                self.error = 'Too Many Requests'
                message = None
                error_number = 4
            else:
                message = create_msg(request)
                if message:
                    self.send_email(message)
                    redirect_url = request.form['redirect']
                    return werkzeug.utils.redirect(redirect_url, code=302)
            error_url = create_error_url(error_number, self.error, request)
            return werkzeug.utils.redirect(error_url, code=302)
        # Renders test form on localhost
        return self.render_template('index.html',
                                    error=self.error,
                                    url=message,
                                    status=200)


<commit_msg>Redirect_url set from message rather than raw form data<commit_after>import os
import urlparse
import smtplib
import werkzeug
import urllib
from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException
from werkzeug.wsgi import SharedDataMiddleware
from jinja2 import Environment, FileSystemLoader
from email.mime.text import MIMEText
from conf import EMAIL, TOKN, CEILING
from validate_email import validate_email
from datetime import datetime

"""
WSGI Application

This application listens to a form. When the form is submitted, this
application takes the information submitted, formats it into a python
dictiononary, then emails it to a specified email
"""
class Forms(object):
    """
    This class listens for a form submission, checks that the data is valid, and
    sends the form data in a formatted message to the email specified in conf.py
    """

    def __init__(self, rater):
        # Sets up the path to the template files
        template_path = os.path.join(os.path.dirname(__file__), 'templates')
        self.rater = rater
        self.error = None
        # Initiates jinja environment
        self.jinja_env = Environment(loader=FileSystemLoader(template_path),
                                     autoescape=True)
        # When the browser is pointed at the root of the website, call
        # on_form_page
        self.url_map = Map([Rule('/', endpoint='form_page')])

    # Renders a webpage based on a template
    def render_template(self, template_name, status, **context):
        # Render template
        t = self.jinja_env.get_template(template_name)
        # Returns response object with rendered template
        return Response(t.render(context), mimetype='text/html', status=status)

    # Really important. Handles deciding what happens
    def dispatch_request(self, request):
        adapter = self.url_map.bind_to_environ(request.environ)
        try:
            endpoint, values = adapter.match()
            return getattr(self, 'on_' + endpoint)(request, **values)
        except HTTPException, e:
            return e

    # Starts wsgi_app by creating a Request and Response based on the Request
    def wsgi_app(self, environ, start_response):
        request = Request(environ)
        response = self.dispatch_request(request)
        return response(environ, start_response)

    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    # Sets up and sends the email
    def send_email(self, msg):
        # Format the message
        msg_send = MIMEText(str(msg))
        # Sets up a temporary mail server to send from
        s = smtplib.SMTP('localhost')
        # Attempts to send the mail to EMAIL, with the message formatted as a
        # string
        try:
            s.sendmail('theform', EMAIL, msg_send.as_string())
            s.quit()
        except:
            s.quit()

    # Checks for valid form data, calls send_email, returns a redirect
    def on_form_page(self, request):
        self.error = None
        self.rater.increment_rate()
        message = None
        error_number = 0
        if request.method == 'POST':
            if not is_valid_email(request):
                self.error = 'Invalid Email'
                message = None
                error_number = 1
            elif not validate_name(request):
                self.error = 'Invalid Name'
                message = None
                error_number = 2
            elif (not is_hidden_field_empty(request)
                  or not is_valid_token(request)):
                self.error = 'Improper Form Submission'
                message = None
                error_number = 3
            elif self.rater.is_rate_violation():
                self.error = 'Too Many Requests'
                message = None
                error_number = 4
            else:
                message = create_msg(request)
                if message:
                    self.send_email(message)
                    redirect_url = message['redirect']
                    return werkzeug.utils.redirect(redirect_url, code=302)
            error_url = create_error_url(error_number, self.error, request)
            return werkzeug.utils.redirect(error_url, code=302)
        # Renders test form on localhost
        return self.render_template('index.html',
                                    error=self.error,
                                    url=message,
                                    status=200)



{'fraction_non_alphanumeric': 0.06970597951767427, 'fraction_numerical': 0.003303600925008259, 'mean_word_length': 2.149098474341193, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2712776', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2345, 'n_words': 758}