def test_get_open_datarequests_number(self):

        n_datarequests = 7
        count = 'example'

        db.func = MagicMock()
        db.func.count.return_value = count

        filter_by = MagicMock()
        filter_by.scalar.return_value = n_datarequests

        query = MagicMock()
        query.filter_by = MagicMock(return_value=filter_by)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        db.DataRequest.id = 'id'
        result = db.DataRequest.get_open_datarequests_number()

        # Assertions
        self.assertEquals(n_datarequests, result)
        query.filter_by.assert_called_once_with(closed=False)
        model.Session.query.assert_called_once_with(count)
        db.func.count.assert_called_once_with(db.DataRequest.id)
Example #2
0
def datarequest_comments(context, data_dict):
    """
    Total count of active data request comments across an organisation
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_end_date = data_dict.get('utc_end_date', None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(func.count(distinct(Comment.id))).filter(
            _and_(CommentThread.url.like(DATAREQUEST_LIKE),
                  Comment.state == ACTIVE_STATE,
                  Comment.creation_date >= utc_start_date,
                  Comment.creation_date < utc_end_date,
                  db.DataRequest.organization_id == org_id)).join(
                      CommentThread,
                      CommentThread.id == Comment.thread_id).join(
                          db.DataRequest, db.DataRequest.id == _replace_(
                              CommentThread.url, DATAREQUEST_PREFIX,
                              ''))).scalar()

    except Exception as e:
        log.error(str(e))
Example #3
0
def datarequests_min_one_comment_follower(context, data_dict):
    """
    Number of Data Requests across an organisation with at least one comment follower
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_end_date = data_dict.get('utc_end_date', None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(func.count(distinct(
            db.DataRequest.id))).filter(
                _and_(CommentThread.url.like(DATAREQUEST_LIKE),
                      Comment.state == ACTIVE_STATE,
                      Comment.creation_date >= utc_start_date,
                      Comment.creation_date < utc_end_date,
                      db.DataRequest.organization_id == org_id)).join(
                          CommentThread, CommentThread.url == func.concat(
                              DATAREQUEST_PREFIX, db.DataRequest.id)).join(
                                  CommentNotificationRecipient,
                                  CommentNotificationRecipient.thread_id ==
                                  CommentThread.id).join(
                                      Comment,
                                      Comment.thread_id == CommentThread.id)
                ).scalar()

    except Exception as e:
        log.error(str(e))
Example #4
0
    def test_get_open_datarequests_number(self):

        n_datarequests = 7
        count = 'example'

        db.func = MagicMock()
        db.func.count.return_value = count

        filter_by = MagicMock()
        filter_by.scalar.return_value = n_datarequests

        query = MagicMock()
        query.filter_by = MagicMock(return_value=filter_by)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        db.DataRequest.id = 'id'
        result = db.DataRequest.get_open_datarequests_number()

        # Assertions
        self.assertEquals(n_datarequests, result)
        query.filter_by.assert_called_once_with(closed=False)
        model.Session.query.assert_called_once_with(count)
        db.func.count.assert_called_once_with(db.DataRequest.id)
Example #5
0
def create_datarequest(original_action, context, data_dict):
    """
    Action to create a new data request. The function checks the access rights
    of the user before creating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    not valid.

    Data QLD modification
    Will send email notification to users of assigned organisation with admin access

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: The ID of the organization you want to asign the
        data request (optional).
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict
    """

    model = context['model']
    session = context['session']

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.CREATE_DATAREQUEST, context, data_dict)

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Store the data
    data_req = db.DataRequest()
    _undictize_datarequest_basic(data_req, data_dict)
    data_req.user_id = context['auth_user_obj'].id
    data_req.open_time = datetime.datetime.utcnow()

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    if datarequest_dict['organization']:
        # Data QLD modification
        users = _get_admin_users_from_organisation(datarequest_dict)
        users.discard(context['auth_user_obj'].id)
        _send_mail(users, 'new_datarequest_organisation', datarequest_dict,
                   'Data Request Created Email')

    return datarequest_dict
