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

 celery_tasks.py

View raw Download
text/x-script.python • 3.43 kiB
Python script, ASCII text executable
        
            
1
import time
2
import os
3
import config
4
import email_send
5
from celery import shared_task
6
from app import db
7
from misc_utils import *
8
from models import *
9
from smtplib import SMTP
10
11
12
@shared_task(ignore_result=False)
13
def send_notification(notification_id, users, level):
14
notification = db.session.get(Notification, notification_id)
15
16
for user in users:
17
db.session.add(UserNotification(db.session.get(User, user), notification, level))
18
19
if db.session.get(User, user).email:
20
with (SMTP(config.MAIL_SERVER) as mail):
21
if notification.data.get("type") == "welcome":
22
message = email_send.make_multipart_message(
23
f"Welcome, {user}",
24
config.NOTIFICATION_EMAIL,
25
db.session.get(User, user).email,
26
"welcome",
27
username=user)
28
29
mail.sendmail(config.NOTIFICATION_EMAIL, db.session.get(User, user).email, message)
30
db.session.commit()
31
32
return 0 # notification sent successfully
33
34
35
@shared_task(ignore_result=False)
36
def merge_heads(head_route, head_branch, base_route, base_branch, simulate=True):
37
server_repo_location = os.path.join(config.REPOS_PATH, base_route.lstrip("/"))
38
if not os.path.isdir(server_repo_location):
39
raise FileNotFoundError(f"Repo {server_repo_location} not found, cannot merge.")
40
41
if base_route == head_route:
42
git_command(server_repo_location, b"", "checkout", f"{base_branch}")
43
if simulate:
44
out, err = git_command(server_repo_location, b"", "merge", "--no-commit", "--no-ff", f"heads/{head_branch}",
45
return_err=True)
46
47
# Undo the merge.
48
git_command(server_repo_location, b"", "merge", "--abort")
49
else:
50
out, err = git_command(server_repo_location, b"", "merge", f"heads/{head_branch}",
51
return_err=True)
52
53
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch
54
55
remote_url = os.path.join(config.BASE_DOMAIN, "git", base_route.lstrip("/"))
56
57
out, err = b"", b""
58
part_out, part_err = git_command(server_repo_location, b"", "remote", "add", "NEW", remote_url, return_err=True)
59
out += part_out
60
err += part_err
61
part_out, part_err = git_command(server_repo_location, b"", "remote", "update", return_err=True)
62
out += part_out
63
err += part_err
64
part_out, part_err = git_command(server_repo_location, b"", "checkout", f"{base_branch}", return_err=True)
65
out += part_out
66
err += part_err
67
if simulate:
68
part_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)
70
else:
71
part_out, part_err = git_command(server_repo_location, b"", "merge", "--allow-unrelated-histories",
72
f"NEW/{head_branch}", return_err=True)
73
out += part_out
74
err += part_err
75
part_out, part_err = git_command(server_repo_location, b"", "remote", "rm", "NEW", return_err=True)
76
out += part_out
77
err += part_err
78
if simulate:
79
# Undo the merge.
80
git_command(server_repo_location, b"", "merge", "--abort")
81
82
return "merge_simulator" if simulate else "merge", out, err, head_route, head_branch, base_route, base_branch
83