def post(self,request):
        data = request.POST.copy()
        passs = request.POST.get('password',None)
        pk = request.POST.get('pk',None)
        group = request.POST.get('groups',None)
        
        if group:
            data['groups'] = group


        try:
            instanced = User.objects.get(pk=pk)
        except:
            instanced = None




        data['date_joined'] = datetime.datetime.now()
        response = {}
        forma = FormCreator()
        forma = forma.form_to_model(modelo=User,excludes = [])
        if  instanced:
            forma.base_fields['password'].required=False
        else:
            forma.base_fields['password'].required=True
        forma.base_fields['email'].required=True
        forma.base_fields['groups'].required=True
        
        data['is_active']=True
        data['is_staff']=True
        
        
        if passs:
            data['password']=make_password(data['password'])
        
        forma = forma(data,instance=instanced)

        if forma.is_valid():
            saved = forma.save()

            response['saved'] = 'ok'
            response['datos'] = {'msg':u'guardao con éxito','liga':'/admin/users/'}
            response['callback'] = 'reg_success_user'

        else:
            response['msg'] = 'Verifique su información'
            response['errors'] = forma.errors


        returns = HttpResponse(simplejson.dumps(response))
        
        return returns
Exemplo n.º 2
0
    def post(self, request):
        data = request.POST.copy()
        data['date_joined'] = datetime.datetime.now()
        instanced = None
        response = {}
        forma = FormCreator()
        forma = forma.advanced_form_to_model(modelo=User, excludes=[])

        forma.base_fields['email'].required = True
        forma.base_fields['groups'].required = True
        grupo = data['groups']
        ges = Group.objects.get(name='%s' % data['groups'])
        data['groups'] = (ges.pk)
        data['is_active'] = True
        data['is_staff'] = True
        pax = data['password']
        data['password'] = make_password(data['password'])
        forma = forma(data, instance=instanced)

        if forma.is_valid():
            saved = forma.save()
            if data['groups'] == 'patient':
                full_name = '%s %s' % (data['first_name'], data['last_name'])
                Patient(full_name='')
            if saved:
                usery = authenticate(username=data['username'], password=pax)
                login(request, usery)

            response['saved'] = 'ok'
            response['datos'] = {
                'msg': u'guardao con éxito',
                'liga': '/%s/home/' % (grupo)
            }
            response['callback'] = 'reg_success'

        else:
            response['msg'] = 'Verifique su información'
            response['errors'] = forma.errors

        returns = HttpResponse(simplejson.dumps(response))

        return returns