Example #6
0
    def test_get_datarequests_comments(self):

        n_comments = 7
        count = 'example'

        db.func = MagicMock()
        db.func.count.return_value = count

        filter_by = MagicMock()
        filter_by.scalar.return_value = n_comments

        query = MagicMock()
        query.filter_by = MagicMock(return_value=filter_by)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        params = {'datarequest_id': 'example_uuid_v4'}
        db.Comment.id = 'id'
        result = db.Comment.get_datarequest_comments_number(**params)

        # Assertions
        self.assertEquals(n_comments, result)
        query.filter_by.assert_called_once_with(**params)
        model.Session.query.assert_called_once_with(count)
        db.func.count.assert_called_once_with(db.Comment.id)
    def test_get_datarequest_followers_number(self):

        n_followers = 7
        count = 'example'

        db.func = MagicMock()
        db.func.count.return_value = count

        filter_by = MagicMock()
        filter_by.scalar.return_value = n_followers

        query = MagicMock()
        query.filter_by = MagicMock(return_value=filter_by)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        params = {
            'datarequest_id': 'example_uuid_v4'
        }
        db.DataRequestFollower.id = 'id'
        result = db.DataRequestFollower.get_datarequest_followers_number(**params)

        # Assertions
        self.assertEquals(n_followers, result)
        query.filter_by.assert_called_once_with(**params)
        model.Session.query.assert_called_once_with(count)
        db.func.count.assert_called_once_with(db.DataRequestFollower.id)
Example #8
0
    def test_get_datarequest_followers_number(self):

        n_followers = 7
        count = "example"

        db.func = MagicMock()
        db.func.count.return_value = count

        filter_by = MagicMock()
        filter_by.scalar.return_value = n_followers

        query = MagicMock()
        query.filter_by = MagicMock(return_value=filter_by)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        params = {"datarequest_id": "example_uuid_v4"}
        db.DataRequestFollower.id = "id"
        result = db.DataRequestFollower.get_datarequest_followers_number(
            **params)

        # Assertions
        self.assertEquals(n_followers, result)
        query.filter_by.assert_called_once_with(**params)
        model.Session.query.assert_called_once_with(count)
        db.func.count.assert_called_once_with(db.DataRequestFollower.id)
Example #9
0
    def _test_get(self, table):
        '''
        Aux method for Comment and Data Requests
        '''
        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        final_query = MagicMock()
        final_query.filter_by.return_value = query_result

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        params = {
            'title': 'Default Title',
            'organization_id': 'example_uuid_v4'
        }
        result = getattr(db, table).get(**params)

        # Assertions
        self.assertEquals(db_response, result)
        final_query.filter_by.assert_called_once_with(**params)
Example #10
0
def datarequests_open_after_x_days(context, data_dict):
    """
    Data requests that have no comments whatsoever, and it has
    been > 10 days since the data request was opened.
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_expected_closure_date = data_dict.get('utc_expected_closure_date',
                                              None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(
            db.DataRequest.id,
            db.DataRequest.title,
            db.DataRequest.open_time,
        ).filter(
            _and_(db.DataRequest.organization_id == org_id,
                  db.DataRequest.closed.is_(False),
                  db.DataRequest.open_time >= utc_start_date,
                  db.DataRequest.open_time <
                  utc_expected_closure_date)).order_by(
                      db.DataRequest.open_time.desc())).all()

    except Exception as e:
        log.error(str(e))
    def _test_get(self, table):
        '''
        Aux method for Comment and Data Requests
        '''
        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        final_query = MagicMock()
        final_query.filter_by.return_value = query_result

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        params = {
            'title': 'Default Title',
            'organization_id': 'example_uuid_v4'
        }
        result = getattr(db, table).get(**params)

        # Assertions
        self.assertEquals(db_response, result)
        final_query.filter_by.assert_called_once_with(**params)
Example #12
0
def datarequests_no_replies_after_x_days(context, data_dict):
    """
    Data request comments that have no replies whatsoever, and it has been > 10 days since the comment was created
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_reply_expected_by_date = data_dict.get('utc_reply_expected_by_date',
                                               None)

    check_org_access(org_id)

    comment_reply = aliased(Comment, name='comment_reply')

    try:
        db.init_db(model)
        comments = (_session_.query(
            Comment.id.label("comment_id"),
            Comment.parent_id, Comment.creation_date, Comment.subject,
            User.name.label('username'), CommentThread.url,
            db.DataRequest.id.label("datarequest_id"), db.DataRequest.title,
            db.DataRequest.open_time, comment_reply.parent_id,
            comment_reply.creation_date, comment_reply.comment).filter(
                _and_(CommentThread.url.like(DATAREQUEST_LIKE),
                      Comment.parent_id.is_(None),
                      Comment.creation_date >= utc_start_date,
                      Comment.creation_date < utc_reply_expected_by_date,
                      Comment.state == ACTIVE_STATE,
                      db.DataRequest.organization_id == org_id,
                      comment_reply.id.is_(None))).join(
                          CommentThread,
                          CommentThread.id == Comment.thread_id).join(
                              User, Comment.user_id == User.id).join(
                                  db.DataRequest,
                                  db.DataRequest.id == _replace_(
                                      CommentThread.url, DATAREQUEST_PREFIX,
                                      '')).
                    outerjoin(
                        (comment_reply,
                         Comment.id == comment_reply.parent_id)).order_by(
                             Comment.creation_date.desc())).all()

        comments_to_show = []
        for comment in comments:
            try:
                check_user_access('create_dataset', {"user": comment.username})
                # User has editor, admin or sysadmin access to a organisation
            except NotAuthorized:
                # User is only a member of a organisation or has no organisation access
                # Add user comment
                comments_to_show.append(comment)
                continue
        return comments_to_show

    except Exception as e:
        log.error(str(e))
Example #13
0
    def test_initdb_initialized(self):
        db.DataRequest = MagicMock()
        db.Comment = MagicMock()

        # Call the function
        model = MagicMock()
        db.init_db(model)

        # Assert that table method has been called
        self.assertEquals(0, db.sa.Table.call_count)
        self.assertEquals(0, model.meta.mapper.call_count)
Example #14
0
    def test_initdb_initialized(self):
        db.DataRequest = MagicMock()
        db.Comment = MagicMock()

        # Call the function
        model = MagicMock()
        db.init_db(model)

        # Assert that table method has been called
        self.assertEquals(0, db.sa.Table.call_count)
        self.assertEquals(0, model.meta.mapper.call_count)
Example #15
0
    def test_initdb_not_initialized(self):

        table_data_request = MagicMock()
        table_comment = MagicMock()

        db.sa.Table = MagicMock(side_effect=[table_data_request, table_comment])

        # Call the function
        model = MagicMock()
        db.init_db(model)

        # Assert that table method has been called
        self.assertEquals(2, db.sa.Table.call_count)
        model.meta.mapper.assert_any_call(db.DataRequest, table_data_request)
        model.meta.mapper.assert_any_call(db.Comment, table_comment)
Example #16
0
    def test_initdb_not_initialized(self):

        table_data_request = MagicMock()
        table_comment = MagicMock()

        db.sa.Table = MagicMock(side_effect=[table_data_request, table_comment])

        # Call the function
        model = MagicMock()
        db.init_db(model)

        # Assert that table method has been called
        self.assertEquals(2, db.sa.Table.call_count)
        model.meta.mapper.assert_any_call(db.DataRequest, table_data_request)
        model.meta.mapper.assert_any_call(db.Comment, table_comment)
    def clearBBDD(self):
        # Clean Solr
        search_index.clear_index()

        # Clean the database
        model.repo.rebuild_db()

        # Delete previous users
        db.init_db(model)
        datarequests = db.DataRequest.get()
        for datarequest in datarequests:
            model.Session.delete(datarequest)

        comments = db.Comment.get()
        for comment in comments:
            model.Session.delete(comment)

        model.Session.commit()
    def clearBBDD(self):
        # Clean Solr
        search_index.clear_index()

        # Clean the database
        model.repo.rebuild_db()

        # Delete previous users
        db.init_db(model)
        datarequests = db.DataRequest.get()
        for datarequest in datarequests:
            model.Session.delete(datarequest)

        comments = db.Comment.get()
        for comment in comments:
            model.Session.delete(comment)

        model.Session.commit()
