Explorar el Código

add members

config file to enable/disable registration and other future admin settings
cube hace 6 días
padre
commit
e3d6613bd4

+ 21
- 1
README.md Ver fichero

@@ -7,4 +7,24 @@ flask app for plurals to publicly share member lists
7 7
 - after cloning, run `py -3 -m venv .venv` in the root directory and then `.venv\Scripts\activate`
8 8
 - then `pip install Flask` inside the virtual env
9 9
 - you might also need to init a database, so use `flask --app myriad init-db`
10
-- to start the site use `flask --app myriad run --debug`
10
+- to start the site use `flask --app myriad run --debug`
11
+
12
+# prod set up
13
+
14
+- not ready yet
15
+
16
+# config
17
+
18
+- create `config.py` in the instance folder and customise the following settings to your needs
19
+```
20
+REGISTRATION: False # only set to True if in a development situation, or to create your first user
21
+```
22
+
23
+# usage
24
+
25
+- the software here is free to use, and there's no requirement to link back
26
+- edit the styles and functionality to suit your needs. i'm sure some of you out there are far better with CSS than I am 
27
+
28
+# dependencies
29
+
30
+- Flask

+ 6
- 7
myriad/__init__.py Ver fichero

@@ -11,15 +11,11 @@ def create_app(test_config=None):
11 11
         DATABASE=os.path.join(app.instance_path, 'database.sqlite'),
12 12
     )
13 13
 
14
-    if test_config is None:
15
-        # load the instance config, if it exists, when not testing
16
-        app.config.from_pyfile('config.py', silent=True)
17
-    else:
18
-        # load the test config if passed in
19
-        app.config.from_mapping(test_config)
20
-
21 14
     # ensure the instance folder exists
22 15
     os.makedirs(app.instance_path, exist_ok=True)
16
+
17
+    app.config.from_pyfile('config.py')
18
+    #print(app.config["REGISTRATION"])
23 19
     
24 20
     from . import db
25 21
     db.init_app(app)
@@ -31,4 +27,7 @@ def create_app(test_config=None):
31 27
     app.register_blueprint(home.bp)
32 28
     app.add_url_rule('/', endpoint='index')
33 29
 
30
+    from . import manage
31
+    app.register_blueprint(manage.bp)
32
+
34 33
     return app

+ 27
- 21
myriad/auth.py Ver fichero

@@ -1,7 +1,7 @@
1 1
 import functools
2 2
 
3 3
 from flask import (
4
-    Blueprint, flash, g, redirect, render_template, request, session, url_for
4
+    Blueprint, flash, g, redirect, render_template, request, session, url_for, current_app
5 5
 )
6 6
 from werkzeug.security import check_password_hash, generate_password_hash
7 7
 
@@ -9,8 +9,34 @@ from myriad.db import get_db
9 9
 
10 10
 bp = Blueprint('auth', __name__, url_prefix='/auth')
11 11
 
12
+def login_required(view):
13
+    @functools.wraps(view)
14
+    def wrapped_view(**kwargs):
15
+        if g.user is None:
16
+            return redirect(url_for('auth.login'))
17
+
18
+        return view(**kwargs)
19
+
20
+    return wrapped_view
21
+
22
+
23
+@bp.before_app_request
24
+def load_logged_in_user():
25
+    user_id = session.get('user_id')
26
+
27
+    if user_id is None:
28
+        g.user = None
29
+    else:
30
+        g.user = get_db().execute(
31
+            'SELECT * FROM user WHERE id = ?', (user_id,)
32
+        ).fetchone()
33
+
34
+
35
+
12 36
 @bp.route('/register', methods=('GET', 'POST'))
13 37
 def register():
38
+    if current_app.config["REGISTRATION"] == False and g.user is None:
39
+        return redirect(url_for("index"))
14 40
     if request.method == 'POST':
15 41
         username = request.form['username']
16 42
         password = request.form['password']
@@ -63,17 +89,6 @@ def login():
63 89
 
64 90
     return render_template('auth/login.html')
65 91
 
66
-@bp.before_app_request
67
-def load_logged_in_user():
68
-    user_id = session.get('user_id')
69
-
70
-    if user_id is None:
71
-        g.user = None
72
-    else:
73
-        g.user = get_db().execute(
74
-            'SELECT * FROM user WHERE id = ?', (user_id,)
75
-        ).fetchone()
76
-
77 92
 
78 93
 @bp.route('/logout')
79 94
 def logout():
@@ -81,12 +96,3 @@ def logout():
81 96
     return redirect(url_for('index'))
82 97
 
83 98
 
84
-def login_required(view):
85
-    @functools.wraps(view)
86
-    def wrapped_view(**kwargs):
87
-        if g.user is None:
88
-            return redirect(url_for('auth.login'))
89
-
90
-        return view(**kwargs)
91
-
92
-    return wrapped_view

+ 7
- 1
myriad/home.py Ver fichero

@@ -16,4 +16,10 @@ def index():
16 16
     #     ' FROM post p JOIN user u ON p.author_id = u.id'
