WebApp

Check-in [d98be4905c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Exceptions partially implemented
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:d98be4905c276396d1a8a18f8bcd2e86a4c82cd652db9815973ba02684ddf3c6
User & Date: sebpc 2018-05-27 16:25:36
Context
2018-05-28
15:59
Rest of exception handling has been implemented check-in: 384da38890 user: sebpc tags: trunk
2018-05-27
16:25
Exceptions partially implemented check-in: d98be4905c user: sebpc tags: trunk
2018-05-26
15:54
viewlog access protected by decorator check-in: 209eecc6f8 user: sebpc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to DBcm.py.

1



2
3
4
5
6

7
8
9


10
11
12
13
import sqlite3




class UseDatabase:
    def __init__(self, database) -> None:
        self.database = database
    def __enter__(self):

        self.conn = sqlite3.connect(self.database)
        self.cursor = self.conn.cursor()
        return self.cursor


    def __exit__(self, exc_type, exc_value, exc_trace) -> None:
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

>
>
>





>
|
|
|
>
>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import sqlite3

class ConnectionError(Exception):
    pass

class UseDatabase:
    def __init__(self, database) -> None:
        self.database = database
    def __enter__(self):
        try:
            self.conn = sqlite3.connect(self.database)
            self.cursor = self.conn.cursor()
            return self.cursor
        except sqlite3.InterfaceError as err:
            raise ConnectionError(err)
    def __exit__(self, exc_type, exc_value, exc_trace) -> None:
        self.conn.commit()
        self.cursor.close()
        self.conn.close()

Deleted __pycache__/DBcm.cpython-36.pyc.

cannot compute difference between binary files

Deleted __pycache__/vsearch.cpython-36.pyc.

cannot compute difference between binary files

Changes to vsearch4web.py.

1
2
3
4

5
6
7
8
9
10
11
..
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

35
36
37
38
39
40

41


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
64
65
66


67
68
69
from flask import Flask, render_template, request, escape, session
from vsearch import search4letters
from DBcm import UseDatabase
from checker import check_logged_in

app = Flask(__name__)

app.secret_key = 'Moj$3kret'

db = 'vsearchlog.db'

@app.route('/login')
................................................................................
    session['logged_in'] = True
    return 'Jestes teraz zalogowany'

@app.route('/logout')
def logout():
    session.pop('logged_in')
    return 'Zostales wylogowany'


def log_request(req: 'flask_request', res: str) -> None:
    with UseDatabase(db) as cursor:
        _SQL = """insert into log
        (phrase, letters, ip, browser_string, results)
        values
        (?, ?, ?, ?, ?);"""

        
        cursor.execute(_SQL, (req.form['phrase'],
                              req.form['letters'],
                              req.remote_addr,
                              req.user_agent.browser,
                              res,))


@app.route('/search4', methods=['POST'])
def  do_search() -> 'html':
	phrase = request.form['phrase']
	letters = request.form['letters']
	title = "Oto twoj wynik"
	result = str(search4letters(phrase, letters))

	log_request(request, result)


	return render_template('results.html',
		the_phrase = phrase,
		the_letters = letters,
		the_title = title,
		the_results = result,)


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
	return render_template('entry.html', the_title='Welcome to search4letters on the web')

@app.route('/viewlog')
@check_logged_in
def view_the_log() -> str:

    with UseDatabase(db) as cursor:
        _SQL = """select phrase, letters, ip, browser_string, results
        from log;"""
        cursor.execute(_SQL)
        contents = cursor.fetchall()
    titles = ('Phraise', 'Letters', 'Remote addr', 'User_agent', 'Results')
    return render_template('viewlog.html',
                               the_title = 'View Log',
                               the_row_titles = titles,
                               the_data = contents,)



#if '__name__' == '__main__':
app.run(debug=True)


|

>







 







>







<
<






>

|
|
|
|
|
>
|
>
>
|
|













>
|
|
|
|
|
|
|
|
|
|
>
>



1
2
3
4
5
6
7
8
9
10
11
12
..
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, render_template, request, escape, session
from vsearch import search4letters
from DBcm import UseDatabase, ConnectionError
from checker import check_logged_in

app = Flask(__name__)

app.secret_key = 'Moj$3kret'

db = 'vsearchlog.db'

@app.route('/login')
................................................................................
    session['logged_in'] = True
    return 'Jestes teraz zalogowany'

@app.route('/logout')
def logout():
    session.pop('logged_in')
    return 'Zostales wylogowany'


def log_request(req: 'flask_request', res: str) -> None:
    with UseDatabase(db) as cursor:
        _SQL = """insert into log
        (phrase, letters, ip, browser_string, results)
        values
        (?, ?, ?, ?, ?);"""


        cursor.execute(_SQL, (req.form['phrase'],
                              req.form['letters'],
                              req.remote_addr,
                              req.user_agent.browser,
                              res,))


@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    title = "Oto twoj wynik"
    result = str(search4letters(phrase, letters))
    try:
        log_request(request, result)
    except Exception as err:
        print('***Login failed with this error: ' + str(err))
    return render_template('results.html',
        the_phrase = phrase,
		the_letters = letters,
		the_title = title,
		the_results = result,)


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
	return render_template('entry.html', the_title='Welcome to search4letters on the web')

@app.route('/viewlog')
@check_logged_in
def view_the_log() -> str:
    try:
        with UseDatabase(db) as cursor:
            _SQL = """select phrase, letters, ip, browser_string, results
            from log;"""
            cursor.execute(_SQL)
            contents = cursor.fetchall()
        titles = ('Phraise', 'Letters', 'Remote addr', 'User_agent', 'Results')
        return render_template('viewlog.html',
                                   the_title = 'View Log',
                                   the_row_titles = titles,
                                   the_data = contents,)
    except ConnectionError as err:
        print('Is your database switched on? Error: ' + str(err))

#if '__name__' == '__main__':
app.run(debug=True)