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 = email_send.make_multipart_message( 23f"Welcome, {user}", 24config.NOTIFICATION_EMAIL, 25db.session.get(User, user).email, 26"welcome", 27username=user) 28 29mail.sendmail(config.NOTIFICATION_EMAIL, db.session.get(User, user).email, message) 30db.session.commit() 31 32return 0 # notification sent successfully 33 34 35@shared_task(ignore_result=False) 36def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True): 37server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/")) 38if not os.path.isdir(server_repo_location): 39raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.") 40 41if base_route == head_route: 42git_command(server_repo_location, b"", "checkout", f"{base_branch}") 43if simulate: 44out, err = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}", 45return_err=True) 46 47# Undo the merge. 48git_command(server_repo_location, b"", "merge", "--abort") 49else: 50out, err = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}", 51return_err=True) 52 53return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch 54 55remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/")) 56 57out, err = b"", b"" 58part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True) 59out += part_out 60err += part_err 61part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True) 62out += part_out 63err += part_err 64part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True) 65out += part_out 66err += part_err 67if simulate: 68part_out, part_err = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 69"--no-commit", "--no-ff", f"NEW/{head_branch}", return_err=True) 70else: 71part_out, part_err = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories", 72f"NEW/{head_branch}", return_err=True) 73out += part_out 74err += part_err 75part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True) 76out += part_out 77err += part_err 78if simulate: 79# Undo the merge. 80git_command(server_repo_location, b"", "merge", "--abort") 81 82return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch 83