Exemple #1
0
def get_user(request):
    user = verify_token(request)
    if not user:
        return HttpResponse(status=403)
    user_id = request.GET.get('id', '')
    if user_id:
        users = User.objects.filter(id=user_id)
        if len(users) != 1:
            response = HttpResponse(json.dumps({'msg': 'User Not Found'}),
                                    content_type='application/json')
            response.status_code = 404
            return response
        user = users[0]
    service_tokens = ServiceToken.objects.filter(user=user)
    providers = []
    for service_token in service_tokens:
        providers.append(service_token.provider)
    school_dict = None
    if user.school:
        school_dict = {'name': user.school.name, 'picKey': user.school.pic_key}
    return JsonResponse({
        'username': user.username,
        'firstName': user.first_name,
        'lastName': user.last_name,
        'picKey': user.pic_key,
        'school': school_dict,
        'providers': providers
    })
Exemple #2
0
def modify_user(request):
    user = verify_token(request)
    if not user:
        return HttpResponse(status=403)
    body_in = json.loads(request.body.decode('utf-8'))

    if 'username' in body_in:
        if has_profanity(body_in['username']):
            response = HttpResponse(json.dumps(
                {'msg': 'Username Contains Profanity'}),
                                    content_type='application/json')
            response.status_code = 400
            return response
        user.username = body_in['username']

    if 'firstName' in body_in:
        if has_profanity(body_in['firstName']):
            response = HttpResponse(json.dumps(
                {'msg': 'First Name Contains Profanity'}),
                                    content_type='application/json')
            response.status_code = 400
            return response
        user.first_name = body_in['firstName']

    if 'lastName' in body_in:
        if has_profanity(body_in['lastName']):
            response = HttpResponse(json.dumps(
                {'msg': 'Last Name Contains Profanity'}),
                                    content_type='application/json')
            response.status_code = 400
            return response
        user.last_name = body_in['lastName']

    if "school" in body_in:
        if body_in["school"] is not None:
            schools = School.objects.filter(name=body_in["school"]["name"])
            if len(schools) != 1:
                response = HttpResponse(json.dumps({'msg':
                                                    'School Not Found'}),
                                        content_type='application/json')
                response.status_code = 404
                return response
            user.school = schools[0]
        else:
            user.school = None
    try:
        user.save()
    except:
        response = HttpResponse(json.dumps({'msg': 'Username Already Exists'}),
                                content_type='application/json')
        response.status_code = 400
        return response
    return HttpResponse(status=200)
Exemple #3
0
def reassign_google_token(request):
    user = verify_token(request)
    if not user:
        return HttpResponse(status=403)

    body_in = json.loads(request.body.decode("utf-8"))
    access_token = body_in["accessToken"]
    print("Access token: ", access_token)
    entries = ServiceToken.objects.filter(access_token=access_token)
    if len(entries) == 1:
        exp_date = entries[0].expiration_date.timestamp()
        if exp_date > time.time():
            entries[0].user_id = user.id
            entries[0].save()
            return HttpResponse(status=200)
    return HttpResponse(status=404)
Exemple #4
0
def follow_class(request):
    if request.method == 'POST':

        user = verify_token(request)
        if not user:
            return HttpResponse(status=403)

        post_json = json.loads(request.body)
        class_pk = post_json.get('classID')
        if class_pk.startswith('"') and class_pk.endswith('"'):
            class_pk = class_pk[1:-1]
        try:
            class_pk_int = int(class_pk)
        except TypeError:
            return HttpResponse(status=400)
        try:
            class_obj = Class.objects.get(pk=class_pk_int)
            user.classes.remove(class_obj)
            return HttpResponse(status=200)
        except ObjectDoesNotExist:
            return HttpResponse(status=404)
Exemple #5
0
def exchange_google_code(request):
    google_client_id = settings.GOOGLE_CLIENT_ID
    google_client_secret = settings.GOOGLE_CLIENT_SECRET

    user = verify_token(request)
    if not user:
        return HttpResponse(status=403)

    body_in = json.loads(request.body.decode("utf-8"))

    headers = {"content-type": "application/json"}
    body_google_req = {
        "code": body_in["code"],
        "client_id": google_client_id,
        "client_secret": google_client_secret,
        "redirect_uri":
        "postmessage",  # I don't why but this has to be postmessage
        "grant_type": "authorization_code"
    }
    google_response = requests.post(
        "https://www.googleapis.com/oauth2/v4/token",
        json=body_google_req,
        headers=headers)
    print("GOOGLE RESP: ", google_response.text)
    body_google_resp = json.loads(google_response.text)
    if "refresh_token" in body_google_resp:
        exp_date = datetime.datetime.utcnow() + datetime.timedelta(
            seconds=body_google_resp["expires_in"])
        ServiceToken.objects.create(
            access_token=body_google_resp["access_token"],
            refresh_token=body_google_resp["refresh_token"],
            provider="google",
            expiration_date=exp_date,
            user_id=user.id)
    return JsonResponse({
        'idToken': body_google_resp["id_token"],
        "accessToken": body_google_resp["access_token"]
    })