Пример #1
0
    def save(self, send_email=True):
        user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
                                                                    password=self.cleaned_data['password1'],
                                                                    email=self.cleaned_data['email'],
                                                                    site=None,
                                                                    send_email=send_email
                                                                    )
        user.username = user.username.strip().lower()
        user.save()         # remove spaces from username and change all to lowercase
        profile = Profile(user=user)
        profile.save()
        organization = Organization.objects.create( #create organization
            name = self.cleaned_data['organization'],
            trade_name = self.cleaned_data['organization'],
            short_name = slugify(self.cleaned_data['shortname']),
        )
        default_place = Place.objects.create(         #create default place
            label = organization.name,                # use same name as label
            active = True,
            place_type = PlaceType.objects.get(description='Matriz'), # mandatory field
            organization = organization,              # link place to this organization
        )
        default_room = Room.objects.create(
            description = 'Sala 1',
            place = default_place,
            room_type=RoomType.objects.all()[0],
        )

        person = Person.objects.create(name=self.cleaned_data['name'], user=user) #, organization=organization)
        person.organization.add(organization)
        profile.org_active = organization                  #set org as active
        profile.temp = self.cleaned_data['password1']      # temporary field (LDAP)
        profile.person = person
        profile.save()
        
        try:
            admin_role = Group.objects.get(name='administrator')
            Role.objects.create(profile=profile, organization=organization, group=admin_role)
            profile.user.groups.add(admin_role)
        except:
            pass
        try:
            professional_role = Group.objects.get(name='professional')
            Role.objects.create(profile=profile, organization=organization, group=professional_role)
            profile.user.groups.add(professional_role)
        except:
            pass

        careprof = CareProfessional()
        careprof.person = person
        pp = ProfessionalProfile()
        pi = ProfessionalIdentification()
        pp.save()
        pi.save()
        careprof.professionalProfile = pp
        careprof.professionalIdentification = pi
        careprof.save()

        return organization
Пример #2
0
def create_user(request):
    if not request.method == 'POST':
        raise Http404

    if User.objects.filter(
            username=request.POST.get('username').strip().lower()):
        messages.success(
            request,
            _('Error adding user <b>%s</b>: Username already exists.') %
            request.POST.get('username').strip().lower())
        return HttpResponseRedirect('/user/add/?clss=error')

    person = get_object_or_404(
        Person,
        pk=request.POST.get('id_person'),
        organization=request.user.get_profile().org_active)
    organization = request.user.get_profile().org_active
    username = request.POST.get('username').strip().lower()
    password = request.POST.get('password')
    pwd_conf = request.POST.get('pwd_conf')
    email = request.POST.get('email_send_user')
    permissions = request.POST.getlist('perms')

    if not password == pwd_conf:
        messages.success(request,
                         _('Error: Supplied passwords are differents.'))
        return HttpResponseRedirect('/user/add/?clss=error')

    site_url = "http://%s" % get_current_site(
        request
    ).domain if not request.is_secure else "http://%s" % get_current_site(
        request).domain
    user = RegistrationProfile.objects.create_inactive_user(
        username, email, password, site_url)

    # this is required! without it, password will not set ok
    user.set_password(password)
    user.save(force_update=True)

    profile = Profile(user=user)
    profile.org_active = organization
    profile.temp = password  # temporary field (LDAP)
    profile.person = person
    profile.save()
    # profile.organization.add(organization)

    define_user_permission(permissions, profile, organization, 'administrator')
    define_user_permission(permissions, profile, organization, 'professional')
    define_user_permission(permissions, profile, organization, 'secretary')
    define_user_permission(permissions, profile, organization, 'client')
    define_user_permission(permissions, profile, organization, 'student')

    messages.success(
        request,
        _('User created successfully. An email will be sent to the user with instructions on how to finish the registration process.'
          ))

    return HttpResponseRedirect('/user/%s/' % profile.person.id)
Пример #3
0
def create_user(request):
    if not request.method == 'POST':
        raise Http404

    if User.objects.filter(username=request.POST.get('username').strip().lower()):
        messages.success(request, _('Error adding user <b>%s</b>: Username already exists.') % request.POST.get('username').strip().lower())
        return HttpResponseRedirect('/user/add/?clss=error')
        
    person = get_object_or_404(Person, pk=request.POST.get('id_person'), organization=request.user.get_profile().org_active)
    organization = request.user.get_profile().org_active
    username = request.POST.get('username').strip().lower()
    password = request.POST.get('password')
    pwd_conf = request.POST.get('pwd_conf')
    email = request.POST.get('email_send_user')
    permissions = request.POST.getlist('perms')

    if not password == pwd_conf:
        messages.success(request, _('Error: Supplied passwords are differents.'))
        return HttpResponseRedirect('/user/add/?clss=error')
    
    site_url = "http://%s" % get_current_site(request).domain if not request.is_secure else "http://%s" % get_current_site(request).domain
    user = RegistrationProfile.objects.create_inactive_user(username, email, password, site_url)

    user.set_password(password) # this is required! without it, password will not set ok
    user.save(force_update=True)

    profile = Profile(user=user)
    profile.org_active = organization
    profile.temp = password    # temporary field (LDAP)
    profile.person = person
    profile.save()
    #profile.organization.add(organization)

    if permissions.count('administrator'):
        Role.objects.create(profile=profile, organization=organization, group=Group.objects.get(name='administrator'))
        profile.user.groups.add(Group.objects.get(name='administrator'))
        
    if permissions.count('professional'):
        Role.objects.create(profile=profile, organization=organization, group=Group.objects.get(name='professional'))
        profile.user.groups.add(Group.objects.get(name='professional'))
                    
    if permissions.count('secretary'):
        Role.objects.create(profile=profile, organization=organization, group=Group.objects.get(name='secretary'))
        profile.user.groups.add(Group.objects.get(name='secretary'))
                    
    if permissions.count('client'):
        Role.objects.create(profile=profile, organization=organization, group=Group.objects.get(name='client'))
        profile.user.groups.add(Group.objects.get(name='client'))

    if permissions.count('student'):
        Role.objects.create(profile=profile, organization=organization, group=Group.objects.get(name='student'))
        profile.user.groups.add(Group.objects.get(name='student'))
     
    messages.success(request, _('User created successfully. An email will be sent to the user with instructions on how to finish the registration process.'))

    return HttpResponseRedirect('/user/%s/' % profile.person.id)
Пример #4
0
def form(request, object_id=None):
    """
        Register a new User
        object_id : Person.id
    """
    person = Person.objects.get( pk=object_id, organization=request.user.get_profile().org_active )
    permissions = [] # to check in template, checkbox permissions
    errors = False

    # update
    if hasattr(person,'profile'):
        profile = Profile.objects.get(person=object_id, person__organization=request.user.get_profile().org_active)

        # store permissions
        for x in profile.user.groups.all():
            permissions.append(u"%s" % x.name)

    # save form
    if request.POST:

        permissions = request.POST.getlist('perms')
        username = slugify(request.POST.get('username'))

        if not permissions:
            messages.error(request, _('Select one or more group permission.'))
            errors = True

        if not username:
            messages.error(request, _('Invalid Username'))
            errors = True

        # update
        if hasattr(person,'profile'):
            if not errors:
                profile.user.username = slugify(request.POST.get('username'))
                profile.user.save()
                set_permission(request, profile, permissions)
                messages.success(request, _('User updated successfully'))
                return HttpResponseRedirect('/user/%s/' % person.id)

        # new
        if not hasattr(person,'profile'):
            password = request.POST.get('password')
            pwd_conf = request.POST.get('pwd_conf')
            email = request.POST.get('email_send_user')

            # temp
            user = User()
            user.username = username
            user.password = password # temp
            user.email = email

            profile = Profile()
            profile.user = user

            person.profile = profile

            if User.objects.filter( username=username ):
                messages.error(request, _("Username already exists, try another.") )
                errors = True

            if not password == pwd_conf or not password:
                user.password = ""
                messages.error(request, _('Password confirmation does not match. Please try again') )
                errors = True

            if not email:
                messages.error(request, _('Email address do not match'))
                errors = True

            if not errors:
                site_url = "https://%s" % get_current_site(request).domain

                # overwrite user
                user = RegistrationProfile.objects.create_inactive_user(username, email, password, site_url)
                user.set_password(password)
                user.save()

                profile.user = user
                profile.org_active = request.user.get_profile().org_active
                profile.save()

                person.profile = set_permission(request, profile, permissions)
                person.save()

                # return success to form
                messages.success(request, _('User created successfully. An email will be sent to the user with instructions on how to finish the registration process.'))
                return HttpResponseRedirect('/user/%s/' % person.id)

    # mount form, error, new or update
    return render_to_response('users/users_form.html', {
                        'person': person,
                        'permissions': permissions,
                        }, context_instance=RequestContext(request))
Пример #5
0
def form(request, object_id=None):
    """
        Register a new User
        object_id : Person.id
    """
    person = Person.objects.get(
        pk=object_id, organization=request.user.get_profile().org_active)
    permissions = []  # to check in template, checkbox permissions
    errors = False

    # update
    if hasattr(person, 'profile'):
        profile = Profile.objects.get(
            person=object_id,
            person__organization=request.user.get_profile().org_active)

        # store permissions
        for x in profile.user.groups.all():
            permissions.append(u"%s" % x.name)

    # save form
    if request.POST:

        permissions = request.POST.getlist('perms')
        username = slugify(request.POST.get('username'))

        if not permissions:
            messages.error(request, _('Select one or more group permission.'))
            errors = True

        if not username:
            messages.error(request, _('Invalid Username'))
            errors = True

        # update
        if hasattr(person, 'profile'):
            if not errors:
                profile.user.username = slugify(request.POST.get('username'))
                profile.user.save()
                set_permission(request, profile, permissions)
                messages.success(request, _('User updated successfully'))
                return HttpResponseRedirect('/user/%s/' % person.id)

        # new
        if not hasattr(person, 'profile'):
            password = request.POST.get('password')
            pwd_conf = request.POST.get('pwd_conf')
            email = request.POST.get('email_send_user')

            # temp
            user = User()
            user.username = username
            user.password = password  # temp
            user.email = email

            profile = Profile()
            profile.user = user

            person.profile = profile

            if User.objects.filter(username=username):
                messages.error(request,
                               _("Username already exists, try another."))
                errors = True

            if not password == pwd_conf or not password:
                user.password = ""
                messages.error(
                    request,
                    _('Password confirmation does not match. Please try again')
                )
                errors = True

            if not email:
                messages.error(request, _('Email address do not match'))
                errors = True

            if not errors:
                site_url = "https://%s" % get_current_site(request).domain

                # overwrite user
                user = RegistrationProfile.objects.create_inactive_user(
                    username, email, password, site_url)
                user.set_password(password)
                user.save()

                profile.user = user
                profile.org_active = request.user.get_profile().org_active
                profile.save()

                person.profile = set_permission(request, profile, permissions)
                person.save()

                # return success to form
                messages.success(
                    request,
                    _('User created successfully. An email will be sent to the user with instructions on how to finish the registration process.'
                      ))
                return HttpResponseRedirect('/user/%s/' % person.id)

    # mount form, error, new or update
    return render_to_response('users/users_form.html', {
        'person': person,
        'permissions': permissions,
    },
                              context_instance=RequestContext(request))