def create(self, validated_data):
        detail_data = validated_data.pop('detail')
        # 创建评论
        comment_info = MaterialCommentInfo.objects.create(**validated_data)
        # 处理评论详情
        comment_detail = MaterialCommentDetail.objects.create(
            comment_info=comment_info, **detail_data)
        # 修改根级评论数数据
        if comment_info.parent_comment_id:
            parent_comment = MaterialCommentInfo.objects.get(
                id=comment_info.parent_comment_id)
            parent_comment.comment_num += 1
            parent_comment.save()
        # 文章的评论数数据
        post = comment_info.post
        post.comment_num += 1
        post.save()
        # 评论成功后给被回复人发送邮件
        if comment_info.reply_to_author_id:
            guest = GuestProfile.objects.filter(
                id=comment_info.reply_to_author_id)[0]
            if guest and guest.is_subcribe and guest.email:
                # 取出评论人
                author = GuestProfile.objects.filter(
                    id=comment_info.author_id)[0]
                # 取出被回复评论
                reply_to_comment_detail = MaterialCommentDetail.objects.filter(
                    comment_info_id=comment_info.reply_to_comment_id)[0]
                email_info = {
                    'base_url':
                    SITE_BASE_URL,
                    'receive_name':
                    guest.nick_name,
                    'post_title':
                    post.title,
                    'post_url':
                    '{0}/{1}/{2}'.format(SITE_BASE_URL, post.post_type,
                                         post.id),
                    'comment':
                    reply_to_comment_detail.formatted_content
                    if reply_to_comment_detail else '',
                    'reply_author_name':
                    author.nick_name if author else '',
                    'reply_comment':
                    comment_detail.formatted_content,
                    'unsubscribe_url':
                    '{0}/{1}/?id={2}'.format(SITE_BASE_URL, 'unsubscribe',
                                             guest.uuid),
                    'subscribe_url':
                    '{0}/{1}/?id={2}'.format(SITE_BASE_URL, 'subscribe',
                                             guest.uuid),
                }
                try:
                    send_email(email_info,
                               guest.email,
                               send_type='reply_comment')
                except Exception as e:
                    pass

        return comment_info
def construct_and_send_worker_start_mail(challenge):
    if settings.DEBUG:
        return

    challenge_url = "https://{}/web/challenges/challenge-page/{}".format(
        settings.HOSTNAME, challenge.pk)

    template_data = {
        "CHALLENGE_NAME": challenge.title,
        "CHALLENGE_URL": challenge_url,
    }
    if challenge.image:
        template_data["CHALLENGE_IMAGE_URL"] = challenge.image.url

    template_id = settings.SENDGRID_SETTINGS.get("TEMPLATES").get(
        "CHALLENGE_APPROVAL_EMAIL")

    emails = challenge.creator.get_all_challenge_host_email()
    for email in emails:
        send_email(
            sender=settings.CLOUDCV_TEAM_EMAIL,
            recipient=email,
            template_id=template_id,
            template_data=template_data,
        )
示例#3
0
def send_emails(emails, template_id, template_data):
    """
    Sends email to list of users using provided template
    Arguments:
        emails {list} -- recepient email ids
        template_id {string} -- sendgrid template id
        template_data {dict} -- sendgrid email template data
    """
    for email in emails:
        send_email(
            sender=settings.CLOUDCV_TEAM_EMAIL,
            recipient=email,
            template_id=template_id,
            template_data=template_data,
        )
