]> git.lizzy.rs Git - cheatdb.git/commitdiff
Add topic searching and topic discarding
authorrubenwardy <rw@rubenwardy.com>
Sun, 23 Dec 2018 23:49:49 +0000 (23:49 +0000)
committerrubenwardy <rw@rubenwardy.com>
Sun, 23 Dec 2018 23:54:20 +0000 (23:54 +0000)
app/models.py
app/scss/components.scss
app/templates/base.html
app/templates/macros/topics.html
app/templates/todo/topics.html
app/utils.py
app/views/api.py
app/views/packages/todo.py
migrations/versions/a791b9b74a4c_.py [new file with mode: 0644]

index 488cc35999a751c00e05cf3f62c9c5ca5f2beaa8..9c17da6981af62775a764c94277e7cdf781d46a2 100644 (file)
@@ -804,6 +804,7 @@ class ForumTopic(db.Model):
        author    = db.relationship("User")
 
        wip       = db.Column(db.Boolean, server_default="0")
+       discarded = db.Column(db.Boolean, server_default="0")
 
        type      = db.Column(db.Enum(PackageType), nullable=False)
        title     = db.Column(db.String(200), nullable=False)
@@ -836,6 +837,7 @@ class ForumTopic(db.Model):
                        "posts":  self.posts,
                        "views":  self.views,
                        "is_wip": self.wip,
+                       "discarded":  self.discarded,
                        "created_at": self.created_at.isoformat(),
                }
 
index 97249144377905f273f37c9aa9dd61f5b60630ab..31b96006c627baa54ec082d1c35c198132d5146a 100644 (file)
        color: #7ac;
 }
 
