1

i am wondering how can i configure django admin to see a form, so the auth_user should send emails to subscribe users.

What i have done:

models.py

from django.db import models
from datetime import datetime




class Mail(models.Model):
    email=models.EmailField(max_length=255,unique=True,help_text="Το email του χρήστη",verbose_name="Email")
    timestamp=models.DateTimeField(default=datetime.now,help_text="Η ημερομηνία που ο χρήστης γράφτηκε στην newsletter list",verbose_name="Ημερομηνία εγγραφής")
    delete_link=models.SlugField(unique=True,max_length=255,null=True,blank=True)
    def __unicode__(self):
        return self.email

    class Meta:
        verbose_name = "Email User"
        verbose_name_plural = "Email Users"

forms.py

from django import forms

class MailForm(forms.Form):
        subject = forms.CharField(max_length=255)
        message = forms.CharField(widget=forms.Textarea)
        attachment = forms.FileField()

I am trying to add one link to newsletter app in admin page.Example:

enter image description here

Now somehow, i must write the validation rules for the form (subject and message must not be blank).For validation i am trying to do something like:

def clean_subject(self):
        if self.cleaned_data["subject"]=="" or self.cleaned_data["subject"]==None:
            raise forms.ValidationError("My text goes here")    
        return self.cleaned_data["subject"]

def clean_message(self):
        if self.cleaned_data["message"]=="" or self.cleaned_data["message"]==None:
            raise forms.ValidationError("My text goes here")    
        return self.cleaned_data["message"]

And when the form is not valid django shows up a message like the standar way.Example:

django admin error validation

Finally i must write 1 view to show the form (i am trying to use something standar for django admin, and 1 view with a message that says "Send 130 mails with success."

Any advice how can i configure django admin to do what i described would be usefull!

Chris P
  • 2,059
  • 4
  • 34
  • 68
  • Why don't you try already built newsletter app? see [emencia django newsletter](https://github.com/emencia/emencia-django-newsletter) – Aamir Rind Apr 19 '13 at 19:05

1 Answers1

0

I think you should create a Mail model. First, you will have to create a mail and save it. Then, you can either use the save method of your mail model to send it to all your Emails Users or add a custom admin action to do it. In my opinion, save method is best.

So you should have this in models.py :

from django.db import models
from datetime import datetime
from django.core.mail import send_mail

    class Mail(models.Model):
        ...

        class Meta:
            verbose_name = "Email User"
            verbose_name_plural = "Email Users"

    class MailText(models.Model):
        subject = models.Charfield()
        message = models.Charfield()
        attachment = models.Filefield()
        users = models.ManyToManyField(Mail)
        send_it = models.BooleanField(default=False) #check it if you want to send your email

        def save(self):
            if self.send_it:
                #First you create your list of users
                user_list = []
                for u in self.users:
                    user_list.append(u.email)

                #Then you can send the message. 
                send_mail(str(self.subject), 
                          str(self.message),
                          'from@example.com',
                          user_list, 
                          fail_silently=False)

        class Meta:
            verbose_name = "Emails to send"
            verbose_name_plural = "Emails to send"
Community
  • 1
  • 1
  • 1
    I accept the answer because i want to get out of: https://stackoverflow.com/help/question-bans – Chris P Dec 27 '18 at 10:02