def query(datatype, project, past_days, group_pks, all_users):
    """
    Perform the query
    """
    import datetime
    from aiida import orm
    from aiida.common import timezone
    qbl = orm.QueryBuilder()
    if all_users is False:
        user = orm.User.objects.get_default()
        qbl.append(orm.User, tag='creator', filters={'email': user.email})
    else:
        qbl.append(orm.User, tag='creator')
    data_filters = {}
    if past_days is not None:
        now = timezone.now()
        n_days_ago = now - datetime.timedelta(days=past_days)
        data_filters.update({'ctime': {'>=': n_days_ago}})
    qbl.append(datatype, tag='data', with_user='creator', filters=
        data_filters, project=project)
    if group_pks is not None:
        group_filters = dict()
        group_filters.update({'id': {'in': group_pks}})
        qbl.append(orm.Group, tag='group', filters=group_filters, with_node
            ='data')
    qbl.order_by({datatype: {'ctime': 'asc'}})
    object_list = qbl.distinct()
    return object_list.all()
