Changeset 414 in main


Ignore:
Timestamp:
10/25/11 16:26:15 (8 years ago)
Author:
pcosquer
Message:

send_*mail functions now use celery's task to perform their stuff

Location:
trunk/openPLM/plmapp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/plmapp/controllers/base.py

    r402 r414  
    286286            self._pending_mails.append(mail) 
    287287        else: 
    288              func(*args) 
    289  
     288            func(*args) 
     289 
  • trunk/openPLM/plmapp/mail.py

    r356 r414  
    2727users about a changement in a :class:`.PLMObject`. 
    2828""" 
    29  
    30 from threading import Thread 
     29from collections import Iterable, Mapping 
    3130 
    3231import kjbuckets 
     
    3433from django.conf import settings 
    3534from django.core.mail import EmailMultiAlternatives 
     35from django.db.models import Model 
    3636from django.template.loader import render_to_string 
     37from django.contrib.contenttypes.models import ContentType 
    3738from django.contrib.sites.models import Site 
     39from celery.task import task 
    3840 
    3941from openPLM.plmapp.models import User, DelegationLink, ROLE_OWNER 
     
    4143 
    4244def get_recipients(obj, roles, users): 
    43     recipients = set((u.id for u in users)) 
     45    recipients = set(users) 
    4446    if hasattr(obj, "plmobjectuser_link_plmobject"): 
    4547        manager = obj.plmobjectuserlink_plmobject 
     
    5658    return recipients 
    5759 
     60def convert_users(users): 
     61    if users: 
     62        r = iter(users).next() 
     63        if hasattr(r, "id"): 
     64            users = [x.id for x in users] 
     65    return users 
    5866 
    59 def send_histories_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
     67class CT(object): 
     68    def __init__(self, ct_id, pk): 
     69        self.ct_id = ct_id 
     70        self.pk = pk 
     71 
     72    @classmethod 
     73    def from_object(cls, obj): 
     74        return cls(ContentType.objects.get_for_model(obj).id, obj.pk) 
     75 
     76    def get_object(self): 
     77        ct = ContentType.objects.get_for_id(self.ct_id) 
     78        return ct.get_object_for_this_type(pk=self.pk) 
     79 
     80 
     81def serialize(obj): 
     82    if isinstance(obj, basestring): 
     83        return obj 
     84    if isinstance(obj, Model): 
     85        return CT.from_object(obj) 
     86    elif isinstance(obj, Mapping): 
     87        new_ctx = {} 
     88        for key, value in obj.iteritems(): 
     89            new_ctx[key] = serialize(value) 
     90        return new_ctx 
     91    elif isinstance(obj, Iterable): 
     92        return [serialize(o) for o in obj] 
     93    return obj 
     94 
     95def unserialize(obj): 
     96    if isinstance(obj, basestring): 
     97        return obj 
     98    if isinstance(obj, CT): 
     99        return obj.get_object() 
     100    elif isinstance(obj, Mapping): 
     101        new_ctx = {} 
     102        for key, value in obj.iteritems(): 
     103            new_ctx[key] = unserialize(value) 
     104        return new_ctx 
     105    elif isinstance(obj, Iterable): 
     106        return [unserialize(o) for o in obj] 
     107    return obj 
     108 
     109@task(ignore_result=True) 
     110def do_send_histories_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
    60111              users=(), template="mails/history"): 
    61112    """ 
     
    70121    :type user: :class:`~django.contrib.auth.models.User`  
    71122    :param blacklist: list of emails whose no mail should be sent (empty by default). 
    72     :return: set of emails whose a mail was sent 
    73123 
    74124    .. note:: 
     
    77127        The mail is sent in a separated thread.  
    78128    """ 
    79  
    80     subject = "[PLM] " + unicode(plmobject.object) 
     129     
     130    plmobject = unserialize(plmobject) 
     131    user = unserialize(user) 
     132    subject = "[PLM] " + unicode(plmobject) 
    81133    recipients = get_recipients(plmobject, roles, users)  
     134     
    82135    if recipients: 
    83136        ctx = { 
     
    87140                "user" : user, 
    88141            } 
    89         return send_mail(subject, recipients, ctx, template, blacklist) 
    90     return set() 
     142        do_send_mail(subject, recipients, ctx, template, blacklist) 
    91143 
    92 def send_mail(subject, recipients, ctx, template, blacklist=()): 
     144@task(ignore_result=True) 
     145def do_send_mail(subject, recipients, ctx, template, blacklist=()): 
    93146    if recipients: 
    94         r = iter(recipients).next() 
    95         if hasattr(r, "id"): 
    96             recipients = [x.id for x in recipients] 
     147        ctx = unserialize(ctx) 
    97148        emails = User.objects.filter(id__in=recipients).exclude(email="")\ 
    98149                        .values_list("email", flat=True) 
     
    104155            emails) 
    105156        msg.attach_alternative(html_content, "text/html") 
    106         t = Thread(target=msg.send, kwargs={"fail_silently" : True }) 
    107         t.start() 
    108         return emails 
    109     return set() 
     157        msg.send(fail_silently=True) 
    110158 
     159def send_mail(subject, recipients, ctx, template, blacklist=()): 
     160    ctx = serialize(ctx) 
     161    do_send_mail.delay(subject, convert_users(recipients), 
     162            ctx, template, blacklist)  
     163 
     164def send_histories_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
     165              users=(), template="mails/history"): 
     166    if hasattr(plmobject, "object"): 
     167        plmobject = plmobject.object 
     168    plmobject = CT.from_object(plmobject) 
     169    histories = serialize(histories) 
     170    user = CT.from_object(user) 
     171    do_send_histories_mail.delay(plmobject, roles, last_action, histories, user, blacklist, 
     172              users, template) 
     173 
     174 
Note: See TracChangeset for help on using the changeset viewer.