Beispiel #1
0
def add_user(request):
    """
    Creates a new SEED user.  One of 'organization_id' or 'org_name' is needed.
    Sends invitation email to the new user.

    Payload::

        {
            'organization_id': ID of an existing org to add the new user to,
            'org_name': Name of a new org to create with user as owner
            'first_name': First name of new user
            'last_name': Last name of new user
            'role': {
                'value': The permission level of new user within this org
                    (one of member, viewer, owner)
            },
            'email': Email address of new user.
        }

    Returns::

        {
         'status': 'success',
         'message': email address of new user,
         'org': name of the new org (or existing org),
         'org_created': True if new org created,
         'username': Username of new user
        }


    """
    body = json.loads(request.body)
    org_name = body.get('org_name')
    org_id = body.get('organization_id')
    if ((org_name and org_id) or (not org_name and not org_id)):
        return {
            'status': 'error',
            'message': 'Choose either an existing org or provide a new one'
        }

    if org_id:
        org = Organization.objects.get(pk=org_id)
        org_created = False
    else:
        org, org_created = Organization.objects.get_or_create(name=org_name)

    first_name = body['first_name']
    last_name = body['last_name']
    email = body['email']
    username = body['email']
    user, created = User.objects.get_or_create(username=username.lower())

    # Add the user to the org.  If this is the org's first user,
    # the user becomes the owner/admin automatically.
    # see Organization.add_member()
    if not org.is_member(user):
        org.add_member(user)
    if body.get('role') and body.get('role', {}).get('value'):
        OrganizationUser.objects.filter(
            organization_id=org.pk, user_id=user.pk).update(
                role_level=_get_role_from_js(body['role']['value']))

    if created:
        user.email = email
        user.first_name = first_name
        user.last_name = last_name
    user.save()
    try:
        domain = request.get_host()
    except Exception:
        domain = 'buildingenergy.com'
    invite_to_seed(domain, user.email,
                   default_token_generator.make_token(user), user.pk,
                   first_name)

    return {
        'status': 'success',
        'message': user.email,
        'org': org.name,
        'org_created': org_created,
        'username': user.username
    }
def add_user(request):
    """
    Creates a new SEED user.  One of 'organization_id' or 'org_name' is needed.
    Sends invitation email to the new user.

    Payload::

        {
            'organization_id': ID of an existing org to add the new user to,
            'org_name': Name of a new org to create with user as owner
            'first_name': First name of new user
            'last_name': Last name of new user
            'role': {
                'value': The permission level of new user within this org
                    (one of member, viewer, owner)
            },
            'email': Email address of new user.
        }

    Returns::

        {
         'status': 'success',
         'message': email address of new user,
         'org': name of the new org (or existing org),
         'org_created': True if new org created,
         'username': Username of new user
        }


    """
    body = json.loads(request.body)
    org_name = body.get('org_name')
    org_id = body.get('organization_id')
    if ((org_name and org_id) or (not org_name and not org_id)):
            return {
                'status': 'error',
                'message': 'Choose either an existing org or provide a new one'
            }

    if org_id:
        org = Organization.objects.get(pk=org_id)
        org_created = False
    else:
        org, org_created = Organization.objects.get_or_create(name=org_name)

    first_name = body['first_name']
    last_name = body['last_name']
    email = body['email']
    username = body['email']
    user, created = User.objects.get_or_create(username=username.lower())

    # Add the user to the org.  If this is the org's first user,
    # the user becomes the owner/admin automatically.
    # see Organization.add_member()
    if not org.is_member(user):
        org.add_member(user)
    if body.get('role') and body.get('role', {}).get('value'):
        OrganizationUser.objects.filter(
            organization_id=org.pk,
            user_id=user.pk
        ).update(role_level=_get_role_from_js(body['role']['value']))

    if created:
        user.email = email
        user.first_name = first_name
        user.last_name = last_name
    user.save()
    try:
        domain = request.get_host()
    except Exception:
        domain = 'buildingenergy.com'
    invite_to_seed(domain, user.email,
                   default_token_generator.make_token(user), user.pk,
                   first_name)

    return {'status': 'success', 'message': user.email, 'org': org.name,
            'org_created': org_created, 'username': user.username}
Beispiel #3
0
    def create(self, request):
        """
        Creates a new SEED user.  One of 'organization_id' or 'org_name' is needed.
        Sends invitation email to the new user.
        ---
        parameters:
            - name: organization_id
              description: Organization ID if adding user to an existing organization
              required: false
              type: integer
            - name: org_name
              description: New organization name if creating a new organization for this user
              required: false
              type: string
            - name: first_name
              description: First name of new user
              required: true
              type: string
            - name: last_name
              description: Last name of new user
              required: true
              type: string
            - name: role
              description: one of owner, member, or viewer
              required: true
              type: string
            - name: email
              description: Email address of the new user
              required: true
              type: string
        type:
            status:
                description: success or error
                required: true
                type: string
            message:
                description: email address of new user
                required: true
                type: string
            org:
                description: name of the new org (or existing org)
                required: true
                type: string
            org_created:
                description: True if new org created
                required: true
                type: string
            username:
                description: Username of new user
                required: true
                type: string
            user_id:
                description: User ID (pk) of new user
                required: true
                type: integer
        """
        body = request.data
        org_name = body.get('org_name')
        org_id = body.get('organization_id')
        if (org_name and org_id) or (not org_name and not org_id):
            return JsonResponse({
                'status': 'error',
                'message': 'Choose either an existing org or provide a new one'
            }, status=status.HTTP_409_CONFLICT)

        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        username = body['email']
        user, created = User.objects.get_or_create(username=username.lower())

        if org_id:
            org = Organization.objects.get(pk=org_id)
            org_created = False
        else:
            org, _, _ = create_organization(user, org_name)
            org_created = True

        # Add the user to the org.  If this is the org's first user,
        # the user becomes the owner/admin automatically.
        # see Organization.add_member()
        if not org.is_member(user):
            org.add_member(user)

        if body.get('role'):
            # check if this is a dict, if so, grab the value out of 'value'
            role = body['role']
            if isinstance(role, dict):
                role = role['value']

            OrganizationUser.objects.filter(
                organization_id=org.pk,
                user_id=user.pk
            ).update(role_level=_get_role_from_js(role))

        if created:
            user.email = email
            user.first_name = first_name
            user.last_name = last_name
        user.save()
        try:
            domain = request.get_host()
        except Exception:
            domain = 'buildingenergy.com'  # TODO: What should this value be now?
        invite_to_seed(domain, user.email,
                       default_token_generator.make_token(user), user.pk,
                       first_name)

        return JsonResponse({'status': 'success', 'message': user.email, 'org': org.name,
                             'org_created': org_created, 'username': user.username,
                             'user_id': user.id})
Beispiel #4
0
    def create(self, request):
        """
        Creates a new SEED user.  One of 'organization_id' or 'org_name' is needed.
        Sends invitation email to the new user.
        ---
        parameters:
            - name: organization_id
              description: Organization ID if adding user to an existing organization
              required: false
              type: integer
            - name: org_name
              description: New organization name if creating a new organization for this user
              required: false
              type: string
            - name: first_name
              description: First name of new user
              required: true
              type: string
            - name: last_name
              description: Last name of new user
              required: true
              type: string
            - name: role
              description: one of owner, member, or viewer
              required: true
              type: string
            - name: email
              description: Email address of the new user
              required: true
              type: string
        type:
            status:
                description: success or error
                required: true
                type: string
            message:
                description: email address of new user
                required: true
                type: string
            org:
                description: name of the new org (or existing org)
                required: true
                type: string
            org_created:
                description: True if new org created
                required: true
                type: string
            username:
                description: Username of new user
                required: true
                type: string
            user_id:
                description: User ID (pk) of new user
                required: true
                type: integer
        """
        body = request.data
        org_name = body.get('org_name')
        org_id = body.get('organization_id')
        if (org_name and org_id) or (not org_name and not org_id):
            return JsonResponse(
                {
                    'status': 'error',
                    'message':
                    'Choose either an existing org or provide a new one'
                },
                status=status.HTTP_409_CONFLICT)

        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        username = body['email']
        user, created = User.objects.get_or_create(username=username.lower())

        if org_id:
            org = Organization.objects.get(pk=org_id)
            org_created = False
        else:
            org, _, _ = create_organization(user, org_name)
            org_created = True

        # Add the user to the org.  If this is the org's first user,
        # the user becomes the owner/admin automatically.
        # see Organization.add_member()
        if not org.is_member(user):
            org.add_member(user)

        if body.get('role'):
            # check if this is a dict, if so, grab the value out of 'value'
            role = body['role']
            if isinstance(role, dict):
                role = role['value']

            OrganizationUser.objects.filter(
                organization_id=org.pk,
                user_id=user.pk).update(role_level=_get_role_from_js(role))

        if created:
            user.set_unusable_password()
            user.email = email
            user.first_name = first_name
            user.last_name = last_name
        user.save()

        try:
            domain = request.get_host()
        except Exception:
            domain = 'seed-platform.org'
        invite_to_seed(domain, user.email,
                       default_token_generator.make_token(user), user.pk,
                       first_name)

        return JsonResponse({
            'status': 'success',
            'message': user.email,
            'org': org.name,
            'org_created': org_created,
            'username': user.username,
            'user_id': user.id
        })
Beispiel #5
0
def add_user(request):
    """creates a SEED user with a lowercase username
    json payload in the form:
    {
        u'organization_id': 1,
        u'first_name': u'Bob',
        u'last_name': u'Dole',
        u'role': {
            u'name': u'Member',
            u'value': u'member'
        },
        u'email': u'*****@*****.**'
    }

    """
    body = json.loads(request.body)
    org_name = body.get('org_name')
    org_id = body.get('organization_id')
    if ((org_name and org_id) or (not org_name and not org_id)):
            return {
                'status': 'error',
                'message': 'Choose either an existing org or provide a new one'
            }

    if org_id:
        org = Organization.objects.get(pk=org_id)
        org_created = False
    else:
        org, org_created = Organization.objects.get_or_create(name=org_name)

    first_name = body['first_name']
    last_name = body['last_name']
    email = body['email']
    username = body['email']
    user, created = User.objects.get_or_create(username=username.lower())

    # Add the user to the org.  If this is the org's first user,
    # the user becomes the owner/admin automatically.
    # see Organization.add_member()
    if not org.is_member(user):
        org.add_member(user)
    if body.get('role') and body.get('role', {}).get('value'):
        OrganizationUser.objects.filter(
            organization_id=org.pk,
            user_id=user.pk
        ).update(role_level=_get_role_from_js(body['role']['value']))

    if created:
        user.email = email
        user.first_name = first_name
        user.last_name = last_name
    user.save()
    try:
        domain = request.get_host()
    except Exception:
        domain = 'buildingenergy.com'
    invite_to_seed(domain, user.email,
                   default_token_generator.make_token(user), user.pk,
                   first_name)

    return {'status': 'success', 'message': user.email, 'org': org.name,
            'org_created': org_created, 'username': user.username}
Beispiel #6
0
    def create(self, request):
        """
        Creates a new SEED user.  One of 'organization_id' or 'org_name' is needed.
        Sends invitation email to the new user.
        """
        body = request.data
        org_name = body.get('org_name')
        org_id = request.query_params.get('organization_id', None)
        if (org_name and org_id) or (not org_name and not org_id):
            return JsonResponse({
                'status': 'error',
                'message': 'Choose either an existing org or provide a new one'
            }, status=status.HTTP_409_CONFLICT)

        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        username = body['email']
        user, created = User.objects.get_or_create(username=username.lower())

        if org_id:
            org = Organization.objects.get(pk=org_id)
            org_created = False
        else:
            org, _, _ = create_organization(user, org_name)
            org_created = True

        # Add the user to the org.  If this is the org's first user,
        # the user becomes the owner/admin automatically.
        # see Organization.add_member()
        if not org.is_member(user):
            org.add_member(user)

        if body.get('role'):
            # check if this is a dict, if so, grab the value out of 'value'
            role = body['role']
            try:
                _get_role_from_js(role)
            except Exception:
                return JsonResponse({'status': 'error', 'message': 'valid arguments for role are [viewer, member, '
                                                                   'owner]'},
                                    status=status.HTTP_400_BAD_REQUEST)

            OrganizationUser.objects.filter(
                organization_id=org.pk,
                user_id=user.pk
            ).update(role_level=_get_role_from_js(role))

        if created:
            user.set_unusable_password()
            user.email = email
            user.first_name = first_name
            user.last_name = last_name
        user.save()

        try:
            domain = request.get_host()
        except Exception:
            domain = 'seed-platform.org'
        invite_to_seed(
            domain, user.email, default_token_generator.make_token(user), user.pk, first_name
        )

        return JsonResponse({
            'status': 'success',
            'message': user.email,
            'org': org.name,
            'org_created': org_created,
            'username': user.username,
            'user_id': user.id
        })