Fixed problem when entering invalid data while editing profile
This commit is contained in:
parent
b9467583b7
commit
d8f7d609aa
5 changed files with 44 additions and 8 deletions
31
app.py
31
app.py
|
|
@ -18,6 +18,8 @@ if sys.version_info[0] < 3:
|
||||||
arg_parser = argparse.ArgumentParser()
|
arg_parser = argparse.ArgumentParser()
|
||||||
arg_parser.add_argument('--norun', action='store_true',
|
arg_parser.add_argument('--norun', action='store_true',
|
||||||
help='Don\'t run the app. Useful for debugging.')
|
help='Don\'t run the app. Useful for debugging.')
|
||||||
|
arg_parser.add_argument('--debug', action='store_true',
|
||||||
|
help='Run the app in debug mode.')
|
||||||
arg_parser.add_argument('-p', '--port', type=int, default=5000,
|
arg_parser.add_argument('-p', '--port', type=int, default=5000,
|
||||||
help='The port where to run the app. Defaults to 5000')
|
help='The port where to run the app. Defaults to 5000')
|
||||||
|
|
||||||
|
|
@ -628,6 +630,20 @@ def edit(id):
|
||||||
#def confirm_delete(id):
|
#def confirm_delete(id):
|
||||||
# return render_template('confirm_delete.html')
|
# return render_template('confirm_delete.html')
|
||||||
|
|
||||||
|
# Workaround for problems related to invalid data.
|
||||||
|
# Without that, changes will be lost across requests.
|
||||||
|
def profile_checkpoint():
|
||||||
|
return UserProfile(
|
||||||
|
user=get_current_user(),
|
||||||
|
full_name=request.form['full_name'],
|
||||||
|
biography=request.form['biography'],
|
||||||
|
location=int(request.form['location']),
|
||||||
|
year=int(request.form['year'] if request.form.get('has_year') else '0'),
|
||||||
|
website=request.form['website'] or None,
|
||||||
|
instagram=request.form['instagram'] or None,
|
||||||
|
facebook=request.form['facebook'] or None
|
||||||
|
)
|
||||||
|
|
||||||
@app.route('/edit_profile/', methods=['GET', 'POST'])
|
@app.route('/edit_profile/', methods=['GET', 'POST'])
|
||||||
def edit_profile():
|
def edit_profile():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
|
@ -637,19 +653,26 @@ def edit_profile():
|
||||||
# prevent username to be set to empty
|
# prevent username to be set to empty
|
||||||
username = user.username
|
username = user.username
|
||||||
if username != user.username:
|
if username != user.username:
|
||||||
|
try:
|
||||||
User.update(username=username).where(User.id == user.id).execute()
|
User.update(username=username).where(User.id == user.id).execute()
|
||||||
|
except IntegrityError:
|
||||||
|
flash('That username is already taken')
|
||||||
|
return render_template('edit_profile.html', profile=profile_checkpoint())
|
||||||
website = request.form['website'].strip().replace(' ', '%20')
|
website = request.form['website'].strip().replace(' ', '%20')
|
||||||
if website and not validate_website(website):
|
if website and not validate_website(website):
|
||||||
flash('You should enter a valid URL.')
|
flash('You should enter a valid URL.')
|
||||||
return render_template('edit_profile.html')
|
return render_template('edit_profile.html', profile=profile_checkpoint())
|
||||||
location = int(request.form.get('location'))
|
location = int(request.form.get('location'))
|
||||||
if location == 0:
|
if location == 0:
|
||||||
location = None
|
location = None
|
||||||
UserProfile.update(
|
UserProfile.update(
|
||||||
full_name=request.form['full_name'] or username,
|
full_name=request.form['full_name'] or username,
|
||||||
biography=request.form['biography'],
|
biography=request.form['biography'],
|
||||||
|
year=request.form['year'] if request.form.get('has_year') else None,
|
||||||
|
location=location,
|
||||||
website=website,
|
website=website,
|
||||||
location=location
|
instagram=request.form['instagram'],
|
||||||
|
facebook=request.form['facebook']
|
||||||
).where(UserProfile.user == user).execute()
|
).where(UserProfile.user == user).execute()
|
||||||
return redirect(url_for('user_detail', username=username))
|
return redirect(url_for('user_detail', username=username))
|
||||||
return render_template('edit_profile.html')
|
return render_template('edit_profile.html')
|
||||||
|
|
@ -713,7 +736,7 @@ def username_availability(username):
|
||||||
def location_search(name):
|
def location_search(name):
|
||||||
results = []
|
results = []
|
||||||
for key, value in locations.items():
|
for key, value in locations.items():
|
||||||
if value.startswith(name):
|
if value.lower().startswith(name.lower()):
|
||||||
results.append({'value': key, 'display': value})
|
results.append({'value': key, 'display': value})
|
||||||
return jsonify({'results': results})
|
return jsonify({'results': results})
|
||||||
|
|
||||||
|
|
@ -770,4 +793,4 @@ if __name__ == '__main__':
|
||||||
args = arg_parser.parse_args()
|
args = arg_parser.parse_args()
|
||||||
create_tables()
|
create_tables()
|
||||||
if not args.norun:
|
if not args.norun:
|
||||||
app.run(port=args.port)
|
app.run(port=args.port, debug=args.debug)
|
||||||
|
|
|
||||||
BIN
static/.style.css.swp
Normal file
BIN
static/.style.css.swp
Normal file
Binary file not shown.
|
|
@ -19,6 +19,7 @@ body{margin:0}
|
||||||
.message-options{display:none}
|
.message-options{display:none}
|
||||||
.create_text{width:100%;height:8em}
|
.create_text{width:100%;height:8em}
|
||||||
.biography_text{height:4em}
|
.biography_text{height:4em}
|
||||||
|
.before-toggle:not(:checked) + input{display:none}
|
||||||
.follow_button,input[type="submit"]{background-color:#ff3018;color:white;border-radius:3px;border:1px solid #ff3018}
|
.follow_button,input[type="submit"]{background-color:#ff3018;color:white;border-radius:3px;border:1px solid #ff3018}
|
||||||
.follow_button.following{background-color:transparent;color:#ff3018;border-color:#ff3018}
|
.follow_button.following{background-color:transparent;color:#ff3018;border-color:#ff3018}
|
||||||
.copyright{font-size:smaller;text-align:center;color:#808080}
|
.copyright{font-size:smaller;text-align:center;color:#808080}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,8 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
<p class="copyright">© 2019 Sakuragasaki46.
|
<p class="copyright">© 2019 Sakuragasaki46.
|
||||||
<a href="/about/">About</a></p>
|
<a href="/about/">About</a> - <a href="/terms/">Terms</a> -
|
||||||
|
<a href="/privacy/">Privacy</a></p>
|
||||||
</div>
|
</div>
|
||||||
<script src="/static/lib.js"></script>
|
<script src="/static/lib.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,26 @@
|
||||||
<dl>
|
<dl>
|
||||||
<dt>Username:</dt>
|
<dt>Username:</dt>
|
||||||
<dd><input type="text" class="username-input" name="username" required value="{{ current_user.username }}" autocomplete="off"></dd>
|
<dd><input type="text" class="username-input" name="username" required value="{{ current_user.username }}" autocomplete="off"></dd>
|
||||||
|
{% if not profile %}
|
||||||
{% set profile = current_user.profile %}
|
{% set profile = current_user.profile %}
|
||||||
|
{% endif %}
|
||||||
<dt>Full name:</dt>
|
<dt>Full name:</dt>
|
||||||
<dd><input type="text" name="full_name" value="{{ profile.full_name }}"></dd>
|
<dd><input type="text" name="full_name" value="{{ profile.full_name }}"></dd>
|
||||||
<dt>Biography:</dt>
|
<dt>Biography:</dt>
|
||||||
<dd><textarea class="biography_text" name="biography">{{ profile.biography }}</textarea></dd>
|
<dd><textarea class="biography_text" name="biography">{{ profile.biography }}</textarea></dd>
|
||||||
<dt>Location:</dt>
|
<dt>Location:</dt>
|
||||||
<dd>{% include "includes/location_selector.html" %}</dd>
|
<dd>{% include "includes/location_selector.html" %}</dd>
|
||||||
|
<dt>Generation:</dt>
|
||||||
|
<dd>
|
||||||
|
<input type="checkbox" class="before-toggle" name="has_year" value="1" {% if profile.year %}checked{% endif %}>
|
||||||
|
<input type="number" name="year" value="{{ profile.year or 2000 }}">
|
||||||
|
</dd>
|
||||||
<dt>Website:</dt>
|
<dt>Website:</dt>
|
||||||
<dd><input type="text" name="website" value="{{ profile.website }}"></dd>
|
<dd><input type="text" name="website" value="{{ profile.website or '' }}"></dd>
|
||||||
|
<dt>Instagram:</dt>
|
||||||
|
<dd><input type="text" name="instagram" value="{{ profile.instagram or '' }}"></dd>
|
||||||
|
<dt>Facebook:</dt>
|
||||||
|
<dd><input type="text" name="facebook" value="{{ profile.facebook or '' }}"></dd>
|
||||||
<dd><input type="submit" value="Save"></dd>
|
<dd><input type="submit" value="Save"></dd>
|
||||||
</dl>
|
</dl>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue