<commit_before>#!/usr/bin/env python2

import tornado
import logging
import datetime
import os
import sys
import binascii
import rules
import util
from tornado import web, httpserver
from datetime import datetime

_http_server = None
_https_server = None
_template_root = sys.prefix + '/var/jim/templates'
_log = None
# This is default (test-only) certificate located in ./certs directory.
# default certificate is self-signed, so we don't have 'ca_cert' field
# in the dictionary. Normally, we need one to point to the 'CA'
_test_ssl_options = { 'certfile' : sys.prefix + '/var/jim/certs/cert.pem', 'keyfile': sys.prefix + '/var/jim/certs/key.pem' }

def get_player_id():
    return 42

class DynamicBaseHandler(tornado.web.RequestHandler):
    def finish_failure(self, err = None):
        retval = { 'result': 'failure', 'reason': err }
        self.finish(retval)

    def finish_success(self, args = {}):
        retval = { 'result': 'success' }
        retval.update(args)
        self.finish(retval)

    def get_args(self):
        try:
            args = self.request.query_arguments
            return args
        except:
            self.finish_failure("query parse error")
            return None

    def get_player_args(self):
        args = self.get_args()
        if args == None:
            return None
        try:
            first_name = args['first_name'][0]
        except:
            self.finish_failure("player fist name missing")
            return None
        try:
            last_name = args['last_name'][0]
        except:
            self.finish_failure("player last name missing")
            return None
        try:
            home_phone = args['home_phone'][0]
        except:
            home_phone = None
        try:
            cell_phone = args['cell_phone'][0]
        except:
            cell_phone = None
        try:
            work_phone = args['work_phone'][0]
        except:
            work_phone = None
        if not (home_phone or cell_phone or work_phone):
            self.finish_failure("at least one phone number is required")
            return None
        try:
            email = args['email'][0]
        except:
            self.finish_failure("e-mail is required")
            return None
        try:
            ladder = args['ladder'][0].lower()
        except:
            ladder = 'unranked'
        try:
            company = args['company'][0]
        except:
            self.finish_failure("company name is required")
            return None
        if not ladder in [ 'a', 'b', 'c', 'unranked', 'beginner' ]:
            self.finish_failure("invalid ladder category")
            return None
        try:
            initial_points_str = args['initial_points'][0]
        except:
            initial_points_str = '0'
        try:
            initial_points = int(initial_points_str)
        except:
            self.finish_failure("initial ladder points must be an integer")
            return None
        if initial_points < 0:
            self.finish_failure("initial ladder points cannot be negative")
            return None
        return {'name': first_name + ' ' + last_name,
                'email' : email,
                'home_phone' : home_phone,
                'work_phone' : work_phone,
                'cell_phone' : cell_phone,
                'company': company,
                'ladder': ladder,
                'initial_points': initial_points}

    def initialize(self):
        self.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
        self.set_header("Pragma", "no-cache")
        self.set_header("Expires", "0")

    def get_current_user(self):
        return self.get_secure_cookie('user')

class RootHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect('/login', permanent = True)

class LoginHandler(DynamicBaseHandler):
    def get(self):
        if self.current_user:
            self.redirect('/date')
        else:
            self.render('login.html')

    def post(self):
        # TODO: check the password here
        self.set_secure_cookie('user', self.get_argument('name'))
        self.redirect('/date')

class LogoutHandler(DynamicBaseHandler):
    def get(self):
        self.clear_cookie('user')
        self.redirect('/login')

class DateHandler(DynamicBaseHandler):
    def get(self):
        if self.current_user:
            name = tornado.escape.xhtml_escape(self.current_user)
        else:
            name = 'nobody'
        self.render('date.html',
                    date_string = str(datetime.datetime.now()),
                    user_string = name)

class AddPlayerHandler(DynamicBaseHandler):
    def get(self):
        player = self.get_player_args()<commit_msg>web: bail out if argument extraction fails<commit_after>#!/usr/bin/env python2