Example #19
0
    def test_datarequest_exist(self, first_result, expected_result):

        title = 'DataRequest Title'

        # Prepare the mocks
        def _lower(text):
            # If expected_result == true it's because lower is supossed
            # to return the same result in the two calls
            if expected_result:
                return title.lower()
            else:
                return text

        db.func.lower.side_effect = _lower

        # Query
        query_result = MagicMock()
        query_result.first.return_value = first_result

        final_query = MagicMock()
        final_query.filter.return_value = query_result

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        db.DataRequest.title = 'TITLE'
        result = db.DataRequest.datarequest_exists(title)

        # Assertion
        self.assertEquals(expected_result, result)
        db.func.lower.assert_any_call(db.DataRequest.title)
        db.func.lower.assert_any_call(title)
        # If expected_result == true is because lower is supossed
        # to return the same result in the two calls and the
        # equalization of these results must be True
        final_query.filter.assert_called_once_with(expected_result)
Example #20
0
def datarequests_no_replies_after_x_days(context, data_dict):
    """
    Data request comments that have no replies whatsoever, and it has been > 10 days since the comment was created
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_reply_expected_by_date = data_dict.get('utc_reply_expected_by_date',
                                               None)

    check_org_access(org_id)

    comment_reply = aliased(Comment, name='comment_reply')

    try:
        db.init_db(model)
        return (_session_.query(
            Comment.id.label("comment_id"), Comment.parent_id,
            Comment.creation_date, Comment.subject, CommentThread.url,
            db.DataRequest.id.label("datarequest_id"), db.DataRequest.title,
            db.DataRequest.open_time, comment_reply.parent_id,
            comment_reply.creation_date, comment_reply.comment).filter(
                _and_(
                    CommentThread.url.like(DATAREQUEST_LIKE),
                    Comment.parent_id.is_(None),
                    Comment.creation_date >= utc_start_date,
                    Comment.creation_date < utc_reply_expected_by_date,
                    Comment.state == ACTIVE_STATE,
                    db.DataRequest.organization_id == org_id,
                    comment_reply.id.is_(None))).join(
                        CommentThread,
                        CommentThread.id == Comment.thread_id).join(
                            db.DataRequest, db.DataRequest.id == _replace_(
                                CommentThread.url, DATAREQUEST_PREFIX,
                                '')).outerjoin(
                                    (comment_reply,
                                     Comment.id == comment_reply.parent_id
                                     )).order_by(
                                         Comment.creation_date.desc())).all()

    except Exception as e:
        log.error(str(e))
    def test_datarequest_exist(self, first_result, expected_result):

        title = 'DataRequest Title'

        # Prepare the mocks
        def _lower(text):
            # If expected_result == true it's because lower is supossed
            # to return the same result in the two calls
            if expected_result:
                return title.lower()
            else:
                return text

        db.func.lower.side_effect = _lower

        # Query
        query_result = MagicMock()
        query_result.first.return_value = first_result

        final_query = MagicMock()
        final_query.filter.return_value = query_result

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)

        # Call the method
        db.DataRequest.title = 'TITLE'
        result = db.DataRequest.datarequest_exists(title)

        # Assertion
        self.assertEquals(expected_result, result)
        db.func.lower.assert_any_call(db.DataRequest.title)
        db.func.lower.assert_any_call(title)
        # If expected_result == true is because lower is supossed
        # to return the same result in the two calls and the
        # equalization of these results must be True
        final_query.filter.assert_called_once_with(expected_result)
Example #22
0
def datarequests_for_circumstance(context, data_dict):
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_end_date = data_dict.get('utc_end_date', None)
    circumstance = data_dict.get('circumstance', None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(db.DataRequest).filter(
            db.DataRequest.organization_id == org_id,
            db.DataRequest.close_circumstance == circumstance,
            db.DataRequest.open_time >= utc_start_date,
            db.DataRequest.open_time < utc_end_date,
            db.DataRequest.closed.is_(True)).order_by(
                db.DataRequest.close_time.desc())).all()

    except Exception as e:
        log.error(str(e))
Example #23
0
def datarequests(context, data_dict):
    """
    Return the data requests assigned to an organisation
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_end_date = data_dict.get('utc_end_date', None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(db.DataRequest).filter(
            db.DataRequest.organization_id == org_id,
            func.date(db.DataRequest.open_time) >= utc_start_date,
            func.date(db.DataRequest.open_time) < utc_end_date,
        ).order_by(db.DataRequest.open_time.desc())).all()

    except Exception as e:
        log.error(str(e))
Example #24
0
    def _test_get_ordered_by_date(self, table, time_column):
        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        no_ordered = MagicMock()
        no_ordered.order_by.return_value = query_result

        final_query = MagicMock()
        final_query.filter_by.return_value = no_ordered

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)
        # Mapping
        table = getattr(db, table)
        time_column_value = MagicMock()
        setattr(table, time_column, time_column_value)

        # Call the method
        params = {
            'title': 'Default Title',
            'organization_id': 'example_uuid_v4'
        }
        result = table.get_ordered_by_date(**params)

        # Assertions
        self.assertEquals(db_response, result)
        no_ordered.order_by.assert_called_once_with(time_column_value.desc())
        final_query.filter_by.assert_called_once_with(**params)
Example #25
0
def dataset_comment_followers(context, data_dict):
    """
    Number of dataset comment followers at an organization level
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_end_date = data_dict.get('utc_end_date', None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(
            # We want to count a user each time they follow a comment thread, not just unique user IDs
            func.count(
                distinct(
                    tuple_(CommentNotificationRecipient.user_id,
                           CommentNotificationRecipient.thread_id))
            )).filter(
                _and_(CommentThread.url.like(DATASET_LIKE),
                      Comment.state == ACTIVE_STATE,
                      Comment.creation_date >= utc_start_date,
                      Comment.creation_date < utc_end_date,
                      Package.owner_org == org_id,
                      Package.state == ACTIVE_STATE)).join(
                          CommentThread,
                          CommentThread.id == CommentNotificationRecipient.
                          thread_id).join(Comment).join(
                              Package, Package.name == _replace_(
                                  CommentThread.url, DATASET_PREFIX,
                                  ''))).scalar()

    except Exception as e:
        log.error(str(e))
Example #26
0
    def _test_get_ordered_by_date(self, table, time_column):
        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        no_ordered = MagicMock()
        no_ordered.order_by.return_value = query_result

        final_query = MagicMock()
        final_query.filter_by.return_value = no_ordered

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)
        # Mapping
        table = getattr(db, table)
        time_column_value = MagicMock()
        setattr(table, time_column, time_column_value)

        # Call the method
        params = {
            'title': 'Default Title',
            'organization_id': 'example_uuid_v4'
        }
        result = table.get_ordered_by_date(**params)

        # Assertions
        self.assertEquals(db_response, result)
        no_ordered.order_by.assert_called_once_with(time_column_value.desc())
        final_query.filter_by.assert_called_once_with(**params)
Example #27
0
def open_datarequests_no_comments_after_x_days(context, data_dict):
    """
    Data requests that have no comments whatsoever, and it has
    been > 10 days since the data request was opened.
    :param context:
    :param data_dict:
    :return:
    """
    org_id = data_dict.get('org_id', None)
    utc_start_date = data_dict.get('utc_start_date', None)
    utc_reply_expected_by_date = data_dict.get('utc_reply_expected_by_date',
                                               None)

    check_org_access(org_id)

    try:
        db.init_db(model)
        return (_session_.query(
            db.DataRequest.id,
            db.DataRequest.title,
            db.DataRequest.open_time,
            CommentThread.url,
        ).filter(
            _and_(db.DataRequest.organization_id == org_id,
                  db.DataRequest.closed.is_(False),
                  db.DataRequest.open_time >= utc_start_date,
                  db.DataRequest.open_time < utc_reply_expected_by_date,
                  Comment.id.is_(None))).outerjoin(
                      CommentThread, CommentThread.url == func.concat(
                          DATAREQUEST_PREFIX, db.DataRequest.id)).outerjoin(
                              Comment,
                              Comment.thread_id == CommentThread.id).order_by(
                                  db.DataRequest.open_time.desc())).all()

    except Exception as e:
        log.error(str(e))
Example #28
0
    def _test_get_ordered_by_date(self, table, time_column, params):

        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        no_ordered = MagicMock()
        no_ordered.order_by.return_value = query_result

        final_query = MagicMock()
        final_query.filter.return_value = final_query
        final_query.filter_by.return_value = no_ordered

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)
        # Mapping
        table = getattr(db, table)
        time_column_value = MagicMock()
        title_column_value = MagicMock()
        description_column_value = MagicMock()
        setattr(table, time_column, time_column_value)
        setattr(table, "title", title_column_value)
        setattr(table, "description", description_column_value)

        # Call the method
        result = table.get_ordered_by_date(**params)

        # Calculate expected filter parameters
        expected_filter_by_params = params.copy()

        if "q" in expected_filter_by_params:
            expected_filter_by_params.pop("q")

        if "desc" in expected_filter_by_params:
            expected_filter_by_params.pop("desc")

        query = f'%{params["q"]}%' if "q" in params else None
        desc = True if "desc" in params and params["desc"] is True else False

        # Assertions
        self.assertEquals(db_response, result)
        order = time_column_value.desc() if desc else time_column_value.asc()
        no_ordered.order_by.assert_called_once_with(order)
        final_query.filter_by.assert_called_once_with(
            **expected_filter_by_params)

        # This only happens with the table of data requests
        if query:
            title_column_value.ilike.assert_called_once_with(query)
            description_column_value.ilike.assert_called_once_with(query)
            db.or_.assert_called_once_with(
                title_column_value.ilike.return_value,
                description_column_value.ilike.return_value,
            )

            final_query.filter.assert_called_once_with(db.or_.return_value)
Example #29
0
def open_datarequest(context, data_dict):
    """
    Action to open a data request. Access rights will be checked before
    opening the data request. If the user is not allowed, a NotAuthorized
    exception will be risen.

    :param id: The ID of the data request to be closed
    :type id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict

    """

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    # Check id
    if not datarequest_id:
        raise tk.ValidationError(tk._('Data Request ID has not been included'))

        # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.OPEN_DATAREQUEST, context, data_dict)

    # Get the data request
    result = db.DataRequest.get(id=datarequest_id)

    if not result:
        raise tk.ObjectNotFound(
            tk._('Data Request %s not found in the data base') %
            datarequest_id)

    data_req = result[0]
    data_req.closed = False
    data_req.accepted_dataset_id = None
    data_req.close_time = None
    if tk.h.closing_circumstances_enabled:
        data_req.close_circumstance = None
        data_req.approx_publishing_date = None

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    # Mailing
    users = [data_req.user_id]
    # Creator email
    _send_mail(users, 'open_datarequest_creator', datarequest_dict,
               'Data Request Opened Creator Email')
    if datarequest_dict['organization']:
        users = _get_admin_users_from_organisation(datarequest_dict)
        # Admins of organisation email
        _send_mail(users, 'open_datarequest_organisation', datarequest_dict,
                   'Data Request Opened Admins Email')

    return datarequest_dict
Example #30
0
def close_datarequest(original_action, context, data_dict):
    """
    Action to close a data request. Access rights will be checked before
    closing the data request. If the user is not allowed, a NotAuthorized
    exception will be risen.

    Data QLD modification
    Will send email notification to the data request creator

    :param id: The ID of the data request to be closed
    :type id: string

    :param accepted_dataset_id: The ID of the dataset accepted as solution
        for this data request
    :type accepted_dataset_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict

    """

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    # Check id
    if not datarequest_id:
        raise tk.ValidationError(tk._('Data Request ID has not been included'))

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.CLOSE_DATAREQUEST, context, data_dict)

    # Get the data request
    result = db.DataRequest.get(id=datarequest_id)
    if not result:
        raise tk.ObjectNotFound(
            tk._('Data Request %s not found in the data base') %
            datarequest_id)

    # Validate data
    validator.validate_datarequest_closing(context, data_dict)

    data_req = result[0]

    # Was the data request previously closed?
    if data_req.closed:
        raise tk.ValidationError([tk._('This Data Request is already closed')])

    data_req.closed = True
    data_req.accepted_dataset_id = data_dict.get('accepted_dataset_id') or None
    data_req.close_time = datetime.datetime.utcnow()
    _undictize_datarequest_closing_circumstances(data_req, data_dict)

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    # Mailing
    users = [data_req.user_id]
    _send_mail(users, 'close_datarequest_creator', datarequest_dict,
               'Data Request Closed Send Email')

    return datarequest_dict
Example #31
0
def update_datarequest(original_action, context, data_dict):
    """
    Action to update a data request. The function checks the access rights of
    the user before updating the data request. If the user is not allowed
    a NotAuthorized exception will be risen.

    In addition, you should note that the parameters will be checked and an
    exception (ValidationError) will be risen if some of these parameters are
    invalid.

    Data QLD modification
    Will send email notification if organisation was changed to users of assigned organisation with admin access

    :param id: The ID of the data request to be updated
    :type id: string

    :param title: The title of the data request
    :type title: string

    :param description: A brief description for your data request
    :type description: string

    :param organiztion_id: The ID of the organization you want to asign the
        data request.
    :type organization_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict
    """

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    if not datarequest_id:
        raise tk.ValidationError(tk._('Data Request ID has not been included'))

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.UPDATE_DATAREQUEST, context, data_dict)

    # Get the initial data
    result = db.DataRequest.get(id=datarequest_id)
    if not result:
        raise tk.ObjectNotFound(
            tk._('Data Request %s not found in the data base') %
            datarequest_id)

    data_req = result[0]

    # Avoid the validator to return an error when the user does not change the title
    context['avoid_existing_title_check'] = data_req.title == data_dict[
        'title']

    # Validate data
    validator.validate_datarequest(context, data_dict)

    # Data QLD modification
    organisation_updated = data_req.organization_id != data_dict[
        'organization_id']
    if organisation_updated:
        unassigned_organisation_id = data_req.organization_id

    # Set the data provided by the user in the data_red
    _undictize_datarequest_basic(data_req, data_dict)

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    if datarequest_dict['organization'] and organisation_updated:
        # Data QLD modification
        # Email Admin users of the assigned organisation
        users = _get_admin_users_from_organisation(datarequest_dict)
        users.discard(context['auth_user_obj'].id)
        _send_mail(users, 'new_datarequest_organisation', datarequest_dict,
                   'Data Request Assigned Email')
        # Email Admin users of unassigned organisation
        org_dict = {
            'organization': _get_organization(unassigned_organisation_id)
        }
        users = _get_admin_users_from_organisation(org_dict)
        users.discard(context['auth_user_obj'].id)
        _send_mail(users, 'unassigned_datarequest_organisation',
                   datarequest_dict, 'Data Request Unassigned Email')

    return datarequest_dict
    def _test_get_ordered_by_date(self, table, time_column, params):

        db_response = [MagicMock(), MagicMock(), MagicMock()]

        query_result = MagicMock()
        query_result.all.return_value = db_response

        no_ordered = MagicMock()
        no_ordered.order_by.return_value = query_result

        final_query = MagicMock()
        final_query.filter.return_value = final_query
        final_query.filter_by.return_value = no_ordered

        query = MagicMock()
        query.autoflush = MagicMock(return_value=final_query)

        model = MagicMock()
        model.DomainObject = object
        model.Session.query = MagicMock(return_value=query)

        # Init the database
        db.init_db(model)
        # Mapping
        table = getattr(db, table)
        time_column_value = MagicMock()
        title_column_value = MagicMock()
        description_column_value = MagicMock()
        setattr(table, time_column, time_column_value)
        setattr(table, 'title', title_column_value)
        setattr(table, 'description', description_column_value)

        # Call the method
        result = table.get_ordered_by_date(**params)

        # Calculate expected filter parameters
        expected_filter_by_params = params.copy()

        if 'q' in expected_filter_by_params:
            expected_filter_by_params.pop('q')

        if 'desc' in expected_filter_by_params:
            expected_filter_by_params.pop('desc')

        query = '%{0}%'.format(params['q']) if 'q' in params else None
        desc = True if 'desc' in params and params['desc'] is True else False

        # Assertions
        self.assertEquals(db_response, result)
        order = time_column_value.desc() if desc else time_column_value.asc()
        no_ordered.order_by.assert_called_once_with(order)
        final_query.filter_by.assert_called_once_with(**expected_filter_by_params)

        # This only happens with the table of data requests
        if query:
            title_column_value.ilike.assert_called_once_with(query)
            description_column_value.ilike.assert_called_once_with(query)
            db.or_.assert_called_once_with(title_column_value.ilike.return_value,
                                           description_column_value.ilike.return_value)

            final_query.filter.assert_called_once_with(db.or_.return_value)