[odrs-web] Use the email address as the account username



commit a705eb31249e537a200832b5cb3cc905ccd14939
Author: Richard Hughes <richard hughsie com>
Date:   Mon Jul 1 20:06:39 2019 +0100

    Use the email address as the account username

 Dockerfile                                    |  5 +++++
 app_data/README.md                            |  4 +++-
 app_data/migrations/versions/1b966aab67a1_.py | 32 +++++++++++++++++++++++++++
 app_data/odrs/dbutils.py                      | 10 ++++-----
 app_data/odrs/models.py                       |  4 +---
 app_data/odrs/templates/modadmin.html         |  4 ----
 app_data/odrs/templates/mods.html             |  6 -----
 app_data/odrs/tests/odrs_test.py              | 15 ++++++-------
 app_data/odrs/views_admin.py                  | 19 +++++++---------
 9 files changed, 61 insertions(+), 38 deletions(-)
---
diff --git a/Dockerfile b/Dockerfile
index 15c4b2d..3a8f7b9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -31,6 +31,11 @@ WORKDIR ${ODRS_HOME}
 
 COPY app_data ${ODRS_HOME}
 
+RUN LANG=en_US.utf8 \
+    FLASK_APP=odrs/__init__.py \
+    SQLALCHEMY_TRACK_MODIFICATIONS=False \
+    flask db upgrade
+
 RUN python36 /opt/app-root/src/cron.py ratings /opt/app-root/src/odrs/static/ratings.json
 
 RUN chown -R 1000310000:0 ${ODRS_HOME} && \
diff --git a/app_data/README.md b/app_data/README.md
index 916b8dc..de7874e 100644
--- a/app_data/README.md
+++ b/app_data/README.md
@@ -47,7 +47,9 @@ Email me or grab me on IRC (`hughsie@freenode`).
 
 ## How to build the docker image?
 
-    podman build . --build-arg ODRS_REVIEWS_SECRET=1
+    podman build . \
+        --build-arg ODRS_REVIEWS_SECRET=1 \
+        --build-arg SQLALCHEMY_DATABASE_URI=mysql+pymysql://test:test@localhost/odrs?charset=utf8mb4
     podman images
     podman run --env-file env.cfg <image>
     podman run --env-file env.cfg -it --entrypoint /bin/bash <image>