import tornado
import logging
import datetime
import os
import sys
import binascii
import rules
import util
from tornado import web, httpserver
from datetime import datetime

_http_server = None
_https_server = None
_template_root = sys.prefix + '/var/jim/templates'
_log = None
# This is default (test-only) certificate located in ./certs directory.
# default certificate is self-signed, so we don't have 'ca_cert' field
# in the dictionary. Normally, we need one to point to the 'CA'
_test_ssl_options = { 'certfile' : sys.prefix + '/var/jim/certs/cert.pem', 'keyfile': sys.prefix + '/var/jim/certs/key.pem' }

def get_player_id():
    return 42

class DynamicBaseHandler(tornado.web.RequestHandler):
    def finish_failure(self, err = None):
        retval = { 'result': 'failure', 'reason': err }
        self.finish(retval)

    def finish_success(self, args = {}):
        retval = { 'result': 'success' }
        retval.update(args)
        self.finish(retval)

    def get_args(self):
        try:
            args = self.request.query_arguments
            return args
        except:
            self.finish_failure("query parse error")
            return None

    def get_player_args(self):
        args = self.get_args()
        if args == None:
            return None
        try:
            first_name = args['first_name'][0]
        except:
            self.finish_failure("player fist name missing")
            return None
        try:
            last_name = args['last_name'][0]
        except:
            self.finish_failure("player last name missing")
            return None
        try:
            home_phone = args['home_phone'][0]
        except:
            home_phone = None
        try:
            cell_phone = args['cell_phone'][0]
        except:
            cell_phone = None
        try:
            work_phone = args['work_phone'][0]
        except:
            work_phone = None
        if not (home_phone or cell_phone or work_phone):
            self.finish_failure("at least one phone number is required")
            return None
        try:
            email = args['email'][0]
        except:
            self.finish_failure("e-mail is required")
            return None
        try:
            ladder = args['ladder'][0].lower()
        except:
            ladder = 'unranked'
        try:
            company = args['company'][0]
        except:
            self.finish_failure("company name is required")
            return None
        if not ladder in [ 'a', 'b', 'c', 'unranked', 'beginner' ]:
            self.finish_failure("invalid ladder category")
            return None
        try:
            initial_points_str = args['initial_points'][0]
        except:
            initial_points_str = '0'
        try:
            initial_points = int(initial_points_str)
        except:
            self.finish_failure("initial ladder points must be an integer")
            return None
        if initial_points < 0:
            self.finish_failure("initial ladder points cannot be negative")
            return None
        return {'name': first_name + ' ' + last_name,
                'email' : email,
                'home_phone' : home_phone,
                'work_phone' : work_phone,
                'cell_phone' : cell_phone,
                'company': company,
                'ladder': ladder,
                'initial_points': initial_points}

    def initialize(self):
        self.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
        self.set_header("Pragma", "no-cache")
        self.set_header("Expires", "0")

    def get_current_user(self):
        return self.get_secure_cookie('user')

class RootHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect('/login', permanent = True)

class LoginHandler(DynamicBaseHandler):
    def get(self):
        if self.current_user:
            self.redirect('/date')
        else:
            self.render('login.html')

    def post(self):
        # TODO: check the password here
        self.set_secure_cookie('user', self.get_argument('name'))
        self.redirect('/date')

class LogoutHandler(DynamicBaseHandler):
    def get(self):
        self.clear_cookie('user')
        self.redirect('/login')

class DateHandler(DynamicBaseHandler):
    def get(self):
        if self.current_user:
            name = tornado.escape.xhtml_escape(self.current_user)
        else:
            name = 'nobody'
        self.render('date.html',
                    date_string = str(datetime.datetime.now()),
                    user_string = name)

class AddPlayerHandler(DynamicBaseHandler):
    def get(self):
        player = self.get_player_args()
        if player == None:
            return
{'fraction_non_alphanumeric': 0.1049775644370239, 'fraction_numerical': 0.0033392465824898256, 'mean_word_length': 2.044472681067344, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291031', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2645, 'n_words': 671}