Changeset 344 in main


Ignore:
Timestamp:
09/28/11 07:42:00 (8 years ago)
Author:
pcosquer
Message:

send mails if an user or a group is modified

Location:
trunk/openPLM/plmapp
Files:
4 edited

Legend:

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

    r337 r344  
    3333import openPLM.plmapp.models as models 
    3434from openPLM.plmapp.exceptions import PermissionError 
     35from openPLM.plmapp.mail import send_mail 
    3536 
    3637_controller_rx = re.compile(r"(?P<type>\w+)Controller") 
     
    165166        """ 
    166167        self.object.save() 
    167         self.group_info.save() 
    168168        if self._histo and with_history: 
    169169            self._save_histo("Modify", self._histo)  
    170170            self._histo = "" 
    171171 
    172     def _save_histo(self, action, details): 
     172    def _save_histo(self, action, details, blacklist=(), roles=(), users=()): 
    173173        """ 
    174174        Records *action* with details *details* made by :attr:`_user` in 
    175175        on :attr:`object` in the user histories table. 
    176176        """ 
    177         self.HISTORY.objects.create(plmobject=self.object, action=action, 
     177        h = self.HISTORY.objects.create(plmobject=self.object, action=action, 
    178178                                     details=details, user=self._user) 
     179        roles = [models.ROLE_OWNER] + list(roles) 
     180        send_mail(self, roles, action, [h], self._user, blacklist, 
     181                users) 
    179182 
    180183    def get_verbose_name(self, attr_name): 
  • trunk/openPLM/plmapp/controllers/group.py

    r342 r344  
    3232 
    3333import openPLM.plmapp.models as models 
    34 from openPLM.plmapp.controllers.base import Controller 
     34from openPLM.plmapp.controllers.base import Controller, permission_required 
    3535 
    3636class GroupController(Controller): 
     
    141141                user.groups.remove(group) 
    142142            self._save_histo("User removed", ", ".join((u.username for u in users))) 
    143                  
     143         
     144    @permission_required(role=models.ROLE_OWNER) 
     145    def add_user(self, user): 
     146        user.groups.add(self._histo) 
     147         
    144148 
    145149 
     150 
  • trunk/openPLM/plmapp/controllers/plmobject.py

    r337 r344  
    3131 
    3232import openPLM.plmapp.models as models 
    33 from openPLM.plmapp.mail import send_mail 
    3433from openPLM.plmapp.exceptions import RevisionError, PermissionError,\ 
    3534    PromotionError 
     
    179178            pass 
    180179 
    181     def save(self, with_history=True): 
    182         u""" 
    183         Saves :attr:`object` and records its history in the database. 
    184         If *with_history* is False, the history is not recorded. 
    185         """ 
    186         self.object.save() 
    187         if self._histo and with_history: 
    188             self._save_histo("Modify", self._histo)  
    189             self._histo = "" 
    190  
    191     def _save_histo(self, action, details, blacklist=(), roles=()): 
     180    def _save_histo(self, action, details, blacklist=(), roles=(), users=()): 
    192181        """ 
    193182        Records *action* with details *details* made by :attr:`_user` in 
     
    200189        notified by settings the *roles" argument. 
    201190        """ 
    202         h = models.History.objects.create(plmobject=self.object, action=action, 
    203                                      details=details, user=self._user) 
    204191        roles = ["notified"] + list(roles) 
    205         send_mail(self.object, roles, action, [h], self._user, blacklist) 
     192        super(PLMObjectController, self)._save_histo(action, details, 
     193                blacklist, roles, users) 
    206194 
    207195    def has_permission(self, role): 
  • trunk/openPLM/plmapp/mail.py

    r328 r344  
    3737from django.contrib.sites.models import Site 
    3838 
    39 from openPLM.plmapp.models import User, DelegationLink 
     39from openPLM.plmapp.models import User, DelegationLink, ROLE_OWNER 
     40 
     41 
     42def get_recipients(obj, roles, users): 
     43    recipients = set((u.id for u in users)) 
     44    if hasattr(obj, "plmobjectuser_link_plmobject"): 
     45        manager = obj.plmobjectuserlink_plmobject 
     46        for role in roles: 
     47            users = manager.filter(role__contains=role).values_list("user", flat=True) 
     48            recipients.update(users) 
     49            links = DelegationLink.objects.filter(role__contains=role)\ 
     50                        .values_list("delegator", "delegatee") 
     51            gr = kjbuckets.kjGraph(tuple(links)) 
     52            for u in users: 
     53                recipients.update(gr.reachable(u).items()) 
     54    elif ROLE_OWNER: 
     55        recipients.add(obj.owner.id) 
     56    return recipients 
    4057 
    4158 
    4259def send_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
    43               template="mails/history"): 
     60              users=(), template="mails/history"): 
    4461    """ 
    4562    Sends a mail to users who have role *role* for *plmobject*. 
     
    6077        The mail is sent in a separated thread.  
    6178    """ 
    62     subject = "[PLM] " + unicode(plmobject) 
    6379 
    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  
     80    subject = "[PLM] " + unicode(plmobject.object) 
     81    recipients = get_recipients(plmobject, roles, users)  
    7582    if recipients: 
    7683        emails = User.objects.filter(id__in=recipients).exclude(email="")\ 
Note: See TracChangeset for help on using the changeset viewer.