def monitor_app(self, app_id): logger.debug("Starting monitor_app") app = Application.objects.get(id=app_id) now = timezone.now() if app.paused_until is not None and app.paused_until > now: logger.debug(f"Ignoring App: {app.title} monitor because its paused") return True logger.debug(f"Running diagnostic for: {app.title} ") result = run_app_diagnostic(app) if result["status"] != "OPERATIONAL": if app.notify_email is not None: send_email_message( "diagnostic", app.notify_email, { "subject": f"Errors have been found on {app.title} diagnostic", "details": result["details"] }) if app.notify_slack_channel is not None: send_slack_raw( "diagnostic", app.academy.slackteam.owner.credentialsslack.token, app.notify_slack_channel.slack_id, { "subject": f"Errors have been found on {app.title} diagnostic", **result, }) return False return True
def send_assestment(student_assessment): token = create_token(student_assessment.user, hours_length=48) data = { "SUBJECT": student_assessment.assessment.title, "LINK": f"https://assessment.breatheco.de/{student_assessment.id}?token={token.key}" } send_email_message("assessment", student_assessment.user.email, data) logger.info(f"Survey was sent for user: {str(student_assessment.user.id)}") student_assessment.status = "SENT" student_assessment.save() return True # keep track of sent survays until they get answered # def answer_survey(user, data): # answer = Answer.objects.create(**{ **data, "user": user }) # log = SurveyLog.objects.filter( # user__id=user.id, # cohort__id=answer.cohort.id, # academy__id=answer.academy.id, # mentor__id=answer.academy.id # )
def execute_scripts(self, script_id): logger.debug("Starting execute_scripts") script = MonitorScript.objects.get(id=script_id) app = script.application now = timezone.now() if script.paused_until is not None and script.paused_until > now: logger.debug("Ignoring script ex because its paused") return True result = run_script(script) if result["status"] != "OPERATIONAL": if app.notify_email is not None: send_email_message( "diagnostic", app.notify_email, { "subject": f"Errors have been found on {app.title} script {script.id} (slug: {script.script_slug})", "details": result["details"] }) if app.notify_slack_channel is not None: try: send_slack_raw( "diagnostic", app.academy.slackteam.owner.credentialsslack.token, app.notify_slack_channel.slack_id, { "subject": f"Errors have been found on {app.title} script {script.id} (slug: {script.script_slug})", **result, }) except Exception: return False return False return True
def test_endpoint(self, endpoint_id): logger.debug("Starting monitor_app") endpoint = Endpoint.objects.get(id=endpoint_id) now = timezone.now() if endpoint.paused_until is not None and endpoint.paused_until > now: logger.debug( f"Ignoring App: {endpoint.url} monitor because its paused") return True logger.debug(f"Running diagnostic for: {endpoint.url} ") result = run_endpoint_diagnostic(endpoint.id) if result["status"] != "OPERATIONAL": if endpoint.application.notify_email: send_email_message( "diagnostic", endpoint.application.notify_email, { "subject": f"Errors have been found on {endpoint.application.title} diagnostic", "details": result["details"] }) if (endpoint.application.notify_slack_channel and endpoint.application.academy and hasattr(endpoint.application.academy, 'slackteam') and hasattr(endpoint.application.academy.slackteam.owner, 'credentialsslack')): send_slack_raw( "diagnostic", endpoint.application.academy.slackteam.owner. credentialsslack.token, endpoint.application.notify_slack_channel.slack_id, { "subject": f"Errors have been found on {endpoint.application.title} diagnostic", **result, })
def reset_password(users=None): for user in users: token = Token.create_temp(user) send_email_message( 'pick_password', user.email, {"LINK": os.getenv('API_URL') + f"/v1/auth/password/{token}"}) return True
def resend_invite(token=None,email=None, first_name=None ): params = { "callback": "https://admin.breatheco.de" } querystr = urllib.parse.urlencode(params) url = os.getenv('API_URL', '') + "/v1/auth/member/invite/" + str(token) + "?" + querystr send_email_message("welcome_academy",email, { "email": email, "subject": "Invitation", "LINK": url, "FIST_NAME": first_name })
def send_cohort_survey(self, user_id, survey_id): logger.debug("Starting send_cohort_survey") survey = Survey.objects.filter(id=survey_id).first() if survey is None: logger.error("Survey not found") return False user = User.objects.filter(id=user_id).first() if user is None: logger.error("User not found") return False utc_now = timezone.now() if utc_now > survey.created_at + survey.duration: logger.error("This survey has already expired") return False cu = CohortUser.objects.filter(cohort=survey.cohort, role="STUDENT", user=user).first() if cu is None: raise ValidationException( "This student does not belong to this cohort", 400) answers = generate_user_cohort_survey_answers(user, survey, status='SENT') has_slackuser = hasattr(user, 'slackuser') if not user.email and not has_slackuser: message = f'Author not have email and slack, this survey cannot be send by {str(user.id)}' logger.info(message) raise Exception(message) token = create_token(user, hours_length=48) data = { "SUBJECT": strings[survey.lang]["survey_subject"], "MESSAGE": strings[survey.lang]["survey_message"], "SURVEY_ID": survey_id, "BUTTON": strings[survey.lang]["button_label"], "LINK": f"https://nps.breatheco.de/survey/{survey_id}?token={token.key}", } if user.email: send_email_message("nps_survey", user.email, data) if hasattr(user, 'slackuser') and hasattr(survey.cohort.academy, 'slackteam'): send_slack("nps_survey", user.slackuser, survey.cohort.academy.slackteam, data=data)
def send_assestment(user_assessment): token = create_token(user_assessment.user, hours_length=48) data = { "SUBJECT": user_assessment.assessment.title, "LINK": f"https://assessment.breatheco.de/{user_assessment.id}?token={token.key}" } send_email_message("assessment", user_assessment.user.email, data) logger.info(f"Survey was sent for user: {str(user_assessment.user.id)}") user_assessment.status = "SENT" user_assessment.save() return True
def reset_password(users=None): from breathecode.notify.actions import send_email_message if users is None or len(users) == 0: raise Exception("Missing users") for user in users: token = Token.create_temp(user) # returns true or false if the email was send return send_email_message('pick_password', user.email, { "SUBJECT": "You asked to reset your password at BreatheCode", "LINK": os.getenv('API_URL','') + f"/v1/auth/password/{token}" }) return True
def create(self, validated_data): academy = Academy.objects.filter( id=self.context.get('academy_id')).first() if academy is None: raise ValidationException("Academy not found") role = Role.objects.filter(slug='student').first() if role is None: raise ValidationException("Role student not found") user = None email = None status = "INVITED" if "user" in validated_data: user = User.objects.filter(id=validated_data["user"]).first() if user is None: raise ValidationException("User not found") email = user.email status = "ACTIVE" if "user" not in validated_data: validated_data.pop('invite') email = validated_data["email"] cohort = None if 'cohort' in validated_data: cohort = Cohort.objects.filter( id=validated_data.pop('cohort')).first() invite = UserInvite.objects.filter( email=validated_data['email'], author=self.context.get('request').user).first() if invite is not None: raise ValidationException( "You already invited this user, check for previous invites and resend" ) invite = UserInvite(email=validated_data['email'], first_name=validated_data['first_name'], last_name=validated_data['last_name'], academy=academy, cohort=cohort, role=role, author=self.context.get('request').user, token=random.getrandbits(128)) invite.save() logger.debug("Sending invite email to " + email) params = {"callback": "https://learn.breatheco.de"} querystr = urllib.parse.urlencode(params) url = os.getenv('API_URL') + "/v1/auth/member/invite/" + \ str(invite.token) + "?" + querystr send_email_message( "welcome_academy", email, { "email": email, "subject": "Welcome to Breathecode", "LINK": url, "FIST_NAME": validated_data['first_name'] }) return super().create({ **validated_data, "email": email, "user": user, "academy": academy, "role": role, "status": status })
def register_new_lead(form_entry=None): if form_entry is None: raise Exception('You need to specify the form entry data') if 'location' not in form_entry or form_entry['location'] is None: raise Exception('Missing location information') ac_academy = ActiveCampaignAcademy.objects.filter( academy__slug=form_entry['location']).first() if ac_academy is None: raise Exception(f"No academy found with slug {form_entry['location']}") automations = get_lead_automations(ac_academy, form_entry) if automations: logger.debug("found automations") logger.debug(automations) else: logger.debug("automations not found") tags = get_lead_tags(ac_academy, form_entry) logger.debug("found tags") logger.debug(set(t.slug for t in tags)) LEAD_TYPE = tags[0].tag_type if (automations is None or len(automations) == 0) and len(tags) > 0: if tags[0].automation is None: raise Exception( 'No automation was specified and the the specified tag has no automation either' ) automations = [tags[0].automation.acp_id] if not 'email' in form_entry: raise Exception('The email doesn\'t exist') if not 'first_name' in form_entry: raise Exception('The first name doesn\'t exist') if not 'last_name' in form_entry: raise Exception('The last name doesn\'t exist') if not 'phone' in form_entry: raise Exception('The phone doesn\'t exist') if not 'id' in form_entry: raise Exception('The id doesn\'t exist') contact = { "email": form_entry["email"], "first_name": form_entry["first_name"], "last_name": form_entry["last_name"], "phone": form_entry["phone"] } contact = set_optional(contact, 'utm_url', form_entry) contact = set_optional(contact, 'utm_location', form_entry, "location") contact = set_optional(contact, 'course', form_entry) contact = set_optional(contact, 'utm_language', form_entry, "language") contact = set_optional(contact, 'utm_country', form_entry, "country") contact = set_optional(contact, 'client_comments', form_entry, "client_comments") contact = set_optional(contact, 'gclid', form_entry) contact = set_optional(contact, 'referral_key', form_entry) entry = FormEntry.objects.filter(id=form_entry['id']).first() if not entry: raise Exception('FormEntry not found (id: ' + str(form_entry['id']) + ')') # save geolocalization info # save_get_geolocal(entry, form_enty) if 'contact-us' == tags[0].slug: send_email_message( 'new_contact', ac_academy.academy.marketing_email, { "subject": f"New contact from the website {form_entry['first_name']} {form_entry['last_name']}", "full_name": form_entry['first_name'] + " " + form_entry['last_name'], "client_comments": form_entry['client_comments'], "data": { **form_entry }, # "data": { **form_entry, **address }, }) # ENV Variable to fake lead storage if SAVE_LEADS == 'FALSE': logger.debug( "Ignoring leads because SAVE_LEADS is FALSE on the env variables") return form_entry logger.debug("ready to send contact with following details: ", contact) old_client = AC_Old_Client(ac_academy.ac_url, ac_academy.ac_key) response = old_client.contacts.create_contact(contact) contact_id = response['subscriber_id'] if 'subscriber_id' not in response: logger.error("error adding contact", response) raise APIException('Could not save contact in CRM') client = Client(ac_academy.ac_url, ac_academy.ac_key) if automations: for automation_id in automations: data = { "contactAutomation": { "contact": contact_id, "automation": automation_id } } response = client.contacts.add_a_contact_to_an_automation(data) if 'contacts' not in response: logger.error( f"error triggering automation with id {str(automation_id)}", response) raise APIException('Could not add contact to Automation') else: logger.debug( f"Triggered automation with id {str(automation_id)}", response) auto = Automation.objects.filter( acp_id=automation_id, ac_academy=ac_academy).first() entry.automation_objects.add(auto) for t in tags: data = {"contactTag": {"contact": contact_id, "tag": t.acp_id}} response = client.contacts.add_a_tag_to_contact(data) if 'contacts' in response: entry.tag_objects.add(t.id) entry.storage_status = 'PERSISTED' entry.save() form_entry['storage_status'] = 'PERSISTED' return entry
def send_question(user, cohort=None): answer = Answer(user=user) if cohort is not None: answer.cohort = cohort else: cohorts = CohortUser.objects.filter( user__id=user.id).order_by("-cohort__kickoff_date") _count = cohorts.count() if _count == 1: _cohort = cohorts.first().cohort answer.cohort = _cohort if answer.cohort is None: message = 'Impossible to determine the student cohort, maybe it has more than one, or cero.' logger.info(message) raise Exception(message) else: answer.lang = answer.cohort.language answer.save() has_slackuser = hasattr(user, 'slackuser') if not user.email and not has_slackuser: message = f'User not have email and slack, this survey cannot be send: {str(user.id)}' logger.info(message) raise Exception(message) # if not answer.cohort.syllabus.certificate.name: if not answer.cohort.syllabus: message = 'Cohort not have one Syllabus' logger.info(message) raise Exception(message) # if not answer.cohort.syllabus.certificate.name: if not answer.cohort.syllabus.certificate: message = f'Syllabus not have one Certificate' logger.info(message) raise Exception(message) question_was_sent_previously = Answer.objects.filter( cohort=answer.cohort, user=user, status='SENT').count() question = build_question(answer) if question_was_sent_previously: answer = Answer.objects.filter(cohort=answer.cohort, user=user, status='SENT').first() Token.objects.filter(id=answer.token_id).delete() else: answer.title = question["title"] answer.lowest = question["lowest"] answer.highest = question["highest"] answer.lang = answer.cohort.language answer.save() token = create_token(user, hours_length=48) token_id = Token.objects.filter(key=token).values_list('id', flat=True).first() answer.token_id = token_id answer.save() data = { "QUESTION": question['title'], "HIGHEST": answer.highest, "LOWEST": answer.lowest, "SUBJECT": question['title'], "ANSWER_ID": answer.id, "BUTTON": strings[answer.cohort.language]["button_label"], "LINK": f"https://nps.breatheco.de/{answer.id}?token={token.key}", } if user.email: send_email_message("nps", user.email, data) if hasattr(user, 'slackuser') and hasattr(answer.cohort.academy, 'slackteam'): send_slack("nps", user.slackuser, answer.cohort.academy.slackteam, data=data) # keep track of sent survays until they get answered if not question_was_sent_previously: logger.info(f"Survey was sent for user: {str(user.id)}") answer.status = "SENT" answer.save() return True else: logger.info(f"Survey was resent for user: {str(user.id)}") return True