diff --git a/app_data/migrations/versions/1b966aab67a1_.py b/app_data/migrations/versions/1b966aab67a1_.py
new file mode 100644
index 0000000..0e70b76
--- /dev/null
+++ b/app_data/migrations/versions/1b966aab67a1_.py
@@ -0,0 +1,32 @@
+"""
+
+Revision ID: 1b966aab67a1
+Revises: fd438e12c80c
+Create Date: 2019-07-01 19:44:32.916028
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '1b966aab67a1'
+down_revision = 'b8243269e9cf'
+
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects import mysql
+
+from odrs import db
+
+class OldModerator(db.Model):
+    __tablename__ = 'moderators'
+    __table_args__ = {'mysql_character_set': 'utf8mb4',
+                      'extend_existing': True}
+    email = db.Column(db.Text)
+
+def upgrade():
+    for mod in db.session.query(OldModerator).all():
+        mod.username = mod.email
+    db.session.commit()
+    op.drop_column('moderators', 'email')
+
+def downgrade():
+    op.add_column('moderators', sa.Column('email', mysql.MEDIUMTEXT(collation='utf8mb4_unicode_ci'), 
nullable=True))
diff --git a/app_data/odrs/dbutils.py b/app_data/odrs/dbutils.py
index b6555ac..ef25f03 100644
--- a/app_data/odrs/dbutils.py
+++ b/app_data/odrs/dbutils.py
@@ -12,11 +12,11 @@ def init_db(db):
 
     # ensure admin user exists
     from .models import Moderator
-    if not db.session.query(Moderator).filter(Moderator.username == 'admin').first():
-        u = Moderator(username='admin')
-        u.password = 'Pa$$w0rd'
-        u.is_admin = True
-        db.session.add(u)
+    if not db.session.query(Moderator).filter(Moderator.username == 'admin test com').first():
+        mod = Moderator(username='admin test com')
+        mod.password = 'Pa$$w0rd'
+        mod.is_admin = True
+        db.session.add(mod)
         db.session.commit()
 
 def drop_db(db):
diff --git a/app_data/odrs/models.py b/app_data/odrs/models.py
index 00476ae..ec174af 100644
--- a/app_data/odrs/models.py
+++ b/app_data/odrs/models.py
@@ -183,16 +183,14 @@ class Moderator(db.Model):
     username = Column(Text)
     password_hash = Column('password', Text)
     display_name = Column(Text)
-    email = Column(Text)
     is_enabled = Column(Integer, default=0)
     is_admin = Column(Integer, default=0)
     user_hash = Column(Text)
     locales = Column(Text)
 
-    def __init__(self, username=None, password=None, display_name=None, email=None):
+    def __init__(self, username=None, password=None, display_name=None):
         self.username = username
         self.display_name = display_name
-        self.email = email
         self.is_enabled = False
         self.is_admin = False
         self.user_hash = None
diff --git a/app_data/odrs/templates/modadmin.html b/app_data/odrs/templates/modadmin.html
index 7fb8238..0151da3 100644
--- a/app_data/odrs/templates/modadmin.html
+++ b/app_data/odrs/templates/modadmin.html
@@ -19,10 +19,6 @@
       <td>Languages Spoken<br/>(e.g. <code>en,fr,pl</code> or <code>*</code>):</td>
       <td><input type="text" class="form-control" name="locales" value="{{u.locales}}" required></td>
     </tr>
-    <tr>
-      <td>Contact Email:</td>
-      <td><input type="text" class="form-control" name="email" value="{{u.email}}" required></td>
-    </tr>
     <tr>
       <td>New Password<br/>(optional):</td>
       <td><input type="password" class="form-control" name="password"></td>
diff --git a/app_data/odrs/templates/mods.html b/app_data/odrs/templates/mods.html
index 24ba91c..7b14112 100644
--- a/app_data/odrs/templates/mods.html
+++ b/app_data/odrs/templates/mods.html
@@ -9,7 +9,6 @@
     <th>ID</th>
     <th>Username</th>
     <th>Display Name</th>
-    <th>Email</th>
     <th>Is Enabled</th>
     <th>Is Admin</th>
     <th>User Hashes</th>
@@ -19,7 +18,6 @@
     <td>{{u.moderator_id}}</td>
     <td><a href="{{url_for('.odrs_moderator_show', moderator_id=u.moderator_id)}}">{{u.username}}</a></td>
     <td>{{u.display_name}}</td>
-    <td>{{u.email}}</td>
     <td>{{u.is_enabled}}</td>
     <td>{{u.is_admin}}</td>
     <td>{{u.user_hash}}</td>
@@ -42,10 +40,6 @@
     <th>Display Name:</th>
     <td><input type="text" class="form-control" name="display_name" required></td>
   </tr>
-  <tr>
-    <th>Contact Email:</th>
-    <td><input type="text" class="form-control" name="email" required></td>
-  </tr>
 </table>
   <button class="btn btn-action btn-large" type="submit">Add</button>
 </form>
diff --git a/app_data/odrs/tests/odrs_test.py b/app_data/odrs/tests/odrs_test.py
index 285c097..b3d5241 100644
--- a/app_data/odrs/tests/odrs_test.py
+++ b/app_data/odrs/tests/odrs_test.py
@@ -73,7 +73,7 @@ class OdrsTest(unittest.TestCase):
     def _logout(self):
         return self.app.get('/logout', follow_redirects=True)
 
-    def login(self, username='admin', password='Pa$$w0rd'):
+    def login(self, username='admin test com', password='Pa$$w0rd'):
         rv = self._login(username, password)
         assert b'Logged in' in rv.data, rv.data
         assert b'/admin/show/reported' in rv.data, rv.data
@@ -151,13 +151,12 @@ class OdrsTest(unittest.TestCase):
         rv = self.app.get('/admin/review/1', follow_redirects=True)
         assert b'No review with that ID' in rv.data, rv.data
 
-    def _admin_moderator_add(self, username='dave', password='foobarbaz123.', email='dave dave com'):
+    def _admin_moderator_add(self, username='dave dave com', password='foobarbaz123.'):
 
         return self.app.post('/admin/moderator/add', data=dict(
             password_new=password,
             username_new=username,
             display_name='Dave',
-            email=email,
         ), follow_redirects=True)
 
     def test_admin_add_moderator(self):
@@ -171,7 +170,7 @@ class OdrsTest(unittest.TestCase):
         assert b'The password is too short' in rv.data, rv.data
         rv = self._admin_moderator_add(password='foobarbaz')
         assert b'requires at least one non-alphanumeric' in rv.data, rv.data
-        rv = self._admin_moderator_add(email='foo')
+        rv = self._admin_moderator_add(username='foo')
         assert b'Invalid email address' in rv.data, rv.data
 
         # good values
@@ -226,16 +225,16 @@ class OdrsTest(unittest.TestCase):
     def test_login_logout(self):
 
         # test logging in and out
-        rv = self._login('admin', 'Pa$$w0rd')
+        rv = self._login('admin test com', 'Pa$$w0rd')
         assert b'/admin/show/reported' in rv.data, rv.data
         rv = self._logout()
-        rv = self._login('admin', 'Pa$$w0rd')
+        rv = self._login('admin test com', 'Pa$$w0rd')
         assert b'/admin/show/reported' in rv.data, rv.data
         rv = self._logout()
         assert b'/admin/show/reported' not in rv.data, rv.data
-        rv = self._login('adminx', 'default')
+        rv = self._login('FAILED test com', 'default')
         assert b'Incorrect username' in rv.data, rv.data
-        rv = self._login('admin', 'defaultx')
+        rv = self._login('admin test com', 'defaultx')
         assert b'Incorrect password' in rv.data, rv.data
 
     @staticmethod
diff --git a/app_data/odrs/views_admin.py b/app_data/odrs/views_admin.py
index b09fe98..d39f04d 100644
--- a/app_data/odrs/views_admin.py
+++ b/app_data/odrs/views_admin.py
@@ -315,7 +315,6 @@ def admin_user_ban(user_hash):
     if not current_user.is_admin:
         flash('Unable to ban user as non-admin', 'error')
         return redirect(url_for('.odrs_index'))
-    print(db.session.query(User).all())
     user = db.session.query(User).filter(User.user_hash == user_hash).first()
     if not user:
         flash('No user with that user_hash')
@@ -528,7 +527,7 @@ def admin_moderator_add():
         flash('Unable to add moderator as non-admin', 'error')
         return redirect(url_for('.odrs_index'))
 
-    for key in ['username_new', 'password_new', 'display_name', 'email']:
+    for key in ['username_new', 'password_new', 'display_name']:
         if not key in request.form:
             flash('Unable to add moderator as {} missing'.format(key), 'error')
             return redirect(url_for('.odrs_index'))
@@ -542,9 +541,12 @@ def admin_moderator_add():
     if not _password_check(password):
         return redirect(url_for('.admin_moderator_show_all'))
 
-    # verify email
-    email = request.form['email']
-    if not _email_check(email):
+    # verify username
+    username_new = request.form['username_new']
+    if len(username_new) < 3:
+        flash('Username invalid', 'warning')
+        return redirect(url_for('.admin_moderator_show_all'))
+    if not _email_check(username_new):
         flash('Invalid email address', 'warning')
         return redirect(url_for('.admin_moderator_show_all'))
 
@@ -555,11 +557,7 @@ def admin_moderator_add():
         return redirect(url_for('.admin_moderator_show_all'))
 
     # verify username
-    username_new = request.form['username_new']
-    if len(username_new) < 3:
-        flash('Username invalid', 'warning')
-        return redirect(url_for('.admin_moderator_show_all'))
-    db.session.add(Moderator(username_new, password, display_name, email))
+    db.session.add(Moderator(username_new, password, display_name))
     db.session.commit()
     flash('Added user')
     return redirect(url_for('.admin_moderator_show_all'))
@@ -650,7 +648,6 @@ def admin_user_modify_by_admin(moderator_id):
     # set each thing in turn
     mod.is_enabled = 'is_enabled' in request.form
     for key in ['display_name',
-                'email',
                 'password',
                 'user_hash',
                 'locales']:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]