Changeset 328 in main


Ignore:
Timestamp:
09/26/11 07:53:46 (8 years ago)
Author:
pcosquer
Message:

add mails notification

Location:
trunk/openPLM
Files:
3 added
5 edited

Legend:

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

    r311 r328  
    447447                details = "change state from %(first)s to %(second)s" % \ 
    448448                                     {"first" :state.name, "second" : new_state} 
    449                 blacklist = send_mail(self.object, "sign_", "Promote", 
    450                                       details, self._user) 
    451                 self._save_histo("Promote", details, blacklist) 
     449                self._save_histo("Promote", details, roles=["sign_"]) 
    452450            except IndexError: 
    453451                # FIXME raises it ? 
     
    473471            details = "change state from %(first)s to %(second)s" % \ 
    474472                    {"first" :state.name, "second" : new_state} 
    475             blacklist = send_mail(self.object, "sign_", "Demote", details, self._user) 
    476             self._save_histo("Demote", details, blacklist) 
     473            self._save_histo("Demote", details, roles=["sign_"]) 
    477474        except IndexError: 
    478475            # FIXME raises it ? 
     
    513510            self.__histo = "" 
    514511 
    515     def _save_histo(self, action, details, blacklist=()): 
     512    def _save_histo(self, action, details, blacklist=(), roles=()): 
    516513        """ 
    517514        Records *action* with details *details* made by :attr:`_user` in 
     
    520517        *blacklist*, if given, should be a list of email whose no mail should 
    521518        be sent (empty by default). 
    522         """ 
    523         models.History.objects.create(plmobject=self.object, action=action, 
     519 
     520        A mail is sent to all notified users. Moreover, more roles can be 
     521        notified by settings the *roles" argument. 
     522        """ 
     523        h = models.History.objects.create(plmobject=self.object, action=action, 
    524524                                     details=details, user=self._user) 
    525         send_mail(self.object, "notified", action, details, self._user, blacklist) 
     525        roles = ["notified"] + list(roles) 
     526        send_mail(self.object, roles, action, [h], self._user, blacklist) 
    526527 
    527528    @permission_required(role="owner") 
     
    622623            when :attr:`object` changes 
    623624        """ 
     625        if new_notified != self._user: 
     626            self.check_permission("owner") 
    624627        models.PLMObjectUserLink.objects.create(plmobject=self.object, 
    625628            user=new_notified, role="notified") 
     
    637640            when :attr:`object` changes 
    638641        """ 
    639  
     642         
     643        if notified != self._user: 
     644            self.check_permission("owner") 
    640645        link = models.PLMObjectUserLink.objects.get(plmobject=self.object, 
    641646                user=notified, role="notified") 
  • trunk/openPLM/plmapp/mail.py

    r261 r328  
    2828""" 
    2929 
     30from threading import Thread 
     31 
    3032import kjbuckets 
    3133 
    3234from django.conf import settings 
    33 from django.core.mail import send_mail as sm 
     35from django.core.mail import EmailMultiAlternatives 
     36from django.template.loader import render_to_string 
     37from django.contrib.sites.models import Site 
    3438 
    3539from openPLM.plmapp.models import User, DelegationLink 
    3640 
    37 _TEMPLATE = u""" 
    38 Message from OpenPLM 
    3941 
    40 Modification for {plmobject}: 
    41     - Type : {action} 
    42     - By : {user} 
    43     - Details : 
    44         {details} 
    45 """ 
    46  
    47 def send_mail(plmobject, role, action, details, user, blacklist=()): 
     42def send_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
     43              template="mails/history"): 
    4844    """ 
    4945    Sends a mail to users who have role *role* for *plmobject*. 
     
    5147    :param plmobject: object which was modified 
    5248    :type plmobject: :class:`.PLMObject` 
    53     :param str role: role of the users who should be notified (can be just the 
    54                      first characters of the role to match several roles) 
    55     :param str action: type of modification 
    56     :param str details: details 
     49    :param str roles: list of roles of the users who should be notified 
     50    :param str last_action: type of modification 
     51    :param str histories: list of :class:`.AbstractHistory` 
    5752    :param user: user who made the modification 
    5853    :type user: :class:`~django.contrib.auth.models.User`  
     
    6358 
    6459        This function fails silently if it can not send the mail. 
     60        The mail is sent in a separated thread.  
    6561    """ 
    66     subject = u"[OpenPLM] -- %s" % plmobject 
    67     details = details.replace("\n", "\n" + " " * 8) 
    68     message = _TEMPLATE.format(**locals()) 
    69     users = plmobject.plmobjectuserlink_plmobject.filter(role__contains=role).values_list("user", flat=True) 
    70     recipients = set(users) 
    71     links = DelegationLink.objects.filter(role__contains=role)\ 
    72                 .values_list("delegator", "delegatee") 
    73     gr = kjbuckets.kjGraph(tuple(links)) 
    74     for user in users: 
    75         recipients.update(gr.reachable(user).items()) 
     62    subject = "[PLM] " + unicode(plmobject) 
     63 
     64    recipients = set() 
     65    manager = plmobject.plmobjectuserlink_plmobject 
     66    for role in roles: 
     67        users = manager.filter(role__contains=role).values_list("user", flat=True) 
     68        recipients.update(users) 
     69        links = DelegationLink.objects.filter(role__contains=role)\ 
     70                    .values_list("delegator", "delegatee") 
     71        gr = kjbuckets.kjGraph(tuple(links)) 
     72        for u in users: 
     73            recipients.update(gr.reachable(u).items()) 
     74 
    7675    if recipients: 
    7776        emails = User.objects.filter(id__in=recipients).exclude(email="")\ 
    7877                        .values_list("email", flat=True) 
    7978        emails = set(emails) - set(blacklist) 
    80         sm(subject, message, settings.EMAIL_OPENPLM, emails, fail_silently=True) 
     79        ctx = { 
     80                "last_action" : last_action, 
     81                "histories" : histories,  
     82                "plmobject" : plmobject, 
     83                "user" : user, 
     84                "site" : Site.objects.get_current(), 
     85            } 
     86        html_content = render_to_string(template + ".htm", ctx) 
     87        message = render_to_string(template + ".txt", ctx) 
     88        msg = EmailMultiAlternatives(subject, message, settings.EMAIL_OPENPLM, 
     89                emails) 
     90        msg.attach_alternative(html_content, "text/html") 
     91        t = Thread(target=msg.send, kwargs={"fail_silently" : True }) 
     92        t.start() 
    8193        return emails 
     94 
    8295    return set() 
    8396 
     97 
     98 
  • trunk/openPLM/plmapp/tests/controllers.py

    r261 r328  
    262262        controller.set_role(user, "owner") 
    263263        self.assertEqual(controller.owner, user) 
    264         controller.set_role(user, "notified") 
    265         PLMObjectUserLink.objects.get(user=user, plmobject=controller.object, 
     264        controller.set_role(self.user, "notified") 
     265        PLMObjectUserLink.objects.get(user=self.user, plmobject=controller.object, 
    266266                                      role="notified") 
    267267        controller.set_role(user, level_to_sign_str(0)) 
  • trunk/openPLM/plmapp/views/main.py

    r325 r328  
    5757from django.utils.encoding import iri_to_uri 
    5858from django.utils.translation import ugettext_lazy as _ 
     59from django.forms import HiddenInput 
    5960 
    6061from openPLM.plmapp.exceptions import ControllerError 
     
    620621    object_management_list = models.PLMObjectUserLink.objects.filter(plmobject=obj) 
    621622    object_management_list = object_management_list.order_by("role") 
     623    if not ctx["is_owner"]: 
     624        link = object_management_list.filter(role="notified", user=request.user) 
     625        ctx["is_notified"] = bool(link) 
     626        if link: 
     627            ctx["remove_notify_link"] = link[0] 
     628        else: 
     629            initial = { "type" : "User", 
     630                        "username" : request.user.username 
     631                      } 
     632            form = ReplaceManagementForm(initial=initial) 
     633            for field in ("type", "username"): 
     634                form.fields[field].widget = HiddenInput()  
     635            ctx["notify_self_form"] = form 
    622636    ctx.update({'current_page':'management', 
    623637                'object_management': object_management_list}) 
  • trunk/openPLM/templates/DisplayObjectManagement.htm

    r286 r328  
    55 
    66{% block content %} 
    7 <!--        {{ rel_part_formset.management_form }} --> 
    87    {% if is_owner %} 
    98        <span class="ButtonLeft"> 
    10             <a class="Button" href="./add/">{% trans "ADD" %}</a> 
     9            <a class="Button" href="./add/" >{% trans "Notify" %}</a> 
    1110        </span> 
     11    {% else %} 
     12        {% if is_notified %} 
     13            <form  method="POST" action="./delete/"> 
     14                <input type="hidden" name="link_id" value="{{ remove_notify_link.id }}"/> 
     15                <input name="action" type="submit" value="{% trans "Unnotify me" %}" /> 
     16            </form> 
     17        {% else %} 
     18            <form  method="POST" action="./add/"> 
     19                {{ notify_self_form.as_p }} 
     20                <input name="action" type="submit" value="{% trans "Notify me" %}" /> 
     21            </form> 
     22         {% endif %} 
    1223    {% endif %} 
     24 
    1325    <table class="Content"> 
    1426        {% for link in object_management %} 
Note: See TracChangeset for help on using the changeset viewer.