├── static └── anketa.css ├── .gitignore ├── README.md ├── LICENSE ├── templates ├── index.html └── base.html └── anketa.py /static/anketa.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding-top: 2rem; 3 | padding-bottom: 2rem; 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | build/ 3 | dist/ 4 | *.egg-info/ 5 | .cache 6 | *.log 7 | venv/ 8 | *.db 9 | *.journal 10 | *.db-journal 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | db-workshop-web-app 2 | =================== 3 | 4 | Simple website made using [Flask](http://flask.pocoo.org/), [Bootstrap](https://v4-alpha.getbootstrap.com/getting-started/introduction/) and [SQLite3](https://docs.python.org/3/library/sqlite3.html). 5 | 6 | 7 | Screenshot 8 | ---------- 9 | 10 | ![Screenshot](https://s3-eu-west-1.amazonaws.com/messa-shared-files/2017/03/anketa-screenshot-1.png) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Petr Messner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | {% block content %} 3 | 4 |

Anketa

5 | 6 |

O čem by měl být některý příští PyWorking?

7 | 8 |
9 | 10 | 11 |
12 | 13 | 14 | 15 | {% for suggestion in suggestions %} 16 | 17 | 20 | 36 | 37 | 38 | {% endfor %} 39 | 40 |
18 | {{ suggestion.vote_count }} 19 | 21 | {# tlačítka pro hlasování - kliknutí odešle form #} 22 |
23 | 24 |
25 | {# tlačítko pro přidání hlasu #} 26 | 29 | {# tlačítko pro odebrání hlasu #} 30 | 33 |
34 |
35 |
{{ suggestion.title }}
41 | 42 | {% endblock %} 43 | -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | {# 2 | 3 | Skoro všechno v tomto souboru pochází z Bootstrap Starter template: 4 | 5 | https://v4-alpha.getbootstrap.com/getting-started/introduction/#starter-template 6 | 7 | -#} 8 | 9 | 10 | 11 | 12 | 13 | 14 | {# font awesome pro ikonky #} 15 | 16 | {# aplikační styly #} 17 | 18 | Anketa 19 | 20 | 21 |
22 | {% block content %}{% endblock %} 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /anketa.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from datetime import datetime 4 | import flask 5 | import sqlite3 6 | 7 | # Flask docs: http://flask.pocoo.org/docs/0.12/ 8 | # SQLite docs: https://docs.python.org/3/library/sqlite3.html 9 | 10 | app = flask.Flask(__name__) 11 | 12 | 13 | @app.route('/') 14 | def index(): 15 | ''' 16 | Titulní strana webovky 17 | ''' 18 | conn = get_conn() 19 | prepare_schema(conn) 20 | return flask.render_template('index.html', 21 | suggestions=list_suggestions(conn)) 22 | 23 | 24 | @app.route('/add-suggestion', methods=['POST']) 25 | def add_suggestion(): 26 | ''' 27 | Vložení návrhu (suggestion). 28 | Redirectuje zpět na index. 29 | ''' 30 | title = flask.request.form['suggestion'] 31 | conn = get_conn() 32 | insert_suggestion(conn, title, None) 33 | # https://en.wikipedia.org/wiki/Post/Redirect/Get 34 | return flask.redirect('/') 35 | 36 | 37 | @app.route('/vote', methods=['POST']) 38 | def vote(): 39 | ''' 40 | Vložení hlasu (vote). 41 | Redirectuje zpět na index. 42 | ''' 43 | sug_id = flask.request.form['suggestion_id'] 44 | action = flask.request.form['action'] 45 | conn = get_conn() 46 | if action == 'upvote': 47 | is_upvote = True 48 | elif action == 'downvote': 49 | is_upvote = False 50 | else: 51 | raise Exception('Unknown action {!r}'.format(action)) 52 | insert_vote(conn, sug_id, None, is_upvote) 53 | return flask.redirect('/') 54 | 55 | 56 | def get_conn(): 57 | return sqlite3.connect('anketa.db') 58 | 59 | 60 | def prepare_schema(conn): 61 | ''' 62 | Vytvoření tabulek, se kterými tato aplikace pracuje. 63 | Pokud tabulky už existují, tak se nic neděje. 64 | ''' 65 | c = conn.cursor() 66 | # https://www.sqlite.org/lang_createtable.html 67 | # TODO 68 | # c.execute('CREATE TABLE ...') 69 | 70 | 71 | def insert_suggestion(conn, title, cookie, date=None): 72 | # TODO 73 | pass 74 | 75 | 76 | def insert_vote(conn, suggestion_id, cookie, upvote, date=None): 77 | ''' 78 | Vloží vote pro nějakou suggestion do DB. 79 | ''' 80 | # TODO 81 | pass 82 | 83 | 84 | def list_suggestions(conn): 85 | ''' 86 | Vrátí seznam suggestions z DB. 87 | Návratovou hodnotou je list dictů. 88 | ''' 89 | # TODO 90 | return [ 91 | { 92 | 'id': 1, 93 | 'title': 'Tvorba webu', 94 | 'vote_count': 10, 95 | }, { 96 | 'id': 1, 97 | 'title': 'Statistika', 98 | 'vote_count': 8, 99 | }, { 100 | 'id': 1, 101 | 'title': 'Lineární algebra', 102 | 'vote_count': 5, 103 | }, 104 | ] 105 | 106 | 107 | if __name__ == '__main__': 108 | app.run(debug=True) 109 | --------------------------------------------------------------------------------