0

What I am trying to achieve is having the senders-name, from the current logged in user with the association name, to show up in the receivers inbox like so:

'associaton-name'@domain.com

I have commented it down below where i tried to achieve it in views.py

Can't seem to find any related solutions after days and hours of work.

Really appreciate your help, folks!

Django: 1.10
Python: 3.6


views.py

class mailPost(FormView):
   success_url = '.'
   form_class = mailHandler
   template_name = 'post/post.html'

   def form_valid(self, form):
       messages.add_message(self.request, messages.SUCCESS, 'Email Sent!')
       return super(mailPost, self).form_valid(form)

   def form_invalid(self, form):
       messages.add_message(self.request, messages.WARNING,
                         'Email not sent. Please try again.')
       return super(mailPost, self).form_invalid(form)

   def post(self, request, *args, **kwargs):
       form_class = self.get_form_class()
       form = self.get_form(form_class)

       if form.is_valid():
           sender = "noreply@domain.com"      # Instead of noreply I wish for current requested associaton name
           receiver = form.cleaned_data.get('receiver')
           cc = form.cleaned_data.get('cc')
           bcc = form.cleaned_data.get('bcc')
           subject = form.cleaned_data.get('subject')
           message = form.cleaned_data.get('message')
           time = datetime.now()
           asoc_pk = Association.objects.filter(asoc_name=self.request.user.association)
           asoc = Association.objects.get(id=asoc_pk)

           Email.objects.create(
               sender=sender,
               receiver=receiver,
               cc=cc,
               bcc=bcc,
               subject=subject,
               message=message,
               association=asoc,
               sentTime=time
           )

           msg = EmailMultiAlternatives(subject, message, sender, [receiver], bcc=[bcc], cc=[cc])
           msg.send()

           return self.form_valid(form)
       else:
           return self.form_invalid(form)

models.py

class Email(models.Model):
   sender = models.CharField(max_length=254)
   sentTime = models.DateTimeField(auto_now_add=True, blank=False)
   subject = models.CharField(max_length=254)
   receiver = models.CharField(max_length=254)
   cc = models.CharField(max_length=254)
   bcc = models.CharField(max_length=254)
   message = models.TextField()
   association = models.ForeignKey(Association)

   class Meta:
       db_table = 'Email'    


class Association(models.Model):
    asoc_name = models.CharField(max_length=50, null=True, blank=True, unique=True)


   class Meta:
       db_table = 'Association'     


class Administrator(AbstractUser):
   ...
   association = models.ForeignKey(Association)


   class Meta:
       db_table = 'Administrator'
Niknak
  • 583
  • 1
  • 8
  • 22

1 Answers1

0

I'm not sure I understand your question correctly. You can access the authenticated user (given you are using the Django Authentication system) by calling self.request.user.

You have to create a relation between Association and the user:

class Association(models.Model):
    asoc_name = models.CharField(max_length=50, null=True, blank=True, unique=True)
    # Option 1 - if one user can be a member of several associations
    members = models.ManyToMany(User)

   class Meta:
       db_table = 'Association'

or a new model instance if a user can only be a member of one association:

# Option 2
class Membership(Model):
    association = models.ForeignKey(Association)
    user = models.ForeignKey(User, unique=True)

You get the Association using a direct lookup (or a reverse relation).

 # option 1
 if form.is_valid():
       sender = Association.objects.filter(members=self.request.user).first()
       # sender might be None

 # option 2
 if form.is_valid():
       membership = Membership.objects.filter(user=self.request.user).first()
       if membership:
           sender = membership.association

https://docs.djangoproject.com/en/1.11/topics/db/examples/many_to_many/

Risadinha
  • 16,058
  • 2
  • 88
  • 91
  • i just want to fetch the current user who are a member in a association..and grab the name of that association and pass it along as part of the senders email: e.g. **dortmund@gmail.com** where dortmund is the association name – Niknak May 19 '17 at 19:07
  • I still fail to see where the problem is - why can't you use Django's ORM queries with `self.request.user`? – Risadinha May 19 '17 at 19:09
  • your option 2 in `form.is_valid()` is almost working but i keep getting this error: **Sending a message to reciever@gmail.com from dortmund ...Mailgun API response 400: { "message": "'from' parameter is not a valid address. please check documentation" }** it seems like i just need **@domain.com** in the end to make it work – Niknak May 19 '17 at 19:35