Changeset 356 in main


Ignore:
Timestamp:
09/29/11 14:15:22 (8 years ago)
Author:
pcosquer
Message:

add stuff to sponsor a new user
TODO: views and forms

Location:
trunk/openPLM
Files:
2 added
4 edited

Legend:

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

    r345 r356  
    3333import openPLM.plmapp.models as models 
    3434from openPLM.plmapp.exceptions import PermissionError 
    35 from openPLM.plmapp.mail import send_mail 
     35from openPLM.plmapp.mail import send_histories_mail 
    3636 
    3737_controller_rx = re.compile(r"(?P<type>\w+)Controller") 
     
    179179                                     details=details, user=self._user) 
    180180        roles = [models.ROLE_OWNER] + list(roles) 
    181         send_mail(self, roles, action, [h], self._user, blacklist, 
     181        send_histories_mail(self, roles, action, [h], self._user, blacklist, 
    182182                users) 
    183183 
  • trunk/openPLM/plmapp/controllers/user.py

    r337 r356  
    3030""" 
    3131 
     32from django.conf import settings 
    3233from django.db.models.fields import FieldDoesNotExist 
    3334from django.utils.translation import ugettext_lazy as _ 
     35from django.contrib.sites.models import Site 
    3436 
    3537import openPLM.plmapp.models as models 
     38from openPLM.plmapp.mail import send_mail 
     39from openPLM.plmapp.utils import generate_password 
    3640from openPLM.plmapp.exceptions import PermissionError 
    3741from openPLM.plmapp.controllers.base import Controller, permission_required 
     
    220224        return self.delegationlink_delegator.order_by("role") 
    221225 
     226    @permission_required(role=models.ROLE_OWNER) 
     227    def sponsor(self, username, first_name, last_name, email, group, 
     228            is_contributor=True): 
     229        try: 
     230            # checs *email* 
     231            if settings.RESTRICT_EMAIL_TO_DOMAINS: 
     232                # i don't know if a domain can contains a '@' 
     233                domain = email.rsplit("@", 1)[1] 
     234                if domain not in Site.objects.values_list("domain", flat=True): 
     235                    raise PermissionError("Email's domain not valid")  
     236        except AttributeError: 
     237            # restriction disabled if the setting is not set 
     238            pass 
     239        password = generate_password() 
     240        new_user = models.User.objects.create(username=username, first_name=first_name, 
     241                last_name=last_name, email=email) 
     242        new_user.is_contributor = is_contributor 
     243        new_user.set_password(password) 
     244        new_user.groups.add(group) 
     245        new_user.save() 
     246        link = models.DelegationLink(delegator=self._user, delegatee=new_user, 
     247                role=models.ROLE_SPONSOR) 
     248        link.save() 
     249        ctx = { 
     250                "new_user" : new_user, 
     251                "sponsor" : self._user, 
     252                "password" : password, 
     253               } 
     254        send_mail("New account on openPLM", [new_user], ctx, "mails/new_account")  
     255        models.UserHistory.objects.create(action="Create", user=self._user, 
     256                plmobject=self._user, details="New user: %s" % new_user.username) 
     257        models.UserHistory.objects.create(action="Create", user=self._user, 
     258                plmobject=new_user, details="Account created") 
     259         
     260         
     261 
     262 
  • trunk/openPLM/plmapp/mail.py

    r344 r356  
    5757 
    5858 
    59 def send_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
     59def send_histories_mail(plmobject, roles, last_action, histories, user, blacklist=(), 
    6060              users=(), template="mails/history"): 
    6161    """ 
     
    8181    recipients = get_recipients(plmobject, roles, users)  
    8282    if recipients: 
    83         emails = User.objects.filter(id__in=recipients).exclude(email="")\ 
    84                         .values_list("email", flat=True) 
    85         emails = set(emails) - set(blacklist) 
    8683        ctx = { 
    8784                "last_action" : last_action, 
     
    8986                "plmobject" : plmobject, 
    9087                "user" : user, 
    91                 "site" : Site.objects.get_current(), 
    9288            } 
     89        return send_mail(subject, recipients, ctx, template, blacklist) 
     90    return set() 
     91 
     92def send_mail(subject, recipients, ctx, template, blacklist=()): 
     93    if recipients: 
     94        r = iter(recipients).next() 
     95        if hasattr(r, "id"): 
     96            recipients = [x.id for x in recipients] 
     97        emails = User.objects.filter(id__in=recipients).exclude(email="")\ 
     98                        .values_list("email", flat=True) 
     99        emails = set(emails) - set(blacklist) 
     100        ctx["site"] = Site.objects.get_current() 
    93101        html_content = render_to_string(template + ".htm", ctx) 
    94102        message = render_to_string(template + ".txt", ctx) 
    95103        msg = EmailMultiAlternatives(subject, message, settings.EMAIL_OPENPLM, 
    96                 emails) 
     104            emails) 
    97105        msg.attach_alternative(html_content, "text/html") 
    98106        t = Thread(target=msg.send, kwargs={"fail_silently" : True }) 
    99107        t.start() 
    100108        return emails 
    101  
    102109    return set() 
    103110 
    104  
    105  
  • trunk/openPLM/plmapp/utils.py

    r261 r356  
    2929import re 
    3030import string 
     31import random 
    3132from functools import wraps 
    3233 
     
    220221    return wrapper 
    221222 
     223password_chars = string.ascii_letters + string.digits + "@!=&/*-_" 
     224def generate_password(length=12): 
     225    """ 
     226    Generates a random password of *length* characters. 
     227    """ 
     228    return "".join(random.choice(password_chars) for x in xrange(length)) 
     229 
    222230 
    223231if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.