示例#4
0
    def create(self, request, *args, **kwargs):
        """
        发送邮箱验证码
        :param request: 
        :param args: 
        :param kwargs: 
        :return: 
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)  # status 400

        email = serializer.validated_data["email"]
        nick_name = serializer.validated_data['nick_name']

        send_email_status = send_email(nick_name,
                                       email=email,
                                       send_type='comment')

        if send_email_status != 1:

            context = {"error": send_email_status["msg"]}
            return Response(context, status=status.HTTP_400_BAD_REQUEST)

        else:
            context = {"email": email}
            return Response(context, status.HTTP_201_CREATED)

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
def construct_and_send_eks_cluster_creation_mail(challenge):
    if settings.DEBUG:
        return

    template_data = {"CHALLENGE_NAME": challenge.title}
    if challenge.image:
        template_data["CHALLENGE_IMAGE_URL"] = challenge.image.url

    template_id = settings.SENDGRID_SETTINGS.get("TEMPLATES").get(
        "CLUSTER_CREATION_TEMPLATE")

    send_email(
        sender=settings.CLOUDCV_TEAM_EMAIL,
        recipient=settings.ADMIN_EMAIL,
        template_id=template_id,
        template_data=template_data,
    )
示例#6
0
 def test(self):
     self.assertEqual(len(mail.outbox), 0)
     subject = "Test_Subject"
     body = "Test_Body"
     to = "*****@*****.**"
     send_email(subject, body, to)
     assert len(mail.outbox) == 1
     subject = "Test_Subject1"
     body = "Test_Body1"
     to = ("*****@*****.**", "*****@*****.**")
     send_email(subject, body, to)
     assert len(mail.outbox) == 2
     assert mail.outbox[0].subject == "Test_Subject"
     assert mail.outbox[1].subject == "Test_Subject1"
     assert mail.outbox[0].body == "Test_Body"
     assert mail.outbox[1].body == "Test_Body1"
     assert mail.outbox[0].to[0] == "*****@*****.**"
     assert mail.outbox[1].to[0] == "*****@*****.**"
     assert mail.outbox[1].to[1] == "*****@*****.**"
示例#7
0
    def create(self, request, *args, **kwargs):
        """
        发送邮箱验证码
        :param request: 
        :param args: 
        :param kwargs: 
        :return: 
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)  # status 400

        email = serializer.validated_data["email"]
        nick_name = serializer.validated_data['nick_name']

        email_info = {'receive_name': nick_name}

        try:
            send_email_status = send_email(email_info,
                                           email=email,
                                           send_type='comment')
        except Exception as e:
            send_email_status = 0

        if send_email_status != 1:

            context = {"email": ['发送邮件出错,请检查您的邮箱;如果依旧出错,请联系博主']}
            return Response(context, status=status.HTTP_400_BAD_REQUEST)

        else:
            context = {"email": email}
            return Response(context, status.HTTP_201_CREATED)

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
示例#8
0
def restart_workers_signal_callback(sender, instance, field_name, **kwargs):
    """
    Called when either evaluation_script or test_annotation_script for challenge
    is updated, to restart the challenge workers.
    """
    if settings.DEBUG:
        return

    prev = getattr(instance, "_original_{}".format(field_name))
    curr = getattr(instance, "{}".format(field_name))

    if field_name == "evaluation_script":
        instance._original_evaluation_script = curr
    elif field_name == "test_annotation":
        instance._original_test_annotation = curr

    if prev != curr:
        challenge = None
        if field_name == "test_annotation":
            challenge = instance.challenge
        else:
            challenge = instance

        response = restart_workers([challenge])

        count, failures = response["count"], response["failures"]

        logger.info(
            "The worker service for challenge {} was restarted, as {} was changed."
            .format(challenge.pk, field_name))

        if count != 1:
            logger.warning(
                "Worker(s) for challenge {} couldn't restart! Error: {}".
                format(challenge.id, failures[0]["message"]))
        else:
            challenge_url = "{}/web/challenges/challenge-page/{}".format(
                settings.EVALAI_API_SERVER, challenge.id)
            challenge_manage_url = "{}/web/challenges/challenge-page/{}/manage".format(
                settings.EVALAI_API_SERVER, challenge.id)

            if field_name == "test_annotation":
                file_updated = "Test Annotation"
            elif field_name == "evaluation_script":
                file_updated = "Evaluation script"

            template_data = {
                "CHALLENGE_NAME": challenge.title,
                "CHALLENGE_MANAGE_URL": challenge_manage_url,
                "CHALLENGE_URL": challenge_url,
                "FILE_UPDATED": file_updated,
            }

            if challenge.image:
                template_data["CHALLENGE_IMAGE_URL"] = challenge.image.url

            template_id = settings.SENDGRID_SETTINGS.get("TEMPLATES").get(
                "WORKER_RESTART_EMAIL")

            # Send email notification only when inform_hosts is true
            if challenge.inform_hosts:
                emails = challenge.creator.get_all_challenge_host_email()
                for email in emails:
                    send_email(
                        sender=settings.CLOUDCV_TEAM_EMAIL,
                        recipient=email,
                        template_id=template_id,
                        template_data=template_data,
                    )
示例#9
0
 def run(self, to, title, msg):
     from base.utils import send_email
     r = send_email(to, title, msg)
     return r
示例#10
0
 def test_send_email(self):
     if WORK_ENVIRONMENT: return
     r = send_email('*****@*****.**', '유닛테스트',
                    '유닛테스트임 http://www.daum.net/')
     self.assertEqual(r, True)