+.discardtopic {
+       text-decoration: line-through;
+       a {
+               color: #7ac;
+       }
+       filter: brightness(0.5);
+}
+
 .editor-toolbar, .editor-toolbar.fullscreen {
        margin-bottom: 0 !important;
        background-color: #444 !important;
index 522e3498b1d6ff36f43da93b2e03326eeebfc361..b3f467e53636edfbdb9c2dd1353e1679b5f6a4f4 100644 (file)
@@ -7,7 +7,7 @@
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{% block title %}title{% endblock %} - {{ config.USER_APP_NAME }}</title>
        <link rel="stylesheet" type="text/css" href="/static/bootstrap.css">
-       <link rel="stylesheet" type="text/css" href="/static/custom.css?v=2">
+       <link rel="stylesheet" type="text/css" href="/static/custom.css?v=3">
        {% block headextra %}{% endblock %}
 </head>
 
index a3d40c6e29ec8c526ace02018b40476392afd7f9..66d11b40c31c26937332f6bf59ffcc0c8f269288 100644 (file)
@@ -1,7 +1,6 @@
-{% macro render_topics_table(topics, show_author=True) -%}
+{% macro render_topics_table(topics, show_author=True, show_discard=False) -%}
 <table class="table">
        <tr>
-               <th>Id</th>
                <th></th>
                <th>Title</th>
                {% if show_author %}<th>Author</th>{% endif %}
@@ -10,8 +9,7 @@
                <th>Actions</th>
        </tr>
        {% for topic in topics %}
-               <tr{% if topic.wip %} class="wiptopic"{% endif %}>
-                       <td>{{ topic.topic_id }}</td>
+               <tr class="{% if topic.wip %}wiptopic{% endif %}{% if topic.discarded %}discardtopic{% endif %}">
                        <td>
                                [{{ topic.type.value }}]
                        </td>
                        {% endif %}
                        <td>{{ topic.name or ""}}</td>
                        <td>{% if topic.link %}<a href="{{ topic.link }}">{{ topic.link | domain }}</a>{% endif %}</td>
-                       <td>
-                               <a href="{{ url_for('create_edit_package_page', author=topic.author.username, repo=topic.getRepoURL(), forums=topic.topic_id, title=topic.title, bname=topic.name) }}">Create</a>
+                       <td class="btn-group">
+                               <a class="btn btn-primary"
+                                       href="{{ url_for('create_edit_package_page', author=topic.author.username, repo=topic.getRepoURL(), forums=topic.topic_id, title=topic.title, bname=topic.name) }}">
+                                       Create
+                               </a>
+                               {% if show_discard %}
+                                       <a class="btn btn-{% if topic.discarded %}success{% else %}danger{% endif %} topic-discard" data-tid={{ topic.topic_id }}>
+                                               {% if topic.discarded %}
+                                                       Show
+                                               {% else %}
+                                                       Discard
+                                               {% endif %}
+                                       </a>
+                               {% endif %}
                        </td>
                </tr>
        {% endfor %}
index 4236a40f9eacc19acafafcbdfe1b36f9f422a2f3..d2fcd28a4060c41c7358e48e03076d6363f53c5f 100644 (file)
@@ -5,11 +5,21 @@ Topics to be Added
 {% endblock %}
 
 {% block content %}
+       <a class="btn btn-primary float-right" href="{{ url_for('todo_topics_page', q=query, show_discarded=not show_discarded) }}">
+               {% if not show_discarded %}
+                       Show
+               {% else %}
+                       Hide
+               {% endif %}
+
+               Discarded Topics
+       </a>
+
        <h1>Topics to be Added</h1>
 
        <p>
-               {{ total - (topic_count) }} / {{ total }} packages have been added.
-               {{ topics | count }} remaining.
+               {{ total - (topic_count) }} / {{ total }} topics have been added as packages to CDB.
+               {{ topic_count }} remaining.
        </p>
 
        <form method="GET" action="{{ url_for('todo_topics_page') }}" class="my-4">
@@ -18,7 +28,7 @@ Topics to be Added
        </form>
 
        {% from "macros/topics.html" import render_topics_table %}
-       {{ render_topics_table(topics) }}
+       {{ render_topics_table(topics, show_discard=True) }}
 
        <ul class="pagination mt-4">
                <li class="page-item {% if not prev_url %}disabled{% endif %}">
@@ -37,3 +47,40 @@ Topics to be Added
                </li>
        </ul>
 {% endblock %}
+
+{% block scriptextra %}
+       <script>
+               var csrf_token = "{{ csrf_token() }}";
+       </script>
+       <script>
+               $(".topic-discard").click(function() {
+                       var ele = $(this);
+                       var tid = ele.attr("data-tid");
+                       var discard = !ele.parent().parent().hasClass("discardtopic");
+                       fetch(new Request("{{ url_for('topic_set_discard') }}?tid=" + tid +
+                                       "&discard=" + (discard ? "true" : "false"), {
+                               method: "post",
+                               credentials: "same-origin",
+                               headers: {
+                                       "Accept": "application/json",
+                                       "X-CSRFToken": csrf_token,
+                               },
+                       })).then(function(response) {
+                               response.text().then(function(txt) {
+                                       console.log(JSON.parse(txt));
+                                       if (JSON.parse(txt).discarded) {
+                                               ele.parent().parent().addClass("discardtopic");
+                                               ele.removeClass("btn-danger");
+                                               ele.addClass("btn-success");
+                                               ele.text("Show");
+                                       } else {
+                                               ele.parent().parent().removeClass("discardtopic");
+                                               ele.removeClass("btn-success");
+                                               ele.addClass("btn-danger");
+                                               ele.text("Discard");
+                                       }
+                               }).catch(console.log)
+                       }).catch(console.log)
+               });
+       </script>
+{% endblock %}
index cb6a315af44e0415b22499ea7c508f3519de7a24..21c7fd674ddea5c864f2d3b23a2d25c458327af5 100644 (file)
@@ -68,7 +68,10 @@ def make_flask_user_password(plaintext_str):
        import bcrypt
        plaintext = plaintext_str.encode("UTF-8")
        password = bcrypt.hashpw(plaintext, bcrypt.gensalt())
-       return password.decode("UTF-8")
+       if isinstance(password, str):
+               return password
+       else:
+               return password.decode("UTF-8")
 
 def _do_login_user(user, remember_me=False):
        def _call_or_get(v):
index 9223c8b79c5f73a7514733264bf2f545e6f70286..04f58d66474c5db12664185b70c673d866938e4d 100644 (file)
@@ -19,7 +19,7 @@ from flask import *
 from flask_user import *
 from app import app
 from app.models import *
-from app.utils import is_package_page
+from app.utils import is_package_page, rank_required
 from .packages import QueryBuilder
 
 @app.route("/api/packages/")
@@ -43,3 +43,18 @@ def api_topics_page():
                        .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
        pkgs = [t.getAsDictionary() for t in query.all()]
        return jsonify(pkgs)
+
+
+@app.route("/api/topic_discard/", methods=["POST"])
+@rank_required(UserRank.EDITOR)
+def topic_set_discard():
+       tid = request.args.get("tid")
+       discard = request.args.get("discard")
+       if tid is None or discard is None:
+               abort(400)
+
+       topic = ForumTopic.query.get(tid)
+       topic.discarded = discard == "true"
+       db.session.commit()
+
+       return jsonify(topic.getAsDictionary())
index 596e83aaa33248d582c65a9b04b9f146b83adef0..a29a46e24c0740c6999be359485c8226e4100654 100644 (file)
@@ -54,11 +54,16 @@ def todo_page():
 @app.route("/todo/topics/")
 @login_required
 def todo_topics_page():
-       total = ForumTopic.query.count()
+       query = ForumTopic.query
 
-       query = ForumTopic.query \
-                       .filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
-                       .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
+       show_discarded = request.args.get("show_discarded") == "True"
+       if not show_discarded:
+               query = query.filter_by(discarded=False)
+
+       total = query.count()
+
+       query = query.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
+               .order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
 
        topic_count = query.count()
 
@@ -75,5 +80,5 @@ def todo_topics_page():
                        if query.has_prev else None
 
        return render_template("todo/topics.html", topics=query.items, total=total, \
-                       topic_count=topic_count, query=search, \
+                       topic_count=topic_count, query=search, show_discarded=show_discarded, \
                        next_url=next_url, prev_url=prev_url, page=page, page_max=query.pages)
diff --git a/migrations/versions/a791b9b74a4c_.py b/migrations/versions/a791b9b74a4c_.py
new file mode 100644 (file)
index 0000000..d4f9436
--- /dev/null
@@ -0,0 +1,23 @@
+"""empty message
+
+Revision ID: a791b9b74a4c
+Revises: 44e138485931
+Create Date: 2018-12-23 23:52:02.010281
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'a791b9b74a4c'
+down_revision = '44e138485931'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    op.add_column('forum_topic', sa.Column('discarded', sa.Boolean(), server_default='0', nullable=True))
+
+def downgrade():
+    op.drop_column('forum_topic', 'discarded')