17 17
     #     ' ORDER BY created DESC'
18 18
     # ).fetchall()
19
-    return render_template('index.html')
19
+    return render_template('index.html')
20
+
21
+@bp.route('/full')
22
+def full_list():
23
+    db = get_db()
24
+    members = db.execute('SELECT * FROM member ORDER BY member_name').fetchall()
25
+    return render_template('full.html', memberlist=members)

+ 36
- 0
myriad/manage.py Ver fichero

@@ -0,0 +1,36 @@
1
+from flask import (
2
+    Blueprint, flash, g, redirect, render_template, request, session, url_for
3
+)
4
+
5
+from myriad.auth import login_required
6
+from myriad.db import get_db
7
+
8
+bp = Blueprint('manage', __name__, url_prefix='/manage')
9
+
10
+@bp.route('/new', methods=('GET', 'POST'))
11
+@login_required
12
+def new():
13
+    if request.method == 'POST':
14
+        name = request.form['name']
15
+        bio = request.form['bio']
16
+        #icon = request.form['icon']
17
+        user_id = g.user[0]
18
+        db = get_db()
19
+        error = None
20
+
21
+        if not name:
22
+            error = 'Name is required.'
23
+
24
+        if error is None:   
25
+            db.execute(
26
+                "INSERT INTO member (user_id, member_name, bio) VALUES (?, ?, ?)",
27
+                (user_id, name, bio),
28
+
29
+            )
30
+            db.commit()
31
+
32
+        flash(error)
33
+
34
+        return redirect(url_for('home.full_list'))
35
+    
36
+    return render_template('manage/new.html')

BIN
myriad/static/icons/any.jpg Ver fichero


+ 5
- 0
myriad/static/style.css Ver fichero

@@ -29,6 +29,11 @@ form{
29 29
 label,input{
30 30
     margin:10px;
31 31
 }
32
+form textarea{
33
+    height:100px;
34
+    width:300px;
35
+    vertical-align: top;
36
+}
32 37
 
33 38
 
34 39
 

+ 14
- 3
myriad/templates/base.html Ver fichero

@@ -7,14 +7,25 @@
7 7
 <div id="main">
8 8
   
9 9
   <div class="container" id="nav">
10
-    <div class="heading">myriad</div>
11
-    <div class="navitem">> <a href="{{ url_for('index') }}">Home</a></div>
10
+    <div class="heading">Myriad</div>
11
+    <div class="navitem">> <a href="{{ url_for('home.index') }}">Home</a></div>
12
+    <div class="navitem">> <a href="{{ url_for('home.full_list') }}">Full List</a></div>
13
+
14
+    {% if g.user %}
15
+    <div class="heading">Manage</div>
16
+    <div class="navitem">> <a href="{{ url_for('manage.new') }}">Add New</a></div>
17
+    {% endif %}
18
+
19
+    <div class="heading">Administration</div>
12 20
     {% if g.user %}
13 21
       <div class="navitem">> <a href="{{ url_for('auth.logout') }}">Log out</a></div>
14 22
     {% else %}
15
-      <div class="navitem">> <a href="{{ url_for('auth.register') }}">Register</a></div>
23
+      {% if config["REGISTRATION"] == True %}
24
+        <div class="navitem">> <a href="{{ url_for('auth.register') }}">Register</a></div>
25
+      {% endif %}
16 26
       <div class="navitem">> <a href="{{ url_for('auth.login') }}">Log in</a></div>
17 27
     {% endif %}
28
+
18 29
     </div>
19 30
 
20 31
   <div class="container">

+ 25
- 0
myriad/templates/full.html Ver fichero

@@ -0,0 +1,25 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+  <div class="title">{% block title %}Full List{% endblock %}</div>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+  
9
+    {% for member in memberlist %}
10
+
11
+    <div class="profile">
12
+        <div class="heading"><b>{{ member[3] }}</b> {{ member[4] }}</div>
13
+        <img src="{{ url_for('static', filename='/icons/any.jpg') }}" class="icon">
14
+        <div class="bio">
15
+            {{ member[5] }}
16
+        </div>
17
+        <br class="clear" />
18
+        <!--<div class="heading links"><a href="/geo">my page</a> &#9734 tumblr &#9734</div>
19
+        <img src="/geo/dsgame.webp" class="dsgame">
20
+        <br class="clear" /> -->
21
+    </div>
22
+
23
+    {% endfor %}
24
+
25
+{% endblock %}

+ 15
- 0
myriad/templates/manage/new.html Ver fichero

@@ -0,0 +1,15 @@
1
+{% extends 'base.html' %}
2
+
3
+{% block header %}
4
+  <div class="title">{% block title %}New{% endblock %}</div>
5
+{% endblock %}
6
+
7
+{% block content %}
8
+  <form method="post">
9
+    <label for="name">Name</label>
10
+    <input name="name" id="name" required><br>
11
+    <label for="bio">Description</label>
12
+    <textarea name="bio" id="bio"></textarea><br>
13
+    <input type="submit" value="Submit">
14
+  </form>
15
+{% endblock %}