Monday, February 2, 2015

Re: sending email

Oh, I see what you are trying to do. It looks like you are running needless queries though.

Your newleave model has this: 

    username  =models.ForeignKey(User,  default =1)

That's an FK to User, which means this line in your view:

    to_emails = [u.email for u in User.objects.filter(username__in=[a.username])]

is only querying for information that you already have, and trying to use list comprehension for a query that should only ever return a single result. The line above should be shortened to the following:

    to_emails = [ a.username.email ]

I was previously assuming that you wanted to email the admins of the leave, hence the funny query with a.admins, etc (which was a pure guess since I hadn't seen the model). Now I can see that you want to email the user that is directly attached to the leave. To make it even more slick, in addition to the change above, you can also add a select_related('username') to the original newleave query so that you'll only run a single query, which is much more efficient, like this:


def coporateservices_authorized_Leave(request, id):
    if request.method == 'POST':
        a=newleave.objects.select_related('username').get(id=id)
        form = coporateservices_authoriseleave(request.POST, instance=a)

        if form.is_valid(): 
            form.save()
            to_emails = [ a.username.email ]
            send_mail("RBV Leave Application Email Testing", "Your Leave Application have been approved by"+" "+a.authorized_by1,
            "RBV eLeave <Rbv_eLeave_System>", to_emails)
            return render_to_response('thankyou.html')
    else:
        a=newleave.objects.get(id=id)
    form =  coporateservices_authoriseleave(instance=a)
    return render_to_response('coporate_services_leave_approvial.html', {'form': form}, context_instance=RequestContext(request))


Other musings:

Try to be more consistent with your naming conventions for your classes and function/view names. For example, I would have named your newleave class as NewLeave, using CamelCase (aka CapCase) for all class names. Check out Python's PEP8 for more style guidelines: https://www.python.org/dev/peps/pep-0008/

I would also recommend using string token replacement to fill in strings using data from variables rather than concatenating them using the + notation. For example: 

"Your Leave Application have been approved by {}".format(a.authorized_by1)

It is more conventional and will make it easier to start passing in strings for translation later, if needed.


Glad you got it working though.

-James


On Feb 2, 2015 9:03 PM, "sum abiut" <suabiut@gmail.com> wrote:
Hi James,
I manage to fixe my email issue. Basically what i am trying to do here is to get the app to send an email to the a leave applicant when his/her leave is approved by the department director and at the same time update the data on the databases.

Here is my working view.py code for future referencing.

def coporateservices_authorized_Leave(request, id):
    if request.method == 'POST':
        a=newleave.objects.get(id=id)
        form = coporateservices_authoriseleave(request.POST, instance=a)
        if form.is_valid():
         form.save()
        to_emails = [u.email for u in User.objects.filter(username__in=[a.username])]
        send_mail("RBV Leave Application Email Testing", "Your Leave Application have been approved by"+" "+a.authorized_by1,
        "RBV eLeave <Rbv_eLeave_System>", to_emails)
        return render_to_response('thankyou.html')
    else:
        a=newleave.objects.get(id=id)
        form =  coporateservices_authoriseleave(instance=a)
        return render_to_response('coporate_services_leave_approvial.html', {'form': form}, context_instance=RequestContext(request))

once again thank you very much for your help.


 Cheers.




On Tue, Feb 3, 2015 at 2:38 PM, sum abiut <suabiut@gmail.com> wrote:
Hi James,

if you have a look at my view.py code below. i am trying to send email to a selected user that i have select to update that particular user using a form. The updated part is working fine now, i can update the particular row that i have selected. what i want to do next is to email that particular user that his data has been updated. i am still very confuse about the email part.

view.py

def coporateservices_authorized_Leave(request, id):
if request.method == 'POST':
a=newleave.objects.get(id=id)
form = coporateservices_authoriseleave(request.POST, instance=a)
if form.is_valid():
form.save()
to_emails = [a.admins.all()]
send_mail("RBV Leave Application Email Testing", "Your Leave have been approved",
"RBV eLeave <Rbv_eLeave_System>", [to_emails])
return render_to_response('thankyou.html')
else:
a=newleave.objects.get(id=id)
form = coporateservices_authoriseleave(instance=a)
return render_to_response('coporate_services_leave_approvial.html', {'form': form}, context_instance=RequestContext(request))


model.py

class newleave(models.Model):
first_name = models.CharField(max_length=45)
last_name =models.CharField(max_length=45)
department=models.CharField(max_length =45)
position=models.CharField(max_length =45)
leave_type =models.CharField(max_length=45)
specify_details=models.TextField(default="")
start_date =models.DateField(null=True)
end_date=models.DateField(null=True)
total_working_days=models.IntegerField(null=True)
department_head_authorization =models.CharField(max_length=45, default ="")
authorized_by=models.CharField(max_length=45, default ="")
remarks=models.TextField()
authorization_date =models.DateField(null=True)
corporate_services_authorization =models.CharField(max_length=45)
authorized_by1=models.CharField(max_length=45)
remarks1=models.TextField(default ="")
authoriztaion1_date =models.DateField(null=True)
total_Leave_Left =models.IntegerField(default=20)
username =models.ForeignKey(User, default =1)










On Mon, Feb 2, 2015 at 8:44 PM, monoBOT <monobot.soft@gmail.com> wrote:

2015-02-02 6:51 GMT+01:00 sum abiut <suabiut@gmail.com>:

to_emails = [a.admins.all()]

You are asking for item admins here:


to_emails = [a.admins.all()]



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BxOsGBHGrjKgDV7ZvS%2Bs1EUm571bpQzdGPEn6MGyRKvUqYh2w%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



-




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y4aJYmh1zS--w5vdi6dPJpW_acruQY-JfWbtWUwFgOwHg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWMSDpoKgdzfEEnnVA_q-SVkGqUhgobrZYe-8bb%3DmVR5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment