コード例 #1
0
def main():
    bb = BbRest(key, secret, bbURL)
    userName = '******'
    courseId = 'dev-bs'
    user = bb.GetUser(userId=userName).json()
    #     print(user.request.url)
    print(user)
    print(f"\nuser ID: {user['userName']} \
          email: {user['contact']['email']}")
    course = bb.GetCourse(courseId=courseId).json()
    print(f"course ID: {course['id']} \
          external ID: {course['externalId']}")
    memberships = bb.GetCourseMemberships(courseId=courseId).json()
    print('\nmemberships:', memberships)
    enrollments = bb.GetUserMemberships(userId=userName).json()
    print('\nuser enrollments:', enrollments)
    enrollment = bb.GetMembership(courseId=courseId, userId=userName).json()
    print('\ncourse user enrollment:', enrollment)

    new_user_data = {
        'externalId': 'devcon19_user',
        'userName': '******',
        'name': {
            'given': 'DevCon19',
            'family': 'User'
        },
        'password': '******',
        'contact': {
            'email': '*****@*****.**'
        }
    }
    user_name = new_user_data['userName']
    del_user = bb.DeleteUser(user_name)
    print(f'\nDeleted User: {del_user}')
    new_user = bb.CreateUser(payload=new_user_data)
    print('\n Created new user:', new_user.json())
コード例 #2
0
def users(request):
    task = request.GET.get('task')
    searchBy = request.GET.get('searchBy')
    searchValueUsr = request.GET.get('searchValueUsr')
    if (searchValueUsr is not None):
        searchValueUsr = searchValueUsr.strip()
    print("SEARCHBY: ", searchBy)
    print("SEARCHVALUEUSR: "******"TASK: ", task)
    """View function for users page of site."""
    bb_json = request.session.get('bb_json')
    if (bb_json is None):
        bb = BbRest(KEY, SECRET, f"https://{LEARNFQDN}")
        bb_json = jsonpickle.encode(bb)
        print('pickled BbRest putting it on session')
        request.session['bb_json'] = bb_json
        request.session['target_view'] = 'users'
        return HttpResponseRedirect(reverse('get_auth_code'))
    else:
        print('got BbRest from session')
        bb = jsonpickle.decode(bb_json)
        if bb.is_expired():
            print('expired token')
            request.session['bb_json'] = None
            whoami(request)
        bb.supported_functions()  # This and the following are required after
        bb.method_generator()  # unpickling the pickled object.
        print(f'expiration: {bb.expiration()}')

    if (task == 'search'):
        #Process request...
        print(f"USERS REQUEST: ACTION {task}")
        searchBy = request.GET.get('searchBy')
        searchValueUsr = request.GET.get('searchValue')
        if (searchValueUsr is not None):
            searchValueUsr = searchValueUsr.strip()
        print(f"USERS REQUEST: USR: {searchValueUsr}")
        print(f"Process by {searchBy}")
        if (searchBy == 'externalId'):
            usr = "******" + searchValueUsr
            print(f"user pattern: {usr}")
        elif (searchBy == 'userName'):
            usr = "******" + searchValueUsr
            print(f"user pattern: {usr}")
        resp = bb.GetUser(
            userId=usr,
            params={
                'fields':
                'id, userName, name.given, name.middle, name.family, externalId, contact.email, availability.available, dataSourceId, created'
            },
            sync=True)
        if (resp.status_code == 200):
            user_json = resp.json()
            dskresp = bb.GetDataSource(dataSourceId=user_json['dataSourceId'],
                                       sync=True)
            dsk_json = dskresp.json()
            user_json['dataSourceId'] = dsk_json['externalId']
            user_json['searchValueUsr'] = searchValueUsr
            user_json['searchBy'] = searchBy
            dskresp = bb.GetDataSources(params={'fields': 'id, externalId'},
                                        sync=True)
            dsks_json = dskresp.json()
            print("DSKS:\n", dsks_json["results"])
            dsks = dsks_json["results"]
            dsks = sortDsk(dsks)
            print("SIZE OF DSK LIST:", len(dsks))

            context = {
                'user_json': user_json,
                'dsks_json': dsks,
            }
        else:
            error_json = resp.json()
            print(f"RESPONSE:\n", error_json)
            context = {
                'error_json': error_json,
            }

        return render(request, 'users.html', context=context)

    if (task == 'process'):
        print(f"USERS REQUEST: ACTION {task}")
        print(f"Process by {searchBy}")
        print('Request:\n ')
        print(request)
        payload = {}
        if (request.GET.get('isAvailabilityUpdateRequired1')):
            if (request.GET.get('isAvailabilityUpdateRequired1') == 'true'):
                payload = {
                    'availability': {
                        "available": request.GET.get('selectedAvailability')
                    }
                }
        if (request.GET.get('isDataSourceKeyUpdateRequired1')):
            if (request.GET.get('isDataSourceKeyUpdateRequired1') == 'true'):
                payload["dataSourceId"] = request.GET.get(
                    'selectedDataSourceKey')

        print("PAYLOAD\n")
        for x, y in payload.items():
            print(x, y)

        # Build and make bb request...
        if (searchBy == 'externalId'):
            usr = "******" + searchValueUsr
        elif (searchBy == 'userName'):
            usr = "******" + searchValueUsr

        print(f"user pattern: {usr}")

        resp = bb.UpdateUser(
            userId=usr,
            payload=payload,
            params={
                'fields':
                'id, userName, name.given, name.middle, name.family, externalId, contact.email, availability.available, dataSourceId, created'
            },
            sync=True)
        if (resp.status_code == 200):
            result_json = resp.json()  #return actual error
            dskresp = bb.GetDataSource(
                dataSourceId=result_json['dataSourceId'], sync=True)
            dsk_json = dskresp.json()
            result_json['dataSourceId'] = dsk_json['externalId']

            context = {
                'result_json': result_json,
            }
        else:
            error_json = resp.json()
            print(f"RESPONSE:\n", error_json)
            context = {
                'error_json': error_json,
            }

        return render(request, 'users.html', context=context)

    return render(request, 'users.html')