-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnames.py
executable file
·164 lines (133 loc) · 4.56 KB
/
names.py
1
2
3
4
5
6
7
8
9
10
11
12
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
from flask import Flask, flash, render_template, redirect, request, url_for
from wtforms import Form, TextField, validators
import csv
import logging
import random
import sys
CSV_FILE = 'names.csv'
CSV_HEADERS = (
'Firstname',
'Lastname',
'Firstlastfunny',
'Resolution',
'Explanation',
'Author',
'Comment')
ERROR_NO_NAME_FOUND = 'Kein Name gefunden'
ERROR_EMPTY_SEARCH = \
'Ich brauch mindestens einen Vor- oder Nachnamen'
logging.basicConfig(level=logging.WARN)
class Name:
def __init__(self, firstname, lastname, firstlastfunny):
self.firstname = firstname
self.lastname = lastname
self.firstlastfunny = firstlastfunny
def get_puzzle_name(self):
"""
Get the name in the form that lets you puzzle a bit what's funny about
it. For example, "Sigrid Top" is the puzzle form of "Top, Sigrid".
"""
if self.firstlastfunny:
s = self.lastname + ', ' + self.firstname
else:
s = self.firstname + ' ' + self.lastname
return s
def get_funny_name(self):
"""
Get the name in it's (presumably) funny form. For example,
"Top, Sigrid" is the funny form of "Sigrid Top".
"""
if self.firstlastfunny:
s = self.firstname + self.lastname
else:
s = self.lastname + self.firstname
return s.capitalize()
@staticmethod
def from_csv(f):
"""
Returns a list of names, read from a CSV file.
"""
dr = csv.DictReader(f, fieldnames=CSV_HEADERS, delimiter=';')
names = []
for row in dr:
logging.debug('Got CSV row: %s' % row)
name = Name(
row['Firstname'],
row['Lastname'],
row['Firstlastfunny'])
# The fields are not mandatory, so reading may fail gracefully
# with a default value.
name.resolution = row.get('Resolution')
name.explanation = row.get('Explanation')
name.author = row.get('Author')
name.comment = row.get('Comment')
logging.debug(
'Read name with firstname %s and lastname %s' %
(name.firstname, name.lastname))
names.append(name)
return names
@staticmethod
def names_from_csv(f):
names = Name.from_csv(f)
# Ignore the header, we are only interested in the data.
return names[1:]
@staticmethod
def search_name(names, firstname, lastname):
for name in names:
if ((firstname and firstname in name.firstname) or
(lastname and lastname in name.lastname)):
return name
return None
#
# Initialization.
#
app = Flask(__name__)
app.config['SECRET_KEY'] = 'totally secret key'
with open(CSV_FILE, encoding='utf-8') as csv_file:
names = Name.names_from_csv(csv_file)
@app.route('/name')
def name():
firstname = request.args.get('firstname')
lastname = request.args.get('lastname')
name = Name.search_name(names, firstname, lastname)
return render_template('name.html', name=name)
@app.route('/')
def random_name():
name = random.choice(names)
return render_template('name.html', name=name)
class SearchForm(Form):
firstname = TextField('Firstname:', validators=[validators.Optional()])
lastname = TextField('Lastname:', validators=[validators.Optional()])
error = ''
def validate(self):
if not super(SearchForm, self).validate():
return False
if not self.firstname.data and not self.lastname.data:
msg = ERROR_EMPTY_SEARCH
self.error = msg
return False
return True
@app.route('/search', methods=['GET', 'POST'])
def search_name():
form = SearchForm(request.form)
print(form.errors)
if request.method == 'POST':
if form.validate():
firstname = request.form['firstname']
lastname = request.form['lastname']
name = Name.search_name(names, firstname, lastname)
if not name:
flash(ERROR_NO_NAME_FOUND)
return redirect(url_for('search_name'))
else:
return render_template('name.html', name=name)
else:
flash(form.error)
return redirect(url_for('search_name'))
return render_template('search.html', form=form)
@app.route('/all')
def all_names():
return render_template('names.html', names=names)
if __name__ == '__main__':
app.run(host='0.0.0.0')