celery_tasks.py
Python script, ASCII text executable
1import time 2import os 3import config 4import email_send 5from celery import shared_task 6from app import db 7from misc_utils import * 8from models import * 9from smtplib import SMTP 10 11 12@shared_task(ignore_result=False) 13def send_notification(notification_id, users, level): 14notification = db.session.get(Notification, notification_id) 15 16for user in users: 17db.session.add(UserNotification(db.session.get(User, user), notification, level)) 18 19if db.session.get(User, user).email: 20with (SMTP(config.MAIL_SERVER) as mail): 21if notification.data.get("type") == "welcome": 22message = ("Subject:Welcome" 23+ email_send.render_email_template("welcome.html", username=user)) 24mail.sendmail( 25config.NOTIFICATION_EMAIL, 26db.session.get(User, user).email, 27message, 28) 29db.session.commit() 30 31return 0 # notification sent successfully 32 33 34@shared_task(ignore_result=False) 35def merge_heads(head_route, head_branch, base_route, base_branch): 36server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 37if not os.path.isdir(server_repo_location): 38raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 39 40if base_route == head_route: 41git_command(server_repo_location, b"", "checkout", f"{base_branch}") 42out, err = git_command(server_repo_location, b"", "merge", "--no-ff", f"heads/{head_branch}", return_err=True) 43 44return out, err 45 46remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 47 48git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url) 49git_command(server_repo_location, b"", "remote", "update") 50git_command(server_repo_location, b"", "checkout", f"{base_branch}") 51git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", f"NEW/{head_branch}") 52git_command(server_repo_location, b"", "remote", "rm", "NEW") 53