Changeset 368 in main


Ignore:
Timestamp:
10/04/11 08:12:58 (8 years ago)
Author:
pcosquer
Message:

views:create: restrict available groups to user's groups

Location:
trunk/openPLM/plmapp
Files:
3 edited

Legend:

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

    r358 r368  
    3838from openPLM.plmapp.controllers.user import UserController 
    3939from openPLM.plmapp.widgets import JQueryAutoComplete 
    40 from openPLM.plmapp.exceptions import PermissionError 
    4140 
    4241class PLMObjectForm(forms.Form): 
     
    6261    return re.sub("\s+", " ", data.strip(" ")) 
    6362 
    64 def get_creation_form(cls=m.PLMObject, data=None, empty_allowed=False): 
     63def get_creation_form(user, cls=m.PLMObject, data=None, empty_allowed=False): 
    6564    u""" 
    6665    Returns a creation form suitable to creates an object 
     
    9089            Form.clean = _clean 
    9190        get_creation_form.cache[cls] = Form 
    92     if data: 
    93         return Form(data=data, empty_permitted=empty_allowed) 
    94     else: 
    95         return Form() 
     91    form = Form(data=data, empty_permitted=empty_allowed) if data else Form() 
     92    if issubclass(cls, m.PLMObject): 
     93        form.fields["group"].queryset = user.groupinfo_set.all() 
     94    return form 
    9695get_creation_form.cache = {} 
    9796         
     
    145144            fields = set(cls.get_creation_fields()) 
    146145            fields.update(set(cls.get_modification_fields())) 
    147             fields.difference_update(("type", "lifecycle")) 
     146            fields.difference_update(("type", "lifecycle", "group")) 
    148147        else: 
    149148            fields = set(["username", "first_name", "last_name"]) 
     
    417416        if sponsor is not None: 
    418417            qset = m.GroupInfo.objects.filter(owner__id=sponsor) 
    419             self.fields["groups"].query_set = qset 
     418            self.fields["groups"].queryset = qset 
    420419        self.fields["groups"].help_text = _("The new user will belong to the selected groups")  
    421420        for key, field in self.fields.iteritems(): 
  • trunk/openPLM/plmapp/views/api.py

    r326 r368  
    147147    except KeyError: 
    148148        return {"result" : "error", 'error' : 'bad type'} 
    149     form = forms.get_creation_form(cls, request.POST) 
     149    form = forms.get_creation_form(request.user, cls, request.POST) 
    150150    if form.is_valid(): 
    151151        controller_cls = get_controller(type_name) 
     
    314314    """ 
    315315    try: 
    316         form = forms.get_creation_form(models.get_all_plmobjects()[typename]) 
     316        form = forms.get_creation_form(request.user, 
     317                models.get_all_plmobjects()[typename]) 
    317318    except KeyError: 
    318319        return {"result" : "error", "fields" : []} 
  • trunk/openPLM/plmapp/views/main.py

    r362 r368  
    637637     
    638638    if request.method == 'GET': 
    639         if request.GET: 
    640             type_form = TypeForm(request.GET) 
    641             if type_form.is_valid(): 
    642                 cls = models.get_all_userprofiles_and_plmobjects()[type_form.cleaned_data["type"]] 
    643                 if issubclass(cls, models.Document): 
    644                     class_for_div="ActiveBox4Doc" 
    645                 else: 
    646                     class_for_div="ActiveBox4Part" 
    647                 creation_form = get_creation_form(cls, {'revision':'a', 'lifecycle': str(models.get_default_lifecycle()), }, True) 
    648                 non_modifyable_attributes = get_non_modifyable_attributes('create', request.user, cls) 
     639        type_form = TypeForm(request.GET) 
     640        if type_form.is_valid(): 
     641            cls = models.get_all_userprofiles_and_plmobjects()[type_form.cleaned_data["type"]] 
     642            if issubclass(cls, models.Document): 
     643                class_for_div="ActiveBox4Doc" 
     644            else: 
     645                class_for_div="ActiveBox4Part" 
     646            data = {'revision':'a', 
     647                    'lifecycle': str(models.get_default_lifecycle()), } 
     648            creation_form = get_creation_form(request.user, cls, data, True) 
     649            non_modifyable_attributes = get_non_modifyable_attributes('create', request.user, cls) 
    649650    elif request.method == 'POST': 
    650         if request.POST: 
    651             type_form = TypeForm(request.POST) 
    652             if type_form.is_valid(): 
    653                 type_name = type_form.cleaned_data["type"] 
    654                 cls = models.get_all_userprofiles_and_plmobjects()[type_name] 
    655                 if issubclass(cls, models.Document): 
    656                     class_for_div="ActiveBox4Doc" 
    657                 else: 
    658                     class_for_div="ActiveBox4Part" 
    659                 non_modifyable_attributes = get_non_modifyable_attributes('create', request.user, cls) 
    660                 creation_form = get_creation_form(cls, request.POST) 
    661                 if creation_form.is_valid(): 
    662                     user = request.user 
    663                     controller_cls = get_controller(type_name) 
    664                     controller = controller_cls.create_from_form(creation_form, user) 
    665                     return HttpResponseRedirect(controller.plmobject_url) 
     651        type_form = TypeForm(request.POST) 
     652        if type_form.is_valid(): 
     653            type_name = type_form.cleaned_data["type"] 
     654            cls = models.get_all_userprofiles_and_plmobjects()[type_name] 
     655            if issubclass(cls, models.Document): 
     656                class_for_div="ActiveBox4Doc" 
     657            else: 
     658                class_for_div="ActiveBox4Part" 
     659            non_modifyable_attributes = get_non_modifyable_attributes('create', request.user, cls) 
     660            creation_form = get_creation_form(request.user, cls, request.POST) 
     661            if creation_form.is_valid(): 
     662                user = request.user 
     663                controller_cls = get_controller(type_name) 
     664                controller = controller_cls.create_from_form(creation_form, user) 
     665                return HttpResponseRedirect(controller.plmobject_url) 
    666666    ctx.update({'class4div': class_for_div, 
    667667                'creation_form': creation_form, 
Note: See TracChangeset for help on using the changeset viewer.