def send_notification_digest(notifications: [Notification]): user = notifications[0].user sub = get_email_subscription(user.email) if sub.blacklisted: return msg = Message("{} new notifications".format(len(notifications)), recipients=[user.email]) msg.body = "".join([ "<{}> {}\nView: {}\n\n".format(notification.causer.display_name, notification.title, abs_url(notification.url)) for notification in notifications ]) msg.body += "Manage email settings: {}\nUnsubscribe: {}".format( abs_url_for("users.email_notifications", username=user.username), abs_url_for("users.unsubscribe", token=sub.token)) msg.html = render_template("emails/notification_digest.html", notifications=notifications, user=user, sub=sub) mail.send(msg)
def handleMakeWebhook(gh_user, gh_repo, package, oauth, event, token): url = "https://api.github.com/repos/{}/{}/hooks".format(gh_user, gh_repo) headers = { "Authorization": "token " + oauth } data = { "name": "web", "active": True, "events": [event], "config": { "url": abs_url_for("github.webhook"), "content_type": "json", "secret": token.access_token }, } # First check that the webhook doesn't already exist r = requests.get(url, headers=headers) if r.status_code == 401 or r.status_code == 403: current_user.github_access_token = None db.session.commit() return False if r.status_code != 200: flash("Failed to create webhook, received response from Github " + str(r.status_code) + ": " + str(r.json().get("message")), "danger") return False for hook in r.json(): if hook.get("config") and hook["config"].get("url") and \ hook["config"]["url"] == data["config"]["url"]: flash("Failed to create webhook, as it already exists", "danger") return False # Create it r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 201: db.session.add(token) db.session.commit() return True elif r.status_code == 401 or r.status_code == 403: current_user.github_access_token = None db.session.commit() return False else: flash("Failed to create webhook, received response from Github " + str(r.status_code) + ": " + str(r.json().get("message")), "danger") return False
def getDetailsURL(self, absolute=False): if absolute: from app.utils import abs_url_for return abs_url_for("packages.view", author=self.author.username, name=self.name) else: return url_for("packages.view", author=self.author.username, name=self.name)
def send_single_email(notification): sub = get_email_subscription(notification.user.email) if sub.blacklisted: return msg = Message(notification.title, recipients=[notification.user.email]) msg.body = """ New notification: {} View: {} Manage email settings: {} Unsubscribe: {} """.format(notification.title, abs_url(notification.url), abs_url_for("users.email_notifications", username=notification.user.username), abs_url_for("users.unsubscribe", token=sub.token)) msg.html = render_template("emails/notification.html", notification=notification, sub=sub) mail.send(msg)
def getURL(self, endpoint, absolute=False, **kwargs): if absolute: from app.utils import abs_url_for return abs_url_for(endpoint, author=self.author.username, name=self.name, **kwargs) else: return url_for(endpoint, author=self.author.username, name=self.name, **kwargs)
def send_unsubscribe_verify(email): sub = get_email_subscription(email) if sub.blacklisted: return msg = Message("Confirm unsubscribe", recipients=[email]) msg.body = """ We're sorry to see you go. You just need to do one more thing before your email is blacklisted. Click this link to blacklist email: {} """.format(abs_url_for('users.unsubscribe', token=sub.token)) msg.html = render_template("emails/verify_unsubscribe.html", sub=sub) mail.send(msg)
def sendVerifyEmail(newEmail, token): print("Sending verify email!") msg = Message("Verify email address", recipients=[newEmail]) msg.body = """ This email has been sent to you because someone (hopefully you) has entered your email address as a user's email. If it wasn't you, then just delete this email. If this was you, then please click this link to verify the address: {} """.format(abs_url_for('users.verify_email', token=token)) msg.html = render_template("emails/verify.html", token=token) mail.send(msg)
def setup_webhook(): pid = request.args.get("pid") if pid is None: abort(404) package = Package.query.get(pid) if package is None: abort(404) if not package.checkPerm(current_user, Permission.APPROVE_RELEASE): flash("Only trusted members can use webhooks", "danger") return redirect(package.getDetailsURL()) gh_user, gh_repo = package.getGitHubFullName() if gh_user is None or gh_repo is None: flash("Unable to get Github full name from repo address", "danger") return redirect(package.getDetailsURL()) if current_user.github_access_token is None: return github.authorize("write:repo_hook", redirect_uri=abs_url_for( "github.callback_webhook", pid=pid)) form = SetupWebhookForm(formdata=request.form) if form.validate_on_submit(): token = APIToken() token.name = "GitHub Webhook for " + package.title token.owner = current_user token.access_token = randomString(32) token.package = package event = form.event.data if event != "push" and event != "create": abort(500) if handleMakeWebhook(gh_user, gh_repo, package, current_user.github_access_token, event, token): flash("Successfully created webhook", "success") return redirect(package.getDetailsURL()) else: return redirect(url_for("github.setup_webhook", pid=package.id)) return render_template("github/setup_webhook.html", form=form, package=package)
def send_verify_email(email, token): sub = get_email_subscription(email) if sub.blacklisted: return msg = Message("Confirm email address", recipients=[email]) msg.body = """ This email has been sent to you because someone (hopefully you) has entered your email address as a user's email. If it wasn't you, then just delete this email. If this was you, then please click this link to confirm the address: {} """.format(abs_url_for('users.verify_email', token=token)) msg.html = render_template("emails/verify.html", token=token, sub=sub) mail.send(msg)
def start(): return github.authorize("", redirect_uri=abs_url_for("github.callback"))
def getShieldURL(self, type): from app.utils import abs_url_for return abs_url_for("packages.shield", author=self.author.username, name=self.name, type=type)