By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

Adding and removing labels from posts

roundabout,
created on Monday, 22 July 2024, 12:53:35 (1721652815), received on Wednesday, 31 July 2024, 06:54:51 (1722408891)
Author identity: vlad <vlad.muntoiu@gmail.com>

75007513aa2b0367515817cd60d4ce1d6d3baf7a

app.py

@@ -1250,6 +1250,80 @@ def repository_forum_vote(username, repository, post_id, score):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                return response
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        @repositories.route("/<username>/<repository>/forum/<int:post_id>/label", methods=["POST"])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def repository_forum_label(username, repository, post_id):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            server_repo_location = os.path.join(config.REPOS_PATH, username, repository)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not os.path.exists(server_repo_location):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(404)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not get_permission_level(flask.session.get("username"), username, repository):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(403)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            repo = git.Repo(server_repo_location)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            repo_data = Repo.query.filter_by(route=f"/{username}/{repository}").first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            user = User.query.filter_by(username=flask.session.get("username")).first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            relationships = RepoAccess.query.filter_by(repo=repo_data)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            user_relationship = RepoAccess.query.filter_by(repo=repo_data, user=user).first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            post = Post.query.filter_by(identifier=f"/{username}/{repository}/{post_id}").first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not post:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(404)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if post.parent:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(400)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            label = db.session.get(Label, flask.request.form["label"])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if PostLabel.query.filter_by(post=post, label=label).first():
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                return flask.redirect(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    flask.url_for(".repository_forum_thread", username=username, repository=repository,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                  post_id=post_id),
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    code=303)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            post_label = PostLabel(post, label)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            db.session.add(post_label)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return flask.redirect(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    flask.url_for(".repository_forum_thread", username=username, repository=repository,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                      post_id=post_id),
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    code=303)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        @repositories.route("/<username>/<repository>/forum/<int:post_id>/remove-label")
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def repository_forum_remove_label(username, repository, post_id):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            server_repo_location = os.path.join(config.REPOS_PATH, username, repository)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not os.path.exists(server_repo_location):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(404)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not get_permission_level(flask.session.get("username"), username, repository):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(403)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            repo = git.Repo(server_repo_location)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            repo_data = Repo.query.filter_by(route=f"/{username}/{repository}").first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            user = User.query.filter_by(username=flask.session.get("username")).first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            relationships = RepoAccess.query.filter_by(repo=repo_data)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            user_relationship = RepoAccess.query.filter_by(repo=repo_data, user=user).first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            post = Post.query.filter_by(identifier=f"/{username}/{repository}/{post_id}").first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if not post:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(404)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if post.parent:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                flask.abort(400)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            label = db.session.get(Label, flask.request.args["label"])
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            post_label = PostLabel.query.filter_by(post=post, label=label).first()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            db.session.delete(post_label)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            db.session.commit()
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return flask.redirect(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    flask.url_for(".repository_forum_thread", username=username, repository=repository,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                      post_id=post_id),
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                    code=303)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            @repositories.route("/<username>/<repository>/favourite")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            def repository_favourite(username, repository):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                server_repo_location = os.path.join(config.REPOS_PATH, username, repository)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

models.py

@@ -273,7 +273,7 @@ with (app.app_context()):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                class PostLabel(db.Model):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    id = db.Column(db.Integer, primary_key=True)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    post_identifier = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=False)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                label_identifier = db.Column(db.String(64), db.ForeignKey("label.identifier"), nullable=False)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                label_identifier = db.Column(db.String(162), db.ForeignKey("label.identifier"), nullable=False)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    post = db.relationship("Post", back_populates="labels")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                    label = db.relationship("Label", back_populates="posts")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

templates/post.html

@@ -6,6 +6,9 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            {% if level %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                <h2><a href="{{ post.number }}">{{ post.subject }}</a> <span class="post-number">#{{ post.number }}</span></h2>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            {% else %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            <h2>{{ post.subject }} <span class="post-number">#{{ post.number }}</span></h2>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        {% endif %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        {% if not post.parent %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                <dialog id="add-label">
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    <article class="card">
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        <section class="card-main">
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -16,7 +19,7 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                        {% trans %}Label to add:{% endtrans %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                        <select name="label">
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                            {% for label in repo_data.labels %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                                            <option value="{{ label }}">{{ label }}</option>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                                            <option value="{{ label.identifier }}">{{ label.name }}</option>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                            {% endfor %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                        </select>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    </label>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -29,14 +32,16 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        </section>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    </article>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                </dialog>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            <h2>{{ post.subject }} <span class="post-number">#{{ post.number }}</span></h2>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            <x-hbox class="post-labels">
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            <x-hbox class="post-labels box-center">
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    {% for label in post.labels %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    <a href="/label/{{ label }}">{{ label }}</a>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    <a href="{{ post.number }}/remove-label?label={{ label.label.identifier }}" class="post-label {% if get_permission_level(logged_in_user, username, repository) >= 1 %}removable{% endif %}" style="background-color: {{ label.label.colour_hex }};">{{ label.label.name }}</a>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                    {% endfor %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                <button id="add-label-button" class="button-flat" onclick="document.getElementById('add-label').showModal();">
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    <iconify-icon icon="mdi:plus"></iconify-icon>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                </button>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                {% if get_permission_level(logged_in_user, username, repository) >= 1 %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    <button class="button-flat" onclick="document.getElementById('add-label').showModal();" title="{% trans %}Add a label{% endtrans %}">
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                        <iconify-icon icon="mdi:plus"></iconify-icon>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    </button>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                {% endif %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                </x-hbox>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            {% endif %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            <p>