Ejemplo n.º 1
0
def insert(user):
    """Create a new request

    Args:

        user: Django User

    Returns:

        New account request
    """
    try:
        # check if a user with the same username exists
        _get_user_by_username(user.username)
        raise ApiError("A user with the same username already exists.")
    except ObjectDoesNotExist:
        user.save()

        # Create the account request and save it
        account_request = AccountRequest(
            username=user.username,
            first_name=user.first_name,
            last_name=user.last_name,
            email=user.email,
        )

        context = {"URI": SERVER_URI}
        template_path = "core_website_app/admin/email/request_account_for_admin.html"
        send_mail_api.send_mail_to_administrators(
            subject="New Account Request",
            path_to_template=template_path,
            context=context,
        )
        return account_request.save()
Ejemplo n.º 2
0
def get_all():
    """
        List of opened account requests

        Returns:
            list: List of all requests
    """
    return AccountRequest.objects()
Ejemplo n.º 3
0
def get_all():
    """List of opened account requests

    Returns:

        List of all requests
    """
    return AccountRequest.get_all()
Ejemplo n.º 4
0
def insert(user):
    """
        Create a new request

        Parameters:
            user:

        Returns: new account request
    """
    try:
        # check if a user with the same username exists
        _get_user_by_username(user.username)
        raise ApiError('A user with the same username already exists.')
    except ObjectDoesNotExist:
        user.save()

        # Create the account request and save it
        account_request = AccountRequest(username=user.username)
        return account_request.save()
Ejemplo n.º 5
0
def post(request):
    """ Create a new account request

    Parameters:

        {
            "username": "******",
            "firstname": "firstname",
            "lastname": "lastname",
            "password": "******",
            "email": "email"
        }

    Args:

        request: HTTP request

    Returns:

        - code: 200
          content: Requested Account
        - code: 400
          content: Validation error / missing parameters
    """
    try:
        # Get parameters
        username = request.DATA['username']
        first_name = request.DATA['firstname']
        last_name = request.DATA['lastname']
        password = request.DATA['password']
        email = request.DATA['email']

        account_request_from_request = AccountRequest(username, first_name,
                                                      last_name, password,
                                                      email)
        try:
            # Create the request
            request_content = account_request_api.insert(
                account_request_from_request)
            # Serialize the request
            serializer = AccountRequestSerializer(request_content)
            if serializer.is_valid():
                return Response(serializer.data, status=status.HTTP_200_OK)
            else:
                content = {'message': 'Serialization failed'}
                return Response(content, status=status.HTTP_400_BAD_REQUEST)

        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)

    except Exception as e:
        content = {'message': 'Expected parameters not provided.'}
        logger.exception(e.message)
        return Response(content, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 6
0
def _create_account_request(username="******"):
    """
    Create an AccountRequest object using default parameters

    Parameters:
        username (str):

    Returns:
        AccountRequest object
    """
    return AccountRequest(username=username)
Ejemplo n.º 7
0
def get(account_request_id):
    """Get an account request given its primary key

    Args:

        account_request_id: Primary key of the request

    Returns:

        Account request
    """
    try:
        return AccountRequest.get_by_id(account_request_id)
    except:
        raise ApiError("No request could be found with the given id.")
Ejemplo n.º 8
0
def get(account_request_id):
    """
        Get an account request given its primary key

        Parameters:
            account_request_id (str): Primary key of the request

        Returns:
            :class:`~models.AccountRequest`: The corresponding account request

        Raises:
            MDCSError: If no `request_id` does not correspond to any request.
    """
    try:
        return AccountRequest.get_by_id(account_request_id)
    except:
        raise ApiError('No request could be found with the given id.')
Ejemplo n.º 9
0
def post(request):
    """
        Post a new account request

        Parameters:
            request (HttpRequest): request

        Returns:
            Response object
    """
    try:
        # Get parameters
        username = request.DATA['username']
        first_name = request.DATA['firstname']
        last_name = request.DATA['lastname']
        password = request.DATA['password']
        email = request.DATA['email']

        account_request_from_request = AccountRequest(username, first_name,
                                                      last_name, password,
                                                      email)
        try:
            # Create the request
            request_content = account_request_api.insert(
                account_request_from_request)
            # Serialize the request
            serializer = AccountRequestSerializer(request_content)
            if serializer.is_valid():
                return Response(serializer.data, status=status.HTTP_200_OK)
            else:
                content = {'message': 'Serialization failed'}
                return Response(content, status=status.HTTP_400_BAD_REQUEST)

        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)

    except Exception as e:
        content = {'message': 'Expected parameters not provided.'}
        logger.exception(e.message)
        return Response(content, status=status.HTTP_400_BAD_REQUEST)