Commits

Cédric Bonhomme committed ad56b8e

It is now possible to update information about a user (first name, last name, email and password).

Comments (0)

Files changed (3)

pyaggr3g470r/forms.py

     def validate(self):
         if not Form.validate(self):
             return False
+        return True
+
+class ProfileForm(Form):
+    firstname = TextField("First name", [validators.Required("Please enter your first name.")])
+    lastname = TextField("Last name", [validators.Required("Please enter your last name.")])
+    email = TextField("Email", [validators.Required("Please enter your email.")])
+    password = TextField("Password", [validators.Required("Please enter your password.")])
+    submit = SubmitField("Save")
+
+    def __init__(self, *args, **kwargs):
+        Form.__init__(self, *args, **kwargs)
+
+    def validate(self):
+        if not Form.validate(self):
+            return False
         return True

pyaggr3g470r/templates/profile.html

+{% extends "layout.html" %}
+{% block content %}
+<div class="container">
+    <div class="jumbotron">
+        <form action="" method="post" name="save">
+            {{ form.hidden_tag() }}
+
+            {{ form.firstname.label }}
+            {{ form.firstname(class_="form-control") }} {% for error in form.firstname.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
+
+            {{ form.lastname.label }}
+            {{ form.lastname(class_="form-control") }} {% for error in form.lastname.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
+
+            {{ form.email.label }}
+            {{ form.email(class_="form-control") }} {% for error in form.email.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
+
+            {{ form.password.label }}
+            {{ form.password(class_="form-control") }} {% for error in form.password.errors %} <span style="color: red;">{{ error }}<br /></span>{% endfor %}
+
+            {{ form.submit(class_="btn") }}
+        </form>
+    </div>
+</div><!-- /.container -->
+{% endblock %}

pyaggr3g470r/views.py

 from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user, AnonymousUserMixin
 from collections import defaultdict
 
-from forms import SigninForm, AddFeedForm
+from forms import SigninForm, AddFeedForm, ProfileForm
 from pyaggr3g470r import app, db
 
 
             user.feeds.remove(feed)
             user.save()
             return redirect(url_for('home'))
+
+@app.route('/profile/', methods=['GET', 'POST'])
+@login_required
+def profile():
+    """
+    Edit the profile of the user.
+    """
+    user = models.User.objects(email=g.user.email).first()
+    form = ProfileForm()
+
+    if request.method == 'POST':
+        if form.validate() == False:
+            return render_template('profile.html', form=form)
+
+        form.populate_obj(user)
+        if form.password.data != "":
+            user.set_password(form.password.data)
+        user.save()
+        flash('User "' + user.firstname + '" successfully updated', 'success')
+        return redirect('/profile/')
+
+    if request.method == 'GET':
+        form = ProfileForm(obj=user)
+        return render_template('profile.html', form=form)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.