def GetEducation():
    result = Result()
    expFrom = request.args.get('from') if request.args.get(
        'from') is not None else '1988-01-01'
    expTo = request.args.get('to') if request.args.get(
        'to') is not None else '3000-01-01'
    try:
        parsedDateFrom = datetime.strptime(expFrom, '%Y-%m-%d')
        parsedDateTo = datetime.strptime(expTo, '%Y-%m-%d')

    except ValueError:
        result.AddError('Invalid Datetime format')

    if parsedDateFrom > parsedDateTo:
        result.AddError('Date from cannot be greater then Date from')
        return result.ToResponse()

    query_education = Education.objects(
        BeginDate__gte=parsedDateFrom, EndDate__lte=parsedDateTo)

    result.Value = query_education.to_json()
    return result.ToResponse()
def UpdateEducationById(id):
    result = Result()
    update = request.get_json(force=True)
    try:
        dbObj = Education.objects.filter(Id=id).first()
        updateDbObj = dbObj.to_mongo()
        for (key, value) in update.items():
            updateDbObj[key] = value

        del updateDbObj['_id']
        dbObj.update(**updateDbObj)
        result.Value = dbObj.to_json()
    except mongoengine.errors.ValidationError as e:
        for (field, err) in e.to_dict().items():
            result.AddError(field + " : " + str(err))
    except AttributeError:
        result.AddError(
            'This education does not exist, perhaps you wished to add it?')
    except InvalidQueryError:
        result.AddError('Invalid field in the update statement, please review')
    except:
        result.AddError(sys.exc_info()[0])
    return result.ToResponse()
def refresh():
    result = Result()
    username = get_jwt_identity()
    loggingUser = User.objects(Email=username).first()
    loggingUser = User.objects(Email=username).first()
    userRole = loggingUser.AccountType
    userId = loggingUser.Id
    forToken = TokenInfo(username, userRole, userId)
    if loggingUser is None:
        result.AddError("User not found/Invalid Token")
        return result.ToResponse()
    ret = {
        'access_token': create_access_token(identity=forToken),
        'refresh_token': create_refresh_token(identity=forToken)
    }
    result.Value = json.dumps(ret)
    return result.ToResponse()
def register():
    result = Result()
    if not request.is_json:
        result.AddError("Missing JSON in request")
    username = request.json.get('Email', None)
    password = request.json.get('Password', None)
    if not username:
        result.AddError("Missing username parameter")
    if not password:
        result.AddError("Missing password parameter")

    role = request.json.get('AccountType')
    currentClaim = get_jwt_claims()
    if not IsUserAdmin(currentClaim['role']):
        if not IsUserRoleValid(role):
            result.AddError('Invalid user role')
            return result.ToResponse()

    hashedPassword = hashPassword(password)
    userDict = request.get_json()

    userDict['Password'] = hashedPassword

    try:
        newUser = User(**userDict)
        newUser.save()
        result.Value = "User succefully registered"

    except mongoengine.errors.NotUniqueError:
        result.AddError("Email is not unique")
    except mongoengine.errors.FieldDoesNotExist as e:
        result.AddError("Field does not exist, Details: " + str(e))
    except mongoengine.errors.ValidationError as e:
        for (field, err) in e.to_dict().items():
            result.AddError(field + " : " + str(err))

    return result.ToResponse()