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

Add post notifications

roundabout,
created on Monday, 24 June 2024, 16:46:34 (1719247594), received on Wednesday, 31 July 2024, 06:54:49 (1722408889)
Author identity: vlad <vlad.muntoiu@gmail.com>

c711d80f5a922e2f336a44bbff79cb923c002935

app.py

@@ -324,7 +324,7 @@ def notifications():

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                 notifications=UserNotification.query.filter_by(
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                         user_username=flask.session.get("username")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    ).order_by(UserNotification.id.desc()),
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                             db=db, Commit=Commit
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                             db=db, Commit=Commit, Post=Post
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                 )
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

celery_tasks.py

@@ -14,7 +14,7 @@ from datetime import datetime

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            @shared_task(ignore_result=False)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            def send_notification(user_notification_id):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            from models import UserNotification, Commit
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            from models import UserNotification, Commit, Post
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                user_notification = db.session.get(UserNotification, user_notification_id)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                user = user_notification.user
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                notification = user_notification.notification
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -24,11 +24,12 @@ def send_notification(user_notification_id):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        match notification.data.get("type"):
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            case "welcome":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                message = email_send.make_multipart_message(
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    "Welcome, {username}".format(username=user.username),
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    f"[system] Welcome, {user.username}",
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        config.NOTIFICATION_EMAIL,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        user.email,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        "welcome",
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    username=user.username)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    username=user.username
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                            case "commit":
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                commit = db.session.get(Commit, notification.data.get("commit"))
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                line_separator = "\n\n"        # hack so it works in older Pythons
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        

@@ -41,7 +42,18 @@ def send_notification(user_notification_id):

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        username=user.username,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        commit=commit,
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/repo/" + notification.data.get("repo") + "/commit/" + notification.data.get("commit")
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                        case "post":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            post = db.session.get(Post, notification.data.get("post"))
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            message = email_send.make_multipart_message(
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    f"[post in {notification.data.get('repo')}] {post.subject}",
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    config.NOTIFICATION_EMAIL,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    user.email,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    "forum",
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    username=user.username,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    post=post,
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                    url="https://" if config.suggest_https else "http://" + config.BASE_DOMAIN + "/repo/" + notification.data.get("repo") + "/post/" + notification.data.get("post")
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                            )
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                        mail.sendmail(config.NOTIFICATION_EMAIL, user.email, message)
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                            
                                

templates/notifications.html

@@ -26,6 +26,13 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    {% trans %}Commited by{% endtrans %} <a href="/{{ commit.owner.username }}">{{ commit.owner.username }}</a>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                    {% trans %}in{% endtrans %} <a href="{{ commit.repo.route }}">{{ commit.repo.owner.username }}/{{ commit.repo.name }}</a>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                                </p>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                        {% elif notification.notification.data["type"] == "post" %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            {% set post = db.session.get(Post, notification.notification.data["post"]) %}
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            <h2><a href="{{ notification.notification.data['repo'] + '/forum/' + post.number|string }}">{{ post.subject }}</a></h2>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            <p>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                                {% trans %}Posted by{% endtrans %} <a href="/{{ post.owner.username }}">{{ post.owner.username }}</a>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                                {% trans %}in{% endtrans %} <a href="{{ post.repo.route }}">{{ post.repo.owner.username }}/{{ post.repo.name }}</a>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                                            </p>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                            {% endif %}
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        </section>
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                                                        <section>