Beispiel #1
0
def registration_student_view(request):
    if request.method == 'POST':
        serializer = RegisterationSerializer(data=request.data)
        serializer2 = CreateStudent(data=request.data)
        data = {}
        if serializer.is_valid():
            if serializer2.is_valid():
                user = serializer.save()
                data['respone'] = 'Successful registrated a new user'
                data['email'] = user.email
                data['username'] = user.username
                data['role'] = user.role
                data['address'] = user.address
                data['phone'] = user.phone_number
                data['birth_date'] = user.birth_date
                token = Token.objects.get(user=user).key
                data['token'] = token
                username = user.username
                student = serializer2.save(username)
                data['uni_code'] = student.uni_code
                data['grade'] = student.grade
                data['level'] = student.level
            else:
                data = serializer2.errors
        else:
            data = serializer.errors
        return Response(data)
Beispiel #2
0
def recieve_email(request):
    User = get_user_model()
    code = request.data.get('code')
    response = Response()
    if (code is None):
        return Response({'error': 'code not found'}, 404)

    digits = DigitsSecurity.objects.get(digits=code)
    if (digits is None):
        return Response({'error': 'invalid code'}, 401)
    user_is = digits.user.username
    active_user = User.objects.filter(username=user_is).first()
    if (active_user is None):
        return Response({'error': 'user not found'}, 404)

    role = RegisterationSerializer(active_user).data['role']
    author = RegisterationSerializer(active_user).data['username']

    access_token = generate_access_token(active_user)
    token_version = Token.objects.get(user=active_user)
    if (token_version is None):
        response.data = {'token_version': 'token version not exist'}
    refresh_token = generate_refresh_token(active_user, token_version)

    response.data = {
        'access_token': access_token,
        'role': role,
        'author': author,
    }

    return response
Beispiel #3
0
def registration_parent_view(request):
    if request.method == 'POST':
        serializer = RegisterationSerializer(data=request.data)
        serializer2 = CreateParent(data=request.data)
        data = {}
        # status = 0
        if serializer.is_valid():
            user = serializer.save()
            data['respone'] = 'Successful registrated a new user'
            data['email'] = user.email
            data['username'] = user.username
            data['role'] = user.role
            data['address'] = user.address
            data['phone'] = user.phone_number
            data['birth_date'] = user.birth_date
            token = Token.objects.get(user=user).key
            data['token'] = token
            if serializer2.is_valid():
                username = user.username
                author = get_or_none(User, username=username)
                if username:
                    # check that unicode is exist in database
                    masters = HeadMaster.objects.all()
                    listMaster = []
                    for master in masters:
                        listMaster.append(master.uni_code)
                    
                    if serializer2.uni_code not in listMaster:
                        data['uni_code'] = 'unicode dose not exist'
                        confirmUser = serializer2.cofirm_user
                        userWhichSaved = User.objects.get(username=confirmUser)
                        if userWhichSaved:
                            userWhichSaved.delete()
                        else:
                            data['user'] = '******'
                    else:
                        parent = serializer2.save(user=author)
                        data['uni_code'] = parent.uni_code
                    # status = 201
                else:
                    data['responseUser'] = '******'
                    # status = 404
            else:
                data['responseStudent'] = 'error in validation'
                # status = 400
        else:
            if data:
                data += serializer.errors
            else:
                data = serializer.errors
        return Response(data)
def login_view(request):
    User = get_user_model()
    username = request.data.get('username')
    password = request.data.get('password')
    response = Response()
    if (username is None) or (password is None):
        raise exceptions.AuthenticationFailed('username and password required')

    user = User.objects.filter(username=username).first()
    if (user is None):
        raise exceptions.AuthenticationFailed('user not found')
    if (not user.check_password(password)):
        raise exceptions.AuthenticationFailed('wrong password')

    serialized_user = RegisterationSerializer(user).data['username']

    access_token = generate_access_token(user)
    token_version = Token.objects.get(user=user)
    if (token_version is None):
        response.data = {'token_version': 'token version not exist'}
    refresh_token = generate_refresh_token(user, token_version)

    response.set_cookie(key='refreshtoken', value=refresh_token, httponly=True)
    response.data = {
        'access_token': access_token,
        'user': serialized_user,
    }

    return response
Beispiel #5
0
def login_view(request):
    User = get_user_model()
    username = request.data.get('username')
    password = request.data.get('password')
    response = Response()
    if (username is None) or (password is None):
        raise exceptions.AuthenticationFailed('username and password required')

    user = User.objects.filter(username=username).first()
    if (user is None):
        raise exceptions.AuthenticationFailed('user not found')
    if (not user.check_password(password)):
        raise exceptions.AuthenticationFailed('wrong password')

    role = RegisterationSerializer(user).data['role']
    author = RegisterationSerializer(user).data['username']

    access_token = generate_access_token(user)
    token_version = Token.objects.get(user=user)
    if (token_version is None):
        response.data = {'token_version': 'token version not exist'}
    refresh_token = generate_refresh_token(user, token_version)

    response.set_cookie(key='refreshtoken', value=refresh_token, httponly=True)
    try:
        headers = request.headers.items()
        tokens = ''
        for header in headers:
            if 'Cookie' in header:
                tokens = header[1]
                break
    except:
        tokens = 'error'
    response.data = {
        'access_token': access_token,
        'role': role,
        'author': author,
        'tokens': tokens
    }

    return response
Beispiel #6
0
def profile(request):
    user = request.user
    first_name = RegisterationSerializer(user).data['first_name']
    last_name = RegisterationSerializer(user).data['last_name']
    email = RegisterationSerializer(user).data['email']
    phone_number = RegisterationSerializer(user).data['phone_number']
    image_profile = RegisterationSerializer(user).data['avatar']
    address = RegisterationSerializer(user).data['address']
    subjects = None
    classs = None
    if user.role == 'T':
        teacher = get_object_or_404(Teacher, user=user)
        subjects = CreateTeacher(teacher).data['subject']
        classs = CreateTeacher(teacher).data['class_rooms']
    elif user.role == 'S':
        student = get_object_or_404(Student, user=user)
        subjects = CreateStudent(student).data['subjects']
        classs = CreateStudent(student).data['class_room']

    return Response(
        {
            'name': '{} {}'.format(first_name, last_name),
            'email': email,
            'mobile': phone_number,
            'image_profile': image_profile,
            'address': address,
            'subjects': subjects,
            'classes': classs
        }, 200)
Beispiel #7
0
def registration_headmaster_view(request):

    if request.method == 'POST':
        serializer = RegisterationSerializer(data=request.data)
        serializer2 = CreateHeadMaster(data=request.data)
        data = {}
        # status = 0
        if serializer.is_valid():
            user = serializer.save()
            data['respone'] = 'Successful registrated a new user'
            data['email'] = user.email
            data['username'] = user.username
            data['role'] = user.role
            data['address'] = user.address
            data['phone'] = user.phone_number
            data['birth_date'] = user.birth_date
            token = Token.objects.get(user=user).key
            data['token'] = token
            if serializer2.is_valid():
                username = user.username
                author = get_or_none(User, username=username)
                if username:
                    headmaster = serializer2.save(user=author)
                    data['uni_code'] = headmaster.uni_code
                    data['educationalType'] = headmaster.educational_type
                    data['level'] = headmaster.educational_level
                    # status = 201
                else:
                    data['responseUser'] = '******'
                    # status = 404
            else:
                data['responseHeadMaster'] = 'error in validation'
                # status = 400
        else:
            if data:
                data += serializer.errors
            else:
                data = serializer.errors
        return Response(data)
Beispiel #8
0
def registration_headmaster_view(request):
    if request.method == 'POST':
        serializer = RegisterationSerializer(data=request.data)
        serializer2 = CreateHeadMaster(data=request.data)
        data = {}
        if serializer.is_valid() and serializer2.is_valid():
            user = serializer.save()
            data['respone'] = 'Successful registrated a new user'
            data['email'] = user.email
            data['username'] = user.username
            data['role'] = user.role
            data['address'] = user.address
            data['phone'] = user.phone_number
            data['birth_date'] = user.birth_date
            token = Token.objects.get(user=user).key
            data['token'] = token
            headmaster = serializer2.save(user=user)
            data['uni_code'] = headmaster.uni_code
            data['educationalType'] = headmaster.educational_type
        else:
            data = serializer.errors
            data += serializer2.errors
        return Response(data)
Beispiel #9
0
def profileStudent(request):
    user = request.user
    student = get_object_or_404(Student, user=user)
    data0 = RegisterationSerializer(user)
    data1 = CreateStudent(student)
    data2 = {}
    nested_data = []
    for a in student.subjects.all():
        nested_data.append(
            ClassRank(user=user.username,
                      theclass=student.class_room.name,
                      subject=a.name))

    return Response({
        'user': data0.data,
        'student': data1.data,
        'ranks': nested_data
    })
Beispiel #10
0
def profileTeacher(request):
    user = request.user
    teacher = get_object_or_404(Teacher, user=user)
    data0 = RegisterationSerializer(user)
    data1 = CreateTeacher(teacher)
    data2 = QuestionRank(user=user.username, subject=teacher.subject.name)
    data3 = ReviewRank(user=user.username, subject=teacher.subject.name)
    data4 = LaunchRank(user=user.username, subject=teacher.subject.name)
    data5 = ReportRank(user=user.username, subject=teacher.subject.name)

    return Response({
        'user': data0.data,
        'teacher': data1.data,
        'questions': data2,
        'reviews': data3,
        'launchs': data4,
        'reports': data5
    })