Django 通过电子邮件发送来自模型的用户数据

Django emailing the user data from a model

我有一个 django 功能可以禁止用户并将他们添加到 Banned_User table,它有一个配置文件和 reason_reported 列来将他们标记为被禁止。

我希望向用户发送一封电子邮件,一旦被禁止,在 banned_user table 的 report_reasons 字段中列出原因,说明他们为什么被禁止禁止。

def banning_users(self, request, queryset):

#BANNING FUNCTION
for obj in queryset:
    if hasattr(obj, 'user'):
        # This object is a Profile, so lookup the user
        profile = obj
        user = obj.user
    user.is_active = False
    user.save()

    #ADDING TO TABLE
    banned_user = profile.banned_profile.create(profile=profile)
    reports = banned_user.profile.user_reported_report.all()
    banned_user.save()
    for report in reports:
        banned_user.report_reason.add(report)

    # EMAIL FUNCTION
    subject = 'Ban'
    message = 'You have been banned for the following reasons: []'
    email_from = settings.EMAIL_HOST_USER
    recipient_list = [user.email]
    send_mail( subject, message,email_from, recipient_list)

self.message_user(request, "User is banned and Email has been sent")

禁止的用户模型

class Banned_User(models.Model):
def __str__(self):
return str.join(str(self.profile), str(self.report_reason.report_reason))
#return self.profile
#return str.join(str(self.profile), str(self.report_reason))

profile = models.ForeignKey(
    'Profile',
    on_delete=models.PROTECT,
    blank=False,
    null=False,
    related_name='banned_profile'
)

report_reason = models.ManyToManyField(
    'Report',
    #on_delete=models.PROTECT,
    blank=True,
    #null=True,
)

date_banned = models.DateField(null=True, blank=False,)

报表模型

    class Report(models.Model):
    def __str__(self):
    return str.join(str(self.user_reported), str(self.datetime_sent))

TOXICITY = 'Toxicity'
SPORTSMANSHIP = 'Poor sportsmanship'

REPORT_REASON_CHOICES = (
    (TOXICITY, 'Toxicity'),
    (SPORTSMANSHIP, 'Unsportsmanlike Behaviour'),
)

session = models.ForeignKey(
    'Session',
    on_delete=models.PROTECT,
    blank=False,
    null=False,
)

user_reported = models.ForeignKey(
    'Profile',
    on_delete=models.PROTECT,
    blank=False,
    null=False,
    related_name='user_reported_report',
)

sent_by = models.ForeignKey(
    'Profile',
    on_delete=models.PROTECT,
    blank=False,
    null=False,
    related_name='sent_by_report',
)

report_reason = models.CharField(
    max_length=255,
    choices=REPORT_REASON_CHOICES,
    default=TOXICITY,
)

datetime_sent = models.DateTimeField(auto_now_add=True,)

创建一个模型来存储用户和原因并在视图中发送电子邮件。

例如: 在 models.py

class BannedUser(models.Model):
    user = models.OneToOneField(
        User, related_name='banned', on_delete=models.CASCADE)
    reasons = models.CharField(max_length=300)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __repr__(self):
        return self.user.username

然后在views.py

from django.core.mail import EmailMessage, send_mail

msg = '''Hello {},

We are sorry to inform you that you have been banned from {} 
for the following reasons {}.

This will take effect immediately. Thank you for you understanding.
'''.format(user.username, sitename, user.banned.reasons)

email = EmailMessage(
    'Hello',
    msg,
    'from@example.com',
    ['to1@example.com',],
)
email.send(fail_